summaryrefslogtreecommitdiff
path: root/py/smallint.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2025-07-23 16:14:22 -0500
committerDamien George <damien@micropython.org>2025-10-01 09:23:05 +1000
commita80913292153a14424b29bdb9ca8847e8d35cf73 (patch)
tree9d073ab5c9c1773d30daa89d3062c3ee1bbed380 /py/smallint.c
parent3dd8073c290c077f17ffdee17a019763ad82604d (diff)
py: Add MICROPY_USE_GCC_MUL_OVERFLOW_INTRINSIC.
Most MCUs apart from Cortex-M0 with Thumb 1 have an instruction for computing the "high part" of a multiplication (e.g., the upper 32 bits of a 32x32 multiply). When they do, gcc uses this to implement a small and fast overflow check using the __builtin_mul_overflow intrinsic, which is preferable to the guard division method previously used in smallint.c. However, in contrast to the previous mp_small_int_mul_overflow routine, which checks that the result fits not only within mp_int_t but is SMALL_INT_FITS(), __builtin_mul_overflow only checks for overflow of the C type. As a result, a slight change in the code flow is needed for MP_BINARY_OP_MULTIPLY. Other sites using mp_small_int_mul_overflow already had the result value flow through to a SMALL_INT_FITS check so they didn't need any additional changes. Do similarly for the _ll and _ull multiply overflows checks. Signed-off-by: Jeff Epler <jepler@gmail.com>
Diffstat (limited to 'py/smallint.c')
-rw-r--r--py/smallint.c29
1 files changed, 0 insertions, 29 deletions
diff --git a/py/smallint.c b/py/smallint.c
index a494093d6..eb99b5866 100644
--- a/py/smallint.c
+++ b/py/smallint.c
@@ -26,35 +26,6 @@
#include "py/smallint.h"
-bool mp_small_int_mul_overflow(mp_int_t x, mp_int_t y, mp_int_t *res) {
- // Check for multiply overflow; see CERT INT32-C
- if (x > 0) { // x is positive
- if (y > 0) { // x and y are positive
- if (x > (MP_SMALL_INT_MAX / y)) {
- return true;
- }
- } else { // x positive, y nonpositive
- if (y < (MP_SMALL_INT_MIN / x)) {
- return true;
- }
- } // x positive, y nonpositive
- } else { // x is nonpositive
- if (y > 0) { // x is nonpositive, y is positive
- if (x < (MP_SMALL_INT_MIN / y)) {
- return true;
- }
- } else { // x and y are nonpositive
- if (x != 0 && y < (MP_SMALL_INT_MAX / x)) {
- return true;
- }
- } // End if x and y are nonpositive
- } // End if x is nonpositive
-
- // Result doesn't overflow
- *res = x * y;
- return false;
-}
-
mp_int_t mp_small_int_modulo(mp_int_t dividend, mp_int_t divisor) {
// Python specs require that mod has same sign as second operand
dividend %= divisor;