Skip to content

Commit 58be838

Browse files
dpgeorgeprojectgus
authored andcommitted
py/runtime: Optimise mp_small_int_mul_overflow result.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 1f7ce0f commit 58be838

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

py/runtime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ mp_obj_t MICROPY_WRAP_MP_BINARY_OP(mp_binary_op)(mp_binary_op_t op, mp_obj_t lhs
422422
}
423423

424424
if (mp_obj_is_small_int(lhs)) {
425-
mp_int_t int_res;
426425
mp_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
427426
if (mp_obj_is_small_int(rhs)) {
428427
mp_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
@@ -506,6 +505,7 @@ mp_obj_t MICROPY_WRAP_MP_BINARY_OP(mp_binary_op)(mp_binary_op_t op, mp_obj_t lhs
506505
}
507506
#endif
508507

508+
mp_int_t int_res;
509509
if (mp_small_int_mul_overflow(lhs_val, rhs_val, &int_res)) {
510510
// use higher precision
511511
lhs = mp_obj_new_int_from_ll(lhs_val);
@@ -553,15 +553,15 @@ mp_obj_t MICROPY_WRAP_MP_BINARY_OP(mp_binary_op)(mp_binary_op_t op, mp_obj_t lhs
553553
mp_int_t ans = 1;
554554
while (rhs_val > 0) {
555555
if (rhs_val & 1) {
556-
if (mp_small_int_mul_overflow(ans, lhs_val, &int_res)) {
556+
if (mp_small_int_mul_overflow(ans, lhs_val, &ans)) {
557557
goto power_overflow;
558558
}
559-
ans = int_res;
560559
}
561560
if (rhs_val == 1) {
562561
break;
563562
}
564563
rhs_val /= 2;
564+
mp_int_t int_res;
565565
if (mp_small_int_mul_overflow(lhs_val, lhs_val, &int_res)) {
566566
goto power_overflow;
567567
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy