1 | /**
|
---|
2 | ** @file mruby/numeric.h - Numeric, Integer, Float, Fixnum class
|
---|
3 | **
|
---|
4 | ** See Copyright Notice in mruby.h
|
---|
5 | */
|
---|
6 |
|
---|
7 | #ifndef MRUBY_NUMERIC_H
|
---|
8 | #define MRUBY_NUMERIC_H
|
---|
9 |
|
---|
10 | #include "common.h"
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Numeric class and it's sub-classes.
|
---|
14 | *
|
---|
15 | * Integer, Float and Fixnum
|
---|
16 | */
|
---|
17 | MRB_BEGIN_DECL
|
---|
18 |
|
---|
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 | #ifdef MRB_INT64
|
---|
27 | #define FIXABLE_FLOAT(f) ((f)>=-9223372036854775808.0 && (f)<9223372036854775808.0)
|
---|
28 | #else
|
---|
29 | #define FIXABLE_FLOAT(f) TYPED_FIXABLE(f,mrb_float)
|
---|
30 | #endif
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #ifndef MRB_WITHOUT_FLOAT
|
---|
34 | MRB_API mrb_value mrb_flo_to_fixnum(mrb_state *mrb, mrb_value val);
|
---|
35 | #endif
|
---|
36 | MRB_API mrb_value mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, mrb_int base);
|
---|
37 | /* ArgumentError if format string doesn't match /%(\.[0-9]+)?[aAeEfFgG]/ */
|
---|
38 | #ifndef MRB_WITHOUT_FLOAT
|
---|
39 | MRB_API mrb_value mrb_float_to_str(mrb_state *mrb, mrb_value x, const char *fmt);
|
---|
40 | MRB_API mrb_float mrb_to_flo(mrb_state *mrb, mrb_value x);
|
---|
41 | MRB_API mrb_value mrb_int_value(mrb_state *mrb, mrb_float f);
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | MRB_API mrb_value mrb_num_plus(mrb_state *mrb, mrb_value x, mrb_value y);
|
---|
45 | MRB_API mrb_value mrb_num_minus(mrb_state *mrb, mrb_value x, mrb_value y);
|
---|
46 | MRB_API mrb_value mrb_num_mul(mrb_state *mrb, mrb_value x, mrb_value y);
|
---|
47 |
|
---|
48 | #ifndef __has_builtin
|
---|
49 | #define __has_builtin(x) 0
|
---|
50 | #endif
|
---|
51 |
|
---|
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
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | /*
|
---|
60 | // Clang 3.8 and 3.9 have problem compiling mruby in 32-bit mode, when MRB_INT64 is set
|
---|
61 | // because of missing __mulodi4 and similar functions in its runtime. We need to use custom
|
---|
62 | // implementation for them.
|
---|
63 | */
|
---|
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
|
---|
68 | #endif
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
|
---|
72 |
|
---|
73 | #ifndef MRB_WORD_BOXING
|
---|
74 | # define WBCHK(x) 0
|
---|
75 | #else
|
---|
76 | # define WBCHK(x) !FIXABLE(x)
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | static inline mrb_bool
|
---|
80 | mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
|
---|
81 | {
|
---|
82 | return __builtin_add_overflow(augend, addend, sum) || WBCHK(*sum);
|
---|
83 | }
|
---|
84 |
|
---|
85 | static inline mrb_bool
|
---|
86 | mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
|
---|
87 | {
|
---|
88 | return __builtin_sub_overflow(minuend, subtrahend, difference) || WBCHK(*difference);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static inline mrb_bool
|
---|
92 | mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
|
---|
93 | {
|
---|
94 | return __builtin_mul_overflow(multiplier, multiplicand, product) || WBCHK(*product);
|
---|
95 | }
|
---|
96 |
|
---|
97 | #undef WBCHK
|
---|
98 |
|
---|
99 | #else
|
---|
100 |
|
---|
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)
|
---|
104 |
|
---|
105 | #define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1 - MRB_FIXNUM_SHIFT))
|
---|
106 |
|
---|
107 | static inline mrb_bool
|
---|
108 | mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
|
---|
109 | {
|
---|
110 | mrb_uint x = (mrb_uint)augend;
|
---|
111 | mrb_uint y = (mrb_uint)addend;
|
---|
112 | mrb_uint z = (mrb_uint)(x + y);
|
---|
113 | *sum = (mrb_int)z;
|
---|
114 | return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK);
|
---|
115 | }
|
---|
116 |
|
---|
117 | static inline mrb_bool
|
---|
118 | mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
|
---|
119 | {
|
---|
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);
|
---|
125 | }
|
---|
126 |
|
---|
127 | static inline mrb_bool
|
---|
128 | mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
|
---|
129 | {
|
---|
130 | #if MRB_INT_BIT == 32
|
---|
131 | int64_t n = (int64_t)multiplier * multiplicand;
|
---|
132 | *product = (mrb_int)n;
|
---|
133 | return !FIXABLE(n);
|
---|
134 | #else
|
---|
135 | if (multiplier > 0) {
|
---|
136 | if (multiplicand > 0) {
|
---|
137 | if (multiplier > MRB_INT_MAX / multiplicand) return TRUE;
|
---|
138 | }
|
---|
139 | else {
|
---|
140 | if (multiplicand < MRB_INT_MAX / multiplier) return TRUE;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | else {
|
---|
144 | if (multiplicand > 0) {
|
---|
145 | if (multiplier < MRB_INT_MAX / multiplicand) return TRUE;
|
---|
146 | }
|
---|
147 | else {
|
---|
148 | if (multiplier != 0 && multiplicand < MRB_INT_MAX / multiplier) return TRUE;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | *product = multiplier * multiplicand;
|
---|
152 | return FALSE;
|
---|
153 | #endif
|
---|
154 | }
|
---|
155 |
|
---|
156 | #undef MRB_INT_OVERFLOW_MASK
|
---|
157 | #undef mrb_uint
|
---|
158 | #undef MRB_UINT_MAKE
|
---|
159 | #undef MRB_UINT_MAKE2
|
---|
160 |
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | #ifndef MRB_WITHOUT_FLOAT
|
---|
164 | # include <stdint.h>
|
---|
165 | # include <float.h>
|
---|
166 |
|
---|
167 | # define MRB_FLT_RADIX FLT_RADIX
|
---|
168 |
|
---|
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
|
---|
179 |
|
---|
180 | # else /* not MRB_USE_FLOAT */
|
---|
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
|
---|
190 | # endif /* MRB_USE_FLOAT */
|
---|
191 | #endif /* MRB_WITHOUT_FLOAT */
|
---|
192 |
|
---|
193 | MRB_END_DECL
|
---|
194 |
|
---|
195 | #endif /* MRUBY_NUMERIC_H */
|
---|