mirror of
https://github.com/micropython/micropython.git
synced 2026-01-04 11:10:14 +01:00
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>
This commit is contained in:
committed by
Damien George
parent
3dd8073c29
commit
a809132921
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user