summaryrefslogtreecommitdiff
path: root/py/smallint.h
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-05-28 14:51:12 +0100
committerDamien George <damien.p.george@gmail.com>2014-05-28 14:51:12 +0100
commitd1e355ea8e2a5a17ee126f5c3d173b2e6f33e460 (patch)
treed5ba59452f72a9dcc31fc6cdcf0ad8c08c8713b2 /py/smallint.h
parent813ed3bda6818bd8dd15ee5e3c673a24321e740b (diff)
py: Fix check of small-int overflow when parsing ints.
Also unifies use of SMALL_INT_FITS macro across parser and runtime.
Diffstat (limited to 'py/smallint.h')
-rw-r--r--py/smallint.h5
1 files changed, 5 insertions, 0 deletions
diff --git a/py/smallint.h b/py/smallint.h
index 9c1de42ad..36b55f919 100644
--- a/py/smallint.h
+++ b/py/smallint.h
@@ -26,6 +26,11 @@
// Functions for small integer arithmetic
+// In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
+#define MP_SMALL_INT_MIN ((mp_small_int_t)(((machine_int_t)WORD_MSBIT_HIGH) >> 1))
+#define MP_SMALL_INT_MAX ((mp_small_int_t)(~(MP_SMALL_INT_MIN)))
+#define MP_SMALL_INT_FITS(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
+
bool mp_small_int_mul_overflow(machine_int_t x, machine_int_t y);
machine_int_t mp_small_int_modulo(machine_int_t dividend, machine_int_t divisor);
machine_int_t mp_small_int_floor_divide(machine_int_t num, machine_int_t denom);