7 #ifndef MRUBY_NUMERIC_H 8 #define MRUBY_NUMERIC_H 19 #define TYPED_POSFIXABLE(f,t) ((f) <= (t)MRB_INT_MAX) 20 #define TYPED_NEGFIXABLE(f,t) ((f) >= (t)MRB_INT_MIN) 21 #define TYPED_FIXABLE(f,t) (TYPED_POSFIXABLE(f,t) && TYPED_NEGFIXABLE(f,t)) 22 #define POSFIXABLE(f) TYPED_POSFIXABLE(f,mrb_int) 23 #define NEGFIXABLE(f) TYPED_NEGFIXABLE(f,mrb_int) 24 #define FIXABLE(f) TYPED_FIXABLE(f,mrb_int) 25 #ifndef MRB_WITHOUT_FLOAT 27 #define FIXABLE_FLOAT(f) ((f)>=-9223372036854775808.0 && (f)<9223372036854775808.0) 29 #define FIXABLE_FLOAT(f) TYPED_FIXABLE(f,mrb_float) 33 #ifndef MRB_WITHOUT_FLOAT 38 #ifndef MRB_WITHOUT_FLOAT 49 #define __has_builtin(x) 0 52 #if (defined(__GNUC__) && __GNUC__ >= 5) || \ 53 (__has_builtin(__builtin_add_overflow) && \ 54 __has_builtin(__builtin_sub_overflow) && \ 55 __has_builtin(__builtin_mul_overflow)) 56 # define MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS 64 #ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS 65 #if defined(__clang__) && (__clang_major__ == 3) && (__clang_minor__ >= 8) && \ 66 defined(MRB_32BIT) && defined(MRB_INT64) 67 #undef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS 71 #ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS 73 #ifndef MRB_WORD_BOXING 76 # define WBCHK(x) !FIXABLE(x) 80 mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
82 return __builtin_add_overflow(augend, addend, sum) || WBCHK(*sum);
86 mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
88 return __builtin_sub_overflow(minuend, subtrahend, difference) || WBCHK(*difference);
92 mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
94 return __builtin_mul_overflow(multiplier, multiplicand, product) || WBCHK(*product);
101 #define MRB_UINT_MAKE2(n) uint ## n ## _t 102 #define MRB_UINT_MAKE(n) MRB_UINT_MAKE2(n) 103 #define mrb_uint MRB_UINT_MAKE(MRB_INT_BIT) 105 #define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1 - MRB_FIXNUM_SHIFT)) 108 mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
110 mrb_uint x = (mrb_uint)augend;
111 mrb_uint y = (mrb_uint)addend;
112 mrb_uint z = (mrb_uint)(x + y);
114 return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK);
118 mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
120 mrb_uint x = (mrb_uint)minuend;
121 mrb_uint y = (mrb_uint)subtrahend;
122 mrb_uint z = (mrb_uint)(x - y);
123 *difference = (mrb_int)z;
124 return !!(((x ^ z) & (~y ^ z)) & MRB_INT_OVERFLOW_MASK);
128 mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
130 #if MRB_INT_BIT == 32 131 int64_t n = (int64_t)multiplier * multiplicand;
132 *product = (mrb_int)n;
135 if (multiplier > 0) {
136 if (multiplicand > 0) {
137 if (multiplier > MRB_INT_MAX / multiplicand)
return TRUE;
140 if (multiplicand < MRB_INT_MAX / multiplier)
return TRUE;
144 if (multiplicand > 0) {
145 if (multiplier < MRB_INT_MAX / multiplicand)
return TRUE;
148 if (multiplier != 0 && multiplicand < MRB_INT_MAX / multiplier)
return TRUE;
151 *product = multiplier * multiplicand;
156 #undef MRB_INT_OVERFLOW_MASK 159 #undef MRB_UINT_MAKE2 163 #ifndef MRB_WITHOUT_FLOAT 167 # define MRB_FLT_RADIX FLT_RADIX 169 # ifdef MRB_USE_FLOAT 170 # define MRB_FLT_MANT_DIG FLT_MANT_DIG 171 # define MRB_FLT_EPSILON FLT_EPSILON 172 # define MRB_FLT_DIG FLT_DIG 173 # define MRB_FLT_MIN_EXP FLT_MIN_EXP 174 # define MRB_FLT_MIN FLT_MIN 175 # define MRB_FLT_MIN_10_EXP FLT_MIN_10_EXP 176 # define MRB_FLT_MAX_EXP FLT_MAX_EXP 177 # define MRB_FLT_MAX FLT_MAX 178 # define MRB_FLT_MAX_10_EXP FLT_MAX_10_EXP 181 # define MRB_FLT_MANT_DIG DBL_MANT_DIG 182 # define MRB_FLT_EPSILON DBL_EPSILON 183 # define MRB_FLT_DIG DBL_DIG 184 # define MRB_FLT_MIN_EXP DBL_MIN_EXP 185 # define MRB_FLT_MIN DBL_MIN 186 # define MRB_FLT_MIN_10_EXP DBL_MIN_10_EXP 187 # define MRB_FLT_MAX_EXP DBL_MAX_EXP 188 # define MRB_FLT_MAX DBL_MAX 189 # define MRB_FLT_MAX_10_EXP DBL_MAX_10_EXP
#define MRB_BEGIN_DECL
Start declarations in C mode.
Definition: common.h:26
#define MRB_API
Declare a public MRuby API function.
Definition: common.h:73
#define MRB_END_DECL
End declarations in C mode.
Definition: common.h:28
mruby common platform definition"
Definition: boxing_nan.h:39