summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-05-05 22:54:55 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-05-05 22:54:55 +0900
commit260d38d5ee5228bcfae1a834357ed58bbb96ea80 (patch)
tree12422d1ade4d3db453d84578e04c6db12a0a1e63
parent2ceb71f97021d6d42b802e7edf522dd16357a932 (diff)
parentcf8df563c0ea9b98714e701ad235acbefc091558 (diff)
downloadmruby-260d38d5ee5228bcfae1a834357ed58bbb96ea80.tar.gz
mruby-260d38d5ee5228bcfae1a834357ed58bbb96ea80.zip
Merge pull request #2200 from cremno/add-functions-for-safe-addition-and-subtraction
Add functions for safe addition and subtraction
-rw-r--r--include/mruby/numeric.h35
-rw-r--r--src/numeric.c8
-rw-r--r--src/vm.c25
3 files changed, 44 insertions, 24 deletions
diff --git a/include/mruby/numeric.h b/include/mruby/numeric.h
index 17291c2f2..fe4f3b264 100644
--- a/include/mruby/numeric.h
+++ b/include/mruby/numeric.h
@@ -25,6 +25,41 @@ mrb_value mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_float mrb_to_flo(mrb_state *mrb, mrb_value x);
+#define MRB_UINT_MAKE2(n) uint ## n ## _t
+#define MRB_UINT_MAKE(n) MRB_UINT_MAKE2(n)
+#define mrb_uint MRB_UINT_MAKE(MRB_INT_BIT)
+
+#ifdef MRB_WORD_BOXING
+# define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1 - MRB_FIXNUM_SHIFT))
+#else
+# define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1))
+#endif
+
+static inline mrb_bool
+mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
+{
+ mrb_uint x = (mrb_uint)augend;
+ mrb_uint y = (mrb_uint)addend;
+ mrb_uint z = (mrb_uint)(x + y);
+ *sum = (mrb_int)z;
+ return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK);
+}
+
+static inline mrb_bool
+mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
+{
+ mrb_uint x = (mrb_uint)minuend;
+ mrb_uint y = (mrb_uint)subtrahend;
+ mrb_uint z = (mrb_uint)(x - y);
+ *difference = (mrb_int)z;
+ return !!(((x ^ z) & (~y ^ z)) & MRB_INT_OVERFLOW_MASK);
+}
+
+#undef MRB_INT_OVERFLOW_MASK
+#undef mrb_uint
+#undef MRB_UINT_MAKE
+#undef MRB_UINT_MAKE2
+
#if defined(__cplusplus)
} /* extern "C" { */
#endif
diff --git a/src/numeric.c b/src/numeric.c
index 56835edbf..7b025c34d 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -1109,9 +1109,7 @@ mrb_fixnum_plus(mrb_state *mrb, mrb_value x, mrb_value y)
if (a == 0) return y;
b = mrb_fixnum(y);
- c = a + b;
- if (((a < 0) ^ (b < 0)) == 0 && (a < 0) != (c < 0)) {
- /* integer overflow */
+ if (mrb_int_add_overflow(a, b, &c)) {
return mrb_float_value(mrb, (mrb_float)a + (mrb_float)b);
}
return mrb_fixnum_value(c);
@@ -1147,9 +1145,7 @@ mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y)
mrb_int b, c;
b = mrb_fixnum(y);
- c = a - b;
- if (((a < 0) ^ (b < 0)) != 0 && (a < 0) != (c < 0)) {
- /* integer overflow */
+ if (mrb_int_sub_overflow(a, b, &c)) {
return mrb_float_value(mrb, (mrb_float)a - (mrb_float)b);
}
return mrb_fixnum_value(c);
diff --git a/src/vm.c b/src/vm.c
index 0e3c0a81e..ae19db0b6 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -12,6 +12,7 @@
#include "mruby/class.h"
#include "mruby/hash.h"
#include "mruby/irep.h"
+#include "mruby/numeric.h"
#include "mruby/proc.h"
#include "mruby/range.h"
#include "mruby/string.h"
@@ -1610,12 +1611,7 @@ RETRY_TRY_BLOCK:
x = mrb_fixnum(regs_a[0]);
y = mrb_fixnum(regs_a[1]);
- z = x + y;
-#ifdef MRB_WORD_BOXING
- z = (z << MRB_FIXNUM_SHIFT) / (1 << MRB_FIXNUM_SHIFT);
-#endif
- if ((x < 0) != (z < 0) && ((x < 0) ^ (y < 0)) == 0) {
- /* integer overflow */
+ if (mrb_int_add_overflow(x, y, &z)) {
SET_FLT_VALUE(mrb, regs_a[0], (mrb_float)x + (mrb_float)y);
break;
}
@@ -1673,12 +1669,7 @@ RETRY_TRY_BLOCK:
x = mrb_fixnum(regs[a]);
y = mrb_fixnum(regs[a+1]);
- z = x - y;
-#ifdef MRB_WORD_BOXING
- z = (z << MRB_FIXNUM_SHIFT) / (1 << MRB_FIXNUM_SHIFT);
-#endif
- if (((x < 0) ^ (y < 0)) != 0 && (x < 0) != (z < 0)) {
- /* integer overflow */
+ if (mrb_int_sub_overflow(x, y, &z)) {
SET_FLT_VALUE(mrb, regs[a], (mrb_float)x - (mrb_float)y);
break;
}
@@ -1842,10 +1833,9 @@ RETRY_TRY_BLOCK:
{
mrb_int x = regs[a].attr_i;
mrb_int y = GETARG_C(i);
- mrb_int z = x + y;
+ mrb_int z;
- if (((x < 0) ^ (y < 0)) == 0 && (x < 0) != (z < 0)) {
- /* integer overflow */
+ if (mrb_int_add_overflow(x, y, &z)) {
SET_FLT_VALUE(mrb, regs[a], (mrb_float)x + (mrb_float)y);
break;
}
@@ -1881,10 +1871,9 @@ RETRY_TRY_BLOCK:
{
mrb_int x = regs_a[0].attr_i;
mrb_int y = GETARG_C(i);
- mrb_int z = x - y;
+ mrb_int z;
- if ((x < 0) != (z < 0) && ((x < 0) ^ (y < 0)) != 0) {
- /* integer overflow */
+ if (mrb_int_sub_overflow(x, y, &z)) {
SET_FLT_VALUE(mrb, regs_a[0], (mrb_float)x - (mrb_float)y);
}
else {