blob: be917f4a74bb4d58fde7809faec661c277c61380 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 George438c88d2014-02-22 19:25:23 +000027#include <string.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000028#include <stdio.h>
Damien George88d7bba2014-04-08 23:30:46 +010029#include <assert.h>
Damien George438c88d2014-02-22 19:25:23 +000030
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/parsenumbase.h"
33#include "py/smallint.h"
34#include "py/objint.h"
35#include "py/runtime0.h"
36#include "py/runtime.h"
Damien George438c88d2014-02-22 19:25:23 +000037
Paul Sokolovsky12033df2014-12-30 00:22:10 +020038#if MICROPY_PY_BUILTINS_FLOAT
39#include <math.h>
40#endif
41
Damien George438c88d2014-02-22 19:25:23 +000042#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
43
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030044#if MICROPY_PY_SYS_MAXSIZE
45// Export value for sys.maxsize
stijn0e557fa2014-10-30 14:39:22 +010046#define DIG_MASK ((MPZ_LONG_1 << MPZ_DIG_SIZE) - 1)
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030047STATIC const mpz_dig_t maxsize_dig[MPZ_NUM_DIG_FOR_INT] = {
Paul Sokolovsky722e5622014-09-06 19:17:23 +030048 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) & DIG_MASK,
49 #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) > DIG_MASK
50 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) & DIG_MASK,
51 #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) > DIG_MASK
52 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) & DIG_MASK,
53 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) & DIG_MASK,
54 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 4) & DIG_MASK,
55// (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 5) & DIG_MASK,
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030056 #endif
Damien George8002d5d2014-09-06 17:37:29 +010057 #endif
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030058};
59const mp_obj_int_t mp_maxsize_obj = {
60 {&mp_type_int},
61 {.fixed_dig = 1, .len = MPZ_NUM_DIG_FOR_INT, .alloc = MPZ_NUM_DIG_FOR_INT, .dig = (mpz_dig_t*)maxsize_dig}
62};
63#undef DIG_MASK
64#endif
65
Damien George438c88d2014-02-22 19:25:23 +000066STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
67 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +000068 o->base.type = &mp_type_int;
Damien George438c88d2014-02-22 19:25:23 +000069 mpz_init_zero(&o->mpz);
70 return o;
71}
72
Dave Hylandsc4029e52014-04-07 11:19:51 -070073// This routine expects you to pass in a buffer and size (in *buf and buf_size).
74// If, for some reason, this buffer is too small, then it will allocate a
75// buffer and return the allocated buffer and size in *buf and *buf_size. It
76// is the callers responsibility to free this allocated buffer.
77//
78// The resulting formatted string will be returned from this function and the
79// formatted size will be in *fmt_size.
Damien George88d7bba2014-04-08 23:30:46 +010080//
81// This particular routine should only be called for the mpz representation of the int.
Damien George42f3de92014-10-03 17:44:14 +000082char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size, mp_const_obj_t self_in,
Damien George88d7bba2014-04-08 23:30:46 +010083 int base, const char *prefix, char base_char, char comma) {
84 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
Paul Sokolovskyab7bf282014-05-17 11:08:33 +030085 const mp_obj_int_t *self = self_in;
Dave Hylandsc4029e52014-04-07 11:19:51 -070086
Damien George42f3de92014-10-03 17:44:14 +000087 mp_uint_t needed_size = mpz_as_str_size(&self->mpz, base, prefix, comma);
Dave Hylandsc4029e52014-04-07 11:19:51 -070088 if (needed_size > *buf_size) {
89 *buf = m_new(char, needed_size);
90 *buf_size = needed_size;
91 }
92 char *str = *buf;
93
Damien George88d7bba2014-04-08 23:30:46 +010094 *fmt_size = mpz_as_str_inpl(&self->mpz, base, prefix, base_char, comma, str);
Dave Hylandsc4029e52014-04-07 11:19:51 -070095
96 return str;
97}
98
Damien Georgeffe911d2014-07-24 14:21:37 +010099mp_int_t mp_obj_int_hash(mp_obj_t self_in) {
100 if (MP_OBJ_IS_SMALL_INT(self_in)) {
101 return MP_OBJ_SMALL_INT_VALUE(self_in);
102 }
103 mp_obj_int_t *self = self_in;
104 return mpz_hash(&self->mpz);
105}
106
Dave Hylandsc4029e52014-04-07 11:19:51 -0700107bool mp_obj_int_is_positive(mp_obj_t self_in) {
108 if (MP_OBJ_IS_SMALL_INT(self_in)) {
109 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
110 }
111 mp_obj_int_t *self = self_in;
112 return !self->mpz.neg;
Damien George438c88d2014-02-22 19:25:23 +0000113}
114
Damien George6837d462015-03-14 22:07:30 +0000115// This must handle int and bool types, and must raise a
116// TypeError if the argument is not integral
117mp_obj_t mp_obj_int_abs(mp_obj_t self_in) {
118 if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) {
119 mp_obj_int_t *self = self_in;
120 mp_obj_int_t *self2 = mp_obj_int_new_mpz();
121 mpz_abs_inpl(&self2->mpz, &self->mpz);
122 return self2;
123 } else {
124 mp_int_t val = mp_obj_get_int(self_in);
125 if (val == MP_SMALL_INT_MIN) {
126 return mp_obj_new_int_from_ll(-val);
127 } else {
128 if (val < 0) {
129 val = -val;
130 }
131 return MP_OBJ_NEW_SMALL_INT(val);
132 }
133 }
134}
135
Damien Georgeecc88e92014-08-30 00:35:11 +0100136mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damien George438c88d2014-02-22 19:25:23 +0000137 mp_obj_int_t *o = o_in;
138 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100139 case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
140 case MP_UNARY_OP_POSITIVE: return o_in;
141 case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
142 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 George6ac5dce2014-05-21 19:42:43 +0100143 default: return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +0000144 }
145}
146
Damien Georgeecc88e92014-08-30 00:35:11 +0100147mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000148 const mpz_t *zlhs;
Damien George06201ff2014-03-01 19:50:50 +0000149 const mpz_t *zrhs;
150 mpz_t z_int;
151 mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
Damien George438c88d2014-02-22 19:25:23 +0000152
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000153 // lhs could be a small int (eg small-int + mpz)
154 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
155 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
156 zlhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +0000157 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000158 zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
159 } else {
Damien George0aa5d512014-03-29 17:28:20 +0000160 // unsupported type
Damien George6ac5dce2014-05-21 19:42:43 +0100161 return MP_OBJ_NULL;
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000162 }
163
Damien Georged17926d2014-03-30 13:35:08 +0100164 // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
Damien George438c88d2014-02-22 19:25:23 +0000165 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George06201ff2014-03-01 19:50:50 +0000166 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
167 zrhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +0000168 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien George438c88d2014-02-22 19:25:23 +0000169 zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
Damien Georgefb510b32014-06-01 13:32:54 +0100170#if MICROPY_PY_BUILTINS_FLOAT
Damien George0aa5d512014-03-29 17:28:20 +0000171 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
172 return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300173#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0aa5d512014-03-29 17:28:20 +0000174 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
175 return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
176#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300177#endif
Damien George438c88d2014-02-22 19:25:23 +0000178 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100179 // delegate to generic function to check for extra cases
180 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Damien George438c88d2014-02-22 19:25:23 +0000181 }
182
Damien George52608102014-03-08 15:04:54 +0000183 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100184#if MICROPY_PY_BUILTINS_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100185 } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
Damien George52608102014-03-08 15:04:54 +0000186 mp_float_t flhs = mpz_as_float(zlhs);
187 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +0000188 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +0000189#endif
Damien George438c88d2014-02-22 19:25:23 +0000190
Damien Georged17926d2014-03-30 13:35:08 +0100191 } else if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien George438c88d2014-02-22 19:25:23 +0000192 mp_obj_int_t *res = mp_obj_int_new_mpz();
193
194 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100195 case MP_BINARY_OP_ADD:
196 case MP_BINARY_OP_INPLACE_ADD:
Damien George438c88d2014-02-22 19:25:23 +0000197 mpz_add_inpl(&res->mpz, zlhs, zrhs);
198 break;
Damien Georged17926d2014-03-30 13:35:08 +0100199 case MP_BINARY_OP_SUBTRACT:
200 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George438c88d2014-02-22 19:25:23 +0000201 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
202 break;
Damien Georged17926d2014-03-30 13:35:08 +0100203 case MP_BINARY_OP_MULTIPLY:
204 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien George438c88d2014-02-22 19:25:23 +0000205 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
206 break;
Damien Georged17926d2014-03-30 13:35:08 +0100207 case MP_BINARY_OP_FLOOR_DIVIDE:
208 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
Damien George438c88d2014-02-22 19:25:23 +0000209 mpz_t rem; mpz_init_zero(&rem);
210 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
Damien Georgeecf5b772014-04-04 11:13:51 +0000211 if (zlhs->neg != zrhs->neg) {
Rachel Dowdall56402792014-03-22 20:19:24 +0000212 if (!mpz_is_zero(&rem)) {
213 mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
214 mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
215 }
216 }
Damien George438c88d2014-02-22 19:25:23 +0000217 mpz_deinit(&rem);
218 break;
219 }
Damien Georged17926d2014-03-30 13:35:08 +0100220 case MP_BINARY_OP_MODULO:
221 case MP_BINARY_OP_INPLACE_MODULO: {
Damien George2d7ff072014-03-20 16:28:41 +0000222 mpz_t quo; mpz_init_zero(&quo);
223 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
224 mpz_deinit(&quo);
Damien Georgeecf5b772014-04-04 11:13:51 +0000225 // Check signs and do Python style modulo
226 if (zlhs->neg != zrhs->neg) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000227 mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
228 }
Damien George2d7ff072014-03-20 16:28:41 +0000229 break;
230 }
Damien George438c88d2014-02-22 19:25:23 +0000231
Damien Georged17926d2014-03-30 13:35:08 +0100232 case MP_BINARY_OP_AND:
233 case MP_BINARY_OP_INPLACE_AND:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200234 mpz_and_inpl(&res->mpz, zlhs, zrhs);
235 break;
Damien Georged17926d2014-03-30 13:35:08 +0100236 case MP_BINARY_OP_OR:
237 case MP_BINARY_OP_INPLACE_OR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200238 mpz_or_inpl(&res->mpz, zlhs, zrhs);
239 break;
Damien Georged17926d2014-03-30 13:35:08 +0100240 case MP_BINARY_OP_XOR:
241 case MP_BINARY_OP_INPLACE_XOR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200242 mpz_xor_inpl(&res->mpz, zlhs, zrhs);
243 break;
Damien George438c88d2014-02-22 19:25:23 +0000244
Damien Georged17926d2014-03-30 13:35:08 +0100245 case MP_BINARY_OP_LSHIFT:
246 case MP_BINARY_OP_INPLACE_LSHIFT:
247 case MP_BINARY_OP_RSHIFT:
248 case MP_BINARY_OP_INPLACE_RSHIFT: {
Damien Georgec9aa58e2014-07-31 13:41:43 +0000249 mp_int_t irhs = mp_obj_int_get_checked(rhs_in);
Damien George06201ff2014-03-01 19:50:50 +0000250 if (irhs < 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100251 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George06201ff2014-03-01 19:50:50 +0000252 }
Damien Georged17926d2014-03-30 13:35:08 +0100253 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
Damien George06201ff2014-03-01 19:50:50 +0000254 mpz_shl_inpl(&res->mpz, zlhs, irhs);
255 } else {
256 mpz_shr_inpl(&res->mpz, zlhs, irhs);
257 }
258 break;
259 }
Damien George438c88d2014-02-22 19:25:23 +0000260
Damien Georged17926d2014-03-30 13:35:08 +0100261 case MP_BINARY_OP_POWER:
262 case MP_BINARY_OP_INPLACE_POWER:
Damien George438c88d2014-02-22 19:25:23 +0000263 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
264 break;
265
266 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100267 return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +0000268 }
269
270 return res;
271
272 } else {
273 int cmp = mpz_cmp(zlhs, zrhs);
274 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100275 case MP_BINARY_OP_LESS:
Damien George438c88d2014-02-22 19:25:23 +0000276 return MP_BOOL(cmp < 0);
Damien Georged17926d2014-03-30 13:35:08 +0100277 case MP_BINARY_OP_MORE:
Damien George438c88d2014-02-22 19:25:23 +0000278 return MP_BOOL(cmp > 0);
Damien Georged17926d2014-03-30 13:35:08 +0100279 case MP_BINARY_OP_LESS_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000280 return MP_BOOL(cmp <= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100281 case MP_BINARY_OP_MORE_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000282 return MP_BOOL(cmp >= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100283 case MP_BINARY_OP_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000284 return MP_BOOL(cmp == 0);
Damien George438c88d2014-02-22 19:25:23 +0000285
286 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100287 return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +0000288 }
289 }
290}
291
Damien George40f3c022014-07-03 13:25:24 +0100292mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100293 if (MP_SMALL_INT_FITS(value)) {
Damien George438c88d2014-02-22 19:25:23 +0000294 return MP_OBJ_NEW_SMALL_INT(value);
295 }
296 return mp_obj_new_int_from_ll(value);
297}
298
299mp_obj_t mp_obj_new_int_from_ll(long long val) {
300 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George95307432014-09-10 22:10:33 +0100301 mpz_set_from_ll(&o->mpz, val, true);
302 return o;
303}
304
305mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
306 mp_obj_int_t *o = mp_obj_int_new_mpz();
307 mpz_set_from_ll(&o->mpz, val, false);
Damien George438c88d2014-02-22 19:25:23 +0000308 return o;
309}
310
Damien George40f3c022014-07-03 13:25:24 +0100311mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Damien George438c88d2014-02-22 19:25:23 +0000312 // SMALL_INT accepts only signed numbers, of one bit less size
313 // than word size, which totals 2 bits less for unsigned numbers.
314 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
315 return MP_OBJ_NEW_SMALL_INT(value);
316 }
317 return mp_obj_new_int_from_ll(value);
318}
319
Paul Sokolovsky12033df2014-12-30 00:22:10 +0200320#if MICROPY_PY_BUILTINS_FLOAT
321mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
Damien George6fd4b362015-01-02 23:04:09 +0000322 int cl = fpclassify(val);
323 if (cl == FP_INFINITE) {
324 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int"));
325 } else if (cl == FP_NAN) {
326 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int"));
327 } else {
David Steinberg0fb17f62015-01-13 15:22:30 +0000328 mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
329 if (icl == MP_FP_CLASS_FIT_SMALLINT) {
330 return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
331 } else {
332 mp_obj_int_t *o = mp_obj_int_new_mpz();
333 mpz_set_from_float(&o->mpz, val);
334 return o;
335 }
Damien George6fd4b362015-01-02 23:04:09 +0000336 }
Paul Sokolovsky12033df2014-12-30 00:22:10 +0200337}
338#endif
339
Damien Georged182b982014-08-30 14:19:41 +0100340mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) {
Damien George438c88d2014-02-22 19:25:23 +0000341 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien Georged182b982014-08-30 14:19:41 +0100342 mp_uint_t n = mpz_set_from_str(&o->mpz, *str, len, neg, base);
Damien George503d6112014-05-28 14:07:21 +0100343 *str += n;
Damien George438c88d2014-02-22 19:25:23 +0000344 return o;
345}
346
Damien Georgebe6d8be2014-12-05 23:13:52 +0000347mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Damien George438c88d2014-02-22 19:25:23 +0000348 if (MP_OBJ_IS_SMALL_INT(self_in)) {
349 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000350 } else {
Damien George503d6112014-05-28 14:07:21 +0100351 const mp_obj_int_t *self = self_in;
Damien Georgebe6d8be2014-12-05 23:13:52 +0000352 // hash returns actual int value if it fits in mp_int_t
Damien Georgec9aa58e2014-07-31 13:41:43 +0000353 return mpz_hash(&self->mpz);
Damien George438c88d2014-02-22 19:25:23 +0000354 }
Damien George438c88d2014-02-22 19:25:23 +0000355}
356
Damien George40f3c022014-07-03 13:25:24 +0100357mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Damien George8270e382014-04-03 11:00:54 +0000358 if (MP_OBJ_IS_SMALL_INT(self_in)) {
359 return MP_OBJ_SMALL_INT_VALUE(self_in);
360 } else {
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300361 const mp_obj_int_t *self = self_in;
Damien George40f3c022014-07-03 13:25:24 +0100362 mp_int_t value;
Damien George8270e382014-04-03 11:00:54 +0000363 if (mpz_as_int_checked(&self->mpz, &value)) {
364 return value;
365 } else {
366 // overflow
Damien Georgeea13f402014-04-05 18:32:08 +0100367 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word"));
Damien George8270e382014-04-03 11:00:54 +0000368 }
369 }
Damien George438c88d2014-02-22 19:25:23 +0000370}
371
Damien Georgefb510b32014-06-01 13:32:54 +0100372#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000373mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
374 if (MP_OBJ_IS_SMALL_INT(self_in)) {
375 return MP_OBJ_SMALL_INT_VALUE(self_in);
376 } else {
377 mp_obj_int_t *self = self_in;
378 return mpz_as_float(&self->mpz);
379 }
380}
381#endif
382
Damien George438c88d2014-02-22 19:25:23 +0000383#endif