-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathemulator5.cpp
More file actions
2109 lines (1995 loc) · 100 KB
/
emulator5.cpp
File metadata and controls
2109 lines (1995 loc) · 100 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
/**************************** emulator5.cpp ********************************
* Author: Agner Fog
* date created: 2018-02-18
* Last modified: 2024-07-31
* Version: 1.13
* Project: Binary tools for ForwardCom instruction set
* Description:
* Emulator: Execution functions for single format instructions, continued
*
* Copyright 2018-2024 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
#include "stdafx.h"
// Format 1.3 B. Two vector registers and a broadcast 8-bit immediate operand.
static uint64_t gp2vec (CThread * t) {
// Move value of general purpose register RS to scalar in vector register RD.
uint8_t rd = t->operands[0];
uint8_t rs = t->operands[4];
uint64_t result = t->registers[rs]; // read general purpose register
t->vectorLength[rd] = dataSizeTable[t->operandType]; // set length of destination
t->vect = 4; // stop vector loop
return result;
}
static uint64_t vec2gp (CThread * t) {
// Move value of first element of vector register RS to general purpose register RD.
uint8_t rd = t->operands[0];
uint8_t rs = t->operands[4];
uint8_t size = dataSizeTable[t->operandType];
if (size > t->vectorLength[rs]) size = t->vectorLength[rs]; // limit size to vector length
uint64_t result = *(uint64_t*)(t->vectors.buf() + t->MaxVectorLength*rs); // read directly from vector
if (size < 8) result &= ((uint64_t)1 << size*8) - 1; // mask off to size
t->registers[rd] = result; // write to general purpose register
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD
t->returnType &= ~ 0x100; // debug return type not vector
return result;
}
static uint64_t make_sequence (CThread * t) {
// Make a vector with RS sequential numbers. First value is IM1.
uint8_t rd = t->operands[0];
uint8_t rs = t->operands[4];
int32_t val = int8_t(t->pInstr->b[0]); // immediate operand, sign extended integer
uint64_t num = t->registers[rs]; // number of elements
uint32_t elementSize = dataSizeTable[t->operandType];
uint8_t dsizelog = dataSizeTableLog[t->operandType]; // log2(elementsize)
SNum temp;
// limit length
uint64_t length = num << dsizelog;
if (length > t->MaxVectorLength) {
length = t->MaxVectorLength; num = length >> dsizelog;
}
// set length of rd
t->vectorLength[rd] = (uint32_t)length;
// loop through destination vector
for (uint32_t pos = 0; pos < length; pos += elementSize) {
switch (t->operandType) {
case 0: case 1: case 2: case 3:
t->writeVectorElement(rd, (uint64_t)(int64_t)val, pos); break;
case 4:
t->writeVectorElement(rd, (uint64_t)(int64_t)val, pos); // int128
t->writeVectorElement(rd, (uint64_t)((int64_t)val >> 63), pos+8); break;
case 5: // float
temp.f = float(val); // convert to float
t->writeVectorElement(rd, temp.q, pos);
break;
case 6: // double
temp.d = double(val); // convert to double
t->writeVectorElement(rd, temp.q, pos);
break;
default:
t->interrupt(INT_WRONG_PARAMETERS);
}
val++; // increment value
}
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD
return 0;
}
static uint64_t compress(CThread * t) {
// Compress vector RT of length RS to a vector of half the length and half the element size.
// Double precision -> single precision, 64-bit integer -> 32-bit integer, etc.
// operands:
uint8_t rd = t->operands[0];
uint8_t rs = t->operands[4];
uint8_t IM1 = t->parm[4].b;
uint32_t oldLength = t->vectorLength[rs]; // (uint32_t)t->registers[rs];
uint32_t newLength = oldLength / 2;
uint32_t pos; // position in destination vector
uint8_t overflowU = 0; // unsigned overflow in current element
uint8_t overflowS = 0; // signed overflow in current element
uint8_t overflowU2 = 0; // unsigned overflow in any element
uint8_t overflowS2 = 0; // signed overflow in any element
uint8_t overflowF2 = 0; // floating point overflow in any element
SNum mask = t->parm[3]; // options mask
int8_t * source = t->vectors.buf() + (uint64_t)rs * t->MaxVectorLength; // address of RS data
int8_t * destination = t->vectors.buf() + (uint64_t)rd * t->MaxVectorLength; // address of RD data
uint8_t roundingMode = (IM1 >> 4) & 7; // floating point rounding mode
if ((IM1 & 0x80) == 0) roundingMode = (mask.i >> MSKI_ROUNDING) & 7;
uint8_t exceptionControl = IM1 & 7; // floating point exception enable bits
if ((IM1 & 8) == 0) { // floating point exception control
//exceptionControl = mask.i >> (MSKI_EXCEPTIONS + 1) & 7; // exceptions from NUMCONTR
exceptionControl = (mask.i >> MSKI_EXCEPTIONS) & 7; // exceptions from NUMCONTR
}
switch (t->operandType) { // source operand type
case 0: // int8 -> int4
for (pos = 0; pos < newLength; pos += 1) {
union {
uint16_t s;
uint8_t b[2];
} u;
u.s = *(uint16_t*)(source + 2*pos); // two values to convert to one byte
for (int i = 0; i < 2; i++) { // loop for two bytes to convert
uint8_t val = u.b[i];
overflowU = val > 0x0F; // unsigned overflow
overflowS = val - 0xF8 > 0x0F; // signed overflow
overflowU2 |= overflowU; overflowS2 |= overflowS;
switch (IM1 & 7) {
case 0: default: // wrap around
break;
case 4: // signed integer overflow gives zero
if (overflowS) val = 0;
break;
case 5: // signed integer overflow gives signed saturation
if (overflowS) val = 0x7 + (val >> 7);
break;
case 6: // unsigned integer overflow gives zero
if (overflowU) val = 0;
break;
case 7: // unsigned integer overflow gives unsigned saturation
if (overflowU) val = 0xF;
break;
}
u.b[i] = val;
}
uint8_t val2 = (u.b[0] & 0xF) | u.b[1] << 4;
*(uint8_t*)(destination + pos) = val2; // store two values
}
t->returnType = 0x110;
break;
case 1: // int16 -> int8
for (pos = 0; pos < newLength; pos += 1) {
uint16_t val = *(uint16_t*)(source + 2*pos); // value to convert
overflowU = val > 0xFF; // unsigned overflow
overflowS = val - 0xFF80 > 0xFF; // signed overflow
overflowU2 |= overflowU; overflowS2 |= overflowS;
switch (IM1 & 7) {
case 0: default: // wrap around
break;
case 4: // signed integer overflow gives zero
if (overflowS) val = 0;
break;
case 5: // signed integer overflow gives signed saturation
if (overflowS) val = 0x7F + (val >> 15);
break;
case 6: // unsigned integer overflow gives zero
if (overflowU) val = 0;
break;
case 7: // unsigned integer overflow gives unsigned saturation
if (overflowU) val = 0xFF;
break;
}
*(uint8_t*)(destination + pos) = (uint8_t)val; // store value
}
t->returnType = 0x110;
break;
case 2: // int32 -> int16
for (pos = 0; pos < newLength; pos += 2) {
uint32_t val = *(uint32_t*)(source + 2*pos); // value to convert
overflowU = val > 0xFFFF; // unsigned overflow
overflowS = val - 0xFFFF8000 > 0xFFFF; // signed overflow
switch (IM1 & 7) {
case 0: default: // wrap around
break;
case 4: // signed integer overflow gives zero
if (overflowS) val = 0;
break;
case 5: // signed integer overflow gives signed saturation
if (overflowS) val = 0x7FFF + (val >> 31);
break;
case 6: // unsigned integer overflow gives zero
if (overflowU) val = 0;
break;
case 7: // unsigned integer overflow gives unsigned saturation
if (overflowU) val = 0xFFFF;
break;
}
*(uint16_t*)(destination + pos) = (uint16_t)val; // store value
}
t->returnType = 0x111;
break;
case 3: // int64 -> int32
for (pos = 0; pos < newLength; pos += 4) {
uint64_t val = *(uint64_t*)(source + 2*pos); // value to convert
overflowU = val > 0xFFFFFFFFU; // unsigned overflow
overflowS = val - 0xFFFFFFFF80000000 > 0xFFFFFFFFU; // signed overflow
switch (IM1 & 7) {
case 0: default: // wrap around
break;
case 4: // signed integer overflow gives zero
if (overflowS) val = 0;
break;
case 5: // signed integer overflow gives signed saturation
if (overflowS) val = 0x7FFFFFFF + (val >> 63);
break;
case 6: // unsigned integer overflow gives zero
if (overflowU) val = 0;
break;
case 7: // unsigned integer overflow gives unsigned saturation
if (overflowU) val = 0xFFFFFFFF;
break;
}
*(uint32_t*)(destination + pos) = (uint32_t)val; // store value
}
t->returnType = 0x112;
break;
case 4: // int128 -> int64
for (pos = 0; pos < newLength; pos += 8) {
uint64_t valLo = *(uint64_t*)(source + 2*pos); // value to convert, low part
uint64_t valHi = *(uint64_t*)(source + 2*pos + 8); // value to convert, high part
overflowU = valHi != 0; // unsigned overflow
if ((int64_t)valLo < 0) overflowS = valHi+1 != 0; // signed overflow
else overflowS = valHi != 0;
overflowU2 |= overflowU; overflowS2 |= overflowS;
switch (IM1 & 7) {
case 0: default: // wrap around
break;
case 4: // signed integer overflow gives zero
if (overflowS) valLo = 0;
break;
case 5: // signed integer overflow gives signed saturation
if (overflowS) valLo = nsign_d + (valHi >> 63);
break;
case 6: // unsigned integer overflow gives zero
if (overflowU) valHi = valLo = 0;
break;
case 7: // unsigned integer overflow gives unsigned saturation
if (overflowU) valLo = 0xFFFFFFFFFFFFFFFF;
break;
}
}
t->returnType = 0x113;
break;
case 5: // float -> float16
for (pos = 0; pos < newLength; pos += 2) {
SNum val;
val.i = *(uint32_t*)(source + 2 * pos); // value to convert
uint32_t val2 = roundToHalfPrecision(val.f, t);
if (!isnan_h(val2)) {
// check overflow
overflowS = isinf_h(val2) && !isinf_f(val.i);// detect overflow
overflowF2 |= overflowS;
if (overflowS) { // check for overflow
if (exceptionControl & 1) { // overflow exception -> NaN
val2 = (uint16_t)t->makeNan(nan_overflow_conv, 1); // overflow
}
}
else if ((exceptionControl & 6) && val2 << 1 == 0 && val.f != 0.f) {
val2 = (uint16_t)t->makeNan(nan_underflow, 1); // underflow exception (inexact implies underflow)
}
else if ((exceptionControl & 4) && half2float(val2) != val.f) {
val2 = (uint16_t)t->makeNan(nan_inexact, 1); // inexact exception
}
}
*(uint16_t*)(destination + pos) = val2; // store value
}
t->returnType = 0x118;
break;
case 6: // double -> float
for (pos = 0; pos < newLength; pos += 4) {
SNum val1, val2;
val1.q = *(uint64_t*)(source + 2 * pos); // value to convert
// check NaN and INF
if (isnan_or_inf_d(val1.q)) {
val2.f = float(val1.d); // convert to single precision, no rounding
}
else {
val2.f = float(val1.d); // convert to single precision
// check rounding mode
switch (roundingMode) {
case 0: default: // nearest or even
break;
case 1: // down
if (val2.f > val1.d) {
if (val2.f == 0.f) val2.i = 0x80000001; // 0 -> subnormal negative
else if (val2.i > 0) val2.i--;
else val2.i++;
}
break;
case 2: // up
if (val2.f < val1.d) {
if (val2.f == 0.f) val2.i = 0x00000001; // 0 -> subnormal positive
else if (val2.i > 0) val2.i++;
else val2.i--;
}
break;
case 3: // towards zero
if (val1.d > 0. && val2.f > val1.d && (val2.i & 0x7FFFFFFF) > 0) {
val2.i--;
}
if (val1.d < 0. && val2.f < val1.d && (val2.i & 0x7FFFFFFF) > 0) {
val2.i--;
}
break;
case 4: // odd if not exact
if (val2.f > val1.d && (val2.i & 1) == 0 && (val2.i & 0x7FFFFFFF) > 0) val2.i--;
if (val2.f < val1.d && (val2.i & 1) == 0 && (val2.i & 0x7FFFFFFF) < 0x7F7FFFFF) val2.i++;
break;
}
// check overflow
overflowS = isinf_f(val2.i) && !isinf_d(val1.q); // detect overflow
overflowF2 |= overflowS;
if (overflowS) { // check for overflow
if (exceptionControl & 1) { // overflow exception -> NaN
val2.q = t->makeNan(nan_overflow_conv, 5); // overflow
}
}
else if ((exceptionControl & 6) && val2.f == 0.f && val1.d != 0.) {
val2.q = t->makeNan(nan_underflow, 5); // underflow exception
}
else if ((exceptionControl & 4) && val2.f != val1.d) {
val2.q = t->makeNan(nan_inexact, 5); // inexact exception
}
}
*(uint32_t*)(destination + pos) = val2.i; // store value
}
t->returnType = 0x115;
break;
default:
t->interrupt(INT_WRONG_PARAMETERS);
}
// check overflow traps
/*
if (mask.i & MSK_OVERFL_ALL) {
if ((mask.i & MSK_OVERFL_SIGN) && overflowS2) t->interrupt(INT_OVERFL_SIGN); // signed overflow
else if ((mask.i & MSK_OVERFL_UNSIGN) && overflowU2) t->interrupt(INT_OVERFL_UNSIGN); // unsigned overflow
else if ((mask.i & MSK_OVERFL_FLOAT) && overflowF2) t->interrupt(INT_OVERFL_FLOAT); // float overflow
} */
t->vectorLength[rd] = newLength; // save new vector length
t->vect = 4; // stop vector loop
t->running = 2; // don't save. result has already been saved
return 0;
}
static uint64_t expand(CThread * t) {
// Expand vector RS to a vector of the double length and the double element size.
// OT specifies the element size or precision of the destination.
// Half precision -> single precision, 32-bit integer -> 64-bit integer, etc.
// Operands:
uint8_t rd = t->operands[0];
uint8_t rs = t->operands[4];
uint8_t IM1 = t->parm[4].b;
if (IM1 & 0xFC) t->interrupt(INT_WRONG_PARAMETERS);
bool signExtend = (IM1 & 2) == 0;
uint32_t initLength = t->vectorLength[rs];
uint32_t newLength = 2 * initLength;
if (newLength > t->MaxVectorLength) newLength = t->MaxVectorLength;
// uint32_t oldLength = newLength / 2;
uint32_t pos; // position in source vector
int8_t * source = t->vectors.buf() + (uint32_t)rs * t->MaxVectorLength; // address of RT data
int8_t * destination = t->vectors.buf() + (uint32_t)rd * t->MaxVectorLength; // address of RD data
if (rd == rs) {
// source and destination are the same. Make a temporary copy of source to avoid overwriting
memcpy(t->tempBuffer, source, initLength);
source = t->tempBuffer;
}
switch (t->operandType) {
case 0: // int4 -> int8
for (pos = 0; pos < newLength; pos += 1) {
uint8_t val1 = *(uint8_t*)(source + pos); // values to convert
union {
uint16_t s;
uint8_t b[2];
int8_t bs[2];
} val2;
if (signExtend) {
val2.bs[0] = (int8_t)val1 << 4 >> 4; // sign extend
val2.bs[1] = (int8_t)val1 >> 4; // sign extend
}
else {
val2.b[0] = val1 & 0xF; // zero extend
val2.b[1] = val1 >> 4; // zero extend
}
*(uint16_t*)(destination + pos*2) = val2.s; // store value
}
break;
case 1: // int8 -> int16
for (pos = 0; pos < newLength; pos += 1) {
uint16_t val = *(uint8_t*)(source + pos); // value to convert
if (signExtend) val = uint16_t((int16_t)(val << 8) >> 8); // sign extend
*(uint16_t*)(destination + pos*2) = val; // store value
}
break;
case 2: // int16 -> int32
for (pos = 0; pos < newLength; pos += 2) {
uint32_t val = *(uint16_t*)(source + pos); // value to convert
if (signExtend) val = uint32_t((int32_t)(val << 16) >> 16); // sign extend
*(uint32_t*)(destination + pos*2) = val; // store value
}
break;
case 3: // int32 -> int64
for (pos = 0; pos < newLength; pos += 4) {
uint64_t val = *(uint32_t*)(source + pos); // value to convert
if (signExtend) val = uint64_t((int64_t)(val << 32) >> 32); // sign extend
*(uint64_t*)(destination + pos*2) = val; // store value
}
break;
case 4: // int64 -> int128
for (pos = 0; pos < newLength; pos += 8) {
uint64_t valLo = *(uint64_t*)(source + pos); // value to convert
uint64_t valHi = 0;
if (signExtend) valHi = uint64_t((int64_t)valLo >> 63); // sign extend
*(uint64_t*)(destination + pos*2) = valLo; // store low part
*(uint64_t*)(destination + pos*2 + 8) = valHi; // store high part
}
break;
case 5: // float16 -> float
for (pos = 0; pos < newLength; pos += 2) {
uint16_t val1 = *(uint16_t*)(source + pos); // value to convert
float val2 = half2float(val1); // convert half precision to float
*(float*)(destination + pos*2) = val2; // store value
}
break;
case 6: // float -> double
for (pos = 0; pos < newLength; pos += 4) {
SNum val1;
val1.i = *(uint32_t*)(source + pos); // value to convert
double val2 = val1.f; // convert to double precision
// (assume that this platform follows the undocumented standard of left-justifying NaN payloads)
*(double*)(destination + pos*2) = val2; // store value
}
break;
default:
t->interrupt(INT_WRONG_PARAMETERS);
}
t->vectorLength[rd] = newLength; // save new vector length
t->vect = 4; // stop vector loop
t->running = 2; // don't save. result has already been saved
return 0;
}
static uint64_t float2int (CThread * t) {
// Conversion of floating point to signed or unsigned integer with the same operand size.
// The rounding mode and overflow control is specified in IM1.
SNum a = t->parm[1];
SNum b = t->parm[4];
int64_t result = 0;
uint32_t dataSize = dataSizeTable[t->operandType];
uint8_t roundingMode = b.b >> 4;
if ((roundingMode & 8) == 0) { // rounding mode determined by NUMCONTR
SNum mask = t->parm[3]; // options mask
roundingMode = mask.i >> MSKI_ROUNDING;
}
roundingMode &= 7;
uint8_t signMode = b.b & 7; // bit 0: unsigned, bit 1-2 overflow control
bool overflow = false;
if (dataSize == 2) { // float16 -> int16
const float max = (float)(int32_t)0x7FFF;
const float min = -max - 1.0f;
const float umax = (float)(uint32_t)0xFFFFu;
if (isnan_h(a.s)) {
result = (b.b & 0x08) ? 0x8000u : 0;
}
else {
float f = half2float(a.s);
if ((signMode & 1) == 0) { // signed float16
switch (roundingMode) { // rounding mode:
case 0: // nearest or even
overflow = f >= max + 0.5f || f < min - 0.5f;
result = (int)(nearbyint(f));
break;
case 1: // down
overflow = f >= max + 1.0f || f < min;
result = (int)(floor(f));
break;
case 2: // up
overflow = f > max || f <= min - 1.0f;
result = (int)(ceil(f));
break;
case 3: // towards zero
overflow = f >= max + 1.0f || f <= min - 1.0f;
result = (int)(f);
break;
case 4: // odd if not exact
overflow = f >= max + 0.5f || f < min;
result = (int)(nearbyint(f));
if (float(result) < f && !(result & 1)) result++;
if (float(result) > f && !(result & 1)) result--;
break;
case 5: // nearest, with ties away from zero
overflow = f >= max + 0.5f || f < min - 0.5f;
result = (int)(nearbyint(f));
if (result >= 0 && float(result) == f - 0.5f) result++;
if (result <= 0 && float(result) == f + 0.5f) result--;
break;
}
if (overflow) {
switch (signMode >> 1) {
case 0: // overflow gives INT_MIN
default:
result = 0x8000u; break;
case 1: // overflow gives zero
result = 0; break;
case 2: // overflow saturates
if (result < 0) result = 0x8000u;
else result = 0x7FFFu;
break;
}
}
}
else { // unsigned float16
switch (roundingMode) { // rounding mode:
case 0: // nearest or even
overflow = f >= umax + 0.5f || f < - 0.5f;
result = (int)(nearbyint(f));
break;
case 1: // down
case 3: // towards zero
overflow = f >= umax + 1.0f || f < 0;
result = (int)(floor(f));
break;
case 2: // up
overflow = f > umax || f <= - 1.0f;
result = (int)(ceil(f));
break;
overflow = f >= max + 1.0f || f <= min - 1.0f;
result = (int)(f);
break;
case 4: // odd if not exact
overflow = f > umax || f < 0.f;
result = (int)(nearbyint(f));
if (float(result) < f && !(result & 1)) result++;
if (float(result) > f && !(result & 1)) result--;
break;
case 5: // nearest, with ties away from zero
overflow = f >= umax + 0.5f || f <= - 0.5f;
result = (int)(nearbyint(f));
if (float(result) == f - 0.5f) result++;
break;
}
if (overflow) {
switch (signMode >> 1) {
case 0: // overflow gives UINT_MAX
default:
result = 0xFFFFu; break;
case 1: // overflow gives zero
result = 0; break;
}
}
}
}
}
else if (dataSize == 4) { // float -> int32
const float max = (float)(int32_t)nsign_f;
const float min = -max - 1.0f;
const float umax = (float)(uint32_t)0xFFFFFFFFu;
if (isnan_f(a.i)) {
result = (b.b & 0x08) ? 0x80000000u : 0;
}
else {
if ((signMode & 1) == 0) { // signed float32
switch (roundingMode) { // rounding mode:
case 0: // nearest or even
if (a.f >= max + 0.5f || a.f < min - 0.5f) overflow = true;
result = (int64_t)(nearbyint(a.f));
break;
case 1: // down
if (a.f >= max + 1.0f || a.f <= min) overflow = true;
result = (int64_t)(floor(a.f));
break;
case 2: // up
if (a.f > max || a.f <= min - 1.0f) overflow = true;
result = (int64_t)(ceil(a.f));
break;
case 3: // towards zero
if (a.f > max || a.f <= min - 1.0f) overflow = true;
result = (int64_t)(a.f);
break;
case 4: // odd if not exact
overflow = a.f >= max + 0.5f || a.f < min;
result = (int)(nearbyint(a.f));
if (double(result) < a.f && !(result & 1)) result++;
if (double(result) > a.f && !(result & 1)) result--;
break;
case 5: // nearest, with ties away from zero
overflow = a.f >= max + 0.5f || a.f < min - 0.5f;
result = (int)(nearbyint(a.f));
if (result >= 0 && double(result) == a.f - 0.5) result++;
if (result <= 0 && double(result) == a.f + 0.5) result--;
break;
}
if (overflow) {
switch (signMode >> 1) {
case 0: // overflow gives INT_MIN
default:
result = 0x80000000u; break;
case 1: // overflow gives zero
result = 0; break;
case 2: // overflow saturates
if (result < 0) result = 0x80000000u;
else result = 0x7FFFFFFFu;
break;
}
}
}
else { // unsigned float32
switch (roundingMode) { // rounding mode:
case 0: // nearest or even
overflow = a.f >= umax + 0.5f || a.f < - 0.5f;
result = (int64_t)(nearbyint(a.f));
break;
case 1: // down
case 3: // towards zero
overflow = a.f >= umax + 1.0f || a.f < 0.0f;
result = (int64_t)(floor(a.f));
break;
case 2: // up
overflow = a.f > umax || a.f <= -1.0f;
result = (int64_t)(ceil(a.f));
break;
case 4: // odd if not exact
overflow = a.f > umax || a.f < 0.f;
result = (int64_t)(nearbyint(a.f));
if (double(result) < a.f && !(result & 1)) result++;
if (double(result) > a.f && !(result & 1)) result--;
break;
case 5: // nearest, with ties away from zero
overflow = a.f >= umax + 0.5f || a.f <= - 0.5f;
result = (int64_t)(nearbyint(a.f));
if (double(result) == a.f - 0.5) result++;
break;
}
if (overflow) {
switch (signMode >> 1) {
case 0: // overflow gives UINT_MAX
default:
result = 0xFFFFFFFFu; break;
case 1: // overflow gives zero
result = 0; break;
}
}
}
}
}
else if (dataSize == 8) { // double -> int64
const double max = (double)(int64_t)nsign_d;
const double min = -max - 1.0f;
const double umax = (double)0xFFFFFFFFFFFFFFFFu;
if (isnan_d(a.q)) {
result = (b.b & 0x08) ? sign_d : 0;
}
else {
if ((signMode & 1) == 0) { // signed float64
switch (roundingMode) { // rounding mode:
case 0: // nearest or even
if (a.d >= max + 0.5 || a.d < min - 0.5) overflow = true;
result = (int64_t)(nearbyint(a.d));
break;
case 1: // down
if (a.d >= max + 1.0 || a.d <= min) overflow = true;
result = (int64_t)(floor(a.d));
break;
case 2: // up
if (a.d > max || a.d <= min - 1.0) overflow = true;
result = (int64_t)(ceil(a.d));
break;
case 3: // towards zero
if (a.d >= max + 1.0 || a.d <= min - 1.0) overflow = true;
result = (int64_t)(a.d);
break;
case 4: // odd if not exact
overflow = a.d > max || a.d < min;
result = (int64_t)(nearbyint(a.d));
if (double(result) < a.d && !(result & 1)) result++;
if (double(result) > a.d && !(result & 1)) result--;
break;
case 5: // nearest, with ties away from zero
overflow = a.d >= max + 0.5 || a.d < min - 0.5;
result = (int64_t)(nearbyint(a.d));
if (result >= 0 && double(result) == a.d - 0.5) result++;
if (result <= 0 && double(result) == a.d + 0.5) result--;
break;
}
if (overflow) {
switch (signMode >> 1) {
case 0: // overflow gives INT_MIN
default:
result = sign_d; break;
case 1: // overflow gives zero
result = 0; break;
case 2: // overflow saturates
if (result < 0) result = sign_d;
else result = nsign_d;
break;
}
}
}
else { // unsigned float64
switch (roundingMode) { // rounding mode:
case 0: // nearest or even
overflow = a.d >= umax + 0.5 || a.d < - 0.5;
result = (uint64_t)(nearbyint(a.d));
break;
case 1: case 3: // down
overflow = a.d >= umax + 1.0 || a.d < 0.0;
result = (uint64_t)(floor(a.d));
break;
case 2: // up
overflow = a.d > umax || a.d <= -1.0;
result = (uint64_t)(ceil(a.d));
break;
case 4: // odd of not exact
overflow = a.d >= umax || a.d < 0;
result = (int64_t)(nearbyint(a.d));
if (double(result) < a.d && !(result & 1)) result++;
if (double(result) > a.d && !(result & 1)) result--;
break;
case 5: // nearest with ties away from zero
overflow = a.d >= umax + 0.5 || a.f <= - 0.5;
result = (int64_t)(nearbyint(a.d));
if (double(result) == a.d - 0.5) result++;
break;
}
if (overflow) {
switch (signMode >> 1) {
case 0: // overflow gives UINT_MAX
default:
result = 0xFFFFFFFFFFFFFFFFu; break;
case 1: // overflow gives zero
result = 0; break;
}
}
}
}
}
else t->interrupt(INT_WRONG_PARAMETERS);
/* Traps not supported
if (overflow && (mask.i & MSK_OVERFL_SIGN)) {
t->interrupt(INT_OVERFL_SIGN); // signed overflow
result = dataSizeMask[t->operandType] >> 1; // INT_MAX
}
if (invalid && (mask.i & MSK_FLOAT_NAN_LOSS)) {
t->interrupt(INT_FLOAT_NAN_LOSS); // nan converted to integer
result = dataSizeMask[t->operandType] >> 1; // INT_MAX
} */
if ((t->operandType & 7) >= 5) t->operandType -= 3; // debug return type is integer
return result;
}
static uint64_t int2float (CThread * t) {
// Conversion of signed or unsigned integer to floating point with same operand size.
SNum a = t->parm[1];
SNum IM1 = t->parm[4];
bool isSigned = (IM1.b & 1) == 0; // signed integer
bool inexactX = (IM1.b & 4) != 0; // make NaN exception if inexact
SNum result;
uint32_t dataSize = dataSizeTable[t->operandType];
switch (dataSize) {
case 2: // int16 -> float16
if (isSigned) {
result.s = float2half(float(a.ss));
if (inexactX && int32_t(half2float(result.s)) != a.ss) {
result.q = t->makeNan(nan_inexact, 1);
}
}
else { // unsigned
result.s = float2half(float(a.s));
if (inexactX && uint32_t(half2float(result.s)) != a.s) {
result.q = t->makeNan(nan_inexact, 1);
}
}
t->returnType = 0x118; // debug return type is float16
break;
case 4: // int32 -> float
if (isSigned) {
result.f = (float)a.is;
if (inexactX && int32_t(result.f) != a.is) {
result.q = t->makeNan(nan_inexact, 5);
}
}
else {
result.f = (float)a.i;
if (inexactX && uint32_t(result.f) != a.i) {
result.q = t->makeNan(nan_inexact, 5);
}
}
t->returnType = 0x115; // debug return type is float
break;
case 8: // int64 -> double
if (isSigned) {
result.d = (double)a.qs;
if (inexactX && int64_t(result.d) != a.qs) {
result.q = t->makeNan(nan_inexact, 6);
}
}
else {
result.d = (double)a.q;
if (inexactX && uint64_t(result.d) != a.q) {
result.q = t->makeNan(nan_inexact, 6);
}
}
t->returnType = 0x116; // debug return type is double
break;
default:
t->interrupt(INT_WRONG_PARAMETERS);
result.q = 0;
}
return result.q;
}
static uint64_t round_ (CThread * t) {
// Round floating point to integer in floating point representation.
// The rounding mode is specified in IM1.
// Conversion of floating point to signed integer with the same operand size.
// The rounding mode is specified in IM1.
SNum a = t->parm[1];
SNum b = t->parm[4];
SNum result; result.q = 0;
uint32_t dataSize = dataSizeTable[t->operandType];
uint32_t roundingMode = b.i & 7;
if ((b.i & 8) == 0) {
// rounding mode determined by NUMCONTR
SNum mask = t->parm[3]; // options mask
roundingMode = (mask.i >> MSKI_ROUNDING) & 7;
}
if (dataSize == 4) { // float -> int32
switch (roundingMode) { // rounding mode:
case 0: // nearest or even
result.f = nearbyintf(a.f);
break;
case 1: // down
result.f = floorf(a.f);
break;
case 2: // up
result.f = ceilf(a.f);
break;
case 3: // towards zero
result.f = truncf(a.f);
break;
case 4: // odd if not exact
result.f = nearbyintf(a.f);
if (result.f < a.f && (result.i & 1) == 0) result.f += 1.0f;
if (result.f > a.f && (result.i & 1) == 0) result.f -= 1.0f;
break;
default: t->interrupt(INT_WRONG_PARAMETERS);
}
}
else if (dataSize == 8) { // double -> int64
switch (roundingMode) { // rounding mode:
case 0: // nearest or even
result.d = nearbyint(a.d);
break;
case 1: // down
result.d = floor(a.d);
break;
case 2: // up
result.d = ceil(a.d);
break;
case 3: // towards zero
result.d = trunc(a.d);
break;
case 4: // odd if not exact
result.d = nearbyint(a.d);
if (result.d < a.d && (result.i & 1) == 0) result.d += 1.0;
if (result.d > a.d && (result.i & 1) == 0) result.d -= 1.0;
break;
default: t->interrupt(INT_WRONG_PARAMETERS);
}
}
return result.q;
}
static uint64_t round2n (CThread * t) {
// Round to nearest multiple of 2n.
// RD = 2^n * round(2^(−n)*RS).
// n is a signed integer constant in IM1
SNum b = t->parm[4]; // n
//SNum mask = t->parm[3];
uint32_t exponent1;
uint64_t result = 0;
if (t->operandType == 5) { // float
union {
uint32_t i;
float f;
struct {
uint32_t mantissa : 23;
uint32_t exponent : 8;
uint32_t sign : 1;
};
} u;
u.i = t->parm[1].i; // input a
if (isnan_f(u.i)) return u.i; // a is nan
exponent1 = u.exponent;
if (exponent1 == 0) {
u.mantissa = 0; // a is zero or subnormal. return zero
return u.i;
}
exponent1 -= b.i; // subtract b from exponent
if ((int32_t)exponent1 <= 0) { // underflow
//if (mask.i & MSK_FLOAT_UNDERFL) t->interrupt(INT_FLOAT_UNDERFL);
return 0;
}
else if ((int32_t)exponent1 >= 0xFF) { // overflow
//if (mask.i & MSK_OVERFL_FLOAT) t->interrupt(INT_OVERFL_FLOAT);
return inf_f;
}
u.exponent = exponent1;
u.f = nearbyintf(u.f); // round
if (u.f != 0) u.exponent += b.i; // add b to exponent
result = u.i;
}
else if (t->operandType == 6) { // double
union {
uint64_t q;
double d;
struct {
uint64_t mantissa : 52;
uint64_t exponent : 11;
uint64_t sign : 1;
};
} u;
u.q = t->parm[1].q; // input a
if (isnan_d(u.q)) return u.q; // a is nan
exponent1 = u.exponent;
if (exponent1 == 0) {
u.mantissa = 0; // a is zero or subnormal. return zero
return u.q;
}
exponent1 -= b.i; // subtract b from exponent
if ((int32_t)exponent1 <= 0) { // underflow
//if (mask.i & MSK_FLOAT_UNDERFL) t->interrupt(INT_FLOAT_UNDERFL);
return 0;
}
else if ((int32_t)exponent1 >= 0x7FF) { // overflow
//if (mask.i & MSK_OVERFL_FLOAT) t->interrupt(INT_OVERFL_FLOAT);
return inf_d;
}
u.exponent = exponent1;
u.d = nearbyint(u.d); // round
if (u.d != 0) u.exponent += b.i; // add b to exponent
result = u.q;
}
else t->interrupt(INT_WRONG_PARAMETERS);
return result;
}
static uint64_t abs_ (CThread * t) {
// Absolute value of integer.
// IM1 determines handling of overflow: 0: wrap around, 1: saturate, 2: zero, 3: trap
SNum a = t->parm[1]; // x
SNum b = t->parm[4]; // option
uint64_t sizemask = dataSizeMask[t->operandType]; // mask for operand size
uint64_t signbit = (sizemask >> 1) + 1; // just the sign bit
if (a.q & signbit) {
// a is negative
if (t->operandType > 4) { // floating point types
return a.q & ~signbit; // just remove sign bit
}
if ((a.q & sizemask) == signbit) {
// overflow
switch (b.b & ~4) {
case 0: // wrap around
break;
case 1: // saturate
return a.q - 1;
case 2: // zero
return 0;
default:
t->interrupt(INT_WRONG_PARAMETERS);
}
if ((b.b & 4) /* && (t->parm[3].i & MSK_OVERFL_SIGN)*/) { // trap
t->interrupt(INT_OVERFL_SIGN); // signed overflow
}
}
a.qs = - a.qs; // change sign
}
return a.q;
}
static uint64_t broad_ (CThread * t) {
// 18: Broadcast 8-bit signed constant into all elements of RD with length RS (31 in RS field gives scalar output).
// 19: broadcast_max. Broadcast 8-bit constant into all elements of RD with maximum vector length.