Debian Codegraph @

rpython/translator/c/src/int.h

raw symbols SCIP
1 /************************************************************/
2 /*** C header subsection: operations between ints ***/
3
4
5 /* Note for win64:
6
7 'Signed' must be defined as
8
9 __int64 in case of win64
10 long in all other cases
11
12 'SIGNED_MIN' must be defined as
13
14 LLONG_MIN in case of win64
15 LONG_MIN in all other cases
16 */
17
18 /*** unary operations ***/
19
20 #define OP_INT_IS_TRUE(x,r) r = ((x) != 0)
21 #define OP_INT_INVERT(x,r) r = ~(x)
22 #define OP_INT_NEG(x,r) r = -(x)
23
24 #define OP_INT_ABS(x,r) r = (x) >= 0 ? x : -(x)
25
26 /*** binary operations ***/
27
28 #define OP_INT_EQ(x,y,r) r = ((x) == (y))
29 #define OP_INT_NE(x,y,r) r = ((x) != (y))
30 #define OP_INT_LE(x,y,r) r = ((x) <= (y))
31 #define OP_INT_GT(x,y,r) r = ((x) > (y))
32 #define OP_INT_LT(x,y,r) r = ((x) < (y))
33 #define OP_INT_GE(x,y,r) r = ((x) >= (y))
34
35 /* Implement INT_BETWEEN by optimizing for the common case where a and c
36 are constants (the 2nd subtraction below is then constant-folded), or
37 for the case of a == 0 (both subtractions are then constant-folded).
38 Note that the following line only works if a <= c in the first place,
39 which we assume is true. */
40 #define OP_INT_BETWEEN(a,b,c,r) r = (((Unsigned)b - (Unsigned)a) \
41 < ((Unsigned)c - (Unsigned)a))
42
43 #define OP_INT_FORCE_GE_ZERO(a,r) r = (0 > a) ? 0 : (a)
44
45 /* addition, subtraction */
46
47 #define OP_INT_ADD(x,y,r) r = (x) + (y)
48 #define OP_INT_SUB(x,y,r) r = (x) - (y)
49 #define OP_INT_MUL(x,y,r) r = (x) * (y)
50
51
52 #ifdef __GNUC__
53 # if __GNUC__ >= 5
54 # define HAVE_BUILTIN_OVERFLOW
55 # elif defined(__has_builtin) /* clang */
56 # if __has_builtin(__builtin_mul_overflow)
57 # define HAVE_BUILTIN_OVERFLOW
58 # endif
59 # endif
60 #endif
61
62 #ifndef HAVE_BUILTIN_OVERFLOW
63 /* cast to avoid undefined behaviour on overflow */
64 #define OP_INT_ADD_OVF(x,y,r) \
65 r = (Signed)((Unsigned)x + y); \
66 if ((r^x) < 0 && (r^y) < 0) FAIL_OVF("integer addition")
67
68 #define OP_INT_ADD_NONNEG_OVF(x,y,r) /* y can be assumed >= 0 */ \
69 r = (Signed)((Unsigned)x + y); \
70 if ((r&~x) < 0) FAIL_OVF("integer addition")
71
72 #define OP_INT_SUB_OVF(x,y,r) \
73 r = (Signed)((Unsigned)x - y); \
74 if ((r^x) < 0 && (r^~y) < 0) FAIL_OVF("integer subtraction")
75
76 #if SIZEOF_SIGNED * 2 <= SIZEOF_LONG_LONG
77 #define OP_INT_MUL_OVF(x,y,r) \
78 { \
79 long long _lr = (long long)x * y; \
80 r = (Signed)_lr; \
81 if (_lr != (long long)r) FAIL_OVF("integer multiplication"); \
82 }
83 #else
84 #define OP_INT_MUL_OVF(x,y,r) \
85 r = op_llong_mul_ovf(x, y) /* Signed == long long */
86 #endif
87
88 #else /* HAVE_BUILTIN_OVERFLOW */
89 #define OP_INT_ADD_NONNEG_OVF(x,y,r) OP_INT_ADD_OVF(x,y,r)
90 #define OP_INT_ADD_OVF(x,y,r) \
91 if (__builtin_add_overflow(x, y, &r)) FAIL_OVF("integer addition")
92 #define OP_INT_SUB_OVF(x,y,r) \
93 if (__builtin_sub_overflow(x, y, &r)) FAIL_OVF("integer subtraction")
94 #define OP_INT_MUL_OVF(x,y,r) \
95 if (__builtin_mul_overflow(x, y, &r)) FAIL_OVF("integer multiplication")
96 #endif
97
98
99 /* shifting */
100
101 /* NB. shifting has same limitations as C: the shift count must be
102 >= 0 and < LONG_BITS. */
103 #define CHECK_SHIFT_RANGE(y, bits) RPyAssert(y >= 0 && y < bits, \
104 "The shift count is outside of the supported range")
105
106
107 #define OP_INT_RSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, PYPY_LONG_BIT); \
108 r = Py_ARITHMETIC_RIGHT_SHIFT(Signed, x, (y))
109 #define OP_UINT_RSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, PYPY_LONG_BIT); \
110 r = (x) >> (y)
111 #define OP_LLONG_RSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, PYPY_LONGLONG_BIT); \
112 r = Py_ARITHMETIC_RIGHT_SHIFT(PY_LONG_LONG,x, (y))
113 #define OP_ULLONG_RSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, PYPY_LONGLONG_BIT); \
114 r = (x) >> (y)
115 #define OP_LLLONG_RSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, 128); r = (x) >> (y)
116 #define OP_ULLLONG_RSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, 128); r = (x) >> (y)
117
118 /* left-shift of a signed value: C99 makes the result undefined if the
119 value is negative. Force the left-shift to occur on unsigned instead. */
120 #define OP_INT_LSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, PYPY_LONG_BIT); \
121 r = (Signed)(((Unsigned)(x)) << (y))
122 #define OP_LLONG_LSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, PYPY_LONGLONG_BIT); \
123 r = (long long)(((unsigned long long)(x)) << (y))
124 #define OP_LLLONG_LSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, 128); \
125 r = (__int128_t)(((__uint128_t)(x)) << (y))
126
127 #define OP_UINT_LSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, PYPY_LONG_BIT); \
128 r = (x) << (y)
129 #define OP_ULLONG_LSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, PYPY_LONGLONG_BIT); \
130 r = (x) << (y)
131 #define OP_ULLLONG_LSHIFT(x,y,r) CHECK_SHIFT_RANGE(y, 128); \
132 r = (x) << (y)
133
134 /* floor division */
135
136 #define OP_INT_FLOORDIV(x,y,r) r = (x) / (y)
137 #define OP_UINT_FLOORDIV(x,y,r) r = (x) / (y)
138 #define OP_LLONG_FLOORDIV(x,y,r) r = (x) / (y)
139 #define OP_ULLONG_FLOORDIV(x,y,r) r = (x) / (y)
140 #define OP_LLLONG_FLOORDIV(x,y,r) r = (x) / (y)
141 #define OP_ULLLONG_FLOORDIV(x,y,r) r = (x) / (y)
142
143 /* modulus */
144
145 #define OP_INT_MOD(x,y,r) r = (x) % (y)
146 #define OP_UINT_MOD(x,y,r) r = (x) % (y)
147 #define OP_LLONG_MOD(x,y,r) r = (x) % (y)
148 #define OP_ULLONG_MOD(x,y,r) r = (x) % (y)
149 #define OP_LLLONG_MOD(x,y,r) r = (x) % (y)
150 #define OP_ULLLONG_MOD(x,y,r) r = (x) % (y)
151
152 /* bit operations */
153
154 #define OP_INT_AND(x,y,r) r = (x) & (y)
155 #define OP_INT_OR( x,y,r) r = (x) | (y)
156 #define OP_INT_XOR(x,y,r) r = (x) ^ (y)
157
158 /*** conversions ***/
159
160 #define OP_CAST_BOOL_TO_INT(x,r) r = (Signed)(x)
161 #define OP_CAST_BOOL_TO_UINT(x,r) r = (Unsigned)(x)
162 #define OP_CAST_UINT_TO_INT(x,r) r = (Signed)(x)
163 #define OP_CAST_INT_TO_UINT(x,r) r = (Unsigned)(x)
164 #define OP_CAST_INT_TO_LONGLONG(x,r) r = (long long)(x)
165 #define OP_CAST_INT_TO_LONGLONGLONG(x,r) r = (__int128_t)(x)
166 #define OP_CAST_CHAR_TO_INT(x,r) r = (Signed)((unsigned char)(x))
167 #define OP_CAST_INT_TO_CHAR(x,r) r = (char)(x)
168
169 #define OP_TRUNCATE_LONGLONG_TO_INT(x,r) r = (Signed)(x)
170 #define OP_TRUNCATE_LONGLONGLONG_TO_INT(x,r) r = (Signed)(x)
171
172 /* Casting from UniChar to int goes first via "unsigned int".
173 On 64-bit platforms, this forces a signed 32-bit wchar_t
174 to an unsigned integer, which is also what CPython's ord()
175 does. */
176 #define OP_CAST_UNICHAR_TO_INT(x,r) r = ((unsigned int)(x))
177 #define OP_CAST_INT_TO_UNICHAR(x,r) r = (x)
178
179 /* bool operations */
180
181 #define OP_BOOL_NOT(x, r) r = !(x)
182
183 #ifdef __GNUC__
184 # define OP_LIKELY(x, r) r = __builtin_expect((x), 1)
185 # define OP_UNLIKELY(x, r) r = __builtin_expect((x), 0)
186 #else
187 # define OP_LIKELY(x, r) r = (x)
188 # define OP_UNLIKELY(x, r) r = (x)
189 #endif
190
191 RPY_EXTERN long long op_llong_mul_ovf(long long a, long long b);
192
193 /* The definitions above can be used with various types */
194
195 #define OP_UINT_IS_TRUE OP_INT_IS_TRUE
196 #define OP_UINT_INVERT OP_INT_INVERT
197 #define OP_UINT_ADD OP_INT_ADD
198 #define OP_UINT_SUB OP_INT_SUB
199 #define OP_UINT_MUL OP_INT_MUL
200 #define OP_UINT_LT OP_INT_LT
201 #define OP_UINT_LE OP_INT_LE
202 #define OP_UINT_EQ OP_INT_EQ
203 #define OP_UINT_NE OP_INT_NE
204 #define OP_UINT_GT OP_INT_GT
205 #define OP_UINT_GE OP_INT_GE
206 #define OP_UINT_AND OP_INT_AND
207 #define OP_UINT_OR OP_INT_OR
208 #define OP_UINT_XOR OP_INT_XOR
209
210 #define OP_LLONG_IS_TRUE OP_INT_IS_TRUE
211 #define OP_LLONG_NEG OP_INT_NEG
212 #define OP_LLONG_ABS OP_INT_ABS
213 #define OP_LLONG_INVERT OP_INT_INVERT
214
215 #define OP_LLLONG_IS_TRUE OP_INT_IS_TRUE
216 #define OP_LLLONG_NEG OP_INT_NEG
217 #define OP_LLLONG_ABS OP_INT_ABS
218 #define OP_LLLONG_INVERT OP_INT_INVERT
219
220 #define OP_LLONG_ADD OP_INT_ADD
221 #define OP_LLONG_SUB OP_INT_SUB
222 #define OP_LLONG_MUL OP_INT_MUL
223 #define OP_LLONG_LT OP_INT_LT
224 #define OP_LLONG_LE OP_INT_LE
225 #define OP_LLONG_EQ OP_INT_EQ
226 #define OP_LLONG_NE OP_INT_NE
227 #define OP_LLONG_GT OP_INT_GT
228 #define OP_LLONG_GE OP_INT_GE
229 #define OP_LLONG_AND OP_INT_AND
230 #define OP_LLONG_OR OP_INT_OR
231 #define OP_LLONG_XOR OP_INT_XOR
232
233 #define OP_LLLONG_ADD OP_INT_ADD
234 #define OP_LLLONG_SUB OP_INT_SUB
235 #define OP_LLLONG_MUL OP_INT_MUL
236 #define OP_LLLONG_LT OP_INT_LT
237 #define OP_LLLONG_LE OP_INT_LE
238 #define OP_LLLONG_EQ OP_INT_EQ
239 #define OP_LLLONG_NE OP_INT_NE
240 #define OP_LLLONG_GT OP_INT_GT
241 #define OP_LLLONG_GE OP_INT_GE
242 #define OP_LLLONG_AND OP_INT_AND
243 #define OP_LLLONG_OR OP_INT_OR
244 #define OP_LLLONG_XOR OP_INT_XOR
245
246 #define OP_ULLONG_IS_TRUE OP_LLONG_IS_TRUE
247 #define OP_ULLONG_INVERT OP_LLONG_INVERT
248 #define OP_ULLONG_ADD OP_LLONG_ADD
249 #define OP_ULLONG_SUB OP_LLONG_SUB
250 #define OP_ULLONG_MUL OP_LLONG_MUL
251 #define OP_ULLONG_LT OP_LLONG_LT
252 #define OP_ULLONG_LE OP_LLONG_LE
253 #define OP_ULLONG_EQ OP_LLONG_EQ
254 #define OP_ULLONG_NE OP_LLONG_NE
255 #define OP_ULLONG_GT OP_LLONG_GT
256 #define OP_ULLONG_GE OP_LLONG_GE
257 #define OP_ULLONG_AND OP_LLONG_AND
258 #define OP_ULLONG_OR OP_LLONG_OR
259 #define OP_ULLONG_XOR OP_LLONG_XOR
260
261 #define OP_ULLLONG_IS_TRUE OP_LLLONG_IS_TRUE
262 #define OP_ULLLONG_INVERT OP_LLLONG_INVERT
263 #define OP_ULLLONG_ADD OP_LLLONG_ADD
264 #define OP_ULLLONG_SUB OP_LLLONG_SUB
265 #define OP_ULLLONG_MUL OP_LLLONG_MUL
266 #define OP_ULLLONG_LT OP_LLLONG_LT
267 #define OP_ULLLONG_LE OP_LLLONG_LE
268 #define OP_ULLLONG_EQ OP_LLLONG_EQ
269 #define OP_ULLLONG_NE OP_LLLONG_NE
270 #define OP_ULLLONG_GT OP_LLLONG_GT
271 #define OP_ULLLONG_GE OP_LLLONG_GE
272 #define OP_ULLLONG_AND OP_LLLONG_AND
273 #define OP_ULLLONG_OR OP_LLLONG_OR
274 #define OP_ULLLONG_XOR OP_LLLONG_XOR
275