aboutsummaryrefslogtreecommitdiff
path: root/py/objint.c
AgeCommit message (Collapse)Author
2021-06-24all: Fix signed shifts and NULL access errors from -fsanitize=undefined.Jeff Epler
Fixes the following (the line numbers match commit 0e87459e2bfd07): ../../extmod/crypto-algorithms/sha256.c:49:19: runtime error: left shif... ../../extmod/moduasyncio.c:106:35: runtime error: member access within ... ../../py/binary.c:210:13: runtime error: left shift of negative value -... ../../py/mpz.c:744:16: runtime error: negation of -9223372036854775808 ... ../../py/objint.c:109:22: runtime error: left shift of 1 by 31 places c... ../../py/objint_mpz.c:374:9: runtime error: left shift of 4611686018427... ../../py/objint_mpz.c:374:9: runtime error: left shift of negative valu... ../../py/parsenum.c:106:14: runtime error: left shift of 46116860184273... ../../py/runtime.c:395:33: runtime error: left shift of negative value ... ../../py/showbc.c:177:28: runtime error: left shift of negative value -... ../../py/vm.c:321:36: runtime error: left shift of negative value -1``` Testing was done on an amd64 Debian Buster system using gcc-8.3 and these settings: CFLAGS += -g3 -Og -fsanitize=undefined LDFLAGS += -fsanitize=undefined The introduced TASK_PAIRHEAP macro's conditional (x ? &x->i : NULL) assembles (under amd64 gcc 8.3 -Os) to the same as &x->i, since i is the initial field of the struct. However, for the purposes of undefined behavior analysis the conditional is needed. Signed-off-by: Jeff Epler <jepler@gmail.com>
2021-02-04py: Rename BITS_PER_BYTE to MP_BITS_PER_BYTE.Damien George
To give this macro a standard MP_ prefix. Signed-off-by: Damien George <damien@micropython.org>
2020-04-18py/objint: Do not use fpclassify.stijn
For combinations of certain versions of glibc and gcc the definition of fpclassify always takes float as argument instead of adapting itself to float/double/long double as required by the C99 standard. At the time of writing this happens for instance for glibc 2.27 with gcc 7.5.0 when compiled with -Os and glibc 3.0.7 with gcc 9.3.0. When calling fpclassify with double as argument, as in objint.c, this results in an implicit narrowing conversion which is not really correct plus results in a warning when compiled with -Wfloat-conversion. So fix this by spelling out the logic manually.
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2019-11-05all: Convert nlr_raise(mp_obj_new_exception_msg(x)) to mp_raise_msg(x).Damien George
This helper function was added a while ago and these are the remaining cases to convert, to save a bit of code size.
2019-02-12py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API.Damien George
These macros could in principle be (inline) functions so it makes sense to have them lower case, to match the other C API functions. The remaining macros that are upper case are: - MP_OBJ_TO_PTR, MP_OBJ_FROM_PTR - MP_OBJ_NEW_SMALL_INT, MP_OBJ_SMALL_INT_VALUE - MP_OBJ_NEW_QSTR, MP_OBJ_QSTR_VALUE - MP_OBJ_FUN_MAKE_SIG - MP_DECLARE_CONST_xxx - MP_DEFINE_CONST_xxx These must remain macros because they are used when defining const data (at least, MP_OBJ_NEW_SMALL_INT is so it makes sense to have MP_OBJ_SMALL_INT_VALUE also a macro). For those macros that have been made lower case, compatibility macros are provided for the old names so that users do not need to change their code immediately.
2018-12-07py/obj: Add support for __int__ special method.Paul Sokolovsky
Based on the discussion, this special method is available unconditionally, as converting to int is a common operation.
2018-09-27py/objint: Remove TODO about checking of int() arg types with 2 args.Damien George
The arguments are checked by mp_obj_str_get_data and mp_obj_get_int.
2018-03-02py/objint: Remove unreachable code checking for int type in format func.Damien George
All callers of mp_obj_int_formatted() are expected to pass in a valid int object, and they do: - mp_obj_int_print() should always pass through an int object because it is the print special method for int instances. - mp_print_mp_int() checks that the argument is an int, and if not converts it to a small int. This patch saves around 20-50 bytes of code space.
2018-02-21py/objint: Use MP_OBJ_IS_STR_OR_BYTES macro instead of 2 separate ones.Damien George
2017-10-04all: Remove inclusion of internal py header files.Damien George
Header files that are considered internal to the py core and should not normally be included directly are: py/nlr.h - internal nlr configuration and declarations py/bc0.h - contains bytecode macro definitions py/runtime0.h - contains basic runtime enums Instead, the top-level header files to include are one of: py/obj.h - includes runtime0.h and defines everything to use the mp_obj_t type py/runtime.h - includes mpstate.h and hence nlr.h, obj.h, runtime0.h, and defines everything to use the general runtime support functions Additional, specific headers (eg py/objlist.h) can be included if needed.
2017-09-18py/modbuiltins: Implement abs() by dispatching to MP_UNARY_OP_ABS.Paul Sokolovsky
This allows user classes to implement __abs__ special method, and saves code size (104 bytes for x86_64), even though during refactor, an issue was fixed and few optimizations were made: * abs() of minimum (negative) small int value is calculated properly. * objint_longlong and objint_mpz avoid allocating new object is the argument is already non-negative.
2017-08-29all: Convert mp_uint_t to mp_unary_op_t/mp_binary_op_t where appropriateDamien George
The unary-op/binary-op enums are already defined, and there are no arithmetic tricks used with these types, so it makes sense to use the correct enum type for arguments that take these values. It also reduces code size quite a bit for nan-boxing builds.
2017-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2017-07-07py,extmod: Some casts and minor refactors to quiet compiler warnings.Tom Collins
2017-06-15py/objint: In to_bytes(), allow length arg to be any int and check sign.Damien George
2017-06-15py/objint: Support "big" byte-order in int.to_bytes().Damien George
2017-05-06py/objint: In int.from_bytes, only create big-int if really needed.Damien George
This patch ensures that int.from_bytes only creates a big-int if necessary, by checking the value for a small-int overflow as it's being parsed.
2017-04-11py/objint: Use unsigned arithmetic when formatting an integer.Damien George
Otherwise the edge case of the most negative integer value will not convert correctly.
2017-04-11py/objint: Extract small int value directly because type is known.Damien George
2017-04-04py/objint: Consolidate mp_obj_new_int_from_float to one implementation.Damien George
This reduces code duplication and allows to make mp_classify_fp_as_int static, which reduces code size.
2017-03-29py: Change mp_uint_t to size_t for mp_obj_str_get_data len arg.Damien George
2017-03-23py/objint: Handle special case of -0 when classifying fp as int.Damien George
Otherwise -0.0 is classified as a bigint, which for builds without bigints will lead unexpectedly to an overflow.
2017-03-14py/objint: Allow to print long-long ints without using the heap.Damien George
Some stack is allocated to format ints, and when the int implementation uses long-long there should be additional stack allocated compared with the other cases. This patch uses the existing "fmt_int_t" type to determine the amount of stack to allocate.
2017-02-16py/objint: Convert mp_uint_t to size_t where appropriate.Damien George
2017-01-25py/objint: Fix left-shift overflow in checking for large int.Damien George
2017-01-21py/objint: from_bytes(): Implement "byteorder" param and arbitrary precision.Paul Sokolovsky
If result guaranteedly fits in a small int, it is handled in objint.c. Otherwise, it is delegated to mp_obj_int_from_bytes_impl(), which should be implemented by individual objint_*.c, similar to mp_obj_int_to_bytes_impl().
2016-12-28py/objint: Simplify mp_int_format_size and remove unreachable code.Damien George
One never needs to format integers with a base larger than 16 (but code can be easily extended beyond this value if needed in the future).
2016-12-21py/objint: Rename mp_obj_int_as_float to mp_obj_int_as_float_impl.Damien George
And also simplify it to remove the check for small int. This can be done because this function is only ever called if the argument is not a small int.
2016-12-09py/objint: from_bytes, to_bytes: Require byteorder arg, require "little".Paul Sokolovsky
CPython requires byteorder arg, make uPy compatible. As we support only "little", error out on anything else.
2016-10-17py: Use mp_raise_msg helper function where appropriate.Damien George
Saves the following number of bytes of code space: 176 for bare-arm, 352 for minimal, 272 for unix x86-64, 140 for stmhal, 120 for esp8266.
2016-10-11py/objint: Use size_t for arguments that measure bytes/sizes.Damien George
2016-10-11py: Factor duplicated function to calculate size of formatted int.Damien George
2016-03-10py: Use MP_SMALL_INT_POSITIVE_MASK to check if uint fits in a small int.Damien George
Using the original WORD_MSBIT_HIGH-logic resulted in errors when the object model is not REPR_A or REPR_C.
2016-01-11py: Change first arg of type.make_new from mp_obj_t to mp_obj_type_t*.Damien George
The first argument to the type.make_new method is naturally a uPy type, and all uses of this argument cast it directly to a pointer to a type structure. So it makes sense to just have it a pointer to a type from the very beginning (and a const pointer at that). This patch makes such a change, and removes all unnecessary casting to/from mp_obj_t.
2016-01-11py: Change type signature of builtin funs that take variable or kw args.Damien George
With this patch the n_args parameter is changed type from mp_uint_t to size_t.
2016-01-11py: Change type of .make_new and .call args: mp_uint_t becomes size_t.Damien George
This patch changes the type signature of .make_new and .call object method slots to use size_t for n_args and n_kw (was mp_uint_t. Makes code more efficient when mp_uint_t is larger than a machine word. Doesn't affect ports when size_t and mp_uint_t have the same size.
2016-01-08py/objint: Fix classification of float so it works for OBJ_REPR_D.Damien George
2016-01-07py: Change mp_obj_int_is_positive to more general mp_obj_int_sign.Damien George
This function returns the sign (-1, 0 or 1) of the integer object.
2015-11-29py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.Damien George
This allows the mp_obj_t type to be configured to something other than a pointer-sized primitive type. This patch also includes additional changes to allow the code to compile when sizeof(mp_uint_t) != sizeof(void*), such as using size_t instead of mp_uint_t, and various casts.
2015-11-29py: Add MP_ROM_* macros and mp_rom_* types and use them.Damien George
2015-10-20py: Add mp_obj_is_float function (macro) and use it where appropriate.Damien George
2015-05-12py: Convert hash API to use MP_UNARY_OP_HASH instead of ad-hoc function.Damien George
Hashing is now done using mp_unary_op function with MP_UNARY_OP_HASH as the operator argument. Hashing for int, str and bytes still go via fast-path in mp_unary_op since they are the most common objects which need to be hashed. This lead to quite a bit of code cleanup, and should be more efficient if anything. It saves 176 bytes code space on Thumb2, and 360 bytes on x86. The only loss is that the error message "unhashable type" is now the more generic "unsupported type for __hash__".
2015-04-25py: Support conversion of bignum to bytes.Damien George
This gets int.to_bytes working for bignum, and also struct.pack with 'q' and 'Q' args on 32-bit machines. Addresses issue #1155.
2015-04-16py: Overhaul and simplify printf/pfenv mechanism.Damien George
Previous to this patch the printing mechanism was a bit of a tangled mess. This patch attempts to consolidate printing into one interface. All (non-debug) printing now uses the mp_print* family of functions, mainly mp_printf. All these functions take an mp_print_t structure as their first argument, and this structure defines the printing backend through the "print_strn" function of said structure. Printing from the uPy core can reach the platform-defined print code via two paths: either through mp_sys_stdout_obj (defined pert port) in conjunction with mp_stream_write; or through the mp_plat_print structure which uses the MP_PLAT_PRINT_STRN macro to define how string are printed on the platform. The former is only used when MICROPY_PY_IO is defined. With this new scheme printing is generally more efficient (less layers to go through, less arguments to pass), and, given an mp_print_t* structure, one can call mp_print_str for efficiency instead of mp_printf("%s", ...). Code size is also reduced by around 200 bytes on Thumb2 archs.
2015-03-14py: Fix builtin abs so it works for bools and bignum.Damien George
2015-02-27py: Use m_{new,renew,del} consistently.Damien George
This is so all memory requests go through the same interface.
2015-02-08py: Parse big-int/float/imag constants directly in parser.Damien George
Previous to this patch, a big-int, float or imag constant was interned (made into a qstr) and then parsed at runtime to create an object each time it was needed. This is wasteful in RAM and not efficient. Now, these constants are parsed straight away in the parser and turned into objects. This allows constants with large numbers of digits (so addresses issue #1103) and takes us a step closer to #722.
2015-01-24py: Use float-to-int classifications for mp_obj_new_int_from_float() functionsDavid Steinberg