1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
// -*- c -*-
//
// %CopyrightBegin%
//
// Copyright Ericsson AB 2017. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// %CopyrightEnd%
//
OUTLINED_ARITH_2(Fail, Name, BIF, Op1, Op2, Dst) {
Eterm result;
#ifdef DEBUG
Eterm* orig_htop = HTOP;
Eterm* orig_stop = E;
#endif
DEBUG_SWAPOUT;
result = erts_$Name (c_p, $Op1, $Op2);
DEBUG_SWAPIN;
ASSERT(orig_htop == HTOP && orig_stop == E);
ERTS_HOLE_CHECK(c_p);
if (ERTS_LIKELY(is_value(result))) {
$Dst = result;
$NEXT0();
}
$BIF_ERROR_ARITY_2($Fail, $BIF, $Op1, $Op2);
}
i_plus := plus.fetch.execute;
plus.head() {
Eterm PlusOp1, PlusOp2;
}
plus.fetch(Op1, Op2) {
PlusOp1 = $Op1;
PlusOp2 = $Op2;
}
plus.execute(Fail, Dst) {
if (ERTS_LIKELY(is_both_small(PlusOp1, PlusOp2))) {
#ifdef HAVE_OVERFLOW_CHECK_BUILTINS
Sint lhs_tagged, rhs_untagged, res;
/* The value part of immediate integers start right after the tag and
* occupy the rest of the word, so if you squint a bit they look like
* fixed-point integers; as long as you mask the tag away you will get
* correct results from addition/subtraction since they share the same
* notion of zero. It's fairly easy to see that the following holds
* when (a + b) is in range:
*
* (a >> s) + (b >> s) == ((a & ~m) + (b & ~m)) >> s
*
* Where 's' is the tag size and 'm' is the tag mask.
*
* The left-hand side is our fallback in the #else clause and is the
* fastest way to do this safely in plain C. The actual addition will
* never overflow since `Sint` has a much greater range than our
* smalls, so we can use the IS_SSMALL macro to see if the result is
* within range.
*
* What we're doing below is an extension of the right-hand side. By
* treating `a` and `b` as fixed-point integers, all additions whose
* result is out of range will also overflow `Sint` and we can use the
* compiler's overflow intrinsics to check for this condition.
*
* In addition, since the tag lives in the lowest bits we can further
* optimize this by only stripping the tag from either side. The higher
* bits can't influence the tag bits since we bail on overflow, so the
* tag bits from the tagged side will simply appear in the result. */
lhs_tagged = PlusOp1;
rhs_untagged = PlusOp2 & ~_TAG_IMMED1_MASK;
if (ERTS_LIKELY(!__builtin_add_overflow(lhs_tagged, rhs_untagged, &res))) {
ASSERT(is_small(res));
$Dst = res;
$NEXT0();
}
#else
Sint i = signed_val(PlusOp1) + signed_val(PlusOp2);
if (ERTS_LIKELY(IS_SSMALL(i))) {
$Dst = make_small(i);
$NEXT0();
}
#endif
}
$OUTLINED_ARITH_2($Fail, mixed_plus, BIF_splus_2, PlusOp1, PlusOp2, $Dst);
}
i_minus := minus.fetch.execute;
minus.head() {
Eterm MinusOp1, MinusOp2;
}
minus.fetch(Op1, Op2) {
MinusOp1 = $Op1;
MinusOp2 = $Op2;
}
minus.execute(Fail, Dst) {
if (ERTS_LIKELY(is_both_small(MinusOp1, MinusOp2))) {
#ifdef HAVE_OVERFLOW_CHECK_BUILTINS
Sint lhs_tagged, rhs_untagged, res;
/* See plus.execute */
lhs_tagged = MinusOp1;
rhs_untagged = MinusOp2 & ~_TAG_IMMED1_MASK;
if (ERTS_LIKELY(!__builtin_sub_overflow(lhs_tagged, rhs_untagged, &res))) {
ASSERT(is_small(res));
$Dst = res;
$NEXT0();
}
#else
Sint i = signed_val(MinusOp1) - signed_val(MinusOp2);
if (ERTS_LIKELY(IS_SSMALL(i))) {
$Dst = make_small(i);
$NEXT0();
}
#endif
}
$OUTLINED_ARITH_2($Fail, mixed_minus, BIF_sminus_2, MinusOp1, MinusOp2, $Dst);
}
i_increment := increment.fetch.execute;
increment.head() {
Eterm increment_reg_val;
}
increment.fetch(Src) {
increment_reg_val = $Src;
}
increment.execute(IncrementVal, Dst) {
Eterm increment_val = $IncrementVal;
Eterm result;
if (ERTS_LIKELY(is_small(increment_reg_val))) {
#ifdef HAVE_OVERFLOW_CHECK_BUILTINS
Sint lhs_tagged, rhs_untagged, res;
/* See plus.execute */
lhs_tagged = increment_reg_val;
rhs_untagged = (Sint)increment_val << _TAG_IMMED1_SIZE;
if (ERTS_LIKELY(!__builtin_add_overflow(lhs_tagged, rhs_untagged, &res))) {
ASSERT(is_small(res));
$Dst = res;
$NEXT0();
}
#else
Sint i = signed_val(increment_reg_val) + increment_val;
if (ERTS_LIKELY(IS_SSMALL(i))) {
$Dst = make_small(i);
$NEXT0();
}
#endif
}
result = erts_mixed_plus(c_p, increment_reg_val, make_small(increment_val));
ERTS_HOLE_CHECK(c_p);
if (ERTS_LIKELY(is_value(result))) {
$Dst = result;
$NEXT0();
}
ASSERT(c_p->freason != BADMATCH || is_value(c_p->fvalue));
goto find_func_info;
}
i_times(Fail, Op1, Op2, Dst) {
Eterm op1 = $Op1;
Eterm op2 = $Op2;
#ifdef HAVE_OVERFLOW_CHECK_BUILTINS
if (ERTS_LIKELY(is_both_small(op1, op2))) {
/* See plus.execute */
Sint lhs_untagged, rhs_actual, res;
lhs_untagged = op1 & ~_TAG_IMMED1_MASK;
rhs_actual = signed_val(op2);
if (ERTS_LIKELY(!__builtin_mul_overflow(lhs_untagged, rhs_actual, &res))) {
ASSERT(!(res & _TAG_IMMED1_MASK));
$Dst = res | _TAG_IMMED1_SMALL;
$NEXT0();
}
}
#endif
$OUTLINED_ARITH_2($Fail, mixed_times, BIF_stimes_2, op1, op2, $Dst);
}
i_m_div(Fail, Op1, Op2, Dst) {
Eterm op1 = $Op1;
Eterm op2 = $Op2;
$OUTLINED_ARITH_2($Fail, mixed_div, BIF_div_2, op1, op2, $Dst);
}
i_int_div(Fail, Op1, Op2, Dst) {
Eterm op1 = $Op1;
Eterm op2 = $Op2;
if (ERTS_UNLIKELY(op2 == SMALL_ZERO)) {
c_p->freason = BADARITH;
$BIF_ERROR_ARITY_2($Fail, BIF_intdiv_2, op1, op2);
} else if (ERTS_LIKELY(is_both_small(op1, op2))) {
Sint ires = signed_val(op1) / signed_val(op2);
if (ERTS_LIKELY(IS_SSMALL(ires))) {
$Dst = make_small(ires);
$NEXT0();
}
}
$OUTLINED_ARITH_2($Fail, int_div, BIF_intdiv_2, op1, op2, $Dst);
}
i_rem := rem.fetch.execute;
rem.head() {
Eterm RemOp1, RemOp2;
}
rem.fetch(Src1, Src2) {
RemOp1 = $Src1;
RemOp2 = $Src2;
}
rem.execute(Fail, Dst) {
if (ERTS_UNLIKELY(RemOp2 == SMALL_ZERO)) {
c_p->freason = BADARITH;
$BIF_ERROR_ARITY_2($Fail, BIF_rem_2, RemOp1, RemOp2);
} else if (ERTS_LIKELY(is_both_small(RemOp1, RemOp2))) {
$Dst = make_small(signed_val(RemOp1) % signed_val(RemOp2));
$NEXT0();
} else {
$OUTLINED_ARITH_2($Fail, int_rem, BIF_rem_2, RemOp1, RemOp2, $Dst);
}
}
i_band := band.fetch.execute;
band.head() {
Eterm BandOp1, BandOp2;
}
band.fetch(Src1, Src2) {
BandOp1 = $Src1;
BandOp2 = $Src2;
}
band.execute(Fail, Dst) {
if (ERTS_LIKELY(is_both_small(BandOp1, BandOp2))) {
/*
* No need to untag -- TAG & TAG == TAG.
*/
$Dst = BandOp1 & BandOp2;
$NEXT0();
}
$OUTLINED_ARITH_2($Fail, band, BIF_band_2, BandOp1, BandOp2, $Dst);
}
i_bor(Fail, Src1, Src2, Dst) {
if (ERTS_LIKELY(is_both_small($Src1, $Src2))) {
/*
* No need to untag -- TAG | TAG == TAG.
*/
$Dst = $Src1 | $Src2;
$NEXT0();
}
$OUTLINED_ARITH_2($Fail, bor, BIF_bor_2, $Src1, $Src2, $Dst);
}
i_bxor(Fail, Src1, Src2, Dst) {
if (ERTS_LIKELY(is_both_small($Src1, $Src2))) {
/*
* TAG ^ TAG == 0.
*
* Therefore, we perform the XOR operation on the tagged values,
* and OR in the tag bits.
*/
$Dst = ($Src1 ^ $Src2) | make_small(0);
$NEXT0();
}
$OUTLINED_ARITH_2($Fail, bxor, BIF_bxor_2, $Src1, $Src2, $Dst);
}
i_bsl := shift.setup_bsl.execute;
i_bsr := shift.setup_bsr.execute;
shift.head() {
Eterm Op1, Op2;
Sint shift_left_count;
}
shift.setup_bsr(Src1, Src2) {
Op1 = $Src1;
Op2 = $Src2;
shift_left_count = 0;
if (ERTS_LIKELY(is_small(Op2))) {
shift_left_count = -signed_val(Op2);
} else if (is_big(Op2)) {
/*
* N bsr NegativeBigNum == N bsl MAX_SMALL
* N bsr PositiveBigNum == N bsl MIN_SMALL
*/
shift_left_count = make_small(bignum_header_is_neg(*big_val(Op2)) ?
MAX_SMALL : MIN_SMALL);
}
}
shift.setup_bsl(Src1, Src2) {
Op1 = $Src1;
Op2 = $Src2;
shift_left_count = 0;
if (ERTS_LIKELY(is_small(Op2))) {
shift_left_count = signed_val(Op2);
} else if (is_big(Op2)) {
if (bignum_header_is_neg(*big_val(Op2))) {
/*
* N bsl NegativeBigNum is either 0 or -1, depending on
* the sign of N. Since we don't believe this case
* is common, do the calculation with the minimum
* amount of code.
*/
shift_left_count = MIN_SMALL;
} else if (is_integer(Op1)) {
/*
* N bsl PositiveBigNum is too large to represent.
*/
shift_left_count = MAX_SMALL;
}
}
}
shift.execute(Fail, Dst) {
Uint big_words_needed;
if (ERTS_LIKELY(is_small(Op1))) {
Sint int_res = signed_val(Op1);
if (ERTS_UNLIKELY(shift_left_count == 0 || int_res == 0)) {
if (ERTS_UNLIKELY(is_not_integer(Op2))) {
goto shift_error;
}
if (int_res == 0) {
$Dst = Op1;
$NEXT0();
}
} else if (shift_left_count < 0) { /* Right shift */
Eterm bsr_res;
shift_left_count = -shift_left_count;
if (shift_left_count >= SMALL_BITS-1) {
bsr_res = (int_res < 0) ? SMALL_MINUS_ONE : SMALL_ZERO;
} else {
bsr_res = make_small(int_res >> shift_left_count);
}
$Dst = bsr_res;
$NEXT0();
} else if (shift_left_count < SMALL_BITS-1) { /* Left shift */
if ((int_res > 0 &&
((~(Uint)0 << ((SMALL_BITS-1)-shift_left_count)) & int_res) == 0) ||
((~(Uint)0 << ((SMALL_BITS-1)-shift_left_count)) & ~int_res) == 0) {
$Dst = make_small(int_res << shift_left_count);
$NEXT0();
}
}
big_words_needed = 1; /* big_size(small_to_big(Op1)) */
goto big_shift;
} else if (is_big(Op1)) {
if (shift_left_count == 0) {
if (is_not_integer(Op2)) {
goto shift_error;
}
$Dst = Op1;
$NEXT0();
}
big_words_needed = big_size(Op1);
big_shift:
if (shift_left_count > 0) { /* Left shift. */
big_words_needed += (shift_left_count / D_EXP);
} else { /* Right shift. */
if (big_words_needed <= (-shift_left_count / D_EXP)) {
big_words_needed = 3; /* ??? */
} else {
big_words_needed -= (-shift_left_count / D_EXP);
}
}
{
Eterm tmp_big[2];
Sint big_need_size = 1 + BIG_NEED_SIZE(big_words_needed+1);
Eterm* hp;
Eterm* hp_end;
/*
* Slightly conservative check the size to avoid
* allocating huge amounts of memory for bignums that
* clearly would overflow the arity in the header
* word.
*/
if (big_need_size-8 > BIG_ARITY_MAX) {
$SYSTEM_LIMIT($Fail);
}
hp = HeapFragOnlyAlloc(c_p, big_need_size);
if (is_small(Op1)) {
Op1 = small_to_big(signed_val(Op1), tmp_big);
}
Op1 = big_lshift(Op1, shift_left_count, hp);
hp_end = hp + big_need_size;
if (is_big(Op1)) {
hp += bignum_header_arity(*hp) + 1;
}
HRelease(c_p, hp_end, hp);
if (ERTS_UNLIKELY(is_nil(Op1))) {
/*
* This result must have been only slighty larger
* than allowed since it wasn't caught by the
* previous test.
*/
$SYSTEM_LIMIT($Fail);
}
ERTS_HOLE_CHECK(c_p);
$Dst = Op1;
$NEXT0();
}
}
/*
* One or more non-integer arguments.
*/
shift_error:
c_p->freason = BADARITH;
if ($Fail) {
$FAIL($Fail);
} else {
reg[0] = Op1;
reg[1] = Op2;
SWAPOUT;
if (IsOpCode(I[0], i_bsl_ssjd)) {
I = handle_error(c_p, I, reg, &bif_export[BIF_bsl_2]->info.mfa);
} else {
ASSERT(IsOpCode(I[0], i_bsr_ssjd));
I = handle_error(c_p, I, reg, &bif_export[BIF_bsr_2]->info.mfa);
}
goto post_error_handling;
}
}
i_int_bnot(Fail, Src, Dst) {
Eterm bnot_val = $Src;
Eterm result;
if (ERTS_LIKELY(is_small(bnot_val))) {
result = make_small(~signed_val(bnot_val));
} else {
result = erts_bnot(c_p, bnot_val);
ERTS_HOLE_CHECK(c_p);
if (ERTS_UNLIKELY(is_nil(result))) {
$BIF_ERROR_ARITY_1($Fail, BIF_bnot_1, bnot_val);
}
}
$Dst = result;
}
|