diff options
author | Angus Gratton <angus@redyak.com.au> | 2025-05-02 15:39:35 +1000 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2025-07-18 00:12:13 +1000 |
commit | e9845ab20ec798c1d5bf00bd3b64ff5d96d94500 (patch) | |
tree | a3cb1999eac4270ca29d1f8556be8491595e5b62 /py/runtime.c | |
parent | 516aa02104c3344903bdda078b7c87f71f94938d (diff) |
py/smallint: Update mp_small_int_mul_overflow() to perform the multiply.
Makes it compatible with the __builtin_mul_overflow() syntax, used in
follow-up commit.
Includes optimisation in runtime.c to minimise the code size impact from
additional param.
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Diffstat (limited to 'py/runtime.c')
-rw-r--r-- | py/runtime.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/py/runtime.c b/py/runtime.c index 90587a010..0ab0626ef 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -505,13 +505,14 @@ mp_obj_t MICROPY_WRAP_MP_BINARY_OP(mp_binary_op)(mp_binary_op_t op, mp_obj_t lhs } #endif - if (mp_small_int_mul_overflow(lhs_val, rhs_val)) { + mp_int_t int_res; + if (mp_small_int_mul_overflow(lhs_val, rhs_val, &int_res)) { // use higher precision lhs = mp_obj_new_int_from_ll(lhs_val); goto generic_binary_op; } else { // use standard precision - return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val); + return MP_OBJ_NEW_SMALL_INT(int_res); } } case MP_BINARY_OP_FLOOR_DIVIDE: @@ -552,19 +553,19 @@ mp_obj_t MICROPY_WRAP_MP_BINARY_OP(mp_binary_op)(mp_binary_op_t op, mp_obj_t lhs mp_int_t ans = 1; while (rhs_val > 0) { if (rhs_val & 1) { - if (mp_small_int_mul_overflow(ans, lhs_val)) { + if (mp_small_int_mul_overflow(ans, lhs_val, &ans)) { goto power_overflow; } - ans *= lhs_val; } if (rhs_val == 1) { break; } rhs_val /= 2; - if (mp_small_int_mul_overflow(lhs_val, lhs_val)) { + mp_int_t int_res; + if (mp_small_int_mul_overflow(lhs_val, lhs_val, &int_res)) { goto power_overflow; } - lhs_val *= lhs_val; + lhs_val = int_res; } lhs_val = ans; } |