blob: 270e16969416802a499d59b6f6b2fa4d8cb63456 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 George660aef62014-04-02 12:22:07 +010027#include <stdlib.h>
Damien George45b43c22014-01-05 01:50:45 +000028#include <assert.h>
Dave Hylandsc4029e52014-04-07 11:19:51 -070029#include <string.h>
Damien George45b43c22014-01-05 01:50:45 +000030
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/parsenum.h"
32#include "py/smallint.h"
33#include "py/objint.h"
34#include "py/objstr.h"
Damien George51dfcb42015-01-01 20:27:54 +000035#include "py/runtime.h"
Damien George271d18e2015-04-25 23:16:39 +010036#include "py/binary.h"
Paul Sokolovsky48b35722014-01-12 17:30:48 +020037
Damien Georgefb510b32014-06-01 13:32:54 +010038#if MICROPY_PY_BUILTINS_FLOAT
Damien George6433bd92014-03-30 23:13:16 +010039#include <math.h>
40#endif
41
Damien Georgee8208a72014-04-04 15:08:23 +010042// This dispatcher function is expected to be independent of the implementation of long int
Damien George5b3f0b72016-01-03 15:55:55 +000043STATIC mp_obj_t mp_obj_int_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000044 (void)type_in;
Damien Georgeee7a8802014-05-11 18:37:21 +010045 mp_arg_check_num(n_args, n_kw, 0, 2, false);
Damien George20006db2014-01-18 14:10:48 +000046
Damien George45b43c22014-01-05 01:50:45 +000047 switch (n_args) {
48 case 0:
49 return MP_OBJ_NEW_SMALL_INT(0);
50
51 case 1:
Damien George813ed3b2014-05-28 14:09:46 +010052 if (MP_OBJ_IS_INT(args[0])) {
53 // already an int (small or long), just return it
54 return args[0];
Dave Hylandsb7f7c652014-08-26 12:44:46 -070055 } else if (MP_OBJ_IS_STR_OR_BYTES(args[0])) {
Damien George5573f9f2014-01-15 22:58:39 +000056 // a string, parse it
Damien George6b341072017-03-25 19:48:18 +110057 size_t l;
Damien George698ec212014-02-08 18:17:23 +000058 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George7d414a12015-02-08 01:57:40 +000059 return mp_parse_num_integer(s, l, 0, NULL);
Damien Georgefb510b32014-06-01 13:32:54 +010060#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeaaef1852015-08-20 23:30:12 +010061 } else if (mp_obj_is_float(args[0])) {
Paul Sokolovsky12033df2014-12-30 00:22:10 +020062 return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
Damien George6433bd92014-03-30 23:13:16 +010063#endif
Damien George5573f9f2014-01-15 22:58:39 +000064 } else {
Damien George813ed3b2014-05-28 14:09:46 +010065 // try to convert to small int (eg from bool)
Damien George5573f9f2014-01-15 22:58:39 +000066 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
67 }
Damien George45b43c22014-01-05 01:50:45 +000068
xybc178ea42014-01-14 21:39:05 +080069 case 2:
Damien Georgeee7a8802014-05-11 18:37:21 +010070 default: {
Damien George5573f9f2014-01-15 22:58:39 +000071 // should be a string, parse it
72 // TODO proper error checking of argument types
Damien George6b341072017-03-25 19:48:18 +110073 size_t l;
Damien George698ec212014-02-08 18:17:23 +000074 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George7d414a12015-02-08 01:57:40 +000075 return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]), NULL);
Damien George5fa93b62014-01-22 14:35:10 +000076 }
Damien George45b43c22014-01-05 01:50:45 +000077 }
78}
79
David Steinbergca377b12015-01-13 15:21:17 +000080#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgefc245d12017-04-04 16:45:49 +100081
82typedef enum {
83 MP_FP_CLASS_FIT_SMALLINT,
84 MP_FP_CLASS_FIT_LONGINT,
85 MP_FP_CLASS_OVERFLOW
86} mp_fp_as_int_class_t;
87
88STATIC mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
David Steinbergca377b12015-01-13 15:21:17 +000089 union {
90 mp_float_t f;
91#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
92 uint32_t i;
93#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
94 uint32_t i[2];
95#endif
96 } u = {val};
97
98 uint32_t e;
99#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
100 e = u.i;
101#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
102 e = u.i[MP_ENDIANNESS_LITTLE];
103#endif
104#define MP_FLOAT_SIGN_SHIFT_I32 ((MP_FLOAT_FRAC_BITS + MP_FLOAT_EXP_BITS) % 32)
105#define MP_FLOAT_EXP_SHIFT_I32 (MP_FLOAT_FRAC_BITS % 32)
106
Tom Collins145796f2017-06-30 16:23:29 -0700107 if (e & (1U << MP_FLOAT_SIGN_SHIFT_I32)) {
David Steinbergca377b12015-01-13 15:21:17 +0000108#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
109 e |= u.i[MP_ENDIANNESS_BIG] != 0;
110#endif
Damien Georgec0735192017-03-23 23:51:35 +1100111 if ((e & ~(1 << MP_FLOAT_SIGN_SHIFT_I32)) == 0) {
112 // handle case of -0 (when sign is set but rest of bits are zero)
113 e = 0;
114 } else {
115 e += ((1 << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32;
116 }
David Steinbergca377b12015-01-13 15:21:17 +0000117 } else {
118 e &= ~((1 << MP_FLOAT_EXP_SHIFT_I32) - 1);
119 }
Damien Georgeda3dffa2016-01-08 17:57:30 +0000120 // 8 * sizeof(uintptr_t) counts the number of bits for a small int
121 // TODO provide a way to configure this properly
122 if (e <= ((8 * sizeof(uintptr_t) + MP_FLOAT_EXP_BIAS - 3) << MP_FLOAT_EXP_SHIFT_I32)) {
David Steinbergca377b12015-01-13 15:21:17 +0000123 return MP_FP_CLASS_FIT_SMALLINT;
124 }
125#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
126 if (e <= (((sizeof(long long) * BITS_PER_BYTE) + MP_FLOAT_EXP_BIAS - 2) << MP_FLOAT_EXP_SHIFT_I32)) {
127 return MP_FP_CLASS_FIT_LONGINT;
128 }
129#endif
130#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
131 return MP_FP_CLASS_FIT_LONGINT;
132#else
133 return MP_FP_CLASS_OVERFLOW;
134#endif
135}
136#undef MP_FLOAT_SIGN_SHIFT_I32
137#undef MP_FLOAT_EXP_SHIFT_I32
Damien Georgefc245d12017-04-04 16:45:49 +1000138
139mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
140 int cl = fpclassify(val);
141 if (cl == FP_INFINITE) {
142 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "can't convert inf to int"));
143 } else if (cl == FP_NAN) {
144 mp_raise_ValueError("can't convert NaN to int");
145 } else {
146 mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
147 if (icl == MP_FP_CLASS_FIT_SMALLINT) {
148 return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
149 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
150 } else {
151 mp_obj_int_t *o = mp_obj_int_new_mpz();
152 mpz_set_from_float(&o->mpz, val);
153 return MP_OBJ_FROM_PTR(o);
154 }
155 #else
156 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
157 } else if (icl == MP_FP_CLASS_FIT_LONGINT) {
158 return mp_obj_new_int_from_ll((long long)val);
159 #endif
160 } else {
161 mp_raise_ValueError("float too big");
162 }
163 #endif
164 }
165}
166
David Steinbergca377b12015-01-13 15:21:17 +0000167#endif
168
Damien Georged1ae6ae2017-03-14 14:54:20 +1100169#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
170typedef mp_longint_impl_t fmt_int_t;
Damien Georgee31fbd92017-04-11 15:18:35 +1000171typedef unsigned long long fmt_uint_t;
Damien Georged1ae6ae2017-03-14 14:54:20 +1100172#else
173typedef mp_int_t fmt_int_t;
Damien Georgee31fbd92017-04-11 15:18:35 +1000174typedef mp_uint_t fmt_uint_t;
Damien Georged1ae6ae2017-03-14 14:54:20 +1100175#endif
176
Damien George7f9d1d62015-04-09 23:56:15 +0100177void mp_obj_int_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000178 (void)kind;
Dave Hylandsc4029e52014-04-07 11:19:51 -0700179 // The size of this buffer is rather arbitrary. If it's not large
180 // enough, a dynamic one will be allocated.
Damien Georged1ae6ae2017-03-14 14:54:20 +1100181 char stack_buf[sizeof(fmt_int_t) * 4];
Dave Hylandsc4029e52014-04-07 11:19:51 -0700182 char *buf = stack_buf;
Damien George6dff3df2016-10-11 13:20:11 +1100183 size_t buf_size = sizeof(stack_buf);
184 size_t fmt_size;
Dave Hylandsc4029e52014-04-07 11:19:51 -0700185
186 char *str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size, self_in, 10, NULL, '\0', '\0');
Damien George7f9d1d62015-04-09 23:56:15 +0100187 mp_print_str(print, str);
Dave Hylandsc4029e52014-04-07 11:19:51 -0700188
189 if (buf != stack_buf) {
Damien George4d77e1a2015-02-27 09:34:51 +0000190 m_del(char, buf, buf_size);
Damien George5fa93b62014-01-22 14:35:10 +0000191 }
Damien George45b43c22014-01-05 01:50:45 +0000192}
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200193
Damien George1c70cbf2014-08-30 00:38:16 +0100194STATIC const uint8_t log_base2_floor[] = {
Dave Hylandsc4029e52014-04-07 11:19:51 -0700195 0, 1, 1, 2,
196 2, 2, 2, 3,
197 3, 3, 3, 3,
198 3, 3, 3, 4,
Damien Georgeea6a9582016-12-28 12:46:20 +1100199 /* if needed, these are the values for higher bases
Dave Hylandsc4029e52014-04-07 11:19:51 -0700200 4, 4, 4, 4,
201 4, 4, 4, 4,
202 4, 4, 4, 4,
203 4, 4, 4, 5
Damien Georgeea6a9582016-12-28 12:46:20 +1100204 */
Dave Hylandsc4029e52014-04-07 11:19:51 -0700205};
206
Damien George8bb7d952016-10-11 13:11:32 +1100207size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma) {
Damien Georgeea6a9582016-12-28 12:46:20 +1100208 assert(2 <= base && base <= 16);
209 size_t num_digits = num_bits / log_base2_floor[base - 1] + 1;
Damien George8bb7d952016-10-11 13:11:32 +1100210 size_t num_commas = comma ? num_digits / 3 : 0;
211 size_t prefix_len = prefix ? strlen(prefix) : 0;
Dave Hylandsc4029e52014-04-07 11:19:51 -0700212 return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
213}
214
Paul Sokolovskyd72bc272014-06-06 23:08:37 +0300215// This routine expects you to pass in a buffer and size (in *buf and *buf_size).
Dave Hylandsc4029e52014-04-07 11:19:51 -0700216// If, for some reason, this buffer is too small, then it will allocate a
217// buffer and return the allocated buffer and size in *buf and *buf_size. It
218// is the callers responsibility to free this allocated buffer.
219//
220// The resulting formatted string will be returned from this function and the
221// formatted size will be in *fmt_size.
Damien George6dff3df2016-10-11 13:20:11 +1100222char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in,
Dave Hylandsc4029e52014-04-07 11:19:51 -0700223 int base, const char *prefix, char base_char, char comma) {
Damien George88d7bba2014-04-08 23:30:46 +0100224 fmt_int_t num;
Damien George9884a2c2018-03-02 11:01:24 +1100225 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
226 // Only have small ints; get the integer value to format.
227 num = MP_OBJ_SMALL_INT_VALUE(self_in);
228 #else
Damien George88d7bba2014-04-08 23:30:46 +0100229 if (MP_OBJ_IS_SMALL_INT(self_in)) {
230 // A small int; get the integer value to format.
Damien Georgef66df1e2017-04-11 15:16:09 +1000231 num = MP_OBJ_SMALL_INT_VALUE(self_in);
Damien George9884a2c2018-03-02 11:01:24 +1100232 } else {
233 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
Damien George88d7bba2014-04-08 23:30:46 +0100234 // Not a small int.
Damien George9884a2c2018-03-02 11:01:24 +1100235 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
Damien George503d6112014-05-28 14:07:21 +0100236 const mp_obj_int_t *self = self_in;
Damien George40f3c022014-07-03 13:25:24 +0100237 // Get the value to format; mp_obj_get_int truncates to mp_int_t.
Damien George88d7bba2014-04-08 23:30:46 +0100238 num = self->val;
Damien George9884a2c2018-03-02 11:01:24 +1100239 #else
Damien George88d7bba2014-04-08 23:30:46 +0100240 // Delegate to the implementation for the long int.
241 return mp_obj_int_formatted_impl(buf, buf_size, fmt_size, self_in, base, prefix, base_char, comma);
Damien George9884a2c2018-03-02 11:01:24 +1100242 #endif
Dave Hylandsc4029e52014-04-07 11:19:51 -0700243 }
Damien George9884a2c2018-03-02 11:01:24 +1100244 #endif
Damien George88d7bba2014-04-08 23:30:46 +0100245
Dave Hylandsc4029e52014-04-07 11:19:51 -0700246 char sign = '\0';
247 if (num < 0) {
248 num = -num;
249 sign = '-';
250 }
251
Damien George6dff3df2016-10-11 13:20:11 +1100252 size_t needed_size = mp_int_format_size(sizeof(fmt_int_t) * 8, base, prefix, comma);
Dave Hylandsc4029e52014-04-07 11:19:51 -0700253 if (needed_size > *buf_size) {
254 *buf = m_new(char, needed_size);
255 *buf_size = needed_size;
256 }
257 char *str = *buf;
258
259 char *b = str + needed_size;
260 *(--b) = '\0';
261 char *last_comma = b;
262
263 if (num == 0) {
264 *(--b) = '0';
265 } else {
266 do {
Damien Georgee31fbd92017-04-11 15:18:35 +1000267 // The cast to fmt_uint_t is because num is positive and we want unsigned arithmetic
268 int c = (fmt_uint_t)num % base;
269 num = (fmt_uint_t)num / base;
Dave Hylandsc4029e52014-04-07 11:19:51 -0700270 if (c >= 10) {
271 c += base_char - 10;
272 } else {
273 c += '0';
274 }
275 *(--b) = c;
276 if (comma && num != 0 && b > str && (last_comma - b) == 3) {
277 *(--b) = comma;
278 last_comma = b;
279 }
280 }
281 while (b > str && num != 0);
282 }
283 if (prefix) {
284 size_t prefix_len = strlen(prefix);
285 char *p = b - prefix_len;
286 if (p > str) {
287 b = p;
288 while (*prefix) {
289 *p++ = *prefix++;
290 }
291 }
292 }
293 if (sign && b > str) {
294 *(--b) = sign;
295 }
296 *fmt_size = *buf + needed_size - b - 1;
297
298 return b;
299}
300
Damien George88d7bba2014-04-08 23:30:46 +0100301#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
302
Damien Georged6b31e42016-01-07 14:29:12 +0000303int mp_obj_int_sign(mp_obj_t self_in) {
304 mp_int_t val = mp_obj_get_int(self_in);
305 if (val < 0) {
306 return -1;
307 } else if (val > 0) {
308 return 1;
309 } else {
310 return 0;
311 }
Dave Hylandsc4029e52014-04-07 11:19:51 -0700312}
Dave Hylandsc4029e52014-04-07 11:19:51 -0700313
Damien George660aef62014-04-02 12:22:07 +0100314// This is called for operations on SMALL_INT that are not handled by mp_unary_op
Damien George58321dd2017-08-29 13:04:01 +1000315mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
Damien George6ac5dce2014-05-21 19:42:43 +0100316 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200317}
318
Damien George660aef62014-04-02 12:22:07 +0100319// This is called for operations on SMALL_INT that are not handled by mp_binary_op
Damien George58321dd2017-08-29 13:04:01 +1000320mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee8208a72014-04-04 15:08:23 +0100321 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200322}
323
324// This is called only with strings whose value doesn't fit in SMALL_INT
Damien Georgeda36f522017-02-16 16:41:43 +1100325mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) {
Damien George7d0d7212016-10-17 12:17:37 +1100326 mp_raise_msg(&mp_type_OverflowError, "long int not supported in this build");
Damien George23005372014-01-13 19:39:01 +0000327 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200328}
329
Damien George9d68e9c2014-03-12 15:38:15 +0000330// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
331mp_obj_t mp_obj_new_int_from_ll(long long val) {
Damien George7d0d7212016-10-17 12:17:37 +1100332 mp_raise_msg(&mp_type_OverflowError, "small int overflow");
Damien George9d68e9c2014-03-12 15:38:15 +0000333 return mp_const_none;
334}
335
Damien George95307432014-09-10 22:10:33 +0100336// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
337mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
Damien George7d0d7212016-10-17 12:17:37 +1100338 mp_raise_msg(&mp_type_OverflowError, "small int overflow");
Damien George95307432014-09-10 22:10:33 +0100339 return mp_const_none;
340}
341
Damien George40f3c022014-07-03 13:25:24 +0100342mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Damien George9ae51252016-03-10 21:52:56 +0000343 // SMALL_INT accepts only signed numbers, so make sure the input
344 // value fits completely in the small-int positive range.
345 if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200346 return MP_OBJ_NEW_SMALL_INT(value);
347 }
Damien George7d0d7212016-10-17 12:17:37 +1100348 mp_raise_msg(&mp_type_OverflowError, "small int overflow");
Damien George23005372014-01-13 19:39:01 +0000349 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200350}
351
Damien George40f3c022014-07-03 13:25:24 +0100352mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100353 if (MP_SMALL_INT_FITS(value)) {
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200354 return MP_OBJ_NEW_SMALL_INT(value);
355 }
Damien George7d0d7212016-10-17 12:17:37 +1100356 mp_raise_msg(&mp_type_OverflowError, "small int overflow");
Damien George23005372014-01-13 19:39:01 +0000357 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200358}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200359
Damien Georgebe6d8be2014-12-05 23:13:52 +0000360mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200361 return MP_OBJ_SMALL_INT_VALUE(self_in);
362}
363
Damien George40f3c022014-07-03 13:25:24 +0100364mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200365 return MP_OBJ_SMALL_INT_VALUE(self_in);
366}
367
Damien George5fa93b62014-01-22 14:35:10 +0000368#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
369
Damien Georgee8208a72014-04-04 15:08:23 +0100370// This dispatcher function is expected to be independent of the implementation of long int
371// It handles the extra cases for integer-like arithmetic
Damien George58321dd2017-08-29 13:04:01 +1000372mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee8208a72014-04-04 15:08:23 +0100373 if (rhs_in == mp_const_false) {
374 // false acts as 0
375 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0));
376 } else if (rhs_in == mp_const_true) {
377 // true acts as 0
378 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1));
379 } else if (op == MP_BINARY_OP_MULTIPLY) {
Damien Georgefe3e17b2018-02-21 00:20:46 +1100380 if (MP_OBJ_IS_STR_OR_BYTES(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
Damien Georgee8208a72014-04-04 15:08:23 +0100381 // multiply is commutative for these types, so delegate to them
382 return mp_binary_op(op, rhs_in, lhs_in);
383 }
384 }
Damien George6ac5dce2014-05-21 19:42:43 +0100385 return MP_OBJ_NULL; // op not supported
Damien Georgee8208a72014-04-04 15:08:23 +0100386}
387
Damien George5213eb32014-04-13 12:24:13 +0100388// this is a classmethod
Damien George4b72b3a2016-01-03 14:21:40 +0000389STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
Damien George5213eb32014-04-13 12:24:13 +0100390 // TODO: Support signed param (assumes signed=False at the moment)
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000391 (void)n_args;
Damien George5213eb32014-04-13 12:24:13 +0100392
393 // get the buffer info
Damien George57a4b4f2014-04-18 22:29:21 +0100394 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100395 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
Damien George5213eb32014-04-13 12:24:13 +0100396
Damien George58bb73e2017-05-05 21:42:39 +1000397 const byte* buf = (const byte*)bufinfo.buf;
398 int delta = 1;
399 if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
400 buf += bufinfo.len - 1;
401 delta = -1;
Paul Sokolovskybec7bfb2017-01-21 20:14:18 +0300402 }
Damien George58bb73e2017-05-05 21:42:39 +1000403
404 mp_uint_t value = 0;
405 size_t len = bufinfo.len;
406 for (; len--; buf += delta) {
407 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
408 if (value > (MP_SMALL_INT_MAX >> 8)) {
409 // Result will overflow a small-int so construct a big-int
410 return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
411 }
412 #endif
413 value = (value << 8) | *buf;
414 }
415 return mp_obj_new_int_from_uint(value);
Paul Sokolovskya985b452014-04-09 00:40:58 +0300416}
417
Paul Sokolovsky9d787de2016-12-09 21:15:16 +0300418STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
Damien Georgecbf76742015-11-27 13:38:15 +0000419STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
Paul Sokolovskya985b452014-04-09 00:40:58 +0300420
Damien George4b72b3a2016-01-03 14:21:40 +0000421STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
Damien Georgefcdb2392014-10-06 13:45:34 +0000422 // TODO: Support signed param (assumes signed=False)
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000423 (void)n_args;
Damien Georgefcdb2392014-10-06 13:45:34 +0000424
Damien Georgee269cab2017-06-15 14:21:02 +1000425 mp_int_t len = mp_obj_get_int(args[1]);
426 if (len < 0) {
427 mp_raise_ValueError(NULL);
428 }
Damien George8c5632a2017-06-15 13:56:21 +1000429 bool big_endian = args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
Damien Georgefcdb2392014-10-06 13:45:34 +0000430
Damien George05005f62015-01-21 22:48:37 +0000431 vstr_t vstr;
432 vstr_init_len(&vstr, len);
433 byte *data = (byte*)vstr.buf;
Paul Sokolovskya985b452014-04-09 00:40:58 +0300434 memset(data, 0, len);
Damien Georgefcdb2392014-10-06 13:45:34 +0000435
Damien George271d18e2015-04-25 23:16:39 +0100436 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
437 if (!MP_OBJ_IS_SMALL_INT(args[0])) {
Damien George8c5632a2017-06-15 13:56:21 +1000438 mp_obj_int_to_bytes_impl(args[0], big_endian, len, data);
Damien George271d18e2015-04-25 23:16:39 +0100439 } else
440 #endif
441 {
442 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(args[0]);
Damien George8c5632a2017-06-15 13:56:21 +1000443 size_t l = MIN((size_t)len, sizeof(val));
444 mp_binary_set_int(l, big_endian, data + (big_endian ? (len - l) : 0), val);
Damien Georgefcdb2392014-10-06 13:45:34 +0000445 }
446
Damien George05005f62015-01-21 22:48:37 +0000447 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Paul Sokolovskya985b452014-04-09 00:40:58 +0300448}
Paul Sokolovsky9d787de2016-12-09 21:15:16 +0300449STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 3, 4, int_to_bytes);
Paul Sokolovskya985b452014-04-09 00:40:58 +0300450
Damien Georgecbf76742015-11-27 13:38:15 +0000451STATIC const mp_rom_map_elem_t int_locals_dict_table[] = {
452 { MP_ROM_QSTR(MP_QSTR_from_bytes), MP_ROM_PTR(&int_from_bytes_obj) },
453 { MP_ROM_QSTR(MP_QSTR_to_bytes), MP_ROM_PTR(&int_to_bytes_obj) },
Paul Sokolovskya985b452014-04-09 00:40:58 +0300454};
455
456STATIC MP_DEFINE_CONST_DICT(int_locals_dict, int_locals_dict_table);
457
Damien George3e1a5c12014-03-29 13:43:38 +0000458const mp_obj_type_t mp_type_int = {
Damien Georgec5966122014-02-15 16:10:44 +0000459 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000460 .name = MP_QSTR_int,
Damien Georgee8208a72014-04-04 15:08:23 +0100461 .print = mp_obj_int_print,
462 .make_new = mp_obj_int_make_new,
463 .unary_op = mp_obj_int_unary_op,
464 .binary_op = mp_obj_int_binary_op,
Damien George999cedb2015-11-27 17:01:44 +0000465 .locals_dict = (mp_obj_dict_t*)&int_locals_dict,
Damien George5fa93b62014-01-22 14:35:10 +0000466};