From e6ab43e2c0106bd49423ded5f9a96d63acf813c2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 21 Jan 2017 20:15:05 +0300 Subject: py/objint_longlong: Add stub for mp_obj_int_from_bytes_impl(). To be implemented later. --- py/objint_longlong.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'py/objint_longlong.c') diff --git a/py/objint_longlong.c b/py/objint_longlong.c index f5b5d9c93..dbfb39809 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -53,6 +53,10 @@ const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX}; #endif +mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) { + mp_not_implemented(""); +} + void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int)); mp_obj_int_t *self = self_in; -- cgit v1.2.3 From da36f5232dcf02c448ee9933e358b711cfe03f30 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 16 Feb 2017 16:41:43 +1100 Subject: py/objint: Convert mp_uint_t to size_t where appropriate. --- py/obj.h | 2 +- py/objint.c | 2 +- py/objint_longlong.c | 2 +- py/objint_mpz.c | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'py/objint_longlong.c') diff --git a/py/obj.h b/py/obj.h index 1b926ce14..1169a9095 100644 --- a/py/obj.h +++ b/py/obj.h @@ -607,7 +607,7 @@ static inline mp_obj_t mp_obj_new_bool(mp_int_t x) { return x ? mp_const_true : mp_obj_t mp_obj_new_cell(mp_obj_t obj); mp_obj_t mp_obj_new_int(mp_int_t value); mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value); -mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base); +mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base); mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception) mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception) mp_obj_t mp_obj_new_str(const char* data, size_t len, bool make_qstr_if_not_already); diff --git a/py/objint.c b/py/objint.c index 222aa1de7..2a7e3f3fd 100644 --- a/py/objint.c +++ b/py/objint.c @@ -291,7 +291,7 @@ mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { } // This is called only with strings whose value doesn't fit in SMALL_INT -mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) { +mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) { mp_raise_msg(&mp_type_OverflowError, "long int not supported in this build"); return mp_const_none; } diff --git a/py/objint_longlong.c b/py/objint_longlong.c index dbfb39809..b798c91cd 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -273,7 +273,7 @@ mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { } #endif -mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) { +mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) { // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated // TODO check overflow mp_obj_int_t *o = m_new_obj(mp_obj_int_t); diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 2b27df4f6..2cdf22505 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -407,9 +407,9 @@ mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { } #endif -mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) { +mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) { mp_obj_int_t *o = mp_obj_int_new_mpz(); - mp_uint_t n = mpz_set_from_str(&o->mpz, *str, len, neg, base); + size_t n = mpz_set_from_str(&o->mpz, *str, len, neg, base); *str += n; return MP_OBJ_FROM_PTR(o); } -- cgit v1.2.3 From 776883cb80be675fad1fee1ffae5dcdb75f14f64 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 10 Mar 2017 00:22:53 +0100 Subject: py/objint_longlong: Implement mp_obj_int_from_bytes_impl(). This makes int.from_bytes() work for MICROPY_LONGINT_IMPL_LONGLONG. --- py/objint_longlong.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'py/objint_longlong.c') diff --git a/py/objint_longlong.c b/py/objint_longlong.c index b798c91cd..4ab49f337 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -54,7 +54,17 @@ const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX}; #endif mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) { - mp_not_implemented(""); + int delta = 1; + if (!big_endian) { + buf += len - 1; + delta = -1; + } + + mp_longint_impl_t value = 0; + for (; len--; buf += delta) { + value = (value << 8) | *buf; + } + return mp_obj_new_int_from_ll(value); } void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) { -- cgit v1.2.3 From fc245d1ca4367f9876ac32c7e08e169da7db79b9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 4 Apr 2017 16:45:49 +1000 Subject: py/objint: Consolidate mp_obj_new_int_from_float to one implementation. This reduces code duplication and allows to make mp_classify_fp_as_int static, which reduces code size. --- py/objint.c | 56 ++++++++++++++++++++++++++++++++++------------------ py/objint.h | 11 +++-------- py/objint_longlong.c | 20 ------------------- py/objint_mpz.c | 22 +-------------------- 4 files changed, 41 insertions(+), 68 deletions(-) (limited to 'py/objint_longlong.c') diff --git a/py/objint.c b/py/objint.c index 2e8fe4f22..46f9b1666 100644 --- a/py/objint.c +++ b/py/objint.c @@ -80,7 +80,14 @@ STATIC mp_obj_t mp_obj_int_make_new(const mp_obj_type_t *type_in, size_t n_args, } #if MICROPY_PY_BUILTINS_FLOAT -mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) { + +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) { union { mp_float_t f; #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT @@ -130,6 +137,35 @@ mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) { } #undef MP_FLOAT_SIGN_SHIFT_I32 #undef MP_FLOAT_EXP_SHIFT_I32 + +mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { + int cl = fpclassify(val); + if (cl == FP_INFINITE) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "can't convert inf to int")); + } else if (cl == FP_NAN) { + mp_raise_ValueError("can't convert NaN to int"); + } 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 { + mp_raise_ValueError("float too big"); + } + #endif + } +} + #endif #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG @@ -323,24 +359,6 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { return mp_const_none; } -#if MICROPY_PY_BUILTINS_FLOAT -mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { - int cl = fpclassify(val); - if (cl == FP_INFINITE) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int")); - } else if (cl == FP_NAN) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int")); - } 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); - } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "float too big")); - } - } -} -#endif - mp_obj_t mp_obj_new_int(mp_int_t value) { if (MP_SMALL_INT_FITS(value)) { return MP_OBJ_NEW_SMALL_INT(value); diff --git a/py/objint.h b/py/objint.h index 7205761ad..da56c1862 100644 --- a/py/objint.h +++ b/py/objint.h @@ -41,18 +41,13 @@ typedef struct _mp_obj_int_t { extern const mp_obj_int_t mp_maxsize_obj; #if MICROPY_PY_BUILTINS_FLOAT -typedef enum { - MP_FP_CLASS_FIT_SMALLINT, - MP_FP_CLASS_FIT_LONGINT, - MP_FP_CLASS_OVERFLOW -} mp_fp_as_int_class_t; - -mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val); mp_float_t mp_obj_int_as_float_impl(mp_obj_t self_in); -#endif // MICROPY_PY_BUILTINS_FLOAT +#endif size_t mp_int_format_size(size_t num_bits, int base, const char *prefix, char comma); +mp_obj_int_t *mp_obj_int_new_mpz(void); + void mp_obj_int_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind); 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); diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 4ab49f337..540cfebd0 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -263,26 +263,6 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) { return o; } -#if MICROPY_PY_BUILTINS_FLOAT -mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { - int cl = fpclassify(val); - if (cl == FP_INFINITE) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int")); - } else if (cl == FP_NAN) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int")); - } 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); - } else if (icl == MP_FP_CLASS_FIT_LONGINT) { - return mp_obj_new_int_from_ll((long long)val); - } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "float too big")); - } - } -} -#endif - mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) { // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated // TODO check overflow diff --git a/py/objint_mpz.c b/py/objint_mpz.c index ca989e8ad..2353bd8d6 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -74,7 +74,7 @@ const mp_obj_int_t mp_maxsize_obj = { #undef NUM_DIG #endif -STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) { +mp_obj_int_t *mp_obj_int_new_mpz(void) { mp_obj_int_t *o = m_new_obj(mp_obj_int_t); o->base.type = &mp_type_int; mpz_init_zero(&o->mpz); @@ -387,26 +387,6 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) { return mp_obj_new_int_from_ull(value); } -#if MICROPY_PY_BUILTINS_FLOAT -mp_obj_t mp_obj_new_int_from_float(mp_float_t val) { - int cl = fpclassify(val); - if (cl == FP_INFINITE) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int")); - } else if (cl == FP_NAN) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int")); - } 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); - } else { - mp_obj_int_t *o = mp_obj_int_new_mpz(); - mpz_set_from_float(&o->mpz, val); - return MP_OBJ_FROM_PTR(o); - } - } -} -#endif - mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) { mp_obj_int_t *o = mp_obj_int_new_mpz(); size_t n = mpz_set_from_str(&o->mpz, *str, len, neg, base); -- cgit v1.2.3 From 2f0ce2a6f50ba9472ce4bd8f9fe984d830e99ade Mon Sep 17 00:00:00 2001 From: stijn Date: Wed, 26 Apr 2017 13:17:55 +0200 Subject: py: Cleanup use of global DEBUG preprocessor definition The standard preprocessor definition to differentiate debug and non-debug builds is NDEBUG, not DEBUG, so don't rely on the latter: - just delete the use of it in objint_longlong.c as it has been stale code for years anyway (since commit [c4029e5]): SUFFIX isn't used anywhere. - replace DEBUG with MICROPY_DEBUG_NLR in nlr.h: it is rarely used anymore so can be off by default --- py/nlr.h | 2 +- py/objint_longlong.c | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) (limited to 'py/objint_longlong.c') diff --git a/py/nlr.h b/py/nlr.h index 00c1072fe..7a71ef34b 100644 --- a/py/nlr.h +++ b/py/nlr.h @@ -85,7 +85,7 @@ NORETURN void nlr_jump(void *val); NORETURN void nlr_jump_fail(void *val); // use nlr_raise instead of nlr_jump so that debugging is easier -#ifndef DEBUG +#ifndef MICROPY_DEBUG_NLR #define nlr_raise(val) nlr_jump(MP_OBJ_TO_PTR(val)) #else #include "mpstate.h" diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 540cfebd0..f638a5320 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -40,14 +40,6 @@ #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG -// Python3 no longer has "l" suffix for long ints. We allow to use it -// for debugging purpose though. -#ifdef DEBUG -#define SUFFIX "l" -#else -#define SUFFIX "" -#endif - #if MICROPY_PY_SYS_MAXSIZE // Export value for sys.maxsize const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX}; -- cgit v1.2.3