blob: ce091549d493d50a25b5d98e4ebe23fa2b99fd44 [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 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/nlr.h"
32#include "py/parsenum.h"
33#include "py/smallint.h"
34#include "py/objint.h"
35#include "py/objstr.h"
36#include "py/runtime0.h"
37#include "py/runtime.h"
Damien George271d18e2015-04-25 23:16:39 +010038#include "py/binary.h"
Paul Sokolovsky48b35722014-01-12 17:30:48 +020039
Damien Georgefb510b32014-06-01 13:32:54 +010040#if MICROPY_PY_BUILTINS_FLOAT
Damien George6433bd92014-03-30 23:13:16 +010041#include <math.h>
42#endif
43
Damien Georgee8208a72014-04-04 15:08:23 +010044// This dispatcher function is expected to be independent of the implementation of long int
Damien George5b3f0b72016-01-03 15:55:55 +000045STATIC 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 +000046 (void)type_in;
Damien Georgeee7a8802014-05-11 18:37:21 +010047 mp_arg_check_num(n_args, n_kw, 0, 2, false);
Damien George20006db2014-01-18 14:10:48 +000048
Damien George45b43c22014-01-05 01:50:45 +000049 switch (n_args) {
50 case 0:
51 return MP_OBJ_NEW_SMALL_INT(0);
52
53 case 1:
Damien George813ed3b2014-05-28 14:09:46 +010054 if (MP_OBJ_IS_INT(args[0])) {
55 // already an int (small or long), just return it
56 return args[0];
Dave Hylandsb7f7c652014-08-26 12:44:46 -070057 } else if (MP_OBJ_IS_STR_OR_BYTES(args[0])) {
Damien George5573f9f2014-01-15 22:58:39 +000058 // a string, parse it
Damien Georged182b982014-08-30 14:19:41 +010059 mp_uint_t l;
Damien George698ec212014-02-08 18:17:23 +000060 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George7d414a12015-02-08 01:57:40 +000061 return mp_parse_num_integer(s, l, 0, NULL);
Damien Georgefb510b32014-06-01 13:32:54 +010062#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeaaef1852015-08-20 23:30:12 +010063 } else if (mp_obj_is_float(args[0])) {
Paul Sokolovsky12033df2014-12-30 00:22:10 +020064 return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
Damien George6433bd92014-03-30 23:13:16 +010065#endif
Damien George5573f9f2014-01-15 22:58:39 +000066 } else {
Damien George813ed3b2014-05-28 14:09:46 +010067 // try to convert to small int (eg from bool)
Damien George5573f9f2014-01-15 22:58:39 +000068 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
69 }
Damien George45b43c22014-01-05 01:50:45 +000070
xybc178ea42014-01-14 21:39:05 +080071 case 2:
Damien Georgeee7a8802014-05-11 18:37:21 +010072 default: {
Damien George5573f9f2014-01-15 22:58:39 +000073 // should be a string, parse it
74 // TODO proper error checking of argument types
Damien Georged182b982014-08-30 14:19:41 +010075 mp_uint_t l;
Damien George698ec212014-02-08 18:17:23 +000076 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George7d414a12015-02-08 01:57:40 +000077 return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]), NULL);
Damien George5fa93b62014-01-22 14:35:10 +000078 }
Damien George45b43c22014-01-05 01:50:45 +000079 }
80}
81
David Steinbergca377b12015-01-13 15:21:17 +000082#if MICROPY_PY_BUILTINS_FLOAT
83mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
84 union {
85 mp_float_t f;
86#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
87 uint32_t i;
88#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
89 uint32_t i[2];
90#endif
91 } u = {val};
92
93 uint32_t e;
94#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
95 e = u.i;
96#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
97 e = u.i[MP_ENDIANNESS_LITTLE];
98#endif
99#define MP_FLOAT_SIGN_SHIFT_I32 ((MP_FLOAT_FRAC_BITS + MP_FLOAT_EXP_BITS) % 32)
100#define MP_FLOAT_EXP_SHIFT_I32 (MP_FLOAT_FRAC_BITS % 32)
101
102 if (e & (1 << MP_FLOAT_SIGN_SHIFT_I32)) {
103#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
104 e |= u.i[MP_ENDIANNESS_BIG] != 0;
105#endif
Damien Georgec0735192017-03-23 23:51:35 +1100106 if ((e & ~(1 << MP_FLOAT_SIGN_SHIFT_I32)) == 0) {
107 // handle case of -0 (when sign is set but rest of bits are zero)
108 e = 0;
109 } else {
110 e += ((1 << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32;
111 }
David Steinbergca377b12015-01-13 15:21:17 +0000112 } else {
113 e &= ~((1 << MP_FLOAT_EXP_SHIFT_I32) - 1);
114 }
Damien Georgeda3dffa2016-01-08 17:57:30 +0000115 // 8 * sizeof(uintptr_t) counts the number of bits for a small int
116 // TODO provide a way to configure this properly
117 if (e <= ((8 * sizeof(uintptr_t) + MP_FLOAT_EXP_BIAS - 3) << MP_FLOAT_EXP_SHIFT_I32)) {
David Steinbergca377b12015-01-13 15:21:17 +0000118 return MP_FP_CLASS_FIT_SMALLINT;
119 }
120#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
121 if (e <= (((sizeof(long long) * BITS_PER_BYTE) + MP_FLOAT_EXP_BIAS - 2) << MP_FLOAT_EXP_SHIFT_I32)) {
122 return MP_FP_CLASS_FIT_LONGINT;
123 }
124#endif
125#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
126 return MP_FP_CLASS_FIT_LONGINT;
127#else
128 return MP_FP_CLASS_OVERFLOW;
129#endif
130}
131#undef MP_FLOAT_SIGN_SHIFT_I32
132#undef MP_FLOAT_EXP_SHIFT_I32
133#endif
134
Damien Georged1ae6ae2017-03-14 14:54:20 +1100135#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
136typedef mp_longint_impl_t fmt_int_t;
137#else
138typedef mp_int_t fmt_int_t;
139#endif
140
Damien George7f9d1d62015-04-09 23:56:15 +0100141void 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 +0000142 (void)kind;
Dave Hylandsc4029e52014-04-07 11:19:51 -0700143 // The size of this buffer is rather arbitrary. If it's not large
144 // enough, a dynamic one will be allocated.
Damien Georged1ae6ae2017-03-14 14:54:20 +1100145 char stack_buf[sizeof(fmt_int_t) * 4];
Dave Hylandsc4029e52014-04-07 11:19:51 -0700146 char *buf = stack_buf;
Damien George6dff3df2016-10-11 13:20:11 +1100147 size_t buf_size = sizeof(stack_buf);
148 size_t fmt_size;
Dave Hylandsc4029e52014-04-07 11:19:51 -0700149
150 char *str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size, self_in, 10, NULL, '\0', '\0');
Damien George7f9d1d62015-04-09 23:56:15 +0100151 mp_print_str(print, str);
Dave Hylandsc4029e52014-04-07 11:19:51 -0700152
153 if (buf != stack_buf) {
Damien George4d77e1a2015-02-27 09:34:51 +0000154 m_del(char, buf, buf_size);
Damien George5fa93b62014-01-22 14:35:10 +0000155 }
Damien George45b43c22014-01-05 01:50:45 +0000156}
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200157
Damien George1c70cbf2014-08-30 00:38:16 +0100158STATIC const uint8_t log_base2_floor[] = {
Dave Hylandsc4029e52014-04-07 11:19:51 -0700159 0, 1, 1, 2,
160 2, 2, 2, 3,
161 3, 3, 3, 3,
162 3, 3, 3, 4,
Damien Georgeea6a9582016-12-28 12:46:20 +1100163 /* if needed, these are the values for higher bases
Dave Hylandsc4029e52014-04-07 11:19:51 -0700164 4, 4, 4, 4,
165 4, 4, 4, 4,
166 4, 4, 4, 4,
167 4, 4, 4, 5
Damien Georgeea6a9582016-12-28 12:46:20 +1100168 */
Dave Hylandsc4029e52014-04-07 11:19:51 -0700169};
170
Damien George8bb7d952016-10-11 13:11:32 +1100171size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma) {
Damien Georgeea6a9582016-12-28 12:46:20 +1100172 assert(2 <= base && base <= 16);
173 size_t num_digits = num_bits / log_base2_floor[base - 1] + 1;
Damien George8bb7d952016-10-11 13:11:32 +1100174 size_t num_commas = comma ? num_digits / 3 : 0;
175 size_t prefix_len = prefix ? strlen(prefix) : 0;
Dave Hylandsc4029e52014-04-07 11:19:51 -0700176 return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
177}
178
Paul Sokolovskyd72bc272014-06-06 23:08:37 +0300179// This routine expects you to pass in a buffer and size (in *buf and *buf_size).
Dave Hylandsc4029e52014-04-07 11:19:51 -0700180// If, for some reason, this buffer is too small, then it will allocate a
181// buffer and return the allocated buffer and size in *buf and *buf_size. It
182// is the callers responsibility to free this allocated buffer.
183//
184// The resulting formatted string will be returned from this function and the
185// formatted size will be in *fmt_size.
Damien George6dff3df2016-10-11 13:20:11 +1100186char *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 -0700187 int base, const char *prefix, char base_char, char comma) {
Damien George88d7bba2014-04-08 23:30:46 +0100188 fmt_int_t num;
189 if (MP_OBJ_IS_SMALL_INT(self_in)) {
190 // A small int; get the integer value to format.
191 num = mp_obj_get_int(self_in);
192#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
193 } else if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) {
194 // Not a small int.
195#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
Damien George503d6112014-05-28 14:07:21 +0100196 const mp_obj_int_t *self = self_in;
Damien George40f3c022014-07-03 13:25:24 +0100197 // Get the value to format; mp_obj_get_int truncates to mp_int_t.
Damien George88d7bba2014-04-08 23:30:46 +0100198 num = self->val;
199#else
200 // Delegate to the implementation for the long int.
201 return mp_obj_int_formatted_impl(buf, buf_size, fmt_size, self_in, base, prefix, base_char, comma);
202#endif
203#endif
204 } else {
205 // Not an int.
Paul Sokolovskyd72bc272014-06-06 23:08:37 +0300206 **buf = '\0';
Dave Hylandsc4029e52014-04-07 11:19:51 -0700207 *fmt_size = 0;
208 return *buf;
209 }
Damien George88d7bba2014-04-08 23:30:46 +0100210
Dave Hylandsc4029e52014-04-07 11:19:51 -0700211 char sign = '\0';
212 if (num < 0) {
213 num = -num;
214 sign = '-';
215 }
216
Damien George6dff3df2016-10-11 13:20:11 +1100217 size_t needed_size = mp_int_format_size(sizeof(fmt_int_t) * 8, base, prefix, comma);
Dave Hylandsc4029e52014-04-07 11:19:51 -0700218 if (needed_size > *buf_size) {
219 *buf = m_new(char, needed_size);
220 *buf_size = needed_size;
221 }
222 char *str = *buf;
223
224 char *b = str + needed_size;
225 *(--b) = '\0';
226 char *last_comma = b;
227
228 if (num == 0) {
229 *(--b) = '0';
230 } else {
231 do {
232 int c = num % base;
233 num /= base;
234 if (c >= 10) {
235 c += base_char - 10;
236 } else {
237 c += '0';
238 }
239 *(--b) = c;
240 if (comma && num != 0 && b > str && (last_comma - b) == 3) {
241 *(--b) = comma;
242 last_comma = b;
243 }
244 }
245 while (b > str && num != 0);
246 }
247 if (prefix) {
248 size_t prefix_len = strlen(prefix);
249 char *p = b - prefix_len;
250 if (p > str) {
251 b = p;
252 while (*prefix) {
253 *p++ = *prefix++;
254 }
255 }
256 }
257 if (sign && b > str) {
258 *(--b) = sign;
259 }
260 *fmt_size = *buf + needed_size - b - 1;
261
262 return b;
263}
264
Damien George88d7bba2014-04-08 23:30:46 +0100265#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
266
Damien Georged6b31e42016-01-07 14:29:12 +0000267int mp_obj_int_sign(mp_obj_t self_in) {
268 mp_int_t val = mp_obj_get_int(self_in);
269 if (val < 0) {
270 return -1;
271 } else if (val > 0) {
272 return 1;
273 } else {
274 return 0;
275 }
Dave Hylandsc4029e52014-04-07 11:19:51 -0700276}
Dave Hylandsc4029e52014-04-07 11:19:51 -0700277
Damien George6837d462015-03-14 22:07:30 +0000278// This must handle int and bool types, and must raise a
279// TypeError if the argument is not integral
280mp_obj_t mp_obj_int_abs(mp_obj_t self_in) {
281 mp_int_t val = mp_obj_get_int(self_in);
282 if (val < 0) {
283 val = -val;
284 }
285 return MP_OBJ_NEW_SMALL_INT(val);
286}
287
Damien George660aef62014-04-02 12:22:07 +0100288// This is called for operations on SMALL_INT that are not handled by mp_unary_op
Damien Georgeecc88e92014-08-30 00:35:11 +0100289mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damien George6ac5dce2014-05-21 19:42:43 +0100290 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200291}
292
Damien George660aef62014-04-02 12:22:07 +0100293// This is called for operations on SMALL_INT that are not handled by mp_binary_op
Damien Georgeecc88e92014-08-30 00:35:11 +0100294mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee8208a72014-04-04 15:08:23 +0100295 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200296}
297
298// This is called only with strings whose value doesn't fit in SMALL_INT
Damien Georgeda36f522017-02-16 16:41:43 +1100299mp_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 +1100300 mp_raise_msg(&mp_type_OverflowError, "long int not supported in this build");
Damien George23005372014-01-13 19:39:01 +0000301 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200302}
303
Damien George9d68e9c2014-03-12 15:38:15 +0000304// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
305mp_obj_t mp_obj_new_int_from_ll(long long val) {
Damien George7d0d7212016-10-17 12:17:37 +1100306 mp_raise_msg(&mp_type_OverflowError, "small int overflow");
Damien George9d68e9c2014-03-12 15:38:15 +0000307 return mp_const_none;
308}
309
Damien George95307432014-09-10 22:10:33 +0100310// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
311mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
Damien George7d0d7212016-10-17 12:17:37 +1100312 mp_raise_msg(&mp_type_OverflowError, "small int overflow");
Damien George95307432014-09-10 22:10:33 +0100313 return mp_const_none;
314}
315
Damien George40f3c022014-07-03 13:25:24 +0100316mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Damien George9ae51252016-03-10 21:52:56 +0000317 // SMALL_INT accepts only signed numbers, so make sure the input
318 // value fits completely in the small-int positive range.
319 if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200320 return MP_OBJ_NEW_SMALL_INT(value);
321 }
Damien George7d0d7212016-10-17 12:17:37 +1100322 mp_raise_msg(&mp_type_OverflowError, "small int overflow");
Damien George23005372014-01-13 19:39:01 +0000323 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200324}
325
Paul Sokolovskyf79cd6a2014-12-30 00:33:32 +0200326#if MICROPY_PY_BUILTINS_FLOAT
327mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
David Steinberg0fb17f62015-01-13 15:22:30 +0000328 int cl = fpclassify(val);
329 if (cl == FP_INFINITE) {
330 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int"));
331 } else if (cl == FP_NAN) {
332 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int"));
333 } else {
334 mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
335 if (icl == MP_FP_CLASS_FIT_SMALLINT) {
336 return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
337 } else {
338 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "float too big"));
339 }
340 }
Paul Sokolovskyf79cd6a2014-12-30 00:33:32 +0200341}
342#endif
343
Damien George40f3c022014-07-03 13:25:24 +0100344mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100345 if (MP_SMALL_INT_FITS(value)) {
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}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200351
Damien Georgebe6d8be2014-12-05 23:13:52 +0000352mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200353 return MP_OBJ_SMALL_INT_VALUE(self_in);
354}
355
Damien George40f3c022014-07-03 13:25:24 +0100356mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200357 return MP_OBJ_SMALL_INT_VALUE(self_in);
358}
359
Damien George5fa93b62014-01-22 14:35:10 +0000360#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
361
Damien Georgee8208a72014-04-04 15:08:23 +0100362// This dispatcher function is expected to be independent of the implementation of long int
363// It handles the extra cases for integer-like arithmetic
Damien Georgeecc88e92014-08-30 00:35:11 +0100364mp_obj_t mp_obj_int_binary_op_extra_cases(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee8208a72014-04-04 15:08:23 +0100365 if (rhs_in == mp_const_false) {
366 // false acts as 0
367 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0));
368 } else if (rhs_in == mp_const_true) {
369 // true acts as 0
370 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1));
371 } else if (op == MP_BINARY_OP_MULTIPLY) {
Damien George9b7a8ee2014-08-13 13:22:24 +0100372 if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_bytes) || 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 +0100373 // multiply is commutative for these types, so delegate to them
374 return mp_binary_op(op, rhs_in, lhs_in);
375 }
376 }
Damien George6ac5dce2014-05-21 19:42:43 +0100377 return MP_OBJ_NULL; // op not supported
Damien Georgee8208a72014-04-04 15:08:23 +0100378}
379
Damien George5213eb32014-04-13 12:24:13 +0100380// this is a classmethod
Damien George4b72b3a2016-01-03 14:21:40 +0000381STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
Damien George5213eb32014-04-13 12:24:13 +0100382 // TODO: Support signed param (assumes signed=False at the moment)
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000383 (void)n_args;
Damien George5213eb32014-04-13 12:24:13 +0100384
385 // get the buffer info
Damien George57a4b4f2014-04-18 22:29:21 +0100386 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100387 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
Damien George5213eb32014-04-13 12:24:13 +0100388
Paul Sokolovskybec7bfb2017-01-21 20:14:18 +0300389 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
390 // If result guaranteedly fits in small int, use that
Damien Georgeeaa77452017-01-25 14:39:13 +1100391 if (bufinfo.len >= sizeof(mp_uint_t) || !MP_SMALL_INT_FITS(1 << (bufinfo.len * 8 - 1))) {
Paul Sokolovskybec7bfb2017-01-21 20:14:18 +0300392 return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
393 } else
394 #endif
395 {
396 const byte* buf = (const byte*)bufinfo.buf;
397 int delta = 1;
398 if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
399 buf += bufinfo.len - 1;
400 delta = -1;
401 }
Damien George5213eb32014-04-13 12:24:13 +0100402
Paul Sokolovskybec7bfb2017-01-21 20:14:18 +0300403 mp_uint_t value = 0;
404 for (; bufinfo.len--; buf += delta) {
405 value = (value << 8) | *buf;
406 }
407 return mp_obj_new_int_from_uint(value);
408 }
Paul Sokolovskya985b452014-04-09 00:40:58 +0300409}
410
Paul Sokolovsky9d787de2016-12-09 21:15:16 +0300411STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
Damien Georgecbf76742015-11-27 13:38:15 +0000412STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
Paul Sokolovskya985b452014-04-09 00:40:58 +0300413
Damien George4b72b3a2016-01-03 14:21:40 +0000414STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
Paul Sokolovsky9d787de2016-12-09 21:15:16 +0300415 // TODO: Support byteorder param
Damien Georgefcdb2392014-10-06 13:45:34 +0000416 // TODO: Support signed param (assumes signed=False)
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000417 (void)n_args;
Damien Georgefcdb2392014-10-06 13:45:34 +0000418
Paul Sokolovsky9d787de2016-12-09 21:15:16 +0300419 if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
420 mp_not_implemented("");
421 }
422
Damien George963a5a32015-01-16 17:47:07 +0000423 mp_uint_t len = MP_OBJ_SMALL_INT_VALUE(args[1]);
Damien Georgefcdb2392014-10-06 13:45:34 +0000424
Damien George05005f62015-01-21 22:48:37 +0000425 vstr_t vstr;
426 vstr_init_len(&vstr, len);
427 byte *data = (byte*)vstr.buf;
Paul Sokolovskya985b452014-04-09 00:40:58 +0300428 memset(data, 0, len);
Damien Georgefcdb2392014-10-06 13:45:34 +0000429
Damien George271d18e2015-04-25 23:16:39 +0100430 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
431 if (!MP_OBJ_IS_SMALL_INT(args[0])) {
432 mp_obj_int_to_bytes_impl(args[0], false, len, data);
433 } else
434 #endif
435 {
436 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(args[0]);
437 mp_binary_set_int(MIN((size_t)len, sizeof(val)), false, data, val);
Damien Georgefcdb2392014-10-06 13:45:34 +0000438 }
439
Damien George05005f62015-01-21 22:48:37 +0000440 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Paul Sokolovskya985b452014-04-09 00:40:58 +0300441}
Paul Sokolovsky9d787de2016-12-09 21:15:16 +0300442STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 3, 4, int_to_bytes);
Paul Sokolovskya985b452014-04-09 00:40:58 +0300443
Damien Georgecbf76742015-11-27 13:38:15 +0000444STATIC const mp_rom_map_elem_t int_locals_dict_table[] = {
445 { MP_ROM_QSTR(MP_QSTR_from_bytes), MP_ROM_PTR(&int_from_bytes_obj) },
446 { MP_ROM_QSTR(MP_QSTR_to_bytes), MP_ROM_PTR(&int_to_bytes_obj) },
Paul Sokolovskya985b452014-04-09 00:40:58 +0300447};
448
449STATIC MP_DEFINE_CONST_DICT(int_locals_dict, int_locals_dict_table);
450
Damien George3e1a5c12014-03-29 13:43:38 +0000451const mp_obj_type_t mp_type_int = {
Damien Georgec5966122014-02-15 16:10:44 +0000452 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000453 .name = MP_QSTR_int,
Damien Georgee8208a72014-04-04 15:08:23 +0100454 .print = mp_obj_int_print,
455 .make_new = mp_obj_int_make_new,
456 .unary_op = mp_obj_int_unary_op,
457 .binary_op = mp_obj_int_binary_op,
Damien George999cedb2015-11-27 17:01:44 +0000458 .locals_dict = (mp_obj_dict_t*)&int_locals_dict,
Damien George5fa93b62014-01-22 14:35:10 +0000459};