2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-04-02 12:22:07 +01:00
|
|
|
#include <stdlib.h>
|
2014-01-05 01:50:45 +00:00
|
|
|
#include <assert.h>
|
2014-04-07 19:19:51 +01:00
|
|
|
#include <string.h>
|
2014-01-05 01:50:45 +00:00
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/parsenum.h"
|
|
|
|
#include "py/smallint.h"
|
|
|
|
#include "py/objint.h"
|
|
|
|
#include "py/objstr.h"
|
|
|
|
#include "py/runtime.h"
|
2015-04-25 23:16:39 +01:00
|
|
|
#include "py/binary.h"
|
2014-01-12 15:30:48 +00:00
|
|
|
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-30 23:13:16 +01:00
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-04 15:08:23 +01:00
|
|
|
// This dispatcher function is expected to be independent of the implementation of long int
|
2016-01-03 15:55:55 +00:00
|
|
|
STATIC 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) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)type_in;
|
2014-05-11 18:37:21 +01:00
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 2, false);
|
2014-01-18 14:10:48 +00:00
|
|
|
|
2014-01-05 01:50:45 +00:00
|
|
|
switch (n_args) {
|
|
|
|
case 0:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
|
|
|
|
case 1:
|
2019-01-30 07:49:52 +00:00
|
|
|
if (mp_obj_is_int(args[0])) {
|
2014-05-28 14:09:46 +01:00
|
|
|
// already an int (small or long), just return it
|
|
|
|
return args[0];
|
2019-01-30 07:49:52 +00:00
|
|
|
} else if (mp_obj_is_str_or_bytes(args[0])) {
|
2014-01-15 22:58:39 +00:00
|
|
|
// a string, parse it
|
2017-03-25 08:48:18 +00:00
|
|
|
size_t l;
|
2014-02-08 18:17:23 +00:00
|
|
|
const char *s = mp_obj_str_get_data(args[0], &l);
|
2015-02-08 01:57:40 +00:00
|
|
|
return mp_parse_num_integer(s, l, 0, NULL);
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2015-08-20 23:30:12 +01:00
|
|
|
} else if (mp_obj_is_float(args[0])) {
|
2014-12-29 22:22:10 +00:00
|
|
|
return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
|
2014-03-30 23:13:16 +01:00
|
|
|
#endif
|
2014-01-15 22:58:39 +00:00
|
|
|
} else {
|
2018-08-29 15:59:36 +01:00
|
|
|
return mp_unary_op(MP_UNARY_OP_INT, args[0]);
|
2014-01-15 22:58:39 +00:00
|
|
|
}
|
2014-01-05 01:50:45 +00:00
|
|
|
|
2014-01-14 13:39:05 +00:00
|
|
|
case 2:
|
2014-05-11 18:37:21 +01:00
|
|
|
default: {
|
2014-01-15 22:58:39 +00:00
|
|
|
// should be a string, parse it
|
2017-03-25 08:48:18 +00:00
|
|
|
size_t l;
|
2014-02-08 18:17:23 +00:00
|
|
|
const char *s = mp_obj_str_get_data(args[0], &l);
|
2015-02-08 01:57:40 +00:00
|
|
|
return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]), NULL);
|
2014-01-22 14:35:10 +00:00
|
|
|
}
|
2014-01-05 01:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 15:21:17 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2017-04-04 07:45:49 +01:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
MP_FP_CLASS_FIT_SMALLINT,
|
|
|
|
MP_FP_CLASS_FIT_LONGINT,
|
|
|
|
MP_FP_CLASS_OVERFLOW
|
|
|
|
} mp_fp_as_int_class_t;
|
|
|
|
|
|
|
|
STATIC mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
|
2015-01-13 15:21:17 +00:00
|
|
|
union {
|
|
|
|
mp_float_t f;
|
|
|
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
|
|
|
uint32_t i;
|
|
|
|
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
|
|
|
|
uint32_t i[2];
|
|
|
|
#endif
|
|
|
|
} u = {val};
|
|
|
|
|
|
|
|
uint32_t e;
|
|
|
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
|
|
|
e = u.i;
|
|
|
|
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
|
|
|
|
e = u.i[MP_ENDIANNESS_LITTLE];
|
|
|
|
#endif
|
|
|
|
#define MP_FLOAT_SIGN_SHIFT_I32 ((MP_FLOAT_FRAC_BITS + MP_FLOAT_EXP_BITS) % 32)
|
|
|
|
#define MP_FLOAT_EXP_SHIFT_I32 (MP_FLOAT_FRAC_BITS % 32)
|
|
|
|
|
2017-07-01 00:23:29 +01:00
|
|
|
if (e & (1U << MP_FLOAT_SIGN_SHIFT_I32)) {
|
2015-01-13 15:21:17 +00:00
|
|
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
|
|
|
|
e |= u.i[MP_ENDIANNESS_BIG] != 0;
|
|
|
|
#endif
|
2021-06-08 13:45:56 +01:00
|
|
|
if ((e & ~(1U << MP_FLOAT_SIGN_SHIFT_I32)) == 0) {
|
2017-03-23 12:51:35 +00:00
|
|
|
// handle case of -0 (when sign is set but rest of bits are zero)
|
|
|
|
e = 0;
|
|
|
|
} else {
|
2021-06-08 13:45:56 +01:00
|
|
|
e += ((1U << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32;
|
2017-03-23 12:51:35 +00:00
|
|
|
}
|
2015-01-13 15:21:17 +00:00
|
|
|
} else {
|
2021-06-08 13:45:56 +01:00
|
|
|
e &= ~((1U << MP_FLOAT_EXP_SHIFT_I32) - 1);
|
2015-01-13 15:21:17 +00:00
|
|
|
}
|
2016-01-08 17:57:30 +00:00
|
|
|
// 8 * sizeof(uintptr_t) counts the number of bits for a small int
|
|
|
|
// TODO provide a way to configure this properly
|
|
|
|
if (e <= ((8 * sizeof(uintptr_t) + MP_FLOAT_EXP_BIAS - 3) << MP_FLOAT_EXP_SHIFT_I32)) {
|
2015-01-13 15:21:17 +00:00
|
|
|
return MP_FP_CLASS_FIT_SMALLINT;
|
|
|
|
}
|
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
|
2021-02-04 04:32:59 +00:00
|
|
|
if (e <= (((sizeof(long long) * MP_BITS_PER_BYTE) + MP_FLOAT_EXP_BIAS - 2) << MP_FLOAT_EXP_SHIFT_I32)) {
|
2015-01-13 15:21:17 +00:00
|
|
|
return MP_FP_CLASS_FIT_LONGINT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
|
|
|
|
return MP_FP_CLASS_FIT_LONGINT;
|
|
|
|
#else
|
|
|
|
return MP_FP_CLASS_OVERFLOW;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#undef MP_FLOAT_SIGN_SHIFT_I32
|
|
|
|
#undef MP_FLOAT_EXP_SHIFT_I32
|
2017-04-04 07:45:49 +01:00
|
|
|
|
|
|
|
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
|
2020-04-06 09:44:48 +01:00
|
|
|
mp_float_union_t u = {val};
|
|
|
|
// IEEE-754: if biased exponent is all 1 bits...
|
|
|
|
if (u.p.exp == ((1 << MP_FLOAT_EXP_BITS) - 1)) {
|
|
|
|
// ...then number is Inf (positive or negative) if fraction is 0, else NaN.
|
|
|
|
if (u.p.frc == 0) {
|
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("can't convert inf to int"));
|
|
|
|
} else {
|
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("can't convert NaN to int"));
|
|
|
|
}
|
2017-04-04 07:45:49 +01:00
|
|
|
} else {
|
|
|
|
mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
|
|
|
|
if (icl == MP_FP_CLASS_FIT_SMALLINT) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
|
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
|
|
|
|
} else {
|
|
|
|
mp_obj_int_t *o = mp_obj_int_new_mpz();
|
|
|
|
mpz_set_from_float(&o->mpz, val);
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
|
|
|
|
} else if (icl == MP_FP_CLASS_FIT_LONGINT) {
|
|
|
|
return mp_obj_new_int_from_ll((long long)val);
|
|
|
|
#endif
|
|
|
|
} else {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("float too big"));
|
2017-04-04 07:45:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 15:21:17 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-14 03:54:20 +00:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
|
|
|
|
typedef mp_longint_impl_t fmt_int_t;
|
2017-04-11 06:18:35 +01:00
|
|
|
typedef unsigned long long fmt_uint_t;
|
2017-03-14 03:54:20 +00:00
|
|
|
#else
|
|
|
|
typedef mp_int_t fmt_int_t;
|
2017-04-11 06:18:35 +01:00
|
|
|
typedef mp_uint_t fmt_uint_t;
|
2017-03-14 03:54:20 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
void mp_obj_int_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2014-04-07 19:19:51 +01:00
|
|
|
// The size of this buffer is rather arbitrary. If it's not large
|
|
|
|
// enough, a dynamic one will be allocated.
|
2017-03-14 03:54:20 +00:00
|
|
|
char stack_buf[sizeof(fmt_int_t) * 4];
|
2014-04-07 19:19:51 +01:00
|
|
|
char *buf = stack_buf;
|
2016-10-11 03:20:11 +01:00
|
|
|
size_t buf_size = sizeof(stack_buf);
|
|
|
|
size_t fmt_size;
|
2014-04-07 19:19:51 +01:00
|
|
|
|
|
|
|
char *str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size, self_in, 10, NULL, '\0', '\0');
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_print_str(print, str);
|
2014-04-07 19:19:51 +01:00
|
|
|
|
|
|
|
if (buf != stack_buf) {
|
2015-02-27 09:34:51 +00:00
|
|
|
m_del(char, buf, buf_size);
|
2014-04-07 19:19:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 00:38:16 +01:00
|
|
|
STATIC const uint8_t log_base2_floor[] = {
|
2014-04-07 19:19:51 +01:00
|
|
|
0, 1, 1, 2,
|
|
|
|
2, 2, 2, 3,
|
|
|
|
3, 3, 3, 3,
|
|
|
|
3, 3, 3, 4,
|
2016-12-28 01:46:20 +00:00
|
|
|
/* if needed, these are the values for higher bases
|
2014-04-07 19:19:51 +01:00
|
|
|
4, 4, 4, 4,
|
|
|
|
4, 4, 4, 4,
|
|
|
|
4, 4, 4, 4,
|
|
|
|
4, 4, 4, 5
|
2016-12-28 01:46:20 +00:00
|
|
|
*/
|
2014-04-07 19:19:51 +01:00
|
|
|
};
|
|
|
|
|
2016-10-11 03:11:32 +01:00
|
|
|
size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma) {
|
2016-12-28 01:46:20 +00:00
|
|
|
assert(2 <= base && base <= 16);
|
|
|
|
size_t num_digits = num_bits / log_base2_floor[base - 1] + 1;
|
2016-10-11 03:11:32 +01:00
|
|
|
size_t num_commas = comma ? num_digits / 3 : 0;
|
|
|
|
size_t prefix_len = prefix ? strlen(prefix) : 0;
|
2014-04-07 19:19:51 +01:00
|
|
|
return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
|
|
|
|
}
|
|
|
|
|
2014-06-06 21:08:37 +01:00
|
|
|
// This routine expects you to pass in a buffer and size (in *buf and *buf_size).
|
2014-04-07 19:19:51 +01:00
|
|
|
// If, for some reason, this buffer is too small, then it will allocate a
|
|
|
|
// buffer and return the allocated buffer and size in *buf and *buf_size. It
|
|
|
|
// is the callers responsibility to free this allocated buffer.
|
|
|
|
//
|
|
|
|
// The resulting formatted string will be returned from this function and the
|
|
|
|
// formatted size will be in *fmt_size.
|
2016-10-11 03:20:11 +01:00
|
|
|
char *mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in,
|
2014-04-07 19:19:51 +01:00
|
|
|
int base, const char *prefix, char base_char, char comma) {
|
2014-04-07 21:44:12 +01:00
|
|
|
fmt_int_t num;
|
2018-03-02 00:01:24 +00:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
|
|
|
// Only have small ints; get the integer value to format.
|
|
|
|
num = MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
#else
|
2019-01-30 07:49:52 +00:00
|
|
|
if (mp_obj_is_small_int(self_in)) {
|
2014-04-08 23:30:46 +01:00
|
|
|
// A small int; get the integer value to format.
|
2017-04-11 06:16:09 +01:00
|
|
|
num = MP_OBJ_SMALL_INT_VALUE(self_in);
|
2018-03-02 00:01:24 +00:00
|
|
|
} else {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_int));
|
2014-04-08 23:30:46 +01:00
|
|
|
// Not a small int.
|
2018-03-02 00:01:24 +00:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
|
2014-05-28 14:07:21 +01:00
|
|
|
const mp_obj_int_t *self = self_in;
|
2014-07-03 13:25:24 +01:00
|
|
|
// Get the value to format; mp_obj_get_int truncates to mp_int_t.
|
2014-04-07 21:44:12 +01:00
|
|
|
num = self->val;
|
2018-03-02 00:01:24 +00:00
|
|
|
#else
|
2014-04-08 23:30:46 +01:00
|
|
|
// Delegate to the implementation for the long int.
|
|
|
|
return mp_obj_int_formatted_impl(buf, buf_size, fmt_size, self_in, base, prefix, base_char, comma);
|
2018-03-02 00:01:24 +00:00
|
|
|
#endif
|
2014-04-07 21:44:12 +01:00
|
|
|
}
|
2018-03-02 00:01:24 +00:00
|
|
|
#endif
|
2014-04-08 23:30:46 +01:00
|
|
|
|
2014-04-07 19:19:51 +01:00
|
|
|
char sign = '\0';
|
|
|
|
if (num < 0) {
|
|
|
|
num = -num;
|
|
|
|
sign = '-';
|
|
|
|
}
|
|
|
|
|
2016-10-11 03:20:11 +01:00
|
|
|
size_t needed_size = mp_int_format_size(sizeof(fmt_int_t) * 8, base, prefix, comma);
|
2014-04-07 19:19:51 +01:00
|
|
|
if (needed_size > *buf_size) {
|
|
|
|
*buf = m_new(char, needed_size);
|
|
|
|
*buf_size = needed_size;
|
2014-01-22 14:35:10 +00:00
|
|
|
}
|
2014-04-07 19:19:51 +01:00
|
|
|
char *str = *buf;
|
|
|
|
|
|
|
|
char *b = str + needed_size;
|
|
|
|
*(--b) = '\0';
|
|
|
|
char *last_comma = b;
|
|
|
|
|
|
|
|
if (num == 0) {
|
|
|
|
*(--b) = '0';
|
|
|
|
} else {
|
|
|
|
do {
|
2017-04-11 06:18:35 +01:00
|
|
|
// The cast to fmt_uint_t is because num is positive and we want unsigned arithmetic
|
|
|
|
int c = (fmt_uint_t)num % base;
|
|
|
|
num = (fmt_uint_t)num / base;
|
2014-04-07 19:19:51 +01:00
|
|
|
if (c >= 10) {
|
|
|
|
c += base_char - 10;
|
|
|
|
} else {
|
|
|
|
c += '0';
|
|
|
|
}
|
|
|
|
*(--b) = c;
|
|
|
|
if (comma && num != 0 && b > str && (last_comma - b) == 3) {
|
|
|
|
*(--b) = comma;
|
|
|
|
last_comma = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (b > str && num != 0);
|
|
|
|
}
|
|
|
|
if (prefix) {
|
|
|
|
size_t prefix_len = strlen(prefix);
|
|
|
|
char *p = b - prefix_len;
|
|
|
|
if (p > str) {
|
|
|
|
b = p;
|
|
|
|
while (*prefix) {
|
|
|
|
*p++ = *prefix++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sign && b > str) {
|
|
|
|
*(--b) = sign;
|
|
|
|
}
|
|
|
|
*fmt_size = *buf + needed_size - b - 1;
|
|
|
|
|
|
|
|
return b;
|
2014-01-12 15:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 23:30:46 +01:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
|
|
|
|
2016-01-07 14:29:12 +00:00
|
|
|
int mp_obj_int_sign(mp_obj_t self_in) {
|
|
|
|
mp_int_t val = mp_obj_get_int(self_in);
|
|
|
|
if (val < 0) {
|
|
|
|
return -1;
|
|
|
|
} else if (val > 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-07 19:19:51 +01:00
|
|
|
}
|
|
|
|
|
2014-04-02 12:22:07 +01:00
|
|
|
// This is called for operations on SMALL_INT that are not handled by mp_unary_op
|
2017-08-29 04:04:01 +01:00
|
|
|
mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
|
2014-05-21 19:42:43 +01:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-01-27 07:05:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 12:22:07 +01:00
|
|
|
// This is called for operations on SMALL_INT that are not handled by mp_binary_op
|
2017-08-29 04:04:01 +01:00
|
|
|
mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
2014-04-04 15:08:23 +01:00
|
|
|
return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
|
2014-01-12 15:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is called only with strings whose value doesn't fit in SMALL_INT
|
2017-02-16 05:41:43 +00:00
|
|
|
mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("long int not supported in this build"));
|
2014-01-13 19:39:01 +00:00
|
|
|
return mp_const_none;
|
2014-01-12 15:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:38:15 +00:00
|
|
|
// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
|
|
|
|
mp_obj_t mp_obj_new_int_from_ll(long long val) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small int overflow"));
|
2014-03-12 15:38:15 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-09-10 22:10:33 +01:00
|
|
|
// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
|
|
|
|
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small int overflow"));
|
2014-09-10 22:10:33 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
|
2016-03-10 21:52:56 +00:00
|
|
|
// SMALL_INT accepts only signed numbers, so make sure the input
|
|
|
|
// value fits completely in the small-int positive range.
|
|
|
|
if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
|
2014-01-12 15:30:48 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small int overflow"));
|
2014-01-13 19:39:01 +00:00
|
|
|
return mp_const_none;
|
2014-01-12 15:30:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_obj_t mp_obj_new_int(mp_int_t value) {
|
2014-05-28 14:51:12 +01:00
|
|
|
if (MP_SMALL_INT_FITS(value)) {
|
2014-01-12 15:30:48 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("small int overflow"));
|
2014-01-13 19:39:01 +00:00
|
|
|
return mp_const_none;
|
2014-01-05 01:50:45 +00:00
|
|
|
}
|
2014-01-18 14:07:16 +00:00
|
|
|
|
2014-12-05 23:13:52 +00:00
|
|
|
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
|
2014-01-18 14:07:16 +00:00
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
|
2014-01-18 14:07:16 +00:00
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
}
|
|
|
|
|
2014-01-22 14:35:10 +00:00
|
|
|
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
|
|
|
|
2014-04-04 15:08:23 +01:00
|
|
|
// This dispatcher function is expected to be independent of the implementation of long int
|
|
|
|
// It handles the extra cases for integer-like arithmetic
|
2017-08-29 04:04:01 +01:00
|
|
|
mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
2014-04-04 15:08:23 +01:00
|
|
|
if (rhs_in == mp_const_false) {
|
|
|
|
// false acts as 0
|
|
|
|
return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0));
|
|
|
|
} else if (rhs_in == mp_const_true) {
|
|
|
|
// true acts as 0
|
|
|
|
return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1));
|
|
|
|
} else if (op == MP_BINARY_OP_MULTIPLY) {
|
2019-01-30 07:49:52 +00:00
|
|
|
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)) {
|
2014-04-04 15:08:23 +01:00
|
|
|
// multiply is commutative for these types, so delegate to them
|
|
|
|
return mp_binary_op(op, rhs_in, lhs_in);
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 19:42:43 +01:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-04-04 15:08:23 +01:00
|
|
|
}
|
|
|
|
|
2014-04-13 12:24:13 +01:00
|
|
|
// this is a classmethod
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
|
2014-04-13 12:24:13 +01:00
|
|
|
// TODO: Support signed param (assumes signed=False at the moment)
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)n_args;
|
2014-04-13 12:24:13 +01:00
|
|
|
|
|
|
|
// get the buffer info
|
2014-04-18 22:29:21 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
2014-04-18 22:59:24 +01:00
|
|
|
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
|
2014-04-08 22:40:58 +01:00
|
|
|
|
2017-05-05 12:42:39 +01:00
|
|
|
const byte *buf = (const byte *)bufinfo.buf;
|
|
|
|
int delta = 1;
|
|
|
|
if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
|
|
|
|
buf += bufinfo.len - 1;
|
|
|
|
delta = -1;
|
|
|
|
}
|
2014-04-13 12:24:13 +01:00
|
|
|
|
2017-05-05 12:42:39 +01:00
|
|
|
mp_uint_t value = 0;
|
|
|
|
size_t len = bufinfo.len;
|
|
|
|
for (; len--; buf += delta) {
|
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
|
|
|
if (value > (MP_SMALL_INT_MAX >> 8)) {
|
|
|
|
// Result will overflow a small-int so construct a big-int
|
|
|
|
return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf);
|
2017-01-21 17:14:18 +00:00
|
|
|
}
|
2017-05-05 12:42:39 +01:00
|
|
|
#endif
|
|
|
|
value = (value << 8) | *buf;
|
2017-01-21 17:14:18 +00:00
|
|
|
}
|
2017-05-05 12:42:39 +01:00
|
|
|
return mp_obj_new_int_from_uint(value);
|
2014-04-08 22:40:58 +01:00
|
|
|
}
|
|
|
|
|
2016-12-09 18:15:16 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes);
|
2015-11-27 13:38:15 +00:00
|
|
|
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
|
2014-04-08 22:40:58 +01:00
|
|
|
|
2016-01-03 14:21:40 +00:00
|
|
|
STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
|
2014-10-06 14:45:34 +01:00
|
|
|
// TODO: Support signed param (assumes signed=False)
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)n_args;
|
2014-10-06 14:45:34 +01:00
|
|
|
|
2017-06-15 05:21:02 +01:00
|
|
|
mp_int_t len = mp_obj_get_int(args[1]);
|
|
|
|
if (len < 0) {
|
|
|
|
mp_raise_ValueError(NULL);
|
|
|
|
}
|
2017-06-15 04:56:21 +01:00
|
|
|
bool big_endian = args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
|
2014-04-08 22:40:58 +01:00
|
|
|
|
2015-01-21 22:48:37 +00:00
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_len(&vstr, len);
|
|
|
|
byte *data = (byte *)vstr.buf;
|
2014-04-08 22:40:58 +01:00
|
|
|
memset(data, 0, len);
|
2014-10-06 14:45:34 +01:00
|
|
|
|
2015-04-25 23:16:39 +01:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
2019-01-30 07:49:52 +00:00
|
|
|
if (!mp_obj_is_small_int(args[0])) {
|
2017-06-15 04:56:21 +01:00
|
|
|
mp_obj_int_to_bytes_impl(args[0], big_endian, len, data);
|
2015-04-25 23:16:39 +01:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(args[0]);
|
2017-06-15 04:56:21 +01:00
|
|
|
size_t l = MIN((size_t)len, sizeof(val));
|
|
|
|
mp_binary_set_int(l, big_endian, data + (big_endian ? (len - l) : 0), val);
|
2014-10-06 14:45:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 22:48:37 +00:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2014-04-08 22:40:58 +01:00
|
|
|
}
|
2016-12-09 18:15:16 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 3, 4, int_to_bytes);
|
2014-04-08 22:40:58 +01:00
|
|
|
|
2015-11-27 13:38:15 +00:00
|
|
|
STATIC const mp_rom_map_elem_t int_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_from_bytes), MP_ROM_PTR(&int_from_bytes_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_to_bytes), MP_ROM_PTR(&int_to_bytes_obj) },
|
2014-04-08 22:40:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(int_locals_dict, int_locals_dict_table);
|
|
|
|
|
2014-03-29 13:43:38 +00:00
|
|
|
const mp_obj_type_t mp_type_int = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_int,
|
2014-04-04 15:08:23 +01:00
|
|
|
.print = mp_obj_int_print,
|
|
|
|
.make_new = mp_obj_int_make_new,
|
|
|
|
.unary_op = mp_obj_int_unary_op,
|
|
|
|
.binary_op = mp_obj_int_binary_op,
|
2015-11-27 17:01:44 +00:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&int_locals_dict,
|
2014-01-22 14:35:10 +00:00
|
|
|
};
|