Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / include / mruby / numeric.h
1 /*
2 ** 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 #define FIXABLE_FLOAT(f) TYPED_FIXABLE(f,double)
27 #endif
28
29 #ifndef MRB_WITHOUT_FLOAT
30 MRB_API mrb_value mrb_flo_to_fixnum(mrb_state *mrb, mrb_value val);
31 #endif
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);
37 #endif
38
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);
43
44 #ifndef __has_builtin
45   #define __has_builtin(x) 0
46 #endif
47
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
53 #endif
54
55 /*
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.
59 */
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
64 #endif
65 #endif
66
67 #ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
68
69 #ifndef MRB_WORD_BOXING
70 # define WBCHK(x) 0
71 #else
72 # define WBCHK(x) !FIXABLE(x)
73 #endif
74
75 static inline mrb_bool
76 mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
77 {
78   return __builtin_add_overflow(augend, addend, sum) || WBCHK(*sum);
79 }
80
81 static inline mrb_bool
82 mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
83 {
84   return __builtin_sub_overflow(minuend, subtrahend, difference) || WBCHK(*difference);
85 }
86
87 static inline mrb_bool
88 mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
89 {
90   return __builtin_mul_overflow(multiplier, multiplicand, product) || WBCHK(*product);
91 }
92
93 #undef WBCHK
94
95 #else
96
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)
100
101 #define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1 - MRB_FIXNUM_SHIFT))
102
103 static inline mrb_bool
104 mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
105 {
106   mrb_uint x = (mrb_uint)augend;
107   mrb_uint y = (mrb_uint)addend;
108   mrb_uint z = (mrb_uint)(x + y);
109   *sum = (mrb_int)z;
110   return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK);
111 }
112
113 static inline mrb_bool
114 mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
115 {
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);
121 }
122
123 static inline mrb_bool
124 mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
125 {
126 #if MRB_INT_BIT == 32
127   int64_t n = (int64_t)multiplier * multiplicand;
128   *product = (mrb_int)n;
129   return !FIXABLE(n);
130 #else
131   if (multiplier > 0) {
132     if (multiplicand > 0) {
133       if (multiplier > MRB_INT_MAX / multiplicand) return TRUE;
134     }
135     else {
136       if (multiplicand < MRB_INT_MAX / multiplier) return TRUE;
137     }
138   }
139   else {
140     if (multiplicand > 0) {
141       if (multiplier < MRB_INT_MAX / multiplicand) return TRUE;
142     }
143     else {
144       if (multiplier != 0 && multiplicand < MRB_INT_MAX / multiplier) return TRUE;
145     }
146   }
147   *product = multiplier * multiplicand;
148   return FALSE;
149 #endif
150 }
151
152 #undef MRB_INT_OVERFLOW_MASK
153 #undef mrb_uint
154 #undef MRB_UINT_MAKE
155 #undef MRB_UINT_MAKE2
156
157 #endif
158
159 MRB_END_DECL
160
161 #endif  /* MRUBY_NUMERIC_H */