diff options
| author | cremno <[email protected]> | 2014-05-05 15:28:26 +0200 |
|---|---|---|
| committer | cremno <[email protected]> | 2014-05-05 15:28:26 +0200 |
| commit | cf8df563c0ea9b98714e701ad235acbefc091558 (patch) | |
| tree | 84524b8b9d7020b7fbe7aa09497bed444b1122ae /include | |
| parent | 0f3930426515848be755c5480df354fa9e2109f4 (diff) | |
| download | mruby-cf8df563c0ea9b98714e701ad235acbefc091558.tar.gz mruby-cf8df563c0ea9b98714e701ad235acbefc091558.zip | |
add function for checked mrb_int subtraction
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 35be32a01..fe4f3b264 100644 --- a/include/mruby/numeric.h +++ b/include/mruby/numeric.h @@ -45,6 +45,16 @@ mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum) 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 |
