2 ** mruby/numeric.h - Numeric, Integer, Float, Fixnum class
4 ** See Copyright Notice in mruby.h
7 #ifndef MRUBY_NUMERIC_H
8 #define MRUBY_NUMERIC_H
13 * Numeric class and it's sub-classes.
15 * Integer, Float and Fixnum
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
26 #define FIXABLE_FLOAT(f) TYPED_FIXABLE(f,double)
29 #ifndef MRB_WITHOUT_FLOAT
30 MRB_API mrb_value mrb_flo_to_fixnum(mrb_state *mrb, mrb_value val);
32 MRB_API mrb_value mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, mrb_int base);
33 /* ArgumentError if format string doesn't match /%(\.[0-9]+)?[aAeEfFgG]/ */
34 #ifndef MRB_WITHOUT_FLOAT
35 MRB_API mrb_value mrb_float_to_str(mrb_state *mrb, mrb_value x, const char *fmt);
36 MRB_API mrb_float mrb_to_flo(mrb_state *mrb, mrb_value x);
39 mrb_value mrb_fixnum_plus(mrb_state *mrb, mrb_value x, mrb_value y);
40 mrb_value mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y);
41 mrb_value mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y);
42 mrb_value mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y);
45 #define __has_builtin(x) 0
48 #if (defined(__GNUC__) && __GNUC__ >= 5) || \
49 (__has_builtin(__builtin_add_overflow) && \
50 __has_builtin(__builtin_sub_overflow) && \
51 __has_builtin(__builtin_mul_overflow))
52 # define MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
56 // Clang 3.8 and 3.9 have problem compiling mruby in 32-bit mode, when MRB_INT64 is set
57 // because of missing __mulodi4 and similar functions in its runtime. We need to use custom
58 // implementation for them.
60 #ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
61 #if defined(__clang__) && (__clang_major__ == 3) && (__clang_minor__ >= 8) && \
62 defined(MRB_32BIT) && defined(MRB_INT64)
63 #undef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
67 #ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
69 #ifndef MRB_WORD_BOXING
72 # define WBCHK(x) !FIXABLE(x)
75 static inline mrb_bool
76 mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
78 return __builtin_add_overflow(augend, addend, sum) || WBCHK(*sum);
81 static inline mrb_bool
82 mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
84 return __builtin_sub_overflow(minuend, subtrahend, difference) || WBCHK(*difference);
87 static inline mrb_bool
88 mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
90 return __builtin_mul_overflow(multiplier, multiplicand, product) || WBCHK(*product);
97 #define MRB_UINT_MAKE2(n) uint ## n ## _t
98 #define MRB_UINT_MAKE(n) MRB_UINT_MAKE2(n)
99 #define mrb_uint MRB_UINT_MAKE(MRB_INT_BIT)
101 #define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1 - MRB_FIXNUM_SHIFT))
103 static inline mrb_bool
104 mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
106 mrb_uint x = (mrb_uint)augend;
107 mrb_uint y = (mrb_uint)addend;
108 mrb_uint z = (mrb_uint)(x + y);
110 return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK);
113 static inline mrb_bool
114 mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
116 mrb_uint x = (mrb_uint)minuend;
117 mrb_uint y = (mrb_uint)subtrahend;
118 mrb_uint z = (mrb_uint)(x - y);
119 *difference = (mrb_int)z;
120 return !!(((x ^ z) & (~y ^ z)) & MRB_INT_OVERFLOW_MASK);
123 static inline mrb_bool
124 mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
126 #if MRB_INT_BIT == 32
127 int64_t n = (int64_t)multiplier * multiplicand;
128 *product = (mrb_int)n;
131 if (multiplier > 0) {
132 if (multiplicand > 0) {
133 if (multiplier > MRB_INT_MAX / multiplicand) return TRUE;
136 if (multiplicand < MRB_INT_MAX / multiplier) return TRUE;
140 if (multiplicand > 0) {
141 if (multiplier < MRB_INT_MAX / multiplicand) return TRUE;
144 if (multiplier != 0 && multiplicand < MRB_INT_MAX / multiplier) return TRUE;
147 *product = multiplier * multiplicand;
152 #undef MRB_INT_OVERFLOW_MASK
155 #undef MRB_UINT_MAKE2
161 #endif /* MRUBY_NUMERIC_H */