-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathemulator3.cpp
More file actions
2577 lines (2451 loc) · 102 KB
/
emulator3.cpp
File metadata and controls
2577 lines (2451 loc) · 102 KB
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************** emulator3.cpp ********************************
* Author: Agner Fog
* date created: 2018-02-18
* Last modified: 2024-08-01
* Version: 1.13
* Project: Binary tools for ForwardCom instruction set
* Description:
* Emulator: Execution functions for multiformat instructions
*
* Copyright 2018-2024 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
#include "stdafx.h"
// get intrinsic functions for _mm_getcsr and _mm_setcsr to control floating point rounding and exceptions
#if defined(_M_X64) || defined(__x86_64__) || defined(__amd64) || defined(__SSE2__)
#if defined(__FMA__) || defined(__AVX2__)
#define FMA_AVAILABLE 1
#else
#define FMA_AVAILABLE 0 // FMA instructions available
#endif
#if defined(_MSC_VER) && !FMA_AVAILABLE
#include <xmmintrin.h>
#else
#include <immintrin.h>
#endif
#define MCSCR_AVAILABLE 1 // MXCSR register available (x86-64 only)
#else
#define MCSCR_AVAILABLE 0
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////
// functions for detecting exceptions and controlling rounding mode on the CPU that runs the emulator
// Note: these functions are only available in x86 systems with SSE2 or x64 enabled
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Error message if MXCSR not available
void errorFpControlMissing() {
static int repeated = 0;
if (!repeated) {
fprintf(stderr, "Warning: Emulator cannot control floating point exceptions and rounding mode on this platform");
repeated = 1;
}
}
void setRoundingMode(uint8_t r) {
// change rounding mode
#if MCSCR_AVAILABLE
// Rounding mode 4: "odd if not exact" is not supported on x86-64 platform.
// Implement it by rounding up and down. If the two values are different then take the odd one.
if (r == 4) r = 1;
uint32_t e = _mm_getcsr();
e = (e & 0x9FFF) | (r & 3) << 13;
_mm_setcsr(e);
#else
errorFpControlMissing();
#endif
}
void clearExceptionFlags() {
// clear exception flags before detecting exceptions
#if MCSCR_AVAILABLE
uint32_t e = _mm_getcsr();
_mm_setcsr(e & 0xFFC0);
#else
errorFpControlMissing();
#endif
}
uint32_t getExceptionFlags() {
// read exception flags after instructions that may cause exceptions
// 1: invalid operation
// 2: denormal
// 4: divide by zero
// 8: overflow
// 0x10: underflow
// 0x20: precision
#if MCSCR_AVAILABLE
return _mm_getcsr() & 0x3F;
#else
errorFpControlMissing();
return 0;
#endif
}
void enableSubnormals(uint32_t e) {
// enable or disable subnormal numbers
#if MCSCR_AVAILABLE
uint32_t x = _mm_getcsr();
if (e != 0) {
_mm_setcsr(x & ~0x8040);
}
else {
_mm_setcsr(x | 0x8040);
}
#else
errorFpControlMissing();
#endif
}
uint32_t roundToHalfPrecision(float fresult, CThread * t) {
// round a result to half precision, using the specified rounding mode
uint32_t mask = t->parm[3].i;
uint32_t roundingMode = (mask >> MSKI_ROUNDING) & 7;
uint32_t hresult = float2half(fresult); // convert to half
uint32_t abshresult = hresult & 0x7FFF; // hresult without sign bit
float bresult = half2float(hresult); // convert back to float to check rounding
if (isnan_or_inf_h(hresult))
return hresult;
switch (roundingMode) {
case 0: default: // ties to even
break;
case 1: // down
if (bresult > fresult && abshresult != 0) { // round down
if (hresult & 0x8000) { // negative
if (abshresult < 0x7C00) hresult++;
}
else { // positive
hresult--;
}
}
break;
case 2: // up
if (bresult < fresult && abshresult != 0) { // round up
if (hresult & 0x8000) { // negative
hresult--;
}
else { // positive
if (abshresult < 0x7C00) hresult++;
}
}
break;
case 3: // truncate
if (hresult & 0x8000) { // negative
if (bresult < fresult && abshresult != 0) {
hresult--; // round up towards 0
}
}
else { // positive
if (bresult > fresult && abshresult != 0) {
hresult--; // round down towards 0
}
}
break;
case 4: // odd if not exact
if (bresult != fresult && (hresult & 1) == 0) {
// even and not exact. round to nearest odd
int isNegative = (hresult & 0x8000) != 0;
int isLow = bresult < fresult;
if (isNegative ^ isLow) { // fix the absense of logical xor operator
if (abshresult < 0x7C00) hresult++;
}
else {
if (abshresult != 0) hresult--;
}
}
break;
}
return hresult;
}
/////////////////////////////
// Multi-format instructions
/////////////////////////////
uint64_t f_nop(CThread * t) {
// No operation
t->running = 2; // don't save RD
t->returnType = 0; // debug return output
return 0;
}
static uint64_t f_store(CThread * t) {
// Store value RD to memory
uint8_t rd = t->operands[0];
uint64_t value = t->registers[rd];
if (t->vect) {
value = t->readVectorElement(rd, t->vectorOffset);
}
// check mask
if (t->parm[3].b & 1) {
uint64_t address = t->memAddress; // memory address
if (t->vect) address += t->vectorOffset;
t->writeMemoryOperand(value, address);
}
else { // mask is 0. This instruction has no fallback. Don't write
/*
uint8_t fallback = t->operands[2]; // mask is 0. get fallback
if (fallback == 0x1F) value = 0;
else if (t->vect) value = t->readVectorElement(fallback, t->vectorOffset);
else value = t->registers[fallback];*/
}
t->returnType = (t->returnType & ~0x10) | 0x20; // return type is memory
t->running = 2; // don't save RD
return 0;
}
static uint64_t f_move(CThread * t) {
// copy value
return t->parm[2].q;
}
static uint64_t f_prefetch(CThread * t) {
// prefetch from address. not emulated
return f_nop(t);
}
static uint64_t f_sign_extend(CThread * t) {
// sign-extend integer to 64 bits
int64_t value = 0;
switch (t->operandType) {
case 0:
value = (int64_t)(int8_t)t->parm[2].b;
break;
case 1:
value = (int64_t)(int16_t)t->parm[2].s;
break;
case 2:
value = (int64_t)(int32_t)t->parm[2].i;
break;
case 3:
value = (int64_t)t->parm[2].q;
break;
default:
t->interrupt(INT_WRONG_PARAMETERS);
value = 0;
}
t->operandType = 3; // change operand size of result
if (t->vect) {
t->vectorLength[t->operands[0]] = t->vectorLengthR = 8; // change vector length of result and stop vector loop
}
t->returnType = (t->returnType & ~7) | 3; // debug return output
return (uint64_t)value;
}
static uint64_t f_sign_extend_add(CThread * t) {
// sign-extend integer to 64 bits and add 64-bit register
int64_t value = 0;
uint8_t options = 0;
if (t->fInstr->tmplate == 0xE) options = t->pInstr->a.im5;
switch (t->operandType) {
case 0:
value = (int64_t)(int8_t)t->parm[2].b;
break;
case 1:
value = (int64_t)(int16_t)t->parm[2].s;
break;
case 2:
value = (int64_t)(int32_t)t->parm[2].i;
break;
case 3:
value = (int64_t)t->parm[2].q;
break;
default:
t->interrupt(INT_WRONG_PARAMETERS);
value = 0;
}
value <<= options;
uint8_t r1 = t->operands[4]; // first operand. g.p. register
value += t->registers[r1]; // read register with full size
t->operandType = 3; // change operand size of result
t->returnType = (t->returnType & ~7) | 3; // debug return output
if (t->vect) t->interrupt(INT_WRONG_PARAMETERS);
return (uint64_t)value;
}
static uint64_t f_compare(CThread * t) {
// compare two source operands and generate a boolean result
// get condition code
uint8_t cond = 0;
uint32_t mask = t->parm[3].i; // mask register value or NUMCONTR
if (t->fInstr->tmplate == 0xE && (t->fInstr->imm2 & 2)) {
cond = t->pInstr->a.im5; // E template. get condition from IM5
}
// get operands
SNum a = t->parm[1];
SNum b = t->parm[2];
uint8_t operandType = t->operandType;
if ((t->fInstr->imm2 & 4) && operandType < 5) {
b = t->parm[4]; // avoid immediate operand shifted by imm3
}
if (t->pInstr->a.op1 == II_COMPARE_HH) {
// float16 compare
if (operandType != 1) t->interrupt(INT_WRONG_PARAMETERS);
a.f = half2float(a.s); // convert to float32
b.f = half2float(b.s);
operandType = 5; // treat as float32
}
uint64_t result = 0;
uint8_t cond1 = cond >> 1 & 3; // bit 1 - 2 of condition
bool isnan = false;
// select operand type
if (operandType < 5) { // integer types
uint64_t sizeMask = dataSizeMask[t->operandType]; // mask for data size
uint64_t signBit = (sizeMask >> 1) + 1; // sign bit
a.q &= sizeMask; b.q &= sizeMask; // mask to desired size
if (cond1 != 3 && !(cond & 8)) { // signed
a.q ^= signBit; b.q ^= signBit; // flip sign bit to use unsigned compare
}
switch (cond1) { // select condition
case 0: // a == b
result = a.q == b.q;
break;
case 1: // a < b
result = a.q < b.q;
break;
case 2: // a > b
result = a.q > b.q;
break;
case 3: // abs(a) < abs(b). Not officially supported in version 1.11
if (a.q & signBit) a.q = (~a.q + 1) & sizeMask; // change sign. overflow allowed
if (b.q & signBit) b.q = (~b.q + 1) & sizeMask; // change sign. overflow allowed
result = a.q < b.q;
break;
}
}
else if (operandType == 5) { // float
//half2float(b.s)
isnan = isnan_f(a.i) || isnan_f(b.i); // check for NaN
if (!isnan) {
switch (cond1) { // select condition
case 0: // a == b
result = a.f == b.f;
break;
case 1: // a < b
result = a.f < b.f;
break;
case 2: // a > b
result = a.f > b.f;
break;
case 3: // abs(a) < abs(b)
result = fabsf(a.f) < fabsf(b.f);
break;
}
}
}
else if (operandType == 6) { // double
isnan = isnan_d(a.q) || isnan_d(b.q);
if (!isnan) {
switch (cond1) { // select condition
case 0: // a == b
result = a.d == b.d;
break;
case 1: // a < b
result = a.d < b.d;
break;
case 2: // a > b
result = a.d > b.d;
break;
case 3: // abs(a) < abs(b)
result = fabs(a.d) < fabs(b.d);
break;
}
}
}
else t->interrupt(INT_WRONG_PARAMETERS); // unsupported type
// invert result
if (cond & 1) result ^= 1;
// check for NaN
if (isnan) {
result = (cond >> 3) & 1; // bit 3 tells what to get if unordered
//if (t->parm[3].i & MSK_FLOAT_NAN_LOSS) t->interrupt(INT_FLOAT_NAN_LOSS); // mask bit 29: trap if NaN loss
}
// mask and fallback
uint8_t fallbackreg = t->operands[2];
uint64_t fallback = (fallbackreg & 0x1F) != 0x1F ? t->readRegister(fallbackreg) : 0;
switch (cond >> 4) {
case 0: // normal fallback
if (!(mask & 1)) result = fallback;
break;
case 1: // mask & result & fallback
result &= mask & fallback;
break;
case 2: // mask & (result | fallback)
result = mask & (result | fallback);
break;
case 3: // mask & (result ^ fallback)
result = mask & (result ^ fallback);
break;
}
if ((t->returnType & 7) >= 5) t->returnType -= 3; // debug return output must be integer
result &= 1; // use only bit 0 of result
if ((t->operands[1] & 0x1F) < 7) {
// There is a mask. get remaining bits from mask
result |= (t->parm[3].q & ~(uint64_t)1);
}
t->parm[3].b = 1; // prevent normal mask operation
return result;
}
uint64_t f_add(CThread * t) {
// add two numbers
SNum a = t->parm[1];
SNum b = t->parm[2];
uint32_t mask = t->parm[3].i;
SNum result;
uint32_t roundingMode = (mask >> MSKI_ROUNDING) & 7;
bool detectExceptions = (mask & (0xF << MSKI_EXCEPTIONS)) != 0; // make NaN if exceptions
uint8_t operandType = t->operandType;
if (((mask ^ t->lastMask) & (1<<MSK_SUBNORMAL)) != 0) {
// subnormal status changed
enableSubnormals (mask & (1<<MSK_SUBNORMAL));
t->lastMask = mask;
}
// operand type
if (operandType < 4) { // integer
// uint64_t sizeMask = dataSizeMask[t->operandType]; // mask for data size
result.q = a.q + b.q;
}
else if (operandType == 5) { // float
bool nana = isnan_f(a.i); // check for NaN input
bool nanb = isnan_f(b.i);
if (nana && nanb) { // both are NaN
return (a.i << 1) > (b.i << 1) ? a.i : b.i; // return the biggest payload
}
else if (nana) return a.q;
else if (nanb) return b.q;
if (roundingMode != 0) setRoundingMode(roundingMode);
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
result.f = a.f + b.f; // this is the actual addition
if (roundingMode == 4) {
// special case for rounding mode 4 (odd if not exact)
setRoundingMode(2); // try with both round up and round down
SNum roundUpResult;
roundUpResult.f = a.f + b.f;
if (roundUpResult.i & 1) result = roundUpResult; // choose the odd result
}
if (isnan_f(result.i)) {
// the result is NaN but neither input is NaN. This must be INF-INF
result.q = t->makeNan(nan_invalid_inf_sub_inf, operandType);
}
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
if ((mask & (1<<MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_add, operandType);
else if ((mask & (1<<MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1<<MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
if (roundingMode != 0) setRoundingMode(0); // reset rounding mode
}
else if (operandType == 6) { // double
bool nana = isnan_d(a.q); // check for NaN input
bool nanb = isnan_d(b.q);
if (nana && nanb) { // both are NaN
return (a.q << 1) > (b.q << 1) ? a.q : b.q; // return the biggest payload
}
else if (nana) return a.q;
else if (nanb) return b.q;
if (roundingMode != 0) setRoundingMode(roundingMode);
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
result.d = a.d + b.d; // this is the actual addition
if (roundingMode == 4) {
// special case for rounding mode 4 (odd if not exact)
setRoundingMode(2); // try with both round up and round down
SNum roundUpResult;
roundUpResult.d = a.d + b.d;
if (roundUpResult.q & 1) result = roundUpResult; // choose the odd result
}
if (isnan_d(result.q)) {
// the result is NaN but neither input is NaN. This must be INF-INF
result.q = t->makeNan(nan_invalid_inf_sub_inf, operandType);
}
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
if ((mask & (1<<MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_add, operandType);
else if ((mask & (1<<MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1<<MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
if (roundingMode != 0) setRoundingMode(0); // reset rounding mode
}
else {
// unsupported operand type
t->interrupt(INT_WRONG_PARAMETERS);
result.i = 0;
}
return result.q;
}
uint64_t f_sub(CThread * t) {
// subtract two numbers
SNum a = t->parm[1];
SNum b = t->parm[2];
uint32_t mask = t->parm[3].i;
SNum result;
uint32_t roundingMode = (mask >> MSKI_ROUNDING) & 7;
bool detectExceptions = (mask & (0xF << MSKI_EXCEPTIONS)) != 0; // make NaN if exceptions
uint8_t operandType = t->operandType;
if (((mask ^ t->lastMask) & (1<<MSK_SUBNORMAL)) != 0) {
// subnormal status changed
enableSubnormals (mask & (1<<MSK_SUBNORMAL));
t->lastMask = mask;
}
if (operandType < 4) { // integer
result.q = a.q - b.q; // subtract
}
else if (operandType == 5) { // float
bool nana = isnan_f(a.i); // check for NaN input
bool nanb = isnan_f(b.i);
if (nana && nanb) { // both are NaN
return (a.i << 1) > (b.i << 1) ? a.i : b.i; // return the biggest payload
}
else if (nana) return a.q;
else if (nanb) return b.q;
if (roundingMode != 0) setRoundingMode(roundingMode);
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
result.f = a.f - b.f; // this is the actual subtraction
if (roundingMode == 4) {
// special case for rounding mode 4 (odd if not exact)
setRoundingMode(2); // try with both round up and round down
SNum roundUpResult;
roundUpResult.f = a.f - b.f;
if (roundUpResult.i & 1) result = roundUpResult; // choose the odd result
}
if (isnan_f(result.i)) {
// the result is NaN but neither input is NaN. This must be INF-INF
result.q = t->makeNan(nan_invalid_inf_sub_inf, operandType);
}
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
if ((mask & (1<<MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_add, operandType);
else if ((mask & (1<<MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1<<MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
if (roundingMode != 0) setRoundingMode(0); // reset rounding mode
}
else if (operandType == 6) {// double
bool nana = isnan_d(a.q); // check for NaN input
bool nanb = isnan_d(b.q);
if (nana && nanb) { // both are NaN
return (a.q << 1) > (b.q << 1) ? a.q : b.q; // return the biggest payload
}
else if (nana) return a.q;
else if (nanb) return b.q;
if (roundingMode != 0) setRoundingMode(roundingMode);
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
result.d = a.d - b.d; // this is the actual subtraction
if (roundingMode == 4) {
// special case for rounding mode 4 (odd if not exact)
setRoundingMode(2); // try with both round up and round down
SNum roundUpResult;
roundUpResult.d = a.d - b.d;
if (roundUpResult.q & 1) result = roundUpResult; // choose the odd result
}
if (isnan_d(result.q)) {
// the result is NaN but neither input is NaN. This must be INF-INF
result.q = t->makeNan(nan_invalid_inf_sub_inf, operandType);
}
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
if ((mask & (1<<MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_add, operandType);
else if ((mask & (1<<MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1<<MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
if (roundingMode != 0) setRoundingMode(0); // reset rounding mode
}
else {
// unsupported operand type
t->interrupt(INT_WRONG_PARAMETERS);
result.i = 0;
}
return result.q;
}
extern uint64_t f_sub_rev(CThread * t) {
// subtract two numbers, b-a
uint64_t temp = t->parm[2].q; // swap operands
t->parm[2].q = t->parm[1].q;
t->parm[1].q = temp;
uint64_t retval = f_sub(t);
t->parm[2].q = temp; // restore parm[2] in case it is a constant
return retval;
}
uint64_t f_mul(CThread * t) {
// multiply two numbers
SNum a = t->parm[1];
SNum b = t->parm[2];
uint32_t mask = t->parm[3].i;
SNum result;
uint32_t roundingMode = (mask >> MSKI_ROUNDING) & 7;
bool detectExceptions = (mask & (0xF << MSKI_EXCEPTIONS)) != 0; // make NaN if exceptions
uint8_t operandType = t->operandType;
if (((mask ^ t->lastMask) & (1<<MSK_SUBNORMAL)) != 0) {
// subnormal status changed
enableSubnormals (mask & (1<<MSK_SUBNORMAL));
t->lastMask = mask;
}
if (operandType < 4) {
// integer
result.q = a.q * b.q;
}
else if (operandType == 5) { // float
bool nana = isnan_f(a.i); // check for NaN input
bool nanb = isnan_f(b.i);
if (nana && nanb) { // both are NaN
return (a.i << 1) > (b.i << 1) ? a.i : b.i; // return the biggest payload
}
else if (nana) return a.q;
else if (nanb) return b.q;
if (roundingMode != 0) setRoundingMode(roundingMode);
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
result.f = a.f * b.f; // this is the actual multiplication
if (roundingMode == 4) {
// special case for rounding mode 4 (odd if not exact)
setRoundingMode(2); // try with both round up and round down
SNum roundUpResult;
roundUpResult.f = a.f * b.f;
if (roundUpResult.i & 1) result = roundUpResult; // choose the odd result
}
if (isnan_f(result.i)) {
// the result is NaN but neither input is NaN. This must be 0*INF
result.q = t->makeNan(nan_invalid_0mulinf, operandType);
}
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
if ((mask & (1<<MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_mul, operandType);
else if ((mask & (1<<MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1<<MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
if (roundingMode != 0) setRoundingMode(0); // reset rounding mode
}
else if (operandType == 6) { // double
bool nana = isnan_d(a.q); // check for NaN input
bool nanb = isnan_d(b.q);
if (nana && nanb) { // both are NaN
return (a.q << 1) > (b.q << 1) ? a.q : b.q; // return the biggest payload
}
else if (nana) return a.q;
else if (nanb) return b.q;
if (roundingMode != 0) setRoundingMode(roundingMode);
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
result.d = a.d * b.d; // this is the actual multiplication
if (roundingMode == 4) {
// special case for rounding mode 4 (odd if not exact)
setRoundingMode(2); // try with both round up and round down
SNum roundUpResult;
roundUpResult.d = a.d * b.d;
if (roundUpResult.q & 1) result = roundUpResult; // choose the odd result
}
if (isnan_d(result.q)) {
// the result is NaN but neither input is NaN. This must be 0*INF
result.q = t->makeNan(nan_invalid_0mulinf, operandType);
}
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
if ((mask & (1<<MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_mul, operandType);
else if ((mask & (1<<MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1<<MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
if (roundingMode != 0) setRoundingMode(0); // reset rounding mode
}
else {
// unsupported operand type
t->interrupt(INT_WRONG_PARAMETERS);
result.i = 0;
}
return result.q;
}
uint64_t f_div(CThread * t) {
// divide two floating point numbers or signed integers
SNum a = t->parm[1];
SNum b = t->parm[2];
uint32_t mask = t->parm[3].i;
SNum result;
bool overflow = false;
uint32_t roundingMode = (mask >> MSKI_ROUNDING) & 7; // floating point rounding mode
bool detectExceptions = (mask & (0xF << MSKI_EXCEPTIONS)) != 0; // make NaN if exceptions
bool nana, nanb; // inputs are NaN
uint8_t operandType = t->operandType;
uint8_t options = 0;
uint8_t intRounding = 0; // integer rounding mode
if (t->fInstr->tmplate == 0xE && (t->fInstr->imm2 & 2)) {
options = t->pInstr->a.im5;
intRounding = options & 3;
}
if (((mask ^ t->lastMask) & (1<<MSK_SUBNORMAL)) != 0) {
// subnormal status changed
enableSubnormals (mask & (1<<MSK_SUBNORMAL));
t->lastMask = mask;
}
switch (operandType) {
case 0: // int8
if (a.b == 0x80 && b.bs == -1) { // division overflow
result.i = 0x80; overflow = true;
}
else if (b.b == 0) { // signed division by zero
result.i = a.bs < 0 ? 0x80 : 0x7F;
overflow = true;
}
else {
result.i = a.bs / b.bs;
if (intRounding != 0 && abs(b.bs) != 1) {
int rem = a.bs % b.bs;
switch (intRounding) {
case 3: { // nearest or even
uint32_t r2 = 2*abs(rem);
uint32_t b2 = abs(b.bs);
int s = int8_t(a.i ^ b.i) < 0 ? -1 : 1; // one with sign of result
if (r2 > b2 || (r2 == b2 && (result.b & 1))) result.i += s;
break;}
case 1: // down
if (rem != 0 && int8_t(a.i ^ b.i) < 0 && result.b != 0x80u) result.i--;
break;
case 2: // up
if (rem != 0 && int8_t(a.i ^ b.i) >= 0) result.i++;
break;
}
}
}
break;
case 1: // int16 or float16
if ((options & 0x20) == 0) { // int16
if (a.s == 0x8000u && b.ss == -1) { // division overflow
result.i = 0x8000; overflow = true;
}
else if (b.s == 0) { // signed division by zero
result.i = a.ss < 0 ? 0x8000 : 0x7FFF;
overflow = true;
}
else {
result.i = a.ss / b.ss;
if (intRounding != 0 && abs(b.ss) != 1) {
int16_t rem = a.ss % b.ss;
switch (intRounding) {
case 3: { // nearest or even
uint16_t r2 = 2 * abs(rem);
uint16_t b2 = abs(b.is);
int16_t s = int16_t(a.s ^ b.s) < 0 ? -1 : 1; // one with sign of result
if (r2 > b2 || (r2 == b2 && (result.s & 1))) result.s += s;
break; }
case 1: // down
if (rem != 0 && int16_t(a.s ^ b.s) < 0 && result.s != 0x8000u) result.s--;
break;
case 2: // up
if (rem != 0 && int16_t(a.s ^ b.s) >= 0) result.s++;
break;
}
}
}
}
else {
// float16
float aa = half2float(a.s);
float bb = half2float(b.s);
nana = isnan_h(a.s); // check for NaN input
nanb = isnan_h(b.s);
if (nana && nanb) { // both are NaN
result.i = (a.s << 1) > (b.s << 1) ? a.s : b.s; // return the biggest payload
}
else if (nana) result.i = a.i;
else if (nanb) result.i = b.i;
else if (b.s << 1 == 0) { // division by zero
if (a.s << 1 == 0) { // 0./0. = nan
result.q = t->makeNan(nan_invalid_0div0, operandType);
}
else {
// a / 0. = infinity
if (mask & (1 << MSK_DIVZERO)) result.q = t->makeNan(nan_div0, operandType);
else result.i = inf_h;
}
result.i |= (a.s ^ b.s) & 0x8000; // sign bit
}
else if (isinf_h(a.s) && isinf_h(b.s)) {
result.i = (uint32_t)t->makeNan(nan_invalid_infdivinf, operandType); // INF/INF
result.i |= (a.s ^ b.s) & 0x8000; // sign bit
}
else {
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
float r = aa / bb; // normal division
result.i = roundToHalfPrecision(r, t); // round with specified rounding mode
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
if (isinf_h(result.s)) x = 8; // overflow
else if (result.s << 1 == 0 && r != 0) x = 0x10; // underflow
else if (r != half2float(result.s)) x = 0x20; // inexact
if ((mask & (1 << MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_div, operandType);
else if ((mask & (1 << MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1 << MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
}
t->returnType = 0x118; // float16 return
}
break;
case 2: // int32
if (a.i == 0x80000000 && b.is == -1) { // division overflow
result.i = 0x80000000; overflow = true;
}
else if (b.i == 0) { // signed division by zero
result.i = a.is < 0 ? 0x80000000 : 0x7FFFFFFF;
overflow = true;
}
else {
result.i = a.is / b.is;
if (intRounding != 0 && abs(b.is) != 1) {
int rem = a.is % b.is;
switch (intRounding) {
case 3: { // nearest or even
uint32_t r2 = 2*abs(rem);
uint32_t b2 = abs(b.is);
int s = int32_t(a.i ^ b.i) < 0 ? -1 : 1; // one with sign of result
if (r2 > b2 || (r2 == b2 && (result.i & 1))) result.i += s;
break;}
case 1: // down
if (rem != 0 && int32_t(a.i ^ b.i) < 0 && result.i != 0x80000000u) result.i--;
break;
case 2: // up
if (rem != 0 && int32_t(a.i ^ b.i) >= 0) result.i++;
break;
}
}
}
break;
case 3: // int64
if (a.q == sign_d && b.qs == int64_t(-1)) { // division overflow
result.q = sign_d; overflow = true;
}
else if (b.q == 0) { // signed division by zero
result.q = a.qs < 0 ? sign_d : sign_d-1u;
overflow = true;
}
else {
result.qs = a.qs / b.qs;
if (intRounding != 0 && abs(b.qs) != 1) {
int64_t rem = a.qs % b.qs;
switch (intRounding) {
case 3: { // nearest or even
uint64_t r2 = 2*abs(rem);
uint64_t b2 = abs(b.qs);
int64_t s = int64_t(a.q ^ b.q) < 0 ? -1 : 1; // one with sign of result
if (r2 > b2 || (r2 == b2 && (result.i & 1))) result.q += s;
break;}
case 1: // down
if (rem != 0 && int64_t(a.q ^ b.q) < 0 && result.q != 0x8000000000000000u) result.q--;
break;
case 2: // up
if (rem != 0 && int64_t(a.q ^ b.q) >= 0) result.q++;
break;
}
}
}
break;
case 5: // float
nana = isnan_f(a.i); // check for NaN input
nanb = isnan_f(b.i);
if (nana && nanb) { // both are NaN
result.i = (a.i << 1) > (b.i << 1) ? a.i : b.i; // return the biggest payload
}
else if (nana) result.i = a.i;
else if (nanb) result.i = b.i;
else if (b.i << 1 == 0) { // division by zero
if (a.i << 1 == 0) { // 0./0. = nan
result.q = t->makeNan(nan_invalid_0div0, operandType);
}
else {
// a / 0. = infinity
if (mask & (1<<MSK_DIVZERO)) result.q = t->makeNan(nan_div0, operandType);
else result.i = inf_f;
}
result.i |= (a.i ^ b.i) & sign_f; // sign bit
}
else if (isinf_f(a.i) && isinf_f(b.i)) {
result.i = (uint32_t)t->makeNan(nan_invalid_infdivinf, operandType); // INF/INF
result.i |= (a.i ^ b.i) & sign_f; // sign bit
}
else {
if (roundingMode != 0) setRoundingMode(roundingMode);
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
result.f = a.f / b.f; // normal division
if (roundingMode == 4) {
// special case for rounding mode 4 (odd if not exact)
setRoundingMode(2); // try with both round up and round down
SNum roundUpResult;
roundUpResult.f = a.f / b.f;
if (roundUpResult.i & 1) result = roundUpResult; // choose the odd result
}
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
if ((mask & (1<<MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_div, operandType);
else if ((mask & (1<<MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1<<MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
if (roundingMode != 0) setRoundingMode(0); // reset rounding mode
}
break;
case 6: // double
nana = isnan_d(a.q); // check for NaN input
nanb = isnan_d(b.q);
if (nana && nanb) { // both are NaN
result.q = (a.q << 1) > (b.q << 1) ? a.q : b.q; // return the biggest payload
}
else if (nana) result.q = a.q;
else if (nanb) result.q = b.q;
else if (b.q << 1 == 0) { // division by zero
if (a.q << 1 == 0) { // 0./0. = nan
result.q = t->makeNan(nan_invalid_0div0, operandType);
}
else {
// a / 0. = infinity
if (mask & (1<<MSK_DIVZERO)) result.q = t->makeNan(nan_div0, operandType);
else result.q = inf_d;
}
result.q |= (a.q ^ b.q) & sign_d; // sign bit
}
else if (isinf_d(a.q) && isinf_d(b.q)) {
result.q = t->makeNan(nan_invalid_infdivinf, operandType); // INF/INF
result.q |= (a.q ^ b.q) & sign_d; // sign bit
}
else {
if (roundingMode != 0) setRoundingMode(roundingMode);
if (detectExceptions) clearExceptionFlags(); // clear previous exceptions
result.d = a.d / b.d; // normal division
if (roundingMode == 4) {
// special case for rounding mode 4 (odd if not exact)
setRoundingMode(2); // try with both round up and round down
SNum roundUpResult;
roundUpResult.d = a.d / b.d;
if (roundUpResult.q & 1) result = roundUpResult; // choose the odd result
}
if (detectExceptions) {
uint32_t x = getExceptionFlags(); // read exceptions
//!!
if (x & 0x10)
x += 0;
if ((mask & (1<<MSK_OVERFLOW)) && (x & 8)) result.q = t->makeNan(nan_overflow_div, operandType);
else if ((mask & (1<<MSK_UNDERFLOW)) && (x & 0x10)) result.q = t->makeNan(nan_underflow, operandType);
else if ((mask & (1<<MSK_INEXACT)) && (x & 0x20)) result.q = t->makeNan(nan_inexact, operandType);
}
if (roundingMode != 0) setRoundingMode(0); // reset rounding mode
}
break;
default:
t->interrupt(INT_WRONG_PARAMETERS);
result.i = 0;
}
return result.q;
}
uint64_t f_div_u(CThread * t) {
// divide two unsigned integer numbers
if (t->operandType > 4) {
return f_div(t); // floating point: same as f_div
}
SNum a = t->parm[1];
SNum b = t->parm[2];
SNum result;
bool overflow = false;
uint32_t intRounding = 0; // integer rounding mode
if (t->fInstr->tmplate == 0xE && (t->fInstr->imm2 & 2)) {
intRounding = t->pInstr->a.im5 & 3; // E template. get integer rounding mode from IM5
}
switch (t->operandType) {
case 0: // int8
if (b.b == 0) { // unsigned division by zero
result.i = 0xFF; overflow = true;
}
else {
result.i = a.b / b.b;
if (intRounding >= 2 && b.b != 1) {
uint32_t rem = a.b % b.b;
switch (intRounding) {
case 3: // nearest or even
if (rem*2 > b.b || (rem*2 == b.b && (result.i & 1))) result.i++;
break;
case 2: // up
if (rem != 0) result.i++;
break;
}
}
}
break;
case 1: // int16
if (b.s == 0) {
result.i = 0xFFFF; overflow = true;
}
else {
result.i = a.s / b.s;
if (intRounding >= 2 && b.s != 1) {
uint32_t rem = a.s % b.s;
switch (intRounding) {
case 3: // nearest or even
if (rem*2 > b.s || (rem*2 == b.s && (result.i & 1))) result.i++;
break;