diff options
| author | cremno <[email protected]> | 2014-05-05 15:24:22 +0200 |
|---|---|---|
| committer | cremno <[email protected]> | 2014-05-05 15:24:22 +0200 |
| commit | 0f3930426515848be755c5480df354fa9e2109f4 (patch) | |
| tree | 55997d13d181d852b2522aa0b3b69831b384e352 /include | |
| parent | ff03cea79b710913af1f5decec1952c14ba67812 (diff) | |
| download | mruby-0f3930426515848be755c5480df354fa9e2109f4.tar.gz mruby-0f3930426515848be755c5480df354fa9e2109f4.zip | |
add function for checked mrb_int addition
Diffstat (limited to 'include')
| -rw-r--r-- | include/mruby/numeric.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/include/mruby/numeric.h b/include/mruby/numeric.h index 63a5eb300..35be32a01 100644 --- a/include/mruby/numeric.h +++ b/include/mruby/numeric.h @@ -35,6 +35,16 @@ mrb_float mrb_to_flo(mrb_state *mrb, mrb_value x); # 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); +} + #undef MRB_INT_OVERFLOW_MASK #undef mrb_uint #undef MRB_UINT_MAKE |
