38 #if MICROPY_PY_BUILTINS_FLOAT 60 #if MICROPY_PY_BUILTINS_FLOAT 62 return mp_obj_new_int_from_float(mp_obj_float_get(
args[0]));
80 #if MICROPY_PY_BUILTINS_FLOAT 83 MP_FP_CLASS_FIT_SMALLINT,
84 MP_FP_CLASS_FIT_LONGINT,
86 } mp_fp_as_int_class_t;
88 STATIC mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
91 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 93 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 99 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 101 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 102 e = u.i[MP_ENDIANNESS_LITTLE];
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) 107 if (e & (1
U << MP_FLOAT_SIGN_SHIFT_I32)) {
108 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 111 if ((e & ~(1 << MP_FLOAT_SIGN_SHIFT_I32)) == 0) {
115 e += ((1 << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32;
118 e &= ~((1 << MP_FLOAT_EXP_SHIFT_I32) - 1);
122 if (e <= ((8 *
sizeof(
uintptr_t) + MP_FLOAT_EXP_BIAS - 3) << MP_FLOAT_EXP_SHIFT_I32)) {
123 return MP_FP_CLASS_FIT_SMALLINT;
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;
130 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ 131 return MP_FP_CLASS_FIT_LONGINT;
133 return MP_FP_CLASS_OVERFLOW;
136 #undef MP_FLOAT_SIGN_SHIFT_I32 137 #undef MP_FLOAT_EXP_SHIFT_I32 139 mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
143 }
else if (cl ==
FP_NAN) {
146 mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
147 if (icl == MP_FP_CLASS_FIT_SMALLINT) {
149 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ 152 mpz_set_from_float(&o->mpz, val);
156 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG 157 }
else if (icl == MP_FP_CLASS_FIT_LONGINT) {
169 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG 182 char *buf = stack_buf;
183 size_t buf_size =
sizeof(stack_buf);
189 if (buf != stack_buf) {
190 m_del(
char, buf, buf_size);
208 assert(2 <= base && base <= 16);
210 size_t num_commas = comma ? num_digits / 3 : 0;
211 size_t prefix_len = prefix ?
strlen(prefix) : 0;
212 return num_digits + num_commas + prefix_len + 2;
223 int base,
const char *prefix,
char base_char,
char comma) {
225 if (MP_OBJ_IS_SMALL_INT(self_in)) {
228 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE 231 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG 254 if (needed_size > *buf_size) {
255 *buf =
m_new(
char, needed_size);
256 *buf_size = needed_size;
260 char *b = str + needed_size;
262 char *last_comma = b;
277 if (comma && num != 0 && b > str && (last_comma - b) == 3) {
282 while (b > str && num != 0);
285 size_t prefix_len =
strlen(prefix);
286 char *p = b - prefix_len;
294 if (sign && b > str) {
297 *fmt_size = *buf + needed_size - b - 1;
302 #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE 308 }
else if (val > 0) {
369 #endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE 401 buf += bufinfo.
len - 1;
406 size_t len = bufinfo.
len;
407 for (; len--; buf += delta) {
408 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE 414 value = (value << 8) | *buf;
437 #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE 438 if (!MP_OBJ_IS_SMALL_INT(
args[0])) {
444 size_t l =
MIN((
size_t)len,
sizeof(val));
STATIC const mp_rom_map_elem_t int_locals_dict_table[]
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
void * memset(void *b, int c, size_t len)
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj))
STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args)
mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in)
#define MP_OBJ_IS_TYPE(o, t)
#define m_del(type, ptr, num)
void vstr_init_len(vstr_t *vstr, size_t len)
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)
int mp_print_str(const mp_print_t *print, const char *str)
#define MP_OBJ_FROM_PTR(p)
#define MP_OBJ_NEW_QSTR(qst)
void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw)
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in)
const mp_obj_type_t mp_type_int
const mp_obj_type_t mp_type_bytes
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)
mp_int_t mp_obj_get_int(mp_const_obj_t arg)
mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in)
#define MP_OBJ_SMALL_INT_VALUE(o)
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val)
#define mp_obj_is_float(o)
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes)
size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma)
char * mp_obj_int_formatted(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in, int base, const char *prefix, char base_char, char comma)
mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg)
#define MP_OBJ_NEW_SMALL_INT(small_int)
size_t strlen(const char *s)
STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args)
const char * mp_obj_str_get_data(mp_obj_t self_in, size_t *len)
char * mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in, int base, const char *prefix, char base_char, char comma)
STATIC const uint8_t log_base2_floor[]
mp_longint_impl_t fmt_int_t
mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base)
#define MP_SMALL_INT_FITS(n)
mp_obj_t mp_obj_new_int_from_ll(long long val)
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in)
mp_obj_t mp_obj_new_int(mp_int_t value)
mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs)
int mp_obj_int_sign(mp_obj_t self_in)
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value)
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf)
mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr)
const mp_obj_type_t mp_type_type
unsigned long long fmt_uint_t
NORETURN void mp_raise_ValueError(const char *msg)
STATIC MP_DEFINE_CONST_DICT(int_locals_dict, int_locals_dict_table)
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags)
const mp_obj_type_t mp_type_tuple
#define MP_ENDIANNESS_BIG
void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val)
mp_obj_int_t * mp_obj_int_new_mpz(void)
mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex)
void mp_obj_int_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf)
#define MP_OBJ_IS_STR_OR_BYTES(o)
#define MP_SMALL_INT_POSITIVE_MASK
const mp_obj_type_t mp_type_list
const mp_obj_type_t mp_type_OverflowError