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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
// Integer saturating arighmetic (with some branch-free procedures on x64).
#import "Basic";
#import "Compiler";
#import "Math";
// TODO Comparing implementaitons using dump
#run test_math_ext();
test_math_ext :: () { set_build_options_dc(.{do_output=false});
// main :: () {
write_strings("=====================\n", "--- Test Math_Ext ---\n");
test_op :: (op: string, x: $Tx, y: $Ty, r: $Tr, t: Type, o: bool) -> errors_found: int #expand {
#import "String";
#insert #run replace("tr, to := OP(cast(Tx)x, cast(Ty)y);", "OP", op);
print("%_%(%, %) = % : %\n", op, t, x, y, r, o);
error := 0;
if r != tr { error += 1; print(" > incorrect result value: got % expected %\n", tr, r); };
if t != type_of(tr) { error += 1; print(" > incorrect result type: got % expected %\n", type_of(tr), t); };
if o != to { error += 1; print(" > incorrect overflow flag: got % expected %\n", to, o); };
return error;
}
errors := 0;
// Test signed add.
errors += test_op("add", cast( s8) S8_MAX, cast( s8)1, S8_MAX, s8, true);
errors += test_op("add", cast(s16)S16_MAX, cast( u8)1, S16_MAX, s16, true);
errors += test_op("add", cast(s32)S32_MAX, cast(s32)1, S32_MAX, s32, true);
errors += test_op("add", cast(s64)S64_MAX, cast(u32)1, S64_MAX, s64, true);
errors += test_op("add", cast( s8) S8_MAX, cast( s8) S8_MIN, -1, s8, false);
errors += test_op("add", cast(s16)S16_MAX, cast(s16)S16_MIN, -1, s16, false);
errors += test_op("add", cast(s32)S32_MAX, cast(s32)S32_MIN, -1, s32, false);
errors += test_op("add", cast(s64)S64_MAX, cast(s64)S64_MIN, -1, s64, false);
// Test unsigned add.
errors += test_op("add", cast( u8) U8_MAX, cast( u8)1, U8_MAX, u8, true);
errors += test_op("add", cast(u16)U16_MAX, cast(u16)1, U16_MAX, u16, true);
errors += test_op("add", cast(u32)U32_MAX, cast(u32)1, U32_MAX, u32, true);
errors += test_op("add", cast(u64)U64_MAX, cast(u64)1, U64_MAX, u64, true);
errors += test_op("add", cast( u8) U8_MAX, cast( u8)0, U8_MAX, u8, false);
errors += test_op("add", cast(u16)U16_MAX, cast(u16)0, U16_MAX, u16, false);
errors += test_op("add", cast(u32)U32_MAX, cast(u32)0, U32_MAX, u32, false);
errors += test_op("add", cast(u64)U64_MAX, cast(u64)0, U64_MAX, u64, false);
// Test signed sub.
errors += test_op("sub", cast( s8) S8_MIN, cast( s8)1, S8_MIN, s8, true);
errors += test_op("sub", cast(s16)S16_MIN, cast( u8)1, S16_MIN, s16, true);
errors += test_op("sub", cast(s32)S32_MIN, cast(s32)1, S32_MIN, s32, true);
errors += test_op("sub", cast(s64)S64_MIN, cast(u32)1, S64_MIN, s64, true);
errors += test_op("sub", cast( s8)-1, cast( s8) S8_MAX, S8_MIN, s8, false);
errors += test_op("sub", cast(s16)-1, cast(s16)S16_MAX, S16_MIN, s16, false);
errors += test_op("sub", cast(s32)-1, cast(s32)S32_MAX, S32_MIN, s32, false);
errors += test_op("sub", cast(s64)-1, cast(s64)S64_MAX, S64_MIN, s64, false);
// Test unsigned sub.
errors += test_op("sub", cast( u8)1, cast( u8) U8_MAX, 0, u8, true);
errors += test_op("sub", cast( u8)1, cast(u16)U16_MAX, 0, u16, true);
errors += test_op("sub", cast(u32)1, cast(u32)U32_MAX, 0, u32, true);
errors += test_op("sub", cast(u32)1, cast(u64)U64_MAX, 0, u64, true);
errors += test_op("sub", cast( u8) U8_MAX, cast( u8)0, U8_MAX, u8, false);
errors += test_op("sub", cast(u16)U16_MAX, cast( u8)0, U16_MAX, u16, false);
errors += test_op("sub", cast(u32)U32_MAX, cast(u32)0, U32_MAX, u32, false);
errors += test_op("sub", cast(u64)U64_MAX, cast(u32)0, U64_MAX, u64, false);
// Test signed mul.
errors += test_op("mul", cast( s8) S8_MIN, cast( s8)-1, S8_MAX, s8, true);
errors += test_op("mul", cast(s16)S16_MIN, cast( s8)-1, S16_MAX, s16, true);
errors += test_op("mul", cast(s32)S32_MIN, cast(s32)-1, S32_MAX, s32, true);
errors += test_op("mul", cast(s64)S64_MIN, cast(s32)-1, S64_MAX, s64, true);
errors += test_op("mul", cast( s8) S8_MAX, cast( s8)-2, S8_MIN, s8, true);
errors += test_op("mul", cast(s16)S16_MAX, cast( s8)-2, S16_MIN, s16, true);
errors += test_op("mul", cast(s32)S32_MAX, cast(s32)-2, S32_MIN, s32, true);
errors += test_op("mul", cast(s64)S64_MAX, cast(s32)-2, S64_MIN, s64, true);
errors += test_op("mul", cast( s8)-2, cast( s8) S8_MAX, S8_MIN, s8, true);
errors += test_op("mul", cast( s8)-2, cast(s16)S16_MAX, S16_MIN, s16, true);
errors += test_op("mul", cast(s32)-2, cast(s32)S32_MAX, S32_MIN, s32, true);
errors += test_op("mul", cast(s32)-2, cast(s64)S64_MAX, S64_MIN, s64, true);
errors += test_op("mul", cast( s8) S8_MAX, cast( s8)2, S8_MAX, s8, true);
errors += test_op("mul", cast(s16)S16_MAX, cast( s8)2, S16_MAX, s16, true);
errors += test_op("mul", cast(s32)S32_MAX, cast(s32)2, S32_MAX, s32, true);
errors += test_op("mul", cast(s64)S64_MAX, cast(s32)2, S64_MAX, s64, true);
errors += test_op("mul", cast( s8) S8_MAX, cast( s8)-1, -S8_MAX, s8, false);
errors += test_op("mul", cast(s16)S16_MAX, cast( s8)-1, -S16_MAX, s16, false);
errors += test_op("mul", cast(s32)S32_MAX, cast(s32)-1, -S32_MAX, s32, false);
errors += test_op("mul", cast(s64)S64_MAX, cast(s32)-1, -S64_MAX, s64, false);
errors += test_op("mul", cast( s8) S8_MAX, cast( s8)0, 0, s8, false);
errors += test_op("mul", cast(s16)S16_MAX, cast( u8)0, 0, s16, false);
errors += test_op("mul", cast(s32)S32_MAX, cast(s32)0, 0, s32, false);
errors += test_op("mul", cast(s64)S64_MAX, cast(u32)0, 0, s64, false);
// Test unsigned mul.
errors += test_op("mul", cast( u8) U8_MAX, cast( u8)1, U8_MAX, u8, false);
errors += test_op("mul", cast(u16)U16_MAX, cast( u8)1, U16_MAX, u16, false);
errors += test_op("mul", cast(u32)U32_MAX, cast(u32)1, U32_MAX, u32, false);
errors += test_op("mul", cast(u64)U64_MAX, cast(u32)1, U64_MAX, u64, false);
errors += test_op("mul", cast( u8) U8_MAX, cast( u8)2, U8_MAX, u8, true);
errors += test_op("mul", cast(u16)U16_MAX, cast( u8)2, U16_MAX, u16, true);
errors += test_op("mul", cast(u32)U32_MAX, cast(u32)2, U32_MAX, u32, true);
errors += test_op("mul", cast(u64)U64_MAX, cast(u32)2, U64_MAX, u64, true);
// Test signed div.
errors += test_op("div", cast( s8) S8_MIN, cast( s8)-1, S8_MAX, s8, true);
errors += test_op("div", cast(s16)S16_MIN, cast( s8)-1, S16_MAX, s16, true);
errors += test_op("div", cast(s32)S32_MIN, cast(s32)-1, S32_MAX, s32, true);
errors += test_op("div", cast(s64)S64_MIN, cast(s32)-1, S64_MAX, s64, true);
errors += test_op("div", cast( s8) S8_MAX, cast( s8)-2, - S8_MAX/2, s8, false);
errors += test_op("div", cast(s16)S16_MAX, cast( s8)-2, -S16_MAX/2, s16, false);
errors += test_op("div", cast(s32)S32_MAX, cast(s32)-2, -S32_MAX/2, s32, false);
errors += test_op("div", cast(s64)S64_MAX, cast(s32)-2, -S64_MAX/2, s64, false);
// Test unsigned div.
errors += test_op("div", cast( u8) U8_MAX, cast( u8)2, U8_MAX/2, u8, false);
errors += test_op("div", cast(u16)U16_MAX, cast( u8)2, U16_MAX/2, u16, false);
errors += test_op("div", cast(u32)U32_MAX, cast(u32)2, U32_MAX/2, u32, false);
errors += test_op("div", cast(u64)U64_MAX, cast(u32)2, U64_MAX/2, u64, false);
if errors > 0 print("# Found % %!\n", errors, ifx errors == 1 then "error" else "errors"); else print(" No errors found.\n");
/* PERFORMANCE TEST
#import "Random";
best_generic: float;
best_asm: float;
for 0..100 {
size, time_generic, time_asm := performance_test();
perf_generic := cast(float)size/cast(float)to_microseconds(time_generic);
perf_asm := cast(float)size/cast(float)to_microseconds(time_asm);
best_generic = max(best_generic, perf_generic);
best_asm = max(best_asm, perf_asm);
}
print("method : ops/usec\ngeneric : %\nasm : %\n", best_generic, best_asm);
performance_test :: () -> sum_size: s64, time_generic: Apollo_Time, time_asm: Apollo_Time {
SUM_SIZE := 2000000;
numbers: [..] s64;
array_reserve(*numbers, SUM_SIZE);
for 0..SUM_SIZE-1 {
array_add(*numbers, cast(s64)random_get());
}
sum := 0;
start := current_time_monotonic();
for numbers sum = add_bad(sum, it);
time := current_time_monotonic() - start;
sum_asm := 0;
start_asm := current_time_monotonic();
for numbers sum_asm = add(sum_asm, it);
time_asm := current_time_monotonic() - start_asm;
assert(sum == sum_asm);
return SUM_SIZE, time, time_asm;
}
*/
}
is_signed :: ($t: Type) -> bool { return (cast(*Type_Info_Integer)type_info(t)).signed; }
INTEGER_ARITHMETIC_TYPES_CHECK :: #string DONE
type_info_x := cast(*Type_Info)Tx;
type_info_y := cast(*Type_Info)Ty;
if type_info_x.type != .INTEGER || type_info_y.type != .INTEGER return false, "Non integers values passed.";
tx := cast(*Type_Info_Integer)type_info_x;
ty := cast(*Type_Info_Integer)type_info_y;
largest_type :=
ifx tx.runtime_size > ty.runtime_size then Tx else
ifx ty.runtime_size > tx.runtime_size then Ty else
ifx tx.signed == ty.signed then Tx else
void;
// Only allow to add different signedness values if largest type is the signed one (as in JAI).
if tx.signed == ty.signed {
Tx = largest_type;
Ty = largest_type;
Tr = largest_type;
}
else if tx.signed && Tx == largest_type {
Ty = largest_type;
Tr = largest_type;
}
else if ty.signed && Ty == largest_type {
Tx = largest_type;
Tr = largest_type;
}
else return false, "Number signedness mismatch.";
return true;
DONE
add :: (x: $Tx, y: $Ty) -> result: $Tr, saturated: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; } // #dump
{
#if CPU != .X64 {
// #if #run is_signed(Tr) { // TODO Maybe use this?
#if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 {
#if Tr == s8 { MAX :: S8_MAX; MIN :: S8_MIN; }
#if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; }
#if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; }
#if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; }
if (y > 0 && x > MAX - y) then return MAX, true;
if (y < 0 && x < MIN - y) then return MIN, true;
} else {
#if Tr == u8 { MAX :: U8_MAX; }
#if Tr == u16 { MAX :: U16_MAX; }
#if Tr == u32 { MAX :: U32_MAX; }
#if Tr == u64 { MAX :: U64_MAX; }
if (x > MAX - y) then return MAX, true;
}
return x + y, false;
} else {
#import "String";
result: Tr = ---;
saturated: bool = ---;
S_ADD_ASM :: #string DONE
#asm {
// Calculate limit based on x's sign.
mov limit: gpr, MAX;
mov sign: gpr, x;
shr.SIZE sign, BITS;
add.SIZE limit, sign;
mov result, x;
add.SIZE result, y;
seto saturated;
cmovo result, limit;
}
DONE
#if Tr == s8
#insert #run replace(replace(replace(S_ADD_ASM, ".SIZE", ".b"), "MAX", "127"), "BITS", "7");
#if Tr == s16
#insert #run replace(replace(replace(S_ADD_ASM, ".SIZE", ".w"), "MAX", "32767"), "BITS", "15");
#if Tr == s32
#insert #run replace(replace(replace(S_ADD_ASM, ".SIZE", ".d"), "MAX", "2147483647"), "BITS", "31");
#if Tr == s64
#insert #run replace(replace(replace(S_ADD_ASM, ".SIZE", ".q"), "MAX", "9223372036854775807"), "BITS", "63");
U_ADD_ASM :: #string DONE
#asm {
mov limit: gpr, MAX;
mov result, x;
add.SIZE result, y;
setc saturated;
cmovc result, limit;
}
DONE
#if Tr == u8
#insert #run replace(replace(U_ADD_ASM, ".SIZE", ".b"), "MAX", "255");
#if Tr == u16
#insert #run replace(replace(U_ADD_ASM, ".SIZE", ".w"), "MAX", "65535");
#if Tr == u32
#insert #run replace(replace(U_ADD_ASM, ".SIZE", ".d"), "MAX", "4294967295");
#if Tr == u64
#insert #run replace(replace(U_ADD_ASM, ".SIZE", ".q"), "MAX", "18446744073709551615");
return result, saturated;
}
}
sub :: (x: $Tx, y: $Ty) -> result: $Tr, overflow: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; } //#dump
{
#if CPU != .X64 {
#if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 {
#if Tr == s8 { MAX :: S8_MAX; MIN :: S8_MIN; }
#if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; }
#if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; }
#if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; }
if (y < 0 && x > MAX + y) then return MAX, true;
if (y > 0 && x < MIN + y) then return MIN, true;
} else {
if (y > x) then return 0, true;
}
return x - y, false;
} else {
#import "String";
result: Tr = ---;
saturated: bool = ---;
S_SUB_ASM :: #string DONE
#asm {
// Calculate limit based on x's sign.
mov limit: gpr, MAX;
mov sign: gpr, x;
shr.SIZE sign, BITS;
add.SIZE limit, sign;
mov result, x;
sub.SIZE result, y;
seto saturated;
cmovo result, limit;
}
DONE
#if Tr == s8
#insert #run replace(replace(replace(S_SUB_ASM, ".SIZE", ".b"), "MAX", "127"), "BITS", "7");
#if Tr == s16
#insert #run replace(replace(replace(S_SUB_ASM, ".SIZE", ".w"), "MAX", "32767"), "BITS", "15");
#if Tr == s32
#insert #run replace(replace(replace(S_SUB_ASM, ".SIZE", ".d"), "MAX", "2147483647"), "BITS", "31");
#if Tr == s64
#insert #run replace(replace(replace(S_SUB_ASM, ".SIZE", ".q"), "MAX", "9223372036854775807"), "BITS", "63");
U_SUB_ASM :: #string DONE
#asm {
mov limit: gpr, 0;
mov result, x;
sub.SIZE result, y;
setc saturated;
cmovc result, limit;
}
DONE
#if Tr == u8
#insert #run replace(U_SUB_ASM, ".SIZE", ".b");
#if Tr == u16
#insert #run replace(U_SUB_ASM, ".SIZE", ".w");
#if Tr == u32
#insert #run replace(U_SUB_ASM, ".SIZE", ".d");
#if Tr == u64
#insert #run replace(U_SUB_ASM, ".SIZE", ".q");
return result, saturated;
}
}
mul :: (x: $Tx, y: $Ty) -> result: $Tr, overflow: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; } //#dump
{
#if CPU != .X64 {
// #if #run is_signed(Tr) { // TODO Maybe use this?
#if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 {
#if Tr == s8 { MAX :: S8_MAX; MIN :: S8_MIN; }
#if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; }
#if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; }
#if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; }
if x == 0 || y == 0 then return 0, false;
if x > 0 && y > 0 && x > MAX / y then return MAX, true;
if x < 0 && y < 0 && x < MAX / y then return MAX, true;
if (y < 0 && y < MIN / x) || (x < 0 && x < MIN / y) then return MIN, true;
} else {
if x > MAX / y then return MAX, true;
}
return x * y, false;
} else {
#import "String";
result: Tr = ---;
saturated: bool = ---;
S_MUL_ASM :: #string DONE
#asm {
result === a;
// Calculate limit based on (x^y)'s sign.
mov limit: gpr, MAX;
mov sign: gpr, x;
xor sign, y;
shr.SIZE sign, BITS;
add.SIZE limit, sign;
mov result, x;
imul.SIZE result, y;
seto saturated;
cmovo result, limit;
}
DONE
#if Tr == s8
#insert #run replace(replace(replace(S_MUL_ASM, ".SIZE", ".b"), "MAX", "127"), "BITS", "7");
#if Tr == s16
#insert #run replace(replace(replace(S_MUL_ASM, ".SIZE", ".w"), "MAX", "32767"), "BITS", "15");
#if Tr == s32
#insert #run replace(replace(replace(S_MUL_ASM, ".SIZE", ".d"), "MAX", "2147483647"), "BITS", "31");
#if Tr == s64
#insert #run replace(replace(replace(S_MUL_ASM, ".SIZE", ".q"), "MAX", "9223372036854775807"), "BITS", "63");
U_MUL_ASM :: #string DONE
#asm {
result === a;
result_h: gpr === d;
mov tmp: gpr, x;
mov result, x;
mul.SIZE result_h, result, y;
setc saturated;
sbb tmp, tmp; // SBB performs: dst = dst - (src + CF). Thus, max = 0xFF...FF if CF is 1, otherwise 0x00...00. TODO Improve comment.
or result, tmp;
}
DONE
U_MUL_ASM_8BITS :: #string DONE
#asm {
result === a;
max: gpr;
mov tmp: gpr, x;
mov result, x;
mul.SIZE result, y;
setc saturated;
sbb max, max; // SBB performs: dst = dst - (src + CF). Thus, max = 0xFF...FF if CF is 1, otherwise 0x00...00. TODO Improve comment.
or result, max;
}
DONE
#if Tr == u8
#insert #run replace(U_MUL_ASM_8BITS, ".SIZE", ".b");
#if Tr == u16
#insert #run replace(U_MUL_ASM, ".SIZE", ".w");
#if Tr == u32
#insert #run replace(U_MUL_ASM, ".SIZE", ".d");
#if Tr == u64
#insert #run replace(U_MUL_ASM, ".SIZE", ".q");
return result, saturated;
}
}
div :: (x: $Tx, y: $Ty) -> result: $Tr, overflow: bool #modify { #insert INTEGER_ARITHMETIC_TYPES_CHECK; } //#dump
{
#if CPU != .X64 {
// #if #run is_signed(Tr) { // TODO Maybe use this?
#if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 {
#if Tr == s8 { MAX :: S8_MAX; MIN :: S8_MIN; }
#if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; }
#if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; }
#if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; }
if x == MIN && y == -1 then return MAX, true;
}
return x / y, false;
} else {
#import "String";
result: Tr = ---;
saturated: bool = ---;
S_DIV_ASM :: #string DONE
#asm {
result === a;
// Calculate dividend limit (MIN+1) for the div(MIN/-1) problem.
mov limit: gpr, MIN;
inc limit;
// Detect div(MIN/-1) and flag it on ZF.
mov xT: gpr, MIN;
mov xV: gpr, x;
xor.SIZE xT, xV;
mov yT: gpr, -1;
mov yV: gpr, y;
xor.SIZE yT, yV;
//
or.SIZE xT, yT;
mov result, x;
cmovz result, limit; // Apply dividend limit if ZF.
cqo rdx:, result; // Prepare dividend high bits.
setz saturated;
idiv.SIZE rdx, result, y;
}
DONE
S_DIV_ASM_8BITS :: #string DONE
#asm {
result === a;
// Calculate dividend limit (MIN+1) for the div(MIN/-1) problem.
mov limit: gpr, MIN;
inc limit;
// Detect div(MIN/-1) and flag it on ZF.
mov t_x: gpr, x;
mov t_y: gpr, y;
xor.SIZE t_x, MIN;
xor.SIZE t_y, -1;
or.SIZE t_x, t_y;
mov result, x;
cmovz result, limit; // Apply dividend limit if ZF.
setz saturated;
idiv.SIZE result, y;
}
DONE
#if Tr == s8
#insert #run replace(replace(S_DIV_ASM_8BITS, ".SIZE", ".b"), "MIN", "-128");
#if Tr == s16
#insert #run replace(replace(S_DIV_ASM, ".SIZE", ".w"), "MIN", "-32768");
#if Tr == s32
#insert #run replace(replace(S_DIV_ASM, ".SIZE", ".d"), "MIN", "-2147483648");
#if Tr == s64
#insert #run replace(replace(S_DIV_ASM, ".SIZE", ".q"), "MIN", "-9223372036854775808");
U_DIV_ASM :: #string DONE
#asm {
result === a;
mov result, x;
mov rdx: gpr === d, 0; // Prepare dividend high bits.
div.SIZE rdx, result, y;
mov saturated, 0;
}
DONE
U_DIV_ASM_8BITS :: #string DONE
#asm {
result === a;
mov result, x;
div.SIZE result, y;
mov saturated, 0;
}
DONE
#if Tr == u8
#insert #run replace(U_DIV_ASM_8BITS, ".SIZE", ".b");
#if Tr == u16
#insert #run replace(U_DIV_ASM, ".SIZE", ".w");
#if Tr == u32
#insert #run replace(U_DIV_ASM, ".SIZE", ".d");
#if Tr == u64
#insert #run replace(U_DIV_ASM, ".SIZE", ".q");
return result, saturated;
}
}
|