summaryrefslogtreecommitdiff
path: root/py/emitcommon.c
diff options
context:
space:
mode:
authorYoctopuce dev <dev@yoctopuce.com>2025-01-28 00:26:08 +0100
committerDamien George <damien@micropython.org>2025-08-01 13:35:44 +1000
commit69ead7d98ef30df3b6bd4485633490e80fca1718 (patch)
treee00552cc696fad38a03de442c99c289fb1f8a2e9 /py/emitcommon.c
parentf67a3703118be7d97629130d99630996ff3cb255 (diff)
py/parse: Add support for math module constants and float folding.
Add a new MICROPY_COMP_CONST_FLOAT feature, enabled by in mpy-cross and when compiling with MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES. The new feature leverages the code of MICROPY_COMP_CONST_FOLDING to support folding of floating point constants. If MICROPY_COMP_MODULE_CONST is defined as well, math module constants are made available at compile time. For example: _DEG_TO_GRADIANT = const(math.pi / 180) _INVALID_VALUE = const(math.nan) A few corner cases had to be handled: - The float const folding code should not fold expressions resulting into complex results, as the mpy parser for complex immediates has limitations. - The constant generation code must distinguish between -0.0 and 0.0, which are different even if C consider them as ==. This change removes previous limitations on the use of `const()` expressions that would result in floating point number, so the test cases of micropython/const_error have to be updated. Additional test cases have been added to cover the new repr() code (from a previous commit). A few other simple test cases have been added to handle the use of floats in `const()` expressions, but the float folding code itself is also tested when running general float test cases, as float expressions often get resolved at compile-time (with this change). Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
Diffstat (limited to 'py/emitcommon.c')
-rw-r--r--py/emitcommon.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/py/emitcommon.c b/py/emitcommon.c
index a9eb6e202..1f701db80 100644
--- a/py/emitcommon.c
+++ b/py/emitcommon.c
@@ -25,6 +25,7 @@
*/
#include <assert.h>
+#include <math.h>
#include "py/emit.h"
#include "py/nativeglue.h"
@@ -72,7 +73,21 @@ static bool strictly_equal(mp_obj_t a, mp_obj_t b) {
}
return true;
} else {
- return mp_obj_equal(a, b);
+ if (!mp_obj_equal(a, b)) {
+ return false;
+ }
+ #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_COMP_CONST_FLOAT
+ if (a_type == &mp_type_float) {
+ mp_float_t a_val = mp_obj_float_get(a);
+ if (a_val == (mp_float_t)0.0) {
+ // Although 0.0 == -0.0, they are not strictly_equal and
+ // must be stored as two different constants in .mpy files
+ mp_float_t b_val = mp_obj_float_get(b);
+ return signbit(a_val) == signbit(b_val);
+ }
+ }
+ #endif
+ return true;
}
}