Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <string.h> |
Rachel Dowdall | 5640279 | 2014-03-22 20:19:24 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Damien George | 88d7bba | 2014-04-08 23:30:46 +0100 | [diff] [blame] | 30 | #include <assert.h> |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 31 | |
Paul Sokolovsky | f54bcbf | 2014-05-02 17:47:01 +0300 | [diff] [blame] | 32 | #include "mpconfig.h" |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 33 | #include "nlr.h" |
| 34 | #include "misc.h" |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 35 | #include "qstr.h" |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 36 | #include "parsenumbase.h" |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 37 | #include "obj.h" |
| 38 | #include "mpz.h" |
| 39 | #include "objint.h" |
| 40 | #include "runtime0.h" |
Damien George | 660aef6 | 2014-04-02 12:22:07 +0100 | [diff] [blame] | 41 | #include "runtime.h" |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 42 | |
| 43 | #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ |
| 44 | |
| 45 | STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) { |
| 46 | mp_obj_int_t *o = m_new_obj(mp_obj_int_t); |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 47 | o->base.type = &mp_type_int; |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 48 | mpz_init_zero(&o->mpz); |
| 49 | return o; |
| 50 | } |
| 51 | |
Dave Hylands | c4029e5 | 2014-04-07 11:19:51 -0700 | [diff] [blame] | 52 | // This routine expects you to pass in a buffer and size (in *buf and buf_size). |
| 53 | // If, for some reason, this buffer is too small, then it will allocate a |
| 54 | // buffer and return the allocated buffer and size in *buf and *buf_size. It |
| 55 | // is the callers responsibility to free this allocated buffer. |
| 56 | // |
| 57 | // The resulting formatted string will be returned from this function and the |
| 58 | // formatted size will be in *fmt_size. |
Damien George | 88d7bba | 2014-04-08 23:30:46 +0100 | [diff] [blame] | 59 | // |
| 60 | // This particular routine should only be called for the mpz representation of the int. |
| 61 | char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_obj_t self_in, |
| 62 | int base, const char *prefix, char base_char, char comma) { |
| 63 | assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int)); |
| 64 | mp_obj_int_t *self = self_in; |
Dave Hylands | c4029e5 | 2014-04-07 11:19:51 -0700 | [diff] [blame] | 65 | |
Damien George | 88d7bba | 2014-04-08 23:30:46 +0100 | [diff] [blame] | 66 | uint needed_size = mpz_as_str_size_formatted(&self->mpz, base, prefix, comma); |
Dave Hylands | c4029e5 | 2014-04-07 11:19:51 -0700 | [diff] [blame] | 67 | if (needed_size > *buf_size) { |
| 68 | *buf = m_new(char, needed_size); |
| 69 | *buf_size = needed_size; |
| 70 | } |
| 71 | char *str = *buf; |
| 72 | |
Damien George | 88d7bba | 2014-04-08 23:30:46 +0100 | [diff] [blame] | 73 | *fmt_size = mpz_as_str_inpl(&self->mpz, base, prefix, base_char, comma, str); |
Dave Hylands | c4029e5 | 2014-04-07 11:19:51 -0700 | [diff] [blame] | 74 | |
| 75 | return str; |
| 76 | } |
| 77 | |
| 78 | bool mp_obj_int_is_positive(mp_obj_t self_in) { |
| 79 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| 80 | return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0; |
| 81 | } |
| 82 | mp_obj_int_t *self = self_in; |
| 83 | return !self->mpz.neg; |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Damien George | e8208a7 | 2014-04-04 15:08:23 +0100 | [diff] [blame] | 86 | mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) { |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 87 | mp_obj_int_t *o = o_in; |
| 88 | switch (op) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 89 | case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz)); |
| 90 | case MP_UNARY_OP_POSITIVE: return o_in; |
| 91 | case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; } |
| 92 | case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return o2; } |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 93 | default: return NULL; // op not supported |
| 94 | } |
| 95 | } |
| 96 | |
Damien George | e8208a7 | 2014-04-04 15:08:23 +0100 | [diff] [blame] | 97 | mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
Damien George | cd8b2ba | 2014-03-19 23:15:25 +0000 | [diff] [blame] | 98 | const mpz_t *zlhs; |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 99 | const mpz_t *zrhs; |
| 100 | mpz_t z_int; |
| 101 | mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT]; |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 102 | |
Damien George | cd8b2ba | 2014-03-19 23:15:25 +0000 | [diff] [blame] | 103 | // lhs could be a small int (eg small-int + mpz) |
| 104 | if (MP_OBJ_IS_SMALL_INT(lhs_in)) { |
| 105 | mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in)); |
| 106 | zlhs = &z_int; |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 107 | } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) { |
Damien George | cd8b2ba | 2014-03-19 23:15:25 +0000 | [diff] [blame] | 108 | zlhs = &((mp_obj_int_t*)lhs_in)->mpz; |
| 109 | } else { |
Damien George | 0aa5d51 | 2014-03-29 17:28:20 +0000 | [diff] [blame] | 110 | // unsupported type |
Damien George | ea8d06c | 2014-04-17 23:19:36 +0100 | [diff] [blame] | 111 | return MP_OBJ_NOT_SUPPORTED; |
Damien George | cd8b2ba | 2014-03-19 23:15:25 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 114 | // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it) |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 115 | if (MP_OBJ_IS_SMALL_INT(rhs_in)) { |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 116 | mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in)); |
| 117 | zrhs = &z_int; |
Damien George | 3e1a5c1 | 2014-03-29 13:43:38 +0000 | [diff] [blame] | 118 | } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) { |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 119 | zrhs = &((mp_obj_int_t*)rhs_in)->mpz; |
Damien George | 0aa5d51 | 2014-03-29 17:28:20 +0000 | [diff] [blame] | 120 | #if MICROPY_ENABLE_FLOAT |
| 121 | } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) { |
| 122 | return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in); |
| 123 | } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) { |
| 124 | return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in); |
| 125 | #endif |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 126 | } else { |
Damien George | e8208a7 | 2014-04-04 15:08:23 +0100 | [diff] [blame] | 127 | // delegate to generic function to check for extra cases |
| 128 | return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Damien George | 5260810 | 2014-03-08 15:04:54 +0000 | [diff] [blame] | 131 | if (0) { |
| 132 | #if MICROPY_ENABLE_FLOAT |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 133 | } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) { |
Damien George | 5260810 | 2014-03-08 15:04:54 +0000 | [diff] [blame] | 134 | mp_float_t flhs = mpz_as_float(zlhs); |
| 135 | mp_float_t frhs = mpz_as_float(zrhs); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 136 | return mp_obj_new_float(flhs / frhs); |
Damien George | 5260810 | 2014-03-08 15:04:54 +0000 | [diff] [blame] | 137 | #endif |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 138 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 139 | } else if (op <= MP_BINARY_OP_INPLACE_POWER) { |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 140 | mp_obj_int_t *res = mp_obj_int_new_mpz(); |
| 141 | |
| 142 | switch (op) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 143 | case MP_BINARY_OP_ADD: |
| 144 | case MP_BINARY_OP_INPLACE_ADD: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 145 | mpz_add_inpl(&res->mpz, zlhs, zrhs); |
| 146 | break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 147 | case MP_BINARY_OP_SUBTRACT: |
| 148 | case MP_BINARY_OP_INPLACE_SUBTRACT: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 149 | mpz_sub_inpl(&res->mpz, zlhs, zrhs); |
| 150 | break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 151 | case MP_BINARY_OP_MULTIPLY: |
| 152 | case MP_BINARY_OP_INPLACE_MULTIPLY: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 153 | mpz_mul_inpl(&res->mpz, zlhs, zrhs); |
| 154 | break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 155 | case MP_BINARY_OP_FLOOR_DIVIDE: |
| 156 | case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: { |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 157 | mpz_t rem; mpz_init_zero(&rem); |
| 158 | mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs); |
Damien George | ecf5b77 | 2014-04-04 11:13:51 +0000 | [diff] [blame] | 159 | if (zlhs->neg != zrhs->neg) { |
Rachel Dowdall | 5640279 | 2014-03-22 20:19:24 +0000 | [diff] [blame] | 160 | if (!mpz_is_zero(&rem)) { |
| 161 | mpz_t mpzone; mpz_init_from_int(&mpzone, -1); |
| 162 | mpz_add_inpl(&res->mpz, &res->mpz, &mpzone); |
| 163 | } |
| 164 | } |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 165 | mpz_deinit(&rem); |
| 166 | break; |
| 167 | } |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 168 | case MP_BINARY_OP_MODULO: |
| 169 | case MP_BINARY_OP_INPLACE_MODULO: { |
Damien George | 2d7ff07 | 2014-03-20 16:28:41 +0000 | [diff] [blame] | 170 | mpz_t quo; mpz_init_zero(&quo); |
| 171 | mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs); |
| 172 | mpz_deinit(&quo); |
Damien George | ecf5b77 | 2014-04-04 11:13:51 +0000 | [diff] [blame] | 173 | // Check signs and do Python style modulo |
| 174 | if (zlhs->neg != zrhs->neg) { |
Rachel Dowdall | cde8631 | 2014-03-22 17:29:27 +0000 | [diff] [blame] | 175 | mpz_add_inpl(&res->mpz, &res->mpz, zrhs); |
| 176 | } |
Damien George | 2d7ff07 | 2014-03-20 16:28:41 +0000 | [diff] [blame] | 177 | break; |
| 178 | } |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 179 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 180 | case MP_BINARY_OP_AND: |
| 181 | case MP_BINARY_OP_INPLACE_AND: |
Paul Sokolovsky | 57207b8 | 2014-03-23 01:52:36 +0200 | [diff] [blame] | 182 | mpz_and_inpl(&res->mpz, zlhs, zrhs); |
| 183 | break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 184 | case MP_BINARY_OP_OR: |
| 185 | case MP_BINARY_OP_INPLACE_OR: |
Paul Sokolovsky | 57207b8 | 2014-03-23 01:52:36 +0200 | [diff] [blame] | 186 | mpz_or_inpl(&res->mpz, zlhs, zrhs); |
| 187 | break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 188 | case MP_BINARY_OP_XOR: |
| 189 | case MP_BINARY_OP_INPLACE_XOR: |
Paul Sokolovsky | 57207b8 | 2014-03-23 01:52:36 +0200 | [diff] [blame] | 190 | mpz_xor_inpl(&res->mpz, zlhs, zrhs); |
| 191 | break; |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 192 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 193 | case MP_BINARY_OP_LSHIFT: |
| 194 | case MP_BINARY_OP_INPLACE_LSHIFT: |
| 195 | case MP_BINARY_OP_RSHIFT: |
| 196 | case MP_BINARY_OP_INPLACE_RSHIFT: { |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 197 | // TODO check conversion overflow |
| 198 | machine_int_t irhs = mpz_as_int(zrhs); |
| 199 | if (irhs < 0) { |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 200 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count")); |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 201 | } |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 202 | if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) { |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 203 | mpz_shl_inpl(&res->mpz, zlhs, irhs); |
| 204 | } else { |
| 205 | mpz_shr_inpl(&res->mpz, zlhs, irhs); |
| 206 | } |
| 207 | break; |
| 208 | } |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 209 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 210 | case MP_BINARY_OP_POWER: |
| 211 | case MP_BINARY_OP_INPLACE_POWER: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 212 | mpz_pow_inpl(&res->mpz, zlhs, zrhs); |
| 213 | break; |
| 214 | |
| 215 | default: |
Damien George | ea8d06c | 2014-04-17 23:19:36 +0100 | [diff] [blame] | 216 | return MP_OBJ_NOT_SUPPORTED; |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | return res; |
| 220 | |
| 221 | } else { |
| 222 | int cmp = mpz_cmp(zlhs, zrhs); |
| 223 | switch (op) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 224 | case MP_BINARY_OP_LESS: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 225 | return MP_BOOL(cmp < 0); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 226 | case MP_BINARY_OP_MORE: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 227 | return MP_BOOL(cmp > 0); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 228 | case MP_BINARY_OP_LESS_EQUAL: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 229 | return MP_BOOL(cmp <= 0); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 230 | case MP_BINARY_OP_MORE_EQUAL: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 231 | return MP_BOOL(cmp >= 0); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 232 | case MP_BINARY_OP_EQUAL: |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 233 | return MP_BOOL(cmp == 0); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 234 | |
| 235 | default: |
Damien George | ea8d06c | 2014-04-17 23:19:36 +0100 | [diff] [blame] | 236 | return MP_OBJ_NOT_SUPPORTED; |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | mp_obj_t mp_obj_new_int(machine_int_t value) { |
| 242 | if (MP_OBJ_FITS_SMALL_INT(value)) { |
| 243 | return MP_OBJ_NEW_SMALL_INT(value); |
| 244 | } |
| 245 | return mp_obj_new_int_from_ll(value); |
| 246 | } |
| 247 | |
| 248 | mp_obj_t mp_obj_new_int_from_ll(long long val) { |
| 249 | mp_obj_int_t *o = mp_obj_int_new_mpz(); |
Damien George | 9d68e9c | 2014-03-12 15:38:15 +0000 | [diff] [blame] | 250 | mpz_set_from_ll(&o->mpz, val); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 251 | return o; |
| 252 | } |
| 253 | |
| 254 | mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { |
| 255 | // SMALL_INT accepts only signed numbers, of one bit less size |
| 256 | // than word size, which totals 2 bits less for unsigned numbers. |
| 257 | if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) { |
| 258 | return MP_OBJ_NEW_SMALL_INT(value); |
| 259 | } |
| 260 | return mp_obj_new_int_from_ll(value); |
| 261 | } |
| 262 | |
| 263 | mp_obj_t mp_obj_new_int_from_long_str(const char *str) { |
| 264 | mp_obj_int_t *o = mp_obj_int_new_mpz(); |
| 265 | uint len = strlen(str); |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 266 | int base = 0; |
| 267 | int skip = mp_parse_num_base(str, len, &base); |
| 268 | str += skip; |
| 269 | len -= skip; |
| 270 | uint n = mpz_set_from_str(&o->mpz, str, len, false, base); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 271 | if (n != len) { |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 272 | nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 273 | } |
| 274 | return o; |
| 275 | } |
| 276 | |
| 277 | machine_int_t mp_obj_int_get(mp_obj_t self_in) { |
| 278 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| 279 | return MP_OBJ_SMALL_INT_VALUE(self_in); |
Damien George | eabdf67 | 2014-03-22 20:54:01 +0000 | [diff] [blame] | 280 | } else { |
| 281 | mp_obj_int_t *self = self_in; |
| 282 | return mpz_as_int(&self->mpz); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 283 | } |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) { |
Damien George | 8270e38 | 2014-04-03 11:00:54 +0000 | [diff] [blame] | 287 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| 288 | return MP_OBJ_SMALL_INT_VALUE(self_in); |
| 289 | } else { |
| 290 | mp_obj_int_t *self = self_in; |
| 291 | machine_int_t value; |
| 292 | if (mpz_as_int_checked(&self->mpz, &value)) { |
| 293 | return value; |
| 294 | } else { |
| 295 | // overflow |
Damien George | ea13f40 | 2014-04-05 18:32:08 +0100 | [diff] [blame] | 296 | nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word")); |
Damien George | 8270e38 | 2014-04-03 11:00:54 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Damien George | eabdf67 | 2014-03-22 20:54:01 +0000 | [diff] [blame] | 301 | #if MICROPY_ENABLE_FLOAT |
| 302 | mp_float_t mp_obj_int_as_float(mp_obj_t self_in) { |
| 303 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| 304 | return MP_OBJ_SMALL_INT_VALUE(self_in); |
| 305 | } else { |
| 306 | mp_obj_int_t *self = self_in; |
| 307 | return mpz_as_float(&self->mpz); |
| 308 | } |
| 309 | } |
| 310 | #endif |
| 311 | |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 312 | #endif |