summaryrefslogtreecommitdiffstatshomepage
path: root/py/objfloat.c
Commit message (Collapse)AuthorAge
* py/objfloat: Workaround non-constant NAN definition on Windows MSVC.Angus Gratton2024-11-28
| | | | | | | | | | | Recent MSVC versions have changed the definition of NAN to a non-constant expression! This is a bug, C standard says it should be a constant. Good explanation and workaround at: https://stackoverflow.com/a/79199887 This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* all: Remove the "STATIC" macro and just use "static" instead.Angus Gratton2024-03-07
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The STATIC macro was introduced a very long time ago in commit d5df6cd44a433d6253a61cb0f987835fbc06b2de. The original reason for this was to have the option to define it to nothing so that all static functions become global functions and therefore visible to certain debug tools, so one could do function size comparison and other things. This STATIC feature is rarely (if ever) used. And with the use of LTO and heavy inline optimisation, analysing the size of individual functions when they are not static is not a good representation of the size of code when fully optimised. So the macro does not have much use and it's simpler to just remove it. Then you know exactly what it's doing. For example, newcomers don't have to learn what the STATIC macro is and why it exists. Reading the code is also less "loud" with a lowercase static. One other minor point in favour of removing it, is that it stops bugs with `STATIC inline`, which should always be `static inline`. Methodology for this commit was: 1) git ls-files | egrep '\.[ch]$' | \ xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/" 2) Do some manual cleanup in the diff by searching for the word STATIC in comments and changing those back. 3) "git-grep STATIC docs/", manually fixed those cases. 4) "rg -t python STATIC", manually fixed codegen lines that used STATIC. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/obj: Convert make_new into a mp_obj_type_t slot.Jim Mussared2022-09-19
| | | | | | | | | | | Instead of being an explicit field, it's now a slot like all the other methods. This is a marginal code size improvement because most types have a make_new (100/138 on PYBV11), however it improves consistency in how types are declared, removing the special case for make_new. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* all: Make all mp_obj_type_t defs use MP_DEFINE_CONST_OBJ_TYPE.Jim Mussared2022-09-19
| | | | | | In preparation for upcoming rework of mp_obj_type_t layout. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/parsenum: Optimise when building with complex disabled.Damien George2022-06-23
| | | | | | To reduce code size when MICROPY_PY_BUILTINS_COMPLEX is disabled. Signed-off-by: Damien George <damien@micropython.org>
* py/objfloat: Explain why mp_obj_malloc isn't used.Jim Mussared2022-05-03
| | | | Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/modmath: Add math.tau, math.nan and math.inf constants.stijn2022-01-23
| | | | Configurable by the new MICROPY_PY_MATH_CONSTANTS option.
* py: Rename BITS_PER_BYTE to MP_BITS_PER_BYTE.Damien George2021-02-04
| | | | | | To give this macro a standard MP_ prefix. Signed-off-by: Damien George <damien@micropython.org>
* py: Fix handling of NaN in certain pow implementations.stijn2020-09-11
| | | | | Adds a new compile-time option MICROPY_PY_MATH_POW_FIX_NAN for use with toolchains that don't handle pow-of-NaN correctly.
* py/objfloat: Fix handling of negative float to power of nan.Damien George2020-09-11
| | | | | | | | Prior to this commit, pow(-2, float('nan')) would return (nan+nanj), or raise an exception on targets that don't support complex numbers. This is fixed to return simply nan, as CPython does. Signed-off-by: Damien George <damien@micropython.org>
* all: Fix implicit floating point promotion.stijn2020-04-18
| | | | | | | | | | | | | Initially some of these were found building the unix coverage variant on MacOS because that build uses clang and has -Wdouble-promotion enabled, and clang performs more vigorous promotion checks than gcc. Additionally the codebase has been compiled with clang and msvc (the latter with warning level 3), and with MICROPY_FLOAT_IMPL_FLOAT to find the rest of the conversions. Fixes are implemented either as explicit casts, or by using the correct type, or by using one of the utility functions to handle floating point casting; these have been moved from nativeglue.c to the public API.
* all: Use MP_ERROR_TEXT for all error messages.Jim Mussared2020-04-05
|
* all: Reformat C and Python source code with tools/codeformat.py.Damien George2020-02-28
| | | | This is run with uncrustify 0.70.1, and black 19.10b0.
* py: Removing dangling "else" to improve code format consistency.Damien George2020-02-28
|
* py: Factor out definition of mp_float_union_t to one location.Damien George2020-02-18
|
* py: Expand type equality flags to 3 separate ones, fix bool/namedtuple.Damien George2020-02-11
| | | | | | | | | | | | | | | Both bool and namedtuple will check against other types for equality; int, float and complex for bool, and tuple for namedtuple. So to make them work after the recent commit 3aab54bf434e7f025a91ea05052f1bac439fad8c they would need MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST set. But that makes all bool and namedtuple equality checks less efficient because mp_obj_equal_not_equal() could no longer short-cut x==x, and would need to try __ne__. To improve this, this commit splits the MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST flags into 3 separate flags to give types more fine-grained control over how their equality behaves. These new flags are then used to fix bool and namedtuple equality. Fixes issue #5615 and #5620.
* py: Support non-boolean results for equality and inequality tests.Nicko van Someren2020-01-30
| | | | | | | | | | | | | | | | | | | | | | | | | | This commit implements a more complete replication of CPython's behaviour for equality and inequality testing of objects. This addresses the issues discussed in #5382 and a few other inconsistencies. Improvements over the old code include: - Support for returning non-boolean results from comparisons (as used by numpy and others). - Support for non-reflexive equality tests. - Preferential use of __ne__ methods and MP_BINARY_OP_NOT_EQUAL binary operators for inequality tests, when available. - Fallback to op2 == op1 or op2 != op1 when op1 does not implement the (in)equality operators. The scheme here makes use of a new flag, MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST, in the flags word of mp_obj_type_t to indicate if various shortcuts can or cannot be used when performing equality and inequality tests. Currently four built-in classes have the flag set: float and complex are non-reflexive (since nan != nan) while bytearray and frozenszet instances can equal other builtin class instances (bytes and set respectively). The flag is also set for any new class defined by the user. This commit also includes a more comprehensive set of tests for the behaviour of (in)equality operators implemented in special methods.
* py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API.Damien George2019-02-12
| | | | | | | | | | | | | | | | | | | | | 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.
* py/objfloat: Fix abs(-0.0) so it returns 0.0.Damien George2018-09-27
| | | | | | Nan and inf (signed and unsigned) are also handled correctly by using signbit (they were also handled correctly with "val<0", but that didn't handle -0.0 correctly). A test case is added for this behaviour.
* py: Shorten error messages by using contractions and some rewording.Damien George2018-09-20
|
* py/objfloat: Fix undefined integer behavior hashing negative zero.Jeff Epler2018-05-21
| | | | | | | | | | | Under ubsan, when evaluating hash(-0.) the following diagnostic occurs: ../../py/objfloat.c:102:15: runtime error: negation of -9223372036854775808 cannot be represented in type 'mp_int_t' (aka 'long'); cast to an unsigned type to negate this value to itself So do just that, to tell the compiler that we want to perform this operation using modulo arithmetic rules.
* py/objfloat: Fix undefined shifting behavior in high-quality float hash.Jeff Epler2018-05-21
| | | | | | | | | | When computing e.g. hash(0.4e3) with ubsan enabled, a diagnostic like the following would occur: ../../py/objfloat.c:91:30: runtime error: shift exponent 44 is too large for 32-bit type 'int' By casting constant "1" to the right type the intended value is preserved.
* py/objfloat: Fix case of raising 0 to -infinity.Damien George2018-02-08
| | | | It was raising an exception but it should return infinity.
* py/objfloat: Allow float() to parse anything with the buffer protocol.Damien George2017-11-21
| | | | This generalises and simplifies the code and follows CPython behaviour.
* all: Remove inclusion of internal py header files.Damien George2017-10-04
| | | | | | | | | | | | | | | | 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.
* py/objfloat: Support raising a negative number to a fractional power.Damien George2017-09-26
| | | | | | This returns a complex number, following CPython behaviour. For ports that don't have complex numbers enabled this will raise a ValueError which gives a fail-safe for scripts that were written assuming complex numbers exist.
* py/{objfloat,objcomplex}: Optimise MP_UNARY_OP_ABS by reusing variables.Damien George2017-09-18
|
* py/modbuiltins: Implement abs() by dispatching to MP_UNARY_OP_ABS.Paul Sokolovsky2017-09-18
| | | | | | | | | | 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.
* py/objfloat: Fix binary ops with incompatible objects.Paul Sokolovsky2017-09-02
| | | | | | | | | | | These are now returned as "operation not supported" instead of raising TypeError. In particular, this fixes equality for float vs incompatible types, which now properly results in False instead of exception. This also paves the road to support reverse operation (e.g. __radd__) with float objects. This is achieved by introducing mp_obj_get_float_maybe(), similar to existing mp_obj_get_int_maybe().
* all: Convert mp_uint_t to mp_unary_op_t/mp_binary_op_t where appropriateDamien George2017-08-29
| | | | | | | 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.
* all: Use the name MicroPython consistently in commentsAlexander Steffen2017-07-31
| | | | | There were several different spellings of MicroPython present in comments, when there should be only one.
* py: Reduce str/repr precision of float numbers when floats are 30-bit.Damien George2017-04-21
| | | | | With 30-bit floats there aren't enough bits to faithfully print 7 decimal digits, so reduce the precision to 6 digits.
* py/objfloat: Add implementation of high-quality float hashing.Damien George2017-04-12
| | | | Disabled by default.
* py: Add very simple but correct hashing for float and complex numbers.Damien George2017-04-04
| | | | | | | | | | Hashing of float and complex numbers that are exact (real) integers should return the same integer hash value as hashing the corresponding integer value. Eg hash(1), hash(1.0) and hash(1+0j) should all be the same (this is how Python is specified: if x==y then hash(x)==hash(y)). This patch implements the simplest way of doing float/complex hashing by just converting the value to int and returning that value.
* py: Change mp_uint_t to size_t for mp_obj_str_get_data len arg.Damien George2017-03-29
|
* py/objfloat: Raise ZeroDivisionError for 0 to negative power.Damien George2017-02-03
|
* py: Use mp_raise_msg helper function where appropriate.Damien George2016-10-17
| | | | | 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.
* py/objfloat, py/modmath: Ensure M_PI and M_E defined.Colin Hogben2016-05-12
| | | | | | In some compliation enviroments (e.g. mbed online compiler) with strict standards compliance, <math.h> does not define constants such as M_PI. Provide fallback definitions of M_E and M_PI where needed.
* py: Change first arg of type.make_new from mp_obj_t to mp_obj_type_t*.Damien George2016-01-11
| | | | | | | | 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.
* py: Change type of .make_new and .call args: mp_uint_t becomes size_t.Damien George2016-01-11
| | | | | | | 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.
* py: Add support for 64-bit NaN-boxing object model, on 32-bit machine.Damien George2015-11-29
| | | | | | | | | | | | | To use, put the following in mpconfigport.h: #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_D) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) typedef int64_t mp_int_t; typedef uint64_t mp_uint_t; #define UINT_FMT "%llu" #define INT_FMT "%lld" Currently does not work with native emitter enabled.
* py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.Damien George2015-11-29
| | | | | | | | | 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.
* py: Add object repr "C", where 30-bit floats are stuffed in obj word.Damien George2015-10-20
| | | | | | This new object representation puts floats into the object word instead of on the heap, at the expense of reducing their precision to 30 bits. It only makes sense when the word size is 32-bits.
* py: Make float representation configurable with object representation.Damien George2015-10-20
|
* py: Move float e/pi consts to objfloat and make mp_obj_float_t private.Damien George2015-10-20
|
* py: Add mp_obj_is_float function (macro) and use it where appropriate.Damien George2015-10-20
|
* py: Rename MP_BOOL() to mp_obj_new_bool() for consistency in naming.Paul Sokolovsky2015-10-11
|
* py: Add MP_BINARY_OP_DIVMOD to simplify and consolidate divmod builtin.Damien George2015-06-13
|
* py: Implement mp_format_float for doubles and use where appropriatestijn2015-05-17
| | | | | | | This allows using (almost) the same code for printing floats everywhere, removes the dependency on sprintf and uses just snprintf and applies an msvc-specific fix for snprintf in a single place so nan/inf are now printed correctly.
* py: Fix printing of "inf" and "nan" floating point values.Damien George2015-04-22
|