blob: 137d514de22f604cb299d3a9a961816bb1ecd58e [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)
Damien George8b4fb4f2015-09-15 16:15:57 +010047STATIC const mpz_dig_t maxsize_dig[] = {
48 #define NUM_DIG 1
Paul Sokolovsky722e5622014-09-06 19:17:23 +030049 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) & DIG_MASK,
50 #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) > DIG_MASK
Damien George8b4fb4f2015-09-15 16:15:57 +010051 #undef NUM_DIG
52 #define NUM_DIG 2
53 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) & DIG_MASK,
54 #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) > DIG_MASK
55 #undef NUM_DIG
56 #define NUM_DIG 3
57 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) & DIG_MASK,
58 #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) > DIG_MASK
59 #undef NUM_DIG
60 #define NUM_DIG 4
61 (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) & DIG_MASK,
62 #if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) > DIG_MASK
63 #error cannot encode MP_SSIZE_MAX as mpz
64 #endif
65 #endif
66 #endif
Damien George8002d5d2014-09-06 17:37:29 +010067 #endif
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030068};
69const mp_obj_int_t mp_maxsize_obj = {
70 {&mp_type_int},
Damien George8b4fb4f2015-09-15 16:15:57 +010071 {.fixed_dig = 1, .len = NUM_DIG, .alloc = NUM_DIG, .dig = (mpz_dig_t*)maxsize_dig}
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030072};
73#undef DIG_MASK
Damien George8b4fb4f2015-09-15 16:15:57 +010074#undef NUM_DIG
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030075#endif
76
Damien George438c88d2014-02-22 19:25:23 +000077STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
78 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +000079 o->base.type = &mp_type_int;
Damien George438c88d2014-02-22 19:25:23 +000080 mpz_init_zero(&o->mpz);
81 return o;
82}
83
Dave Hylandsc4029e52014-04-07 11:19:51 -070084// This routine expects you to pass in a buffer and size (in *buf and buf_size).
85// If, for some reason, this buffer is too small, then it will allocate a
86// buffer and return the allocated buffer and size in *buf and *buf_size. It
87// is the callers responsibility to free this allocated buffer.
88//
89// The resulting formatted string will be returned from this function and the
90// formatted size will be in *fmt_size.
Damien George88d7bba2014-04-08 23:30:46 +010091//
92// This particular routine should only be called for the mpz representation of the int.
Damien George42f3de92014-10-03 17:44:14 +000093char *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 +010094 int base, const char *prefix, char base_char, char comma) {
95 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
Damien George999cedb2015-11-27 17:01:44 +000096 const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
Dave Hylandsc4029e52014-04-07 11:19:51 -070097
Damien George8bb7d952016-10-11 13:11:32 +110098 mp_uint_t needed_size = mp_int_format_size(mpz_max_num_bits(&self->mpz), base, prefix, comma);
Dave Hylandsc4029e52014-04-07 11:19:51 -070099 if (needed_size > *buf_size) {
100 *buf = m_new(char, needed_size);
101 *buf_size = needed_size;
102 }
103 char *str = *buf;
104
Damien George88d7bba2014-04-08 23:30:46 +0100105 *fmt_size = mpz_as_str_inpl(&self->mpz, base, prefix, base_char, comma, str);
Dave Hylandsc4029e52014-04-07 11:19:51 -0700106
107 return str;
108}
109
Damien George271d18e2015-04-25 23:16:39 +0100110void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len, byte *buf) {
111 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
Damien George999cedb2015-11-27 17:01:44 +0000112 mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
Damien George271d18e2015-04-25 23:16:39 +0100113 mpz_as_bytes(&self->mpz, big_endian, len, buf);
114}
115
Damien Georged6b31e42016-01-07 14:29:12 +0000116int mp_obj_int_sign(mp_obj_t self_in) {
Dave Hylandsc4029e52014-04-07 11:19:51 -0700117 if (MP_OBJ_IS_SMALL_INT(self_in)) {
Damien Georged6b31e42016-01-07 14:29:12 +0000118 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(self_in);
119 if (val < 0) {
120 return -1;
121 } else if (val > 0) {
122 return 1;
123 } else {
124 return 0;
125 }
Dave Hylandsc4029e52014-04-07 11:19:51 -0700126 }
Damien George999cedb2015-11-27 17:01:44 +0000127 mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georged6b31e42016-01-07 14:29:12 +0000128 if (self->mpz.len == 0) {
129 return 0;
130 } else if (self->mpz.neg == 0) {
131 return 1;
132 } else {
133 return -1;
134 }
Damien George438c88d2014-02-22 19:25:23 +0000135}
136
Damien George6837d462015-03-14 22:07:30 +0000137// This must handle int and bool types, and must raise a
138// TypeError if the argument is not integral
139mp_obj_t mp_obj_int_abs(mp_obj_t self_in) {
140 if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) {
Damien George999cedb2015-11-27 17:01:44 +0000141 mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
Damien George6837d462015-03-14 22:07:30 +0000142 mp_obj_int_t *self2 = mp_obj_int_new_mpz();
143 mpz_abs_inpl(&self2->mpz, &self->mpz);
Damien George999cedb2015-11-27 17:01:44 +0000144 return MP_OBJ_FROM_PTR(self2);
Damien George6837d462015-03-14 22:07:30 +0000145 } else {
146 mp_int_t val = mp_obj_get_int(self_in);
147 if (val == MP_SMALL_INT_MIN) {
148 return mp_obj_new_int_from_ll(-val);
149 } else {
150 if (val < 0) {
151 val = -val;
152 }
153 return MP_OBJ_NEW_SMALL_INT(val);
154 }
155 }
156}
157
Damien Georgeecc88e92014-08-30 00:35:11 +0100158mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damien George999cedb2015-11-27 17:01:44 +0000159 mp_obj_int_t *o = MP_OBJ_TO_PTR(o_in);
Damien George438c88d2014-02-22 19:25:23 +0000160 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300161 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(!mpz_is_zero(&o->mpz));
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000162 case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mpz_hash(&o->mpz));
Damien Georged17926d2014-03-30 13:35:08 +0100163 case MP_UNARY_OP_POSITIVE: return o_in;
Damien George999cedb2015-11-27 17:01:44 +0000164 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); }
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); }
Damien George6ac5dce2014-05-21 19:42:43 +0100166 default: return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +0000167 }
168}
169
Damien Georgeecc88e92014-08-30 00:35:11 +0100170mp_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 +0000171 const mpz_t *zlhs;
Damien George06201ff2014-03-01 19:50:50 +0000172 const mpz_t *zrhs;
173 mpz_t z_int;
174 mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
Damien George438c88d2014-02-22 19:25:23 +0000175
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000176 // lhs could be a small int (eg small-int + mpz)
177 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
178 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
179 zlhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +0000180 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien George999cedb2015-11-27 17:01:44 +0000181 zlhs = &((mp_obj_int_t*)MP_OBJ_TO_PTR(lhs_in))->mpz;
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000182 } else {
Damien George0aa5d512014-03-29 17:28:20 +0000183 // unsupported type
Damien George6ac5dce2014-05-21 19:42:43 +0100184 return MP_OBJ_NULL;
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000185 }
186
Damien Georged17926d2014-03-30 13:35:08 +0100187 // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
Damien George438c88d2014-02-22 19:25:23 +0000188 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George06201ff2014-03-01 19:50:50 +0000189 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
190 zrhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +0000191 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien George999cedb2015-11-27 17:01:44 +0000192 zrhs = &((mp_obj_int_t*)MP_OBJ_TO_PTR(rhs_in))->mpz;
Damien Georgefb510b32014-06-01 13:32:54 +0100193#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeaaef1852015-08-20 23:30:12 +0100194 } else if (mp_obj_is_float(rhs_in)) {
Damien George0aa5d512014-03-29 17:28:20 +0000195 return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300196#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0aa5d512014-03-29 17:28:20 +0000197 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
198 return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
199#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300200#endif
Damien George438c88d2014-02-22 19:25:23 +0000201 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100202 // delegate to generic function to check for extra cases
203 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Damien George438c88d2014-02-22 19:25:23 +0000204 }
205
Damien George52608102014-03-08 15:04:54 +0000206 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100207#if MICROPY_PY_BUILTINS_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100208 } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
Damien Georgee5635f42015-10-01 22:48:48 +0100209 if (mpz_is_zero(zrhs)) {
210 goto zero_division_error;
211 }
Damien George52608102014-03-08 15:04:54 +0000212 mp_float_t flhs = mpz_as_float(zlhs);
213 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +0000214 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +0000215#endif
Damien George438c88d2014-02-22 19:25:23 +0000216
Damien Georged17926d2014-03-30 13:35:08 +0100217 } else if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien George438c88d2014-02-22 19:25:23 +0000218 mp_obj_int_t *res = mp_obj_int_new_mpz();
219
220 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100221 case MP_BINARY_OP_ADD:
222 case MP_BINARY_OP_INPLACE_ADD:
Damien George438c88d2014-02-22 19:25:23 +0000223 mpz_add_inpl(&res->mpz, zlhs, zrhs);
224 break;
Damien Georged17926d2014-03-30 13:35:08 +0100225 case MP_BINARY_OP_SUBTRACT:
226 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George438c88d2014-02-22 19:25:23 +0000227 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
228 break;
Damien Georged17926d2014-03-30 13:35:08 +0100229 case MP_BINARY_OP_MULTIPLY:
230 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien George438c88d2014-02-22 19:25:23 +0000231 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
232 break;
Damien Georged17926d2014-03-30 13:35:08 +0100233 case MP_BINARY_OP_FLOOR_DIVIDE:
234 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
Damien Georgee5635f42015-10-01 22:48:48 +0100235 if (mpz_is_zero(zrhs)) {
236 zero_division_error:
237 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError,
238 "division by zero"));
239 }
Damien George438c88d2014-02-22 19:25:23 +0000240 mpz_t rem; mpz_init_zero(&rem);
241 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
242 mpz_deinit(&rem);
243 break;
244 }
Damien Georged17926d2014-03-30 13:35:08 +0100245 case MP_BINARY_OP_MODULO:
246 case MP_BINARY_OP_INPLACE_MODULO: {
Damien Georgee5635f42015-10-01 22:48:48 +0100247 if (mpz_is_zero(zrhs)) {
248 goto zero_division_error;
249 }
Damien George2d7ff072014-03-20 16:28:41 +0000250 mpz_t quo; mpz_init_zero(&quo);
251 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
252 mpz_deinit(&quo);
253 break;
254 }
Damien George438c88d2014-02-22 19:25:23 +0000255
Damien Georged17926d2014-03-30 13:35:08 +0100256 case MP_BINARY_OP_AND:
257 case MP_BINARY_OP_INPLACE_AND:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200258 mpz_and_inpl(&res->mpz, zlhs, zrhs);
259 break;
Damien Georged17926d2014-03-30 13:35:08 +0100260 case MP_BINARY_OP_OR:
261 case MP_BINARY_OP_INPLACE_OR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200262 mpz_or_inpl(&res->mpz, zlhs, zrhs);
263 break;
Damien Georged17926d2014-03-30 13:35:08 +0100264 case MP_BINARY_OP_XOR:
265 case MP_BINARY_OP_INPLACE_XOR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200266 mpz_xor_inpl(&res->mpz, zlhs, zrhs);
267 break;
Damien George438c88d2014-02-22 19:25:23 +0000268
Damien Georged17926d2014-03-30 13:35:08 +0100269 case MP_BINARY_OP_LSHIFT:
270 case MP_BINARY_OP_INPLACE_LSHIFT:
271 case MP_BINARY_OP_RSHIFT:
272 case MP_BINARY_OP_INPLACE_RSHIFT: {
Damien Georgec9aa58e2014-07-31 13:41:43 +0000273 mp_int_t irhs = mp_obj_int_get_checked(rhs_in);
Damien George06201ff2014-03-01 19:50:50 +0000274 if (irhs < 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100275 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George06201ff2014-03-01 19:50:50 +0000276 }
Damien Georged17926d2014-03-30 13:35:08 +0100277 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
Damien George06201ff2014-03-01 19:50:50 +0000278 mpz_shl_inpl(&res->mpz, zlhs, irhs);
279 } else {
280 mpz_shr_inpl(&res->mpz, zlhs, irhs);
281 }
282 break;
283 }
Damien George438c88d2014-02-22 19:25:23 +0000284
Damien Georged17926d2014-03-30 13:35:08 +0100285 case MP_BINARY_OP_POWER:
286 case MP_BINARY_OP_INPLACE_POWER:
Damien George438c88d2014-02-22 19:25:23 +0000287 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
288 break;
289
Damien Georgee9ce00d2015-06-13 22:40:50 +0100290 case MP_BINARY_OP_DIVMOD: {
Damien Georgee5635f42015-10-01 22:48:48 +0100291 if (mpz_is_zero(zrhs)) {
292 goto zero_division_error;
293 }
Damien Georgee9ce00d2015-06-13 22:40:50 +0100294 mp_obj_int_t *quo = mp_obj_int_new_mpz();
295 mpz_divmod_inpl(&quo->mpz, &res->mpz, zlhs, zrhs);
Damien George999cedb2015-11-27 17:01:44 +0000296 mp_obj_t tuple[2] = {MP_OBJ_FROM_PTR(quo), MP_OBJ_FROM_PTR(res)};
Damien Georgee9ce00d2015-06-13 22:40:50 +0100297 return mp_obj_new_tuple(2, tuple);
298 }
299
Damien George438c88d2014-02-22 19:25:23 +0000300 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100301 return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +0000302 }
303
Damien George999cedb2015-11-27 17:01:44 +0000304 return MP_OBJ_FROM_PTR(res);
Damien George438c88d2014-02-22 19:25:23 +0000305
306 } else {
307 int cmp = mpz_cmp(zlhs, zrhs);
308 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100309 case MP_BINARY_OP_LESS:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300310 return mp_obj_new_bool(cmp < 0);
Damien Georged17926d2014-03-30 13:35:08 +0100311 case MP_BINARY_OP_MORE:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300312 return mp_obj_new_bool(cmp > 0);
Damien Georged17926d2014-03-30 13:35:08 +0100313 case MP_BINARY_OP_LESS_EQUAL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300314 return mp_obj_new_bool(cmp <= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100315 case MP_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300316 return mp_obj_new_bool(cmp >= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100317 case MP_BINARY_OP_EQUAL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300318 return mp_obj_new_bool(cmp == 0);
Damien George438c88d2014-02-22 19:25:23 +0000319
320 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100321 return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +0000322 }
323 }
324}
325
Damien George40f3c022014-07-03 13:25:24 +0100326mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100327 if (MP_SMALL_INT_FITS(value)) {
Damien George438c88d2014-02-22 19:25:23 +0000328 return MP_OBJ_NEW_SMALL_INT(value);
329 }
330 return mp_obj_new_int_from_ll(value);
331}
332
333mp_obj_t mp_obj_new_int_from_ll(long long val) {
334 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George95307432014-09-10 22:10:33 +0100335 mpz_set_from_ll(&o->mpz, val, true);
Damien George999cedb2015-11-27 17:01:44 +0000336 return MP_OBJ_FROM_PTR(o);
Damien George95307432014-09-10 22:10:33 +0100337}
338
339mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
340 mp_obj_int_t *o = mp_obj_int_new_mpz();
341 mpz_set_from_ll(&o->mpz, val, false);
Damien George999cedb2015-11-27 17:01:44 +0000342 return MP_OBJ_FROM_PTR(o);
Damien George438c88d2014-02-22 19:25:23 +0000343}
344
Damien George40f3c022014-07-03 13:25:24 +0100345mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Damien George9ae51252016-03-10 21:52:56 +0000346 // SMALL_INT accepts only signed numbers, so make sure the input
347 // value fits completely in the small-int positive range.
348 if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
Damien George438c88d2014-02-22 19:25:23 +0000349 return MP_OBJ_NEW_SMALL_INT(value);
350 }
Damien George5e981032015-04-22 23:17:34 +0100351 return mp_obj_new_int_from_ull(value);
Damien George438c88d2014-02-22 19:25:23 +0000352}
353
Paul Sokolovsky12033df2014-12-30 00:22:10 +0200354#if MICROPY_PY_BUILTINS_FLOAT
355mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
Damien George6fd4b362015-01-02 23:04:09 +0000356 int cl = fpclassify(val);
357 if (cl == FP_INFINITE) {
358 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int"));
359 } else if (cl == FP_NAN) {
360 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int"));
361 } else {
David Steinberg0fb17f62015-01-13 15:22:30 +0000362 mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
363 if (icl == MP_FP_CLASS_FIT_SMALLINT) {
364 return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
365 } else {
366 mp_obj_int_t *o = mp_obj_int_new_mpz();
367 mpz_set_from_float(&o->mpz, val);
Damien George999cedb2015-11-27 17:01:44 +0000368 return MP_OBJ_FROM_PTR(o);
David Steinberg0fb17f62015-01-13 15:22:30 +0000369 }
Damien George6fd4b362015-01-02 23:04:09 +0000370 }
Paul Sokolovsky12033df2014-12-30 00:22:10 +0200371}
372#endif
373
Damien Georged182b982014-08-30 14:19:41 +0100374mp_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 +0000375 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien Georged182b982014-08-30 14:19:41 +0100376 mp_uint_t n = mpz_set_from_str(&o->mpz, *str, len, neg, base);
Damien George503d6112014-05-28 14:07:21 +0100377 *str += n;
Damien George999cedb2015-11-27 17:01:44 +0000378 return MP_OBJ_FROM_PTR(o);
Damien George438c88d2014-02-22 19:25:23 +0000379}
380
Damien Georgebe6d8be2014-12-05 23:13:52 +0000381mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Damien George438c88d2014-02-22 19:25:23 +0000382 if (MP_OBJ_IS_SMALL_INT(self_in)) {
383 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000384 } else {
Damien George999cedb2015-11-27 17:01:44 +0000385 const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgebe6d8be2014-12-05 23:13:52 +0000386 // hash returns actual int value if it fits in mp_int_t
Damien Georgec9aa58e2014-07-31 13:41:43 +0000387 return mpz_hash(&self->mpz);
Damien George438c88d2014-02-22 19:25:23 +0000388 }
Damien George438c88d2014-02-22 19:25:23 +0000389}
390
Damien George40f3c022014-07-03 13:25:24 +0100391mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Damien George8270e382014-04-03 11:00:54 +0000392 if (MP_OBJ_IS_SMALL_INT(self_in)) {
393 return MP_OBJ_SMALL_INT_VALUE(self_in);
394 } else {
Damien George999cedb2015-11-27 17:01:44 +0000395 const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
Damien George40f3c022014-07-03 13:25:24 +0100396 mp_int_t value;
Damien George8270e382014-04-03 11:00:54 +0000397 if (mpz_as_int_checked(&self->mpz, &value)) {
398 return value;
399 } else {
400 // overflow
Damien Georgeea13f402014-04-05 18:32:08 +0100401 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word"));
Damien George8270e382014-04-03 11:00:54 +0000402 }
403 }
Damien George438c88d2014-02-22 19:25:23 +0000404}
405
Damien Georgefb510b32014-06-01 13:32:54 +0100406#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000407mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
408 if (MP_OBJ_IS_SMALL_INT(self_in)) {
409 return MP_OBJ_SMALL_INT_VALUE(self_in);
410 } else {
Damien George999cedb2015-11-27 17:01:44 +0000411 mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000412 return mpz_as_float(&self->mpz);
413 }
414}
415#endif
416
Damien George438c88d2014-02-22 19:25:23 +0000417#endif