Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
py/objint_mpz.c@0:5868e8752d44, 2016-04-16 (annotated)
- Committer:
- pythontech
- Date:
- Sat Apr 16 17:11:56 2016 +0000
- Revision:
- 0:5868e8752d44
Split off library from repl
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pythontech | 0:5868e8752d44 | 1 | /* |
| pythontech | 0:5868e8752d44 | 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| pythontech | 0:5868e8752d44 | 3 | * |
| pythontech | 0:5868e8752d44 | 4 | * The MIT License (MIT) |
| pythontech | 0:5868e8752d44 | 5 | * |
| pythontech | 0:5868e8752d44 | 6 | * Copyright (c) 2013, 2014 Damien P. George |
| pythontech | 0:5868e8752d44 | 7 | * |
| pythontech | 0:5868e8752d44 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| pythontech | 0:5868e8752d44 | 9 | * of this software and associated documentation files (the "Software"), to deal |
| pythontech | 0:5868e8752d44 | 10 | * in the Software without restriction, including without limitation the rights |
| pythontech | 0:5868e8752d44 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| pythontech | 0:5868e8752d44 | 12 | * copies of the Software, and to permit persons to whom the Software is |
| pythontech | 0:5868e8752d44 | 13 | * furnished to do so, subject to the following conditions: |
| pythontech | 0:5868e8752d44 | 14 | * |
| pythontech | 0:5868e8752d44 | 15 | * The above copyright notice and this permission notice shall be included in |
| pythontech | 0:5868e8752d44 | 16 | * all copies or substantial portions of the Software. |
| pythontech | 0:5868e8752d44 | 17 | * |
| pythontech | 0:5868e8752d44 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| pythontech | 0:5868e8752d44 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| pythontech | 0:5868e8752d44 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| pythontech | 0:5868e8752d44 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| pythontech | 0:5868e8752d44 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| pythontech | 0:5868e8752d44 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| pythontech | 0:5868e8752d44 | 24 | * THE SOFTWARE. |
| pythontech | 0:5868e8752d44 | 25 | */ |
| pythontech | 0:5868e8752d44 | 26 | |
| pythontech | 0:5868e8752d44 | 27 | #include <string.h> |
| pythontech | 0:5868e8752d44 | 28 | #include <stdio.h> |
| pythontech | 0:5868e8752d44 | 29 | #include <assert.h> |
| pythontech | 0:5868e8752d44 | 30 | |
| pythontech | 0:5868e8752d44 | 31 | #include "py/nlr.h" |
| pythontech | 0:5868e8752d44 | 32 | #include "py/parsenumbase.h" |
| pythontech | 0:5868e8752d44 | 33 | #include "py/smallint.h" |
| pythontech | 0:5868e8752d44 | 34 | #include "py/objint.h" |
| pythontech | 0:5868e8752d44 | 35 | #include "py/runtime0.h" |
| pythontech | 0:5868e8752d44 | 36 | #include "py/runtime.h" |
| pythontech | 0:5868e8752d44 | 37 | |
| pythontech | 0:5868e8752d44 | 38 | #if MICROPY_PY_BUILTINS_FLOAT |
| pythontech | 0:5868e8752d44 | 39 | #include <math.h> |
| pythontech | 0:5868e8752d44 | 40 | #endif |
| pythontech | 0:5868e8752d44 | 41 | |
| pythontech | 0:5868e8752d44 | 42 | #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ |
| pythontech | 0:5868e8752d44 | 43 | |
| pythontech | 0:5868e8752d44 | 44 | #if MICROPY_PY_SYS_MAXSIZE |
| pythontech | 0:5868e8752d44 | 45 | // Export value for sys.maxsize |
| pythontech | 0:5868e8752d44 | 46 | #define DIG_MASK ((MPZ_LONG_1 << MPZ_DIG_SIZE) - 1) |
| pythontech | 0:5868e8752d44 | 47 | STATIC const mpz_dig_t maxsize_dig[] = { |
| pythontech | 0:5868e8752d44 | 48 | #define NUM_DIG 1 |
| pythontech | 0:5868e8752d44 | 49 | (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) & DIG_MASK, |
| pythontech | 0:5868e8752d44 | 50 | #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) > DIG_MASK |
| pythontech | 0:5868e8752d44 | 51 | #undef NUM_DIG |
| pythontech | 0:5868e8752d44 | 52 | #define NUM_DIG 2 |
| pythontech | 0:5868e8752d44 | 53 | (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) & DIG_MASK, |
| pythontech | 0:5868e8752d44 | 54 | #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) > DIG_MASK |
| pythontech | 0:5868e8752d44 | 55 | #undef NUM_DIG |
| pythontech | 0:5868e8752d44 | 56 | #define NUM_DIG 3 |
| pythontech | 0:5868e8752d44 | 57 | (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) & DIG_MASK, |
| pythontech | 0:5868e8752d44 | 58 | #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) > DIG_MASK |
| pythontech | 0:5868e8752d44 | 59 | #undef NUM_DIG |
| pythontech | 0:5868e8752d44 | 60 | #define NUM_DIG 4 |
| pythontech | 0:5868e8752d44 | 61 | (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) & DIG_MASK, |
| pythontech | 0:5868e8752d44 | 62 | #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) > DIG_MASK |
| pythontech | 0:5868e8752d44 | 63 | #error cannot encode MP_SSIZE_MAX as mpz |
| pythontech | 0:5868e8752d44 | 64 | #endif |
| pythontech | 0:5868e8752d44 | 65 | #endif |
| pythontech | 0:5868e8752d44 | 66 | #endif |
| pythontech | 0:5868e8752d44 | 67 | #endif |
| pythontech | 0:5868e8752d44 | 68 | }; |
| pythontech | 0:5868e8752d44 | 69 | const mp_obj_int_t mp_maxsize_obj = { |
| pythontech | 0:5868e8752d44 | 70 | {&mp_type_int}, |
| pythontech | 0:5868e8752d44 | 71 | {.fixed_dig = 1, .len = NUM_DIG, .alloc = NUM_DIG, .dig = (mpz_dig_t*)maxsize_dig} |
| pythontech | 0:5868e8752d44 | 72 | }; |
| pythontech | 0:5868e8752d44 | 73 | #undef DIG_MASK |
| pythontech | 0:5868e8752d44 | 74 | #undef NUM_DIG |
| pythontech | 0:5868e8752d44 | 75 | #endif |
| pythontech | 0:5868e8752d44 | 76 | |
| pythontech | 0:5868e8752d44 | 77 | STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) { |
| pythontech | 0:5868e8752d44 | 78 | mp_obj_int_t *o = m_new_obj(mp_obj_int_t); |
| pythontech | 0:5868e8752d44 | 79 | o->base.type = &mp_type_int; |
| pythontech | 0:5868e8752d44 | 80 | mpz_init_zero(&o->mpz); |
| pythontech | 0:5868e8752d44 | 81 | return o; |
| pythontech | 0:5868e8752d44 | 82 | } |
| pythontech | 0:5868e8752d44 | 83 | |
| pythontech | 0:5868e8752d44 | 84 | // This routine expects you to pass in a buffer and size (in *buf and buf_size). |
| pythontech | 0:5868e8752d44 | 85 | // If, for some reason, this buffer is too small, then it will allocate a |
| pythontech | 0:5868e8752d44 | 86 | // buffer and return the allocated buffer and size in *buf and *buf_size. It |
| pythontech | 0:5868e8752d44 | 87 | // is the callers responsibility to free this allocated buffer. |
| pythontech | 0:5868e8752d44 | 88 | // |
| pythontech | 0:5868e8752d44 | 89 | // The resulting formatted string will be returned from this function and the |
| pythontech | 0:5868e8752d44 | 90 | // formatted size will be in *fmt_size. |
| pythontech | 0:5868e8752d44 | 91 | // |
| pythontech | 0:5868e8752d44 | 92 | // This particular routine should only be called for the mpz representation of the int. |
| pythontech | 0:5868e8752d44 | 93 | char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size, mp_const_obj_t self_in, |
| pythontech | 0:5868e8752d44 | 94 | int base, const char *prefix, char base_char, char comma) { |
| pythontech | 0:5868e8752d44 | 95 | assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int)); |
| pythontech | 0:5868e8752d44 | 96 | const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 97 | |
| pythontech | 0:5868e8752d44 | 98 | mp_uint_t needed_size = mpz_as_str_size(&self->mpz, base, prefix, comma); |
| pythontech | 0:5868e8752d44 | 99 | if (needed_size > *buf_size) { |
| pythontech | 0:5868e8752d44 | 100 | *buf = m_new(char, needed_size); |
| pythontech | 0:5868e8752d44 | 101 | *buf_size = needed_size; |
| pythontech | 0:5868e8752d44 | 102 | } |
| pythontech | 0:5868e8752d44 | 103 | char *str = *buf; |
| pythontech | 0:5868e8752d44 | 104 | |
| pythontech | 0:5868e8752d44 | 105 | *fmt_size = mpz_as_str_inpl(&self->mpz, base, prefix, base_char, comma, str); |
| pythontech | 0:5868e8752d44 | 106 | |
| pythontech | 0:5868e8752d44 | 107 | return str; |
| pythontech | 0:5868e8752d44 | 108 | } |
| pythontech | 0:5868e8752d44 | 109 | |
| pythontech | 0:5868e8752d44 | 110 | void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len, byte *buf) { |
| pythontech | 0:5868e8752d44 | 111 | assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int)); |
| pythontech | 0:5868e8752d44 | 112 | mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 113 | mpz_as_bytes(&self->mpz, big_endian, len, buf); |
| pythontech | 0:5868e8752d44 | 114 | } |
| pythontech | 0:5868e8752d44 | 115 | |
| pythontech | 0:5868e8752d44 | 116 | int mp_obj_int_sign(mp_obj_t self_in) { |
| pythontech | 0:5868e8752d44 | 117 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| pythontech | 0:5868e8752d44 | 118 | mp_int_t val = MP_OBJ_SMALL_INT_VALUE(self_in); |
| pythontech | 0:5868e8752d44 | 119 | if (val < 0) { |
| pythontech | 0:5868e8752d44 | 120 | return -1; |
| pythontech | 0:5868e8752d44 | 121 | } else if (val > 0) { |
| pythontech | 0:5868e8752d44 | 122 | return 1; |
| pythontech | 0:5868e8752d44 | 123 | } else { |
| pythontech | 0:5868e8752d44 | 124 | return 0; |
| pythontech | 0:5868e8752d44 | 125 | } |
| pythontech | 0:5868e8752d44 | 126 | } |
| pythontech | 0:5868e8752d44 | 127 | mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 128 | if (self->mpz.len == 0) { |
| pythontech | 0:5868e8752d44 | 129 | return 0; |
| pythontech | 0:5868e8752d44 | 130 | } else if (self->mpz.neg == 0) { |
| pythontech | 0:5868e8752d44 | 131 | return 1; |
| pythontech | 0:5868e8752d44 | 132 | } else { |
| pythontech | 0:5868e8752d44 | 133 | return -1; |
| pythontech | 0:5868e8752d44 | 134 | } |
| pythontech | 0:5868e8752d44 | 135 | } |
| pythontech | 0:5868e8752d44 | 136 | |
| pythontech | 0:5868e8752d44 | 137 | // This must handle int and bool types, and must raise a |
| pythontech | 0:5868e8752d44 | 138 | // TypeError if the argument is not integral |
| pythontech | 0:5868e8752d44 | 139 | mp_obj_t mp_obj_int_abs(mp_obj_t self_in) { |
| pythontech | 0:5868e8752d44 | 140 | if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) { |
| pythontech | 0:5868e8752d44 | 141 | mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 142 | mp_obj_int_t *self2 = mp_obj_int_new_mpz(); |
| pythontech | 0:5868e8752d44 | 143 | mpz_abs_inpl(&self2->mpz, &self->mpz); |
| pythontech | 0:5868e8752d44 | 144 | return MP_OBJ_FROM_PTR(self2); |
| pythontech | 0:5868e8752d44 | 145 | } else { |
| pythontech | 0:5868e8752d44 | 146 | mp_int_t val = mp_obj_get_int(self_in); |
| pythontech | 0:5868e8752d44 | 147 | if (val == MP_SMALL_INT_MIN) { |
| pythontech | 0:5868e8752d44 | 148 | return mp_obj_new_int_from_ll(-val); |
| pythontech | 0:5868e8752d44 | 149 | } else { |
| pythontech | 0:5868e8752d44 | 150 | if (val < 0) { |
| pythontech | 0:5868e8752d44 | 151 | val = -val; |
| pythontech | 0:5868e8752d44 | 152 | } |
| pythontech | 0:5868e8752d44 | 153 | return MP_OBJ_NEW_SMALL_INT(val); |
| pythontech | 0:5868e8752d44 | 154 | } |
| pythontech | 0:5868e8752d44 | 155 | } |
| pythontech | 0:5868e8752d44 | 156 | } |
| pythontech | 0:5868e8752d44 | 157 | |
| pythontech | 0:5868e8752d44 | 158 | mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) { |
| pythontech | 0:5868e8752d44 | 159 | mp_obj_int_t *o = MP_OBJ_TO_PTR(o_in); |
| pythontech | 0:5868e8752d44 | 160 | switch (op) { |
| pythontech | 0:5868e8752d44 | 161 | case MP_UNARY_OP_BOOL: return mp_obj_new_bool(!mpz_is_zero(&o->mpz)); |
| pythontech | 0:5868e8752d44 | 162 | case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mpz_hash(&o->mpz)); |
| pythontech | 0:5868e8752d44 | 163 | case MP_UNARY_OP_POSITIVE: return o_in; |
| pythontech | 0:5868e8752d44 | 164 | case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return MP_OBJ_FROM_PTR(o2); } |
| pythontech | 0:5868e8752d44 | 165 | case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return MP_OBJ_FROM_PTR(o2); } |
| pythontech | 0:5868e8752d44 | 166 | default: return MP_OBJ_NULL; // op not supported |
| pythontech | 0:5868e8752d44 | 167 | } |
| pythontech | 0:5868e8752d44 | 168 | } |
| pythontech | 0:5868e8752d44 | 169 | |
| pythontech | 0:5868e8752d44 | 170 | mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
| pythontech | 0:5868e8752d44 | 171 | const mpz_t *zlhs; |
| pythontech | 0:5868e8752d44 | 172 | const mpz_t *zrhs; |
| pythontech | 0:5868e8752d44 | 173 | mpz_t z_int; |
| pythontech | 0:5868e8752d44 | 174 | mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT]; |
| pythontech | 0:5868e8752d44 | 175 | |
| pythontech | 0:5868e8752d44 | 176 | // lhs could be a small int (eg small-int + mpz) |
| pythontech | 0:5868e8752d44 | 177 | if (MP_OBJ_IS_SMALL_INT(lhs_in)) { |
| pythontech | 0:5868e8752d44 | 178 | mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in)); |
| pythontech | 0:5868e8752d44 | 179 | zlhs = &z_int; |
| pythontech | 0:5868e8752d44 | 180 | } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) { |
| pythontech | 0:5868e8752d44 | 181 | zlhs = &((mp_obj_int_t*)MP_OBJ_TO_PTR(lhs_in))->mpz; |
| pythontech | 0:5868e8752d44 | 182 | } else { |
| pythontech | 0:5868e8752d44 | 183 | // unsupported type |
| pythontech | 0:5868e8752d44 | 184 | return MP_OBJ_NULL; |
| pythontech | 0:5868e8752d44 | 185 | } |
| pythontech | 0:5868e8752d44 | 186 | |
| pythontech | 0:5868e8752d44 | 187 | // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it) |
| pythontech | 0:5868e8752d44 | 188 | if (MP_OBJ_IS_SMALL_INT(rhs_in)) { |
| pythontech | 0:5868e8752d44 | 189 | mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in)); |
| pythontech | 0:5868e8752d44 | 190 | zrhs = &z_int; |
| pythontech | 0:5868e8752d44 | 191 | } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) { |
| pythontech | 0:5868e8752d44 | 192 | zrhs = &((mp_obj_int_t*)MP_OBJ_TO_PTR(rhs_in))->mpz; |
| pythontech | 0:5868e8752d44 | 193 | #if MICROPY_PY_BUILTINS_FLOAT |
| pythontech | 0:5868e8752d44 | 194 | } else if (mp_obj_is_float(rhs_in)) { |
| pythontech | 0:5868e8752d44 | 195 | return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in); |
| pythontech | 0:5868e8752d44 | 196 | #if MICROPY_PY_BUILTINS_COMPLEX |
| pythontech | 0:5868e8752d44 | 197 | } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) { |
| pythontech | 0:5868e8752d44 | 198 | return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in); |
| pythontech | 0:5868e8752d44 | 199 | #endif |
| pythontech | 0:5868e8752d44 | 200 | #endif |
| pythontech | 0:5868e8752d44 | 201 | } else { |
| pythontech | 0:5868e8752d44 | 202 | // delegate to generic function to check for extra cases |
| pythontech | 0:5868e8752d44 | 203 | return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in); |
| pythontech | 0:5868e8752d44 | 204 | } |
| pythontech | 0:5868e8752d44 | 205 | |
| pythontech | 0:5868e8752d44 | 206 | if (0) { |
| pythontech | 0:5868e8752d44 | 207 | #if MICROPY_PY_BUILTINS_FLOAT |
| pythontech | 0:5868e8752d44 | 208 | } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) { |
| pythontech | 0:5868e8752d44 | 209 | if (mpz_is_zero(zrhs)) { |
| pythontech | 0:5868e8752d44 | 210 | goto zero_division_error; |
| pythontech | 0:5868e8752d44 | 211 | } |
| pythontech | 0:5868e8752d44 | 212 | mp_float_t flhs = mpz_as_float(zlhs); |
| pythontech | 0:5868e8752d44 | 213 | mp_float_t frhs = mpz_as_float(zrhs); |
| pythontech | 0:5868e8752d44 | 214 | return mp_obj_new_float(flhs / frhs); |
| pythontech | 0:5868e8752d44 | 215 | #endif |
| pythontech | 0:5868e8752d44 | 216 | |
| pythontech | 0:5868e8752d44 | 217 | } else if (op <= MP_BINARY_OP_INPLACE_POWER) { |
| pythontech | 0:5868e8752d44 | 218 | mp_obj_int_t *res = mp_obj_int_new_mpz(); |
| pythontech | 0:5868e8752d44 | 219 | |
| pythontech | 0:5868e8752d44 | 220 | switch (op) { |
| pythontech | 0:5868e8752d44 | 221 | case MP_BINARY_OP_ADD: |
| pythontech | 0:5868e8752d44 | 222 | case MP_BINARY_OP_INPLACE_ADD: |
| pythontech | 0:5868e8752d44 | 223 | mpz_add_inpl(&res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 224 | break; |
| pythontech | 0:5868e8752d44 | 225 | case MP_BINARY_OP_SUBTRACT: |
| pythontech | 0:5868e8752d44 | 226 | case MP_BINARY_OP_INPLACE_SUBTRACT: |
| pythontech | 0:5868e8752d44 | 227 | mpz_sub_inpl(&res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 228 | break; |
| pythontech | 0:5868e8752d44 | 229 | case MP_BINARY_OP_MULTIPLY: |
| pythontech | 0:5868e8752d44 | 230 | case MP_BINARY_OP_INPLACE_MULTIPLY: |
| pythontech | 0:5868e8752d44 | 231 | mpz_mul_inpl(&res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 232 | break; |
| pythontech | 0:5868e8752d44 | 233 | case MP_BINARY_OP_FLOOR_DIVIDE: |
| pythontech | 0:5868e8752d44 | 234 | case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: { |
| pythontech | 0:5868e8752d44 | 235 | if (mpz_is_zero(zrhs)) { |
| pythontech | 0:5868e8752d44 | 236 | zero_division_error: |
| pythontech | 0:5868e8752d44 | 237 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, |
| pythontech | 0:5868e8752d44 | 238 | "division by zero")); |
| pythontech | 0:5868e8752d44 | 239 | } |
| pythontech | 0:5868e8752d44 | 240 | mpz_t rem; mpz_init_zero(&rem); |
| pythontech | 0:5868e8752d44 | 241 | mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 242 | if (zlhs->neg != zrhs->neg) { |
| pythontech | 0:5868e8752d44 | 243 | if (!mpz_is_zero(&rem)) { |
| pythontech | 0:5868e8752d44 | 244 | mpz_t mpzone; mpz_init_from_int(&mpzone, -1); |
| pythontech | 0:5868e8752d44 | 245 | mpz_add_inpl(&res->mpz, &res->mpz, &mpzone); |
| pythontech | 0:5868e8752d44 | 246 | } |
| pythontech | 0:5868e8752d44 | 247 | } |
| pythontech | 0:5868e8752d44 | 248 | mpz_deinit(&rem); |
| pythontech | 0:5868e8752d44 | 249 | break; |
| pythontech | 0:5868e8752d44 | 250 | } |
| pythontech | 0:5868e8752d44 | 251 | case MP_BINARY_OP_MODULO: |
| pythontech | 0:5868e8752d44 | 252 | case MP_BINARY_OP_INPLACE_MODULO: { |
| pythontech | 0:5868e8752d44 | 253 | if (mpz_is_zero(zrhs)) { |
| pythontech | 0:5868e8752d44 | 254 | goto zero_division_error; |
| pythontech | 0:5868e8752d44 | 255 | } |
| pythontech | 0:5868e8752d44 | 256 | mpz_t quo; mpz_init_zero(&quo); |
| pythontech | 0:5868e8752d44 | 257 | mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 258 | mpz_deinit(&quo); |
| pythontech | 0:5868e8752d44 | 259 | // Check signs and do Python style modulo |
| pythontech | 0:5868e8752d44 | 260 | if (zlhs->neg != zrhs->neg) { |
| pythontech | 0:5868e8752d44 | 261 | mpz_add_inpl(&res->mpz, &res->mpz, zrhs); |
| pythontech | 0:5868e8752d44 | 262 | } |
| pythontech | 0:5868e8752d44 | 263 | break; |
| pythontech | 0:5868e8752d44 | 264 | } |
| pythontech | 0:5868e8752d44 | 265 | |
| pythontech | 0:5868e8752d44 | 266 | case MP_BINARY_OP_AND: |
| pythontech | 0:5868e8752d44 | 267 | case MP_BINARY_OP_INPLACE_AND: |
| pythontech | 0:5868e8752d44 | 268 | mpz_and_inpl(&res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 269 | break; |
| pythontech | 0:5868e8752d44 | 270 | case MP_BINARY_OP_OR: |
| pythontech | 0:5868e8752d44 | 271 | case MP_BINARY_OP_INPLACE_OR: |
| pythontech | 0:5868e8752d44 | 272 | mpz_or_inpl(&res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 273 | break; |
| pythontech | 0:5868e8752d44 | 274 | case MP_BINARY_OP_XOR: |
| pythontech | 0:5868e8752d44 | 275 | case MP_BINARY_OP_INPLACE_XOR: |
| pythontech | 0:5868e8752d44 | 276 | mpz_xor_inpl(&res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 277 | break; |
| pythontech | 0:5868e8752d44 | 278 | |
| pythontech | 0:5868e8752d44 | 279 | case MP_BINARY_OP_LSHIFT: |
| pythontech | 0:5868e8752d44 | 280 | case MP_BINARY_OP_INPLACE_LSHIFT: |
| pythontech | 0:5868e8752d44 | 281 | case MP_BINARY_OP_RSHIFT: |
| pythontech | 0:5868e8752d44 | 282 | case MP_BINARY_OP_INPLACE_RSHIFT: { |
| pythontech | 0:5868e8752d44 | 283 | mp_int_t irhs = mp_obj_int_get_checked(rhs_in); |
| pythontech | 0:5868e8752d44 | 284 | if (irhs < 0) { |
| pythontech | 0:5868e8752d44 | 285 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count")); |
| pythontech | 0:5868e8752d44 | 286 | } |
| pythontech | 0:5868e8752d44 | 287 | if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) { |
| pythontech | 0:5868e8752d44 | 288 | mpz_shl_inpl(&res->mpz, zlhs, irhs); |
| pythontech | 0:5868e8752d44 | 289 | } else { |
| pythontech | 0:5868e8752d44 | 290 | mpz_shr_inpl(&res->mpz, zlhs, irhs); |
| pythontech | 0:5868e8752d44 | 291 | } |
| pythontech | 0:5868e8752d44 | 292 | break; |
| pythontech | 0:5868e8752d44 | 293 | } |
| pythontech | 0:5868e8752d44 | 294 | |
| pythontech | 0:5868e8752d44 | 295 | case MP_BINARY_OP_POWER: |
| pythontech | 0:5868e8752d44 | 296 | case MP_BINARY_OP_INPLACE_POWER: |
| pythontech | 0:5868e8752d44 | 297 | mpz_pow_inpl(&res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 298 | break; |
| pythontech | 0:5868e8752d44 | 299 | |
| pythontech | 0:5868e8752d44 | 300 | case MP_BINARY_OP_DIVMOD: { |
| pythontech | 0:5868e8752d44 | 301 | if (mpz_is_zero(zrhs)) { |
| pythontech | 0:5868e8752d44 | 302 | goto zero_division_error; |
| pythontech | 0:5868e8752d44 | 303 | } |
| pythontech | 0:5868e8752d44 | 304 | mp_obj_int_t *quo = mp_obj_int_new_mpz(); |
| pythontech | 0:5868e8752d44 | 305 | mpz_divmod_inpl(&quo->mpz, &res->mpz, zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 306 | // Check signs and do Python style modulo |
| pythontech | 0:5868e8752d44 | 307 | if (zlhs->neg != zrhs->neg) { |
| pythontech | 0:5868e8752d44 | 308 | mpz_add_inpl(&res->mpz, &res->mpz, zrhs); |
| pythontech | 0:5868e8752d44 | 309 | } |
| pythontech | 0:5868e8752d44 | 310 | mp_obj_t tuple[2] = {MP_OBJ_FROM_PTR(quo), MP_OBJ_FROM_PTR(res)}; |
| pythontech | 0:5868e8752d44 | 311 | return mp_obj_new_tuple(2, tuple); |
| pythontech | 0:5868e8752d44 | 312 | } |
| pythontech | 0:5868e8752d44 | 313 | |
| pythontech | 0:5868e8752d44 | 314 | default: |
| pythontech | 0:5868e8752d44 | 315 | return MP_OBJ_NULL; // op not supported |
| pythontech | 0:5868e8752d44 | 316 | } |
| pythontech | 0:5868e8752d44 | 317 | |
| pythontech | 0:5868e8752d44 | 318 | return MP_OBJ_FROM_PTR(res); |
| pythontech | 0:5868e8752d44 | 319 | |
| pythontech | 0:5868e8752d44 | 320 | } else { |
| pythontech | 0:5868e8752d44 | 321 | int cmp = mpz_cmp(zlhs, zrhs); |
| pythontech | 0:5868e8752d44 | 322 | switch (op) { |
| pythontech | 0:5868e8752d44 | 323 | case MP_BINARY_OP_LESS: |
| pythontech | 0:5868e8752d44 | 324 | return mp_obj_new_bool(cmp < 0); |
| pythontech | 0:5868e8752d44 | 325 | case MP_BINARY_OP_MORE: |
| pythontech | 0:5868e8752d44 | 326 | return mp_obj_new_bool(cmp > 0); |
| pythontech | 0:5868e8752d44 | 327 | case MP_BINARY_OP_LESS_EQUAL: |
| pythontech | 0:5868e8752d44 | 328 | return mp_obj_new_bool(cmp <= 0); |
| pythontech | 0:5868e8752d44 | 329 | case MP_BINARY_OP_MORE_EQUAL: |
| pythontech | 0:5868e8752d44 | 330 | return mp_obj_new_bool(cmp >= 0); |
| pythontech | 0:5868e8752d44 | 331 | case MP_BINARY_OP_EQUAL: |
| pythontech | 0:5868e8752d44 | 332 | return mp_obj_new_bool(cmp == 0); |
| pythontech | 0:5868e8752d44 | 333 | |
| pythontech | 0:5868e8752d44 | 334 | default: |
| pythontech | 0:5868e8752d44 | 335 | return MP_OBJ_NULL; // op not supported |
| pythontech | 0:5868e8752d44 | 336 | } |
| pythontech | 0:5868e8752d44 | 337 | } |
| pythontech | 0:5868e8752d44 | 338 | } |
| pythontech | 0:5868e8752d44 | 339 | |
| pythontech | 0:5868e8752d44 | 340 | mp_obj_t mp_obj_new_int(mp_int_t value) { |
| pythontech | 0:5868e8752d44 | 341 | if (MP_SMALL_INT_FITS(value)) { |
| pythontech | 0:5868e8752d44 | 342 | return MP_OBJ_NEW_SMALL_INT(value); |
| pythontech | 0:5868e8752d44 | 343 | } |
| pythontech | 0:5868e8752d44 | 344 | return mp_obj_new_int_from_ll(value); |
| pythontech | 0:5868e8752d44 | 345 | } |
| pythontech | 0:5868e8752d44 | 346 | |
| pythontech | 0:5868e8752d44 | 347 | mp_obj_t mp_obj_new_int_from_ll(long long val) { |
| pythontech | 0:5868e8752d44 | 348 | mp_obj_int_t *o = mp_obj_int_new_mpz(); |
| pythontech | 0:5868e8752d44 | 349 | mpz_set_from_ll(&o->mpz, val, true); |
| pythontech | 0:5868e8752d44 | 350 | return MP_OBJ_FROM_PTR(o); |
| pythontech | 0:5868e8752d44 | 351 | } |
| pythontech | 0:5868e8752d44 | 352 | |
| pythontech | 0:5868e8752d44 | 353 | mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) { |
| pythontech | 0:5868e8752d44 | 354 | mp_obj_int_t *o = mp_obj_int_new_mpz(); |
| pythontech | 0:5868e8752d44 | 355 | mpz_set_from_ll(&o->mpz, val, false); |
| pythontech | 0:5868e8752d44 | 356 | return MP_OBJ_FROM_PTR(o); |
| pythontech | 0:5868e8752d44 | 357 | } |
| pythontech | 0:5868e8752d44 | 358 | |
| pythontech | 0:5868e8752d44 | 359 | mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { |
| pythontech | 0:5868e8752d44 | 360 | // SMALL_INT accepts only signed numbers, so make sure the input |
| pythontech | 0:5868e8752d44 | 361 | // value fits completely in the small-int positive range. |
| pythontech | 0:5868e8752d44 | 362 | if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) { |
| pythontech | 0:5868e8752d44 | 363 | return MP_OBJ_NEW_SMALL_INT(value); |
| pythontech | 0:5868e8752d44 | 364 | } |
| pythontech | 0:5868e8752d44 | 365 | return mp_obj_new_int_from_ull(value); |
| pythontech | 0:5868e8752d44 | 366 | } |
| pythontech | 0:5868e8752d44 | 367 | |
| pythontech | 0:5868e8752d44 | 368 | #if MICROPY_PY_BUILTINS_FLOAT |
| pythontech | 0:5868e8752d44 | 369 | mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { |
| pythontech | 0:5868e8752d44 | 370 | int cl = fpclassify(val); |
| pythontech | 0:5868e8752d44 | 371 | if (cl == FP_INFINITE) { |
| pythontech | 0:5868e8752d44 | 372 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int")); |
| pythontech | 0:5868e8752d44 | 373 | } else if (cl == FP_NAN) { |
| pythontech | 0:5868e8752d44 | 374 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int")); |
| pythontech | 0:5868e8752d44 | 375 | } else { |
| pythontech | 0:5868e8752d44 | 376 | mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val); |
| pythontech | 0:5868e8752d44 | 377 | if (icl == MP_FP_CLASS_FIT_SMALLINT) { |
| pythontech | 0:5868e8752d44 | 378 | return MP_OBJ_NEW_SMALL_INT((mp_int_t)val); |
| pythontech | 0:5868e8752d44 | 379 | } else { |
| pythontech | 0:5868e8752d44 | 380 | mp_obj_int_t *o = mp_obj_int_new_mpz(); |
| pythontech | 0:5868e8752d44 | 381 | mpz_set_from_float(&o->mpz, val); |
| pythontech | 0:5868e8752d44 | 382 | return MP_OBJ_FROM_PTR(o); |
| pythontech | 0:5868e8752d44 | 383 | } |
| pythontech | 0:5868e8752d44 | 384 | } |
| pythontech | 0:5868e8752d44 | 385 | } |
| pythontech | 0:5868e8752d44 | 386 | #endif |
| pythontech | 0:5868e8752d44 | 387 | |
| pythontech | 0:5868e8752d44 | 388 | mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) { |
| pythontech | 0:5868e8752d44 | 389 | mp_obj_int_t *o = mp_obj_int_new_mpz(); |
| pythontech | 0:5868e8752d44 | 390 | mp_uint_t n = mpz_set_from_str(&o->mpz, *str, len, neg, base); |
| pythontech | 0:5868e8752d44 | 391 | *str += n; |
| pythontech | 0:5868e8752d44 | 392 | return MP_OBJ_FROM_PTR(o); |
| pythontech | 0:5868e8752d44 | 393 | } |
| pythontech | 0:5868e8752d44 | 394 | |
| pythontech | 0:5868e8752d44 | 395 | mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) { |
| pythontech | 0:5868e8752d44 | 396 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| pythontech | 0:5868e8752d44 | 397 | return MP_OBJ_SMALL_INT_VALUE(self_in); |
| pythontech | 0:5868e8752d44 | 398 | } else { |
| pythontech | 0:5868e8752d44 | 399 | const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 400 | // hash returns actual int value if it fits in mp_int_t |
| pythontech | 0:5868e8752d44 | 401 | return mpz_hash(&self->mpz); |
| pythontech | 0:5868e8752d44 | 402 | } |
| pythontech | 0:5868e8752d44 | 403 | } |
| pythontech | 0:5868e8752d44 | 404 | |
| pythontech | 0:5868e8752d44 | 405 | mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) { |
| pythontech | 0:5868e8752d44 | 406 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| pythontech | 0:5868e8752d44 | 407 | return MP_OBJ_SMALL_INT_VALUE(self_in); |
| pythontech | 0:5868e8752d44 | 408 | } else { |
| pythontech | 0:5868e8752d44 | 409 | const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 410 | mp_int_t value; |
| pythontech | 0:5868e8752d44 | 411 | if (mpz_as_int_checked(&self->mpz, &value)) { |
| pythontech | 0:5868e8752d44 | 412 | return value; |
| pythontech | 0:5868e8752d44 | 413 | } else { |
| pythontech | 0:5868e8752d44 | 414 | // overflow |
| pythontech | 0:5868e8752d44 | 415 | nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word")); |
| pythontech | 0:5868e8752d44 | 416 | } |
| pythontech | 0:5868e8752d44 | 417 | } |
| pythontech | 0:5868e8752d44 | 418 | } |
| pythontech | 0:5868e8752d44 | 419 | |
| pythontech | 0:5868e8752d44 | 420 | #if MICROPY_PY_BUILTINS_FLOAT |
| pythontech | 0:5868e8752d44 | 421 | mp_float_t mp_obj_int_as_float(mp_obj_t self_in) { |
| pythontech | 0:5868e8752d44 | 422 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| pythontech | 0:5868e8752d44 | 423 | return MP_OBJ_SMALL_INT_VALUE(self_in); |
| pythontech | 0:5868e8752d44 | 424 | } else { |
| pythontech | 0:5868e8752d44 | 425 | mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); |
| pythontech | 0:5868e8752d44 | 426 | return mpz_as_float(&self->mpz); |
| pythontech | 0:5868e8752d44 | 427 | } |
| pythontech | 0:5868e8752d44 | 428 | } |
| pythontech | 0:5868e8752d44 | 429 | #endif |
| pythontech | 0:5868e8752d44 | 430 | |
| pythontech | 0:5868e8752d44 | 431 | #endif |