-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathemulator4.cpp
More file actions
1801 lines (1671 loc) · 78.9 KB
/
emulator4.cpp
File metadata and controls
1801 lines (1671 loc) · 78.9 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
/**************************** emulator4.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, part 1
*
* Copyright 2018-2024 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
#include "stdafx.h"
// Format 1.0 A. Three general purpose registers
// Currently no instructions with format 1.0
// Format 1.1 C. One general purpose register and a 16 bit immediate operand. int64
static uint64_t move_16s(CThread * t) {
// Move 16-bit sign-extended constant to general purpose register.
return t->parm[2].q;
}
static uint64_t move_16u(CThread * t) {
// Move 16-bit zero-extended constant to general purpose register.
return t->parm[2].s;
}
static uint64_t shift16_add(CThread * t) {
// Shift 16-bit unsigned constant left by 16 and add.
t->parm[2].q <<= 16;
return f_add(t);
}
static uint64_t shifti1_move(CThread * t) {
// RD = IM2 << IM1. Sign-extend IM2 to 32/64 bits and shift left by the unsigned value IM1
return (t->parm[2].qs >> 8) << t->parm[2].b;
}
static uint64_t shifti1_add(CThread * t) {
// RD += IM2 << IM1. Sign-extend IM2 to 32/64 bits and shift left by the unsigned value IM1 and add
t->parm[2].q = (t->parm[2].qs >> 8) << t->parm[2].b;
return f_add(t);
}
static uint64_t shifti1_and(CThread * t) {
// RD &= IM2 << IM1
return t->parm[1].q & ((t->parm[2].qs >> 8) << t->parm[2].b);
}
static uint64_t shifti1_or(CThread * t) {
// RD |= IM2 << IM1
return t->parm[1].q | ((t->parm[2].qs >> 8) << t->parm[2].b);
}
static uint64_t shifti1_xor(CThread * t) {
// RD ^= IM2 << IM1
return t->parm[1].q ^ ((t->parm[2].qs >> 8) << t->parm[2].b);
}
// Format 1.8 B. Two general purpose registers and an 8-bit immediate operand. int64
static uint64_t abs_64(CThread * t) {
// Absolute value of signed integer.
// IM1 determines handling of overflow: 0: wrap around, 1: saturate, 2: zero.
SNum a = t->parm[1];
uint64_t sizeMask = dataSizeMask[t->operandType]; // mask for data size
uint64_t signBit = (sizeMask >> 1) + 1; // sign bit
if ((a.q & sizeMask) == signBit) { // overflow
if (t->parm[2].b & 4) t->interrupt(INT_OVERFL_SIGN);
switch (t->parm[2].b & ~4) {
case 0: return a.q; // wrap around
case 1: return sizeMask >> 1; // saturate
case 2: return 0; // zero
default: t->interrupt(INT_WRONG_PARAMETERS);
}
}
if (a.q & signBit) { // negative
a.qs = - a.qs; // change sign
}
return a.q;
}
static uint64_t shifti_add(CThread * t) {
// Shift and add. RD += RS << IM1
SNum a = t->parm[0];
SNum b = t->parm[1];
SNum c = t->parm[2];
SNum r1, r2; // result
r1.q = b.q << c.b; // shift left
uint8_t nbits = dataSizeTableBits[t->operandType];
if (c.q >= nbits) r1.q = 0; // shift out of range gives zero
r2.q = a.q + r1.q; // add
/*
if (t->numContr & MSK_OVERFL_I) { // check for overflow
if (t->numContr & MSK_OVERFL_SIGN) { // check for signed overflow
uint64_t sizeMask = dataSizeMask[t->operandType]; // mask for data size
uint64_t signBit = (sizeMask >> 1) + 1; // sign bit
uint64_t ovfl = ~(a.q ^ r1.q) & (a.q ^ r2.q); // overflow if a and b have same sign and result has opposite sign
if (r1.qs >> c.b != b.qs || (ovfl & signBit) || c.q >= nbits) t->interrupt(INT_OVERFL_SIGN); // signed overflow
}
else if (t->numContr & MSK_OVERFL_UNSIGN) { // check for unsigned overflow
if (r2.q < a.q || r1.q >> c.b != b.q || c.q >= nbits) t->interrupt(INT_OVERFL_UNSIGN); // unsigned overflow
}
} */
return r2.q; // add
}
uint64_t bitscan_ (CThread * t) {
// Bit scan forward or reverse. Find index to first or last set bit in RS
SNum a = t->parm[1]; // input value
uint8_t IM1 = t->parm[2].b; // immediate operand
a.q &= dataSizeMask[t->operandType]; // mask for operand size
if (a.q == 0) {
a.qs = (IM1 & 0x10) ? -1 : 0; // return 0 or -1 if intput is 0
}
else if (IM1 & 1) {
// reverse
a.q = bitScanReverse(a.q);
}
else {
// forward
a.q = bitScanForward(a.q);
}
return a.q;
}
static uint64_t roundp2(CThread * t) {
// Round up or down to nearest power of 2.
SNum a = t->parm[1]; // input operand
uint8_t IM1 = t->parm[2].b; // immediate operand
a.q &= dataSizeMask[t->operandType]; // mask off unused bits
if (dataSizeTable[t->operandType] > 8) t->interrupt(INT_WRONG_PARAMETERS); // illegal operand type
if (a.q == 0) {
a.qs = IM1 & 0x10 ? -1 : 0; // return 0 or -1 if the intput is 0
}
else if (!(a.q & (a.q-1))) {
return a.q; // the number is a power of 2. Return unchanged
}
else if (IM1 & 1) {
// round up to nearest power of 2
uint32_t s = bitScanReverse(a.q); // highest set bit
if (s+1 >= dataSizeTableBits[t->operandType]) { // overflow
a.qs = IM1 & 0x20 ? -1 : 0; // return 0 or -1 on overflow
}
else {
a.q = (uint64_t)1 << (s+1); // round up
}
}
else {
// round down to nearest power of 2
a.q = (uint64_t)1 << bitScanReverse(a.q);
}
return a.q;
}
static uint32_t popcount32(uint32_t x) { // count bits in 32 bit integer. used by popcount_ function
x = x - ((x >> 1) & 0x55555555);
x = (x >> 2 & 0x33333333) + (x & 0x33333333);
x = (x + (x >> 4)) & 0x0F0F0F0F;
x = (x + (x >> 8)) & 0x00FF00FF;
x = uint16_t(x + (x >> 16));
return x;
}
uint64_t popcount_ (CThread * t) {
// Count the number of bits in RS that are 1
SNum a = t->parm[1]; // value
a.q &= dataSizeMask[t->operandType]; // mask for operand size
return popcount32(a.i) + popcount32(a.q >> 32);
}
static uint64_t read_spec(CThread * t) {
// Read special register RS into g. p. register RD.
uint8_t rs = t->operands[4]; // source register
uint64_t retval = 0;
switch (rs) {
case REG_NUMCONTR & 0x1F: // numcontr register
retval = t->numContr;
break;
case REG_THREADP & 0x1F: // threadp register
retval = t->threadp;
break;
case REG_DATAP & 0x1F: // datap register
retval = t->datap;
break;
default: // other register not implemented
t->interrupt(INT_WRONG_PARAMETERS);
}
return retval;
}
static uint64_t write_spec(CThread * t) {
// Write g. p. register RS to special register RD
uint8_t rd = t->operands[0]; // destination register
SNum a = t->parm[1]; // value
switch (rd) {
case REG_NUMCONTR & 0x1F: // numcontr register
t->numContr = a.i | 1; // bit 0 must be set
if (((t->numContr ^ t->lastMask) & (1<<MSK_SUBNORMAL)) != 0) {
// subnormal status changed
enableSubnormals(t->numContr & (1<<MSK_SUBNORMAL));
}
t->lastMask = t->numContr;
break;
case REG_THREADP & 0x1F: // threadp register
t->threadp = a.q;
break;
case REG_DATAP & 0x1F: // datap register
t->datap = a.q;
break;
default: // other register not implemented
t->interrupt(INT_WRONG_PARAMETERS);
}
t->returnType = 0;
return 0;
}
static uint64_t read_capabilities(CThread * t) {
// Read capabilities register into g. p. register RD
uint8_t capabreg = t->operands[4]; // capabilities register number
if (capabreg < number_of_capability_registers) {
return t->capabilyReg[capabreg];
}
else {
t->interrupt(INT_WRONG_PARAMETERS);
}
return 0;
}
static uint64_t write_capabilities(CThread * t) {
// Write g. p. register to capabilities register RD
uint8_t capabreg = t->operands[0]; // capabilities register number
uint64_t value = t->parm[1].q;
if (capabreg < number_of_capability_registers) {
t->capabilyReg[capabreg] = value;
}
else {
t->interrupt(INT_WRONG_PARAMETERS);
}
t->returnType = 0;
return 0;
}
static uint64_t read_perf(CThread * t) {
// Read performance counter
uint8_t parfreg = t->operands[4]; // performance register number
uint8_t par2 = t->parm[2].b; // second operand
uint64_t result = 0;
switch (parfreg) {
case 0: // reset all performance counters
if (par2 & 1) {
t->perfCounters[perf_cpu_clock_cycles] = 0;
}
if (par2 & 2) {
t->perfCounters[perf_instructions] = 0;
t->perfCounters[perf_2size_instructions] = 0;
t->perfCounters[perf_3size_instructions] = 0;
t->perfCounters[perf_gp_instructions] = 0;
t->perfCounters[perf_gp_instructions_mask0] = 0;
}
if (par2 & 4) {
t->perfCounters[perf_vector_instructions] = 0;
}
if (par2 & 8) {
t->perfCounters[perf_control_transfer_instructions] = 0;
t->perfCounters[perf_direct_jumps] = 0;
t->perfCounters[perf_indirect_jumps] = 0;
t->perfCounters[perf_cond_jumps] = 0;
}
break;
case 1: // CPU clock cycles
result = t->perfCounters[perf_cpu_clock_cycles];
if (par2 == 0) t->perfCounters[perf_cpu_clock_cycles] = 0;
break;
case 2: // number of instructions
switch (par2) {
case 0:
result = t->perfCounters[perf_instructions];
t->perfCounters[perf_instructions] = 0;
t->perfCounters[perf_2size_instructions] = 0;
t->perfCounters[perf_3size_instructions] = 0;
t->perfCounters[perf_gp_instructions] = 0;
t->perfCounters[perf_gp_instructions_mask0] = 0;
break;
case 1:
result = t->perfCounters[perf_instructions];
break;
case 2:
result = t->perfCounters[perf_2size_instructions];
break;
case 3:
result = t->perfCounters[perf_3size_instructions];
break;
case 4:
result = t->perfCounters[perf_gp_instructions];
break;
case 5:
result = t->perfCounters[perf_gp_instructions_mask0];
break;
}
break;
case 3: // number of vector instructions
result = t->perfCounters[perf_vector_instructions];
if (par2 == 0) t->perfCounters[perf_vector_instructions] = 0;
break;
case 4: // vector registers in use
for (int iv = 0; iv < 32; iv++) {
if (t->vectorLength[iv] > 0) result |= (uint64_t)1 << iv;
}
break;
case 5: // jumps, calls, and returns
switch (par2) {
case 0:
result = t->perfCounters[perf_control_transfer_instructions];
t->perfCounters[perf_control_transfer_instructions] = 0;
t->perfCounters[perf_direct_jumps] = 0;
t->perfCounters[perf_indirect_jumps] = 0;
t->perfCounters[perf_cond_jumps] = 0;
break;
case 1: // all jumps, calls, returns
result = t->perfCounters[perf_control_transfer_instructions];
break;
case 2: // direct unconditional jumps, calls, returns
result = t->perfCounters[perf_direct_jumps];
break;
case 3:
result = t->perfCounters[perf_indirect_jumps];
break;
case 4:
result = t->perfCounters[perf_cond_jumps];
break;
}
break;
case 16: // errors counters
switch (par2) {
case 0:
result = 0;
t->perfCounters[perf_unknown_instruction] = 0;
t->perfCounters[perf_wrong_operands] = 0;
t->perfCounters[perf_array_overflow] = 0;
t->perfCounters[perf_read_violation] = 0;
t->perfCounters[perf_write_violation] = 0;
t->perfCounters[perf_misaligned] = 0;
t->perfCounters[perf_address_of_first_error] = 0;
t->perfCounters[perf_type_of_first_error] = 0;
break;
case 1: // unknown instructions
result = t->perfCounters[perf_unknown_instruction];
break;
case 2: // wrong operands for instruction
result = t->perfCounters[perf_wrong_operands];
break;
case 3: // array index out of bounds
result = t->perfCounters[perf_array_overflow];
break;
case 4: // memory read access violation
result = t->perfCounters[perf_read_violation];
break;
case 5: // memory write access violation
result = t->perfCounters[perf_write_violation];
break;
case 6: // memory access misaligned
result = t->perfCounters[perf_misaligned];
break;
case 62: // address of first error
result = t->perfCounters[perf_address_of_first_error];
break;
case 63: // type of first error
result = t->perfCounters[perf_type_of_first_error];
break;
}
break;
default:
t->interrupt(INT_WRONG_PARAMETERS);
}
return result;
}
static uint64_t read_sys(CThread * t) {
// Read system register RS into g. p. register RD
t->interrupt(INT_WRONG_PARAMETERS); // not supported yet
return 0;
}
static uint64_t write_sys(CThread * t) {
// Write g. p. register RS to system register RD
t->interrupt(INT_WRONG_PARAMETERS); // not supported yet
t->returnType = 0;
return 0;
}
static uint64_t push_r(CThread * t) {
// push one or more g.p. registers on a stack pointed to by rd
int32_t step = dataSizeTable[t->operandType];
bool forward = (t->parm[4].i & 0x80) != 0; // false: pre-decrement, true: post-increment pointer
uint8_t reg0 = t->operands[0] & 0x1F; // pointer register
uint8_t reg1 = t->operands[4] & 0x1F; // first push register
uint8_t reglast = t->parm[4].i & 0x1F; // last push register
uint8_t reg;
// check for errors
if (reglast < reg1 || (reg0 >= reg1 && reg0 <= reglast)) {
t->interrupt(INT_WRONG_PARAMETERS);
}
uint64_t pointer = t->registers[reg0];
// loop through registers to push
for (reg = reg1; reg <= reglast; reg++) {
if (!forward) pointer -= (int64_t)step;
uint64_t value = t->registers[reg];
t->writeMemoryOperand(value, pointer);
if (forward) pointer += (int64_t)step;
t->listResult(value);
}
t->registers[reg0] = pointer;
t->running = 2; // don't save stack pointer with reduced operand size
return pointer;
}
static uint64_t pop_r(CThread * t) {
// pop one or more g.p. registers from a stack pointed to by rd
int32_t step = dataSizeTable[t->operandType];
bool regorder = (t->parm[4].i & 0x40) != 0; // false means last register first, true means first register first
uint8_t reg0 = t->operands[0] & 0x1F; // pointer register
uint8_t reg1 = t->operands[4] & 0x1F; // first push register
uint8_t reglast = t->parm[4].i & 0x1F; // last push register
uint8_t reg;
// check for errors
if (reglast < reg1 || (reg0 >= reg1 && reg0 <= reglast)) {
t->interrupt(INT_WRONG_PARAMETERS);
}
uint64_t pointer = t->registers[reg0];
if (regorder) {
// loop through registers to pop in forward order
for (reg = reg1; reg <= reglast; reg++) {
uint64_t value = t->readMemoryOperand(pointer);
t->registers[reg] = value;
pointer += (int64_t)step;
t->listResult(value);
}
}
else {
// loop through registers to pop in reverse order
for (reg = reglast; reg >= reg1; reg--) {
uint64_t value = t->readMemoryOperand(pointer);
t->registers[reg] = value;
pointer += (int64_t)step;
t->listResult(value);
}
}
t->registers[reg0] = pointer;
t->running = 2; // don't save stack pointer with reduced operand size
return pointer;
}
// Format 2.9 A. Three general purpose registers and a 32-bit immediate operand
static uint64_t move_hi32(CThread * t) {
// Load 32-bit constant into the high part of a general purpose register. The low part is zero. RD = IM4 << 32.
return t->parm[2].q << 32;
}
static uint64_t insert_hi32(CThread * t) {
// Insert 32-bit constant into the high part of a general purpose register, leaving the low part unchanged.
return t->parm[2].q << 32 | t->parm[1].i;
}
static uint64_t add_32u(CThread * t) {
// Add zero-extended 32-bit constant to general purpose register
t->parm[2].q = t->parm[2].i;
return f_add(t);
}
static uint64_t sub_32u(CThread * t) {
// Subtract zero-extended 32-bit constant from general purpose register
t->parm[2].q = t->parm[2].i;
return f_sub(t);
}
static uint64_t add_hi32(CThread * t) {
// Add 32-bit constant to high part of general purpose register. RD = RT + (IM6 << 32).
t->parm[2].q <<= 32;
return f_add(t);
}
static uint64_t and_hi32(CThread * t) {
// AND high part of general purpose register with 32-bit constant. RD = RT & (IM6 << 32).
return t->parm[1].q & t->parm[2].q << 32;
}
static uint64_t or_hi32(CThread * t) {
// OR high part of general purpose register with 32-bit constant. RD = RT | (IM6 << 32).
return t->parm[1].q | t->parm[2].q << 32;
}
static uint64_t xor_hi32(CThread * t) {
// XOR high part of general purpose register with 32-bit constant. RD = RT ^ (IM6 << 32).
return t->parm[1].q ^ t->parm[2].q << 32;
}
static uint64_t replace_bits(CThread * t) {
// Replace a group of contiguous bits in RT by a specified constant
SNum a = t->parm[1];
SNum b = t->parm[2];
uint64_t val = b.s; // value to insert
uint8_t pos = uint8_t(b.i >> 16); // start position
uint8_t num = uint8_t(b.i >> 24); // number of bits to replace
if (num > 32 || pos + num > 64) t->interrupt(INT_WRONG_PARAMETERS);
uint64_t mask = ((uint64_t)1 << num) - 1; // mask with 'num' 1-bits
return (a.q & ~(mask << pos)) | ((val & mask) << pos);
}
static uint64_t address_(CThread * t) {
// RD = RT + IM6, RS can be THREADP (28), DATAP (29) or IP (30)
t->returnType = 0x13;
return t->memAddress;
}
// Format 1.2 A. Three vector register operands
static uint64_t set_len(CThread * t) {
// RD = vector register RS with length changed to value of g.p. register RT
// set_len: the new length is indicated in bytes
// set_num: the new length is indicated in elements
uint8_t rd = t->operands[0];
uint8_t rs = t->operands[4];
uint8_t rt = t->operands[5];
uint32_t oldLength = t->vectorLength[rs];
uint64_t newLength = t->registers[rt];
if (t->op & 1) newLength *= dataSizeTable[t->operandType]; // set_num: multiply by operand size
if (newLength > t->MaxVectorLength) newLength = t->MaxVectorLength;
if (newLength > oldLength) {
memcpy(t->vectors.buf() + rd*t->MaxVectorLength, t->vectors.buf() + rs*t->MaxVectorLength, oldLength); // copy first part from RT
memset(t->vectors.buf() + rd*t->MaxVectorLength + oldLength, 0, size_t(newLength - oldLength)); // set the rest to zero
}
else {
memcpy(t->vectors.buf() + rd*t->MaxVectorLength, t->vectors.buf() + rs*t->MaxVectorLength, size_t(newLength)); // copy newLength from RT
}
t->vectorLength[rd] = (uint32_t)newLength; // set new length
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD
return 0;
}
static uint64_t get_len(CThread * t) {
// Get length of vector register RT into general purpose register RD
// get_len: get the length in bytes
// get_num: get the length in elements
uint8_t rd = t->operands[0];
uint8_t rt = t->operands[4];
uint32_t length = t->vectorLength[rt]; // length of RT
if (t->op & 1) length >>= dataSizeTableLog[t->operandType]; // get_num: divide by operand size (round down)
t->registers[rd] = length; // save in g.p. register, not vector register
t->vect = 4; // stop vector loop
t->running = 2; // don't save to vector register RD
t->returnType = 0x12; // debug return output
return length;
}
uint64_t insert_(CThread * t) {
// Replace one element in vector RD, starting at offset RT·OS, with scalar RS
uint64_t pos; // position of element insert
uint8_t rd = t->operands[3]; // source and destination register
uint8_t operandType = t->operandType; // operand type
uint64_t returnval;
uint8_t dsizelog = dataSizeTableLog[operandType]; // log2(elementsize)
t->vectorLengthR = t->vectorLength[rd];
uint8_t sourceVector = t->operands[4]; // source register
if (t->fInstr->format2 == 0x120) { // format 1.2A v1 = insert(v1, v2, r3)
uint8_t rt = t->operands[5]; // index register
pos = t->registers[rt] << dsizelog;
}
else { // format 1.3B v1 = insert(v1, v2, imm)
pos = t->parm[2].q << dsizelog;
}
if (pos == t->vectorOffset) {
if (dsizelog == 4) { // 128 bits.
t->parm[5].q = t->readVectorElement(sourceVector, 8); // high part of 128-bit result
}
returnval = t->readVectorElement(sourceVector, 0); // first element of sourceVector
}
else {
if (dsizelog == 4) { // 128 bits.
t->parm[5].q = t->readVectorElement(rd, t->vectorOffset + 8); // high part of 128-bit result
}
returnval = t->parm[0].q; // rd unchanged
}
return returnval;
}
uint64_t extract_(CThread * t) {
// Extract one element from vector RT, at offset RS·OS or IM1·OS, with size OS
// and broadcast into vector register RD.
uint8_t rd = t->operands[0]; // destination register
uint8_t operandType = t->operandType; // operand type
uint8_t dsizelog = dataSizeTableLog[operandType]; // log2(elementsize)
uint8_t rsource = t->operands[4]; // source vector
uint64_t pos; // position = index * OS
if (t->fInstr->format2 == 0x120) {
uint8_t rt = t->operands[5]; // index register
pos = t->registers[rt] << dsizelog;
}
else { // format 0x130
pos = t->parm[4].q << dsizelog;
}
uint32_t sourceLength = t->vectorLength[rsource]; // length of source vector
uint64_t result;
if (pos >= sourceLength) {
result = 0; // beyond end of source vector
}
else {
int8_t * source = t->vectors.buf() + (uint64_t)rsource * t->MaxVectorLength; // address of rsource data
result = *(uint64_t*)(source+pos); // no problem reading too much, it will be cut off later if the operand size is < 64 bits
if (dsizelog >= 4) { // 128 bits
t->parm[5].q = *(uint64_t*)(source+pos+8); // store high part of 128 bit element
}
}
t->vectorLength[rd] = t->vectorLengthR = sourceLength; // length of destination vector
return result;
}
static uint64_t compress_sparse(CThread * t) {
// Compress sparse vector elements indicated by mask bits into contiguous vector.
uint8_t rd = t->operands[0]; // destination vector
//uint8_t rt = t->operands[4]; // length of input vector not specified
uint8_t rt = t->operands[5]; // source vector
uint8_t rm = t->operands[1]; // mask vector
uint32_t sourceLength = t->vectorLength[rt]; // length of source vector
uint32_t maskLength = t->vectorLength[rm]; // length of mask vector
//uint64_t newLength = t->registers[rt]; // length of destination
uint64_t newLength = sourceLength; // length of destination
uint32_t elementSize = dataSizeTable[t->operandType]; // size of each element
int8_t * source = t->vectors.buf() + rt*t->MaxVectorLength; // address of RT data
int8_t * masksrc = t->vectors.buf() + rm*t->MaxVectorLength; // address of mask data
int8_t * destination = t->vectors.buf() + rd*t->MaxVectorLength; // address of RD data
// limit length
if (newLength > t->MaxVectorLength) newLength = t->MaxVectorLength;
if (newLength > maskLength) newLength = maskLength; // no reason to go beyond mask
if (newLength > sourceLength) { // reading beyond the end of the source vector
memset(source + sourceLength, 0, size_t(newLength - sourceLength)); // make sure the rest is zero
}
uint32_t pos1 = 0; // position in source vector
uint32_t pos2 = 0; // position in destination vector
// loop through mask register
for (pos1 = 0; pos1 < newLength; pos1 += elementSize) {
if (*(masksrc + pos1) & 1) { // check mask bit
// copy from pos1 in source to pos2 in destination
switch (elementSize) {
case 1: // int8
*(destination+pos2) = *(source+pos1);
break;
case 2: // int16
*(uint16_t*)(destination+pos2) = *(uint16_t*)(source+pos1);
break;
case 4: // int32, float
*(uint32_t*)(destination+pos2) = *(uint32_t*)(source+pos1);
break;
case 8: // int64, double
*(uint64_t*)(destination+pos2) = *(uint64_t*)(source+pos1);
break;
case 16: // int128, float128
*(uint64_t*)(destination+pos2) = *(uint64_t*)(source+pos1);
*(uint64_t*)(destination+pos2+8) = *(uint64_t*)(source+pos1+8);
break;
}
pos2 += elementSize;
}
}
// set new length of destination vector
t->vectorLength[rd] = pos2;
t->vect = 4; // stop vector loop
t->running = 2; // don't save. result has already been saved
return 0;
}
static uint64_t expand_sparse(CThread * t) {
// Expand contiguous vector into sparse vector with positions indicated by mask bits
// RS = length of output vector
uint8_t rd = t->operands[0]; // destination vector
uint8_t rs = t->operands[4]; // source vector
uint8_t rt = t->operands[5]; // length indicator
uint8_t rm = t->operands[1]; // mask vector
uint32_t sourceLength = t->vectorLength[rs]; // length of source vector
uint32_t maskLength = t->vectorLength[rm]; // length of mask vector
uint64_t newLength = t->registers[rt]; // length of destination
uint32_t elementSize = dataSizeTable[t->operandType & 7]; // size of each element
int8_t * source = t->vectors.buf() + rs*t->MaxVectorLength; // address of RS data
int8_t * masksrc = t->vectors.buf() + rm*t->MaxVectorLength; // address of mask data
int8_t * destination = t->vectors.buf() + 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, sourceLength);
source = t->tempBuffer;
}
// limit length
if (newLength > t->MaxVectorLength) newLength = t->MaxVectorLength;
if (newLength > maskLength) newLength = maskLength; // no reason to go beyond mask
if (newLength > sourceLength) { // reading beyond the end of the source vector
memset(source + sourceLength, 0, size_t(newLength - sourceLength)); // make sure the rest is zero
}
uint32_t pos1 = 0; // position in source vector
uint32_t pos2 = 0; // position in destination vector
// loop through mask register
for (pos2 = 0; pos2 < newLength; pos2 += elementSize) {
if (*(masksrc + pos2) & 1) { // check mask bit
// copy from pos1 in source to pos2 in destination
switch (elementSize) {
case 1: // int8
*(destination+pos2) = *(source+pos1);
break;
case 2: // int16
*(uint16_t*)(destination+pos2) = *(uint16_t*)(source+pos1);
break;
case 4: // int32, float
*(uint32_t*)(destination+pos2) = *(uint32_t*)(source+pos1);
break;
case 8: // int64, double
*(uint64_t*)(destination+pos2) = *(uint64_t*)(source+pos1);
break;
case 16: // int128, float128
*(uint64_t*)(destination+pos2) = *(uint64_t*)(source+pos1);
*(uint64_t*)(destination+pos2+8) = *(uint64_t*)(source+pos1+8);
break;
}
pos1 += elementSize;
}
else {
// mask is zero. insert zero
switch (elementSize) {
case 1: // int8
*(destination+pos2) = 0;
break;
case 2: // int16
*(uint16_t*)(destination+pos2) = 0;
break;
case 4: // int32, float
*(uint32_t*)(destination+pos2) = 0;
break;
case 8: // int64, double
*(uint64_t*)(destination+pos2) = 0;
break;
case 16: // int128, float128
*(uint64_t*)(destination+pos2) = 0;
*(uint64_t*)(destination+pos2+8) = 0;
break;
}
}
}
// set new length of destination vector
t->vectorLength[rd] = pos2;
t->vect = 4; // stop vector loop
t->running = 2; // don't save. result has already been saved
return 0;
}
static uint64_t broad_(CThread * t) {
// Broadcast first element of source vector into all elements of RD with specified length
uint8_t rlen; // g.p. register indicating length
uint64_t value; // value to broadcast
uint8_t rd = t->operands[0]; // destination vector
if (t->fInstr->format2 == 0x120) {
rlen = t->operands[5]; // RT = length
uint8_t rs = t->operands[4]; // source vector
value = t->readVectorElement(rs, 0); // first element of RS
}
else {
rlen = t->operands[4]; // first source operand = length
value = t->parm[2].q; // immediate operand
}
uint64_t destinationLength = t->registers[rlen]; // value of length register
if (destinationLength > t->MaxVectorLength) destinationLength = t->MaxVectorLength; // limit length
// set length of destination register, let vector loop continue to this length
t->vectorLength[rd] = t->vectorLengthR = (uint32_t)destinationLength;
return value;
}
static uint64_t bits2bool(CThread * t) {
// The lower n bits of RT are unpacked into a boolean vector RD with length RS
// with one bit in each element, where n = RS / OS.
uint8_t rd = t->operands[0]; // destination vector
uint8_t rt = t->operands[5]; // RT = source vector
uint8_t rs = t->operands[4]; // RS indicates length
SNum mask = t->parm[3]; // mask
uint8_t * source = (uint8_t*)t->vectors.buf() + rt*t->MaxVectorLength; // address of RT data
uint8_t * destination = (uint8_t*)t->vectors.buf() + rd*t->MaxVectorLength; // address of RD data
uint64_t destinationLength = t->registers[rs]; // value of RS = length of destination
uint8_t dsizelog = dataSizeTableLog[t->operandType]; // log2(elementsize)
if (destinationLength > t->MaxVectorLength) destinationLength = t->MaxVectorLength; // limit length
// set length of destination register
t->vectorLength[rd] = (uint32_t)destinationLength;
uint32_t num = (uint32_t)destinationLength >> dsizelog; // number of elements
destinationLength = num << dsizelog; // round down length to nearest multiple of element size
// number of bits in source
uint32_t srcnum = t->vectorLength[rt] * 8;
if (num < srcnum) num = srcnum; // limit to the number of bits in source
mask.q &= -(int64_t)2; // remove lower bit of mask. it will be replaced by source bit
// loop through bits
for (uint32_t i = 0; i < num; i++) {
uint8_t bit = (source[i / 8] >> (i & 7)) & 1; // extract single bit from source
switch (dsizelog) {
case 0: // int8
*destination = mask.b | bit; break;
case 1: // int16
*(uint16_t*)destination = mask.s | bit; break;
case 2: // int32
*(uint32_t*)destination = mask.i | bit; break;
case 3: // int64
*(uint64_t*)destination = mask.q | bit; break;
case 4: // int128
*(uint64_t*)destination = mask.q | bit;
*(uint64_t*)(destination+8) = mask.q | bit;
break;
}
destination += (uint64_t)1 << dsizelog;
}
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD
if ((t->returnType & 7) >= 5) t->returnType -= 3; // make return type integer
return 0;
}
static uint64_t shift_expand(CThread * t) {
// Shift vector RS up by RT bytes and extend the vector length by RT.
// The lower RT bytes of RD will be zero.
uint8_t rd = t->operands[0]; // destination vector
uint8_t rs = t->operands[4]; // RS = source vector
uint8_t rt = t->operands[5]; // RT indicates length
uint8_t * source = (uint8_t*)t->vectors.buf() + rs*t->MaxVectorLength; // address of RS data
uint8_t * destination = (uint8_t*)t->vectors.buf() + rd*t->MaxVectorLength; // address of RD data
uint64_t shiftCount = t->registers[rt]; // value of RT = shift count
if (shiftCount > t->MaxVectorLength) shiftCount = t->MaxVectorLength; // limit length
uint32_t sourceLength = t->vectorLength[rs]; // length of source vector
uint32_t destinationLength = sourceLength + (uint32_t)shiftCount; // length of destination vector
if (destinationLength > t->MaxVectorLength) destinationLength = t->MaxVectorLength; // limit length
// set length of destination vector
t->vectorLength[rd] = destinationLength;
// set lower part of destination to zero
memset(destination, 0, size_t(shiftCount));
// copy the rest from source
if (destinationLength > shiftCount) {
memmove(destination + shiftCount, source, size_t(destinationLength - shiftCount));
}
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD. It has already been saved
return 0;
}
static uint64_t shift_reduce(CThread * t) {
// Shift vector RS down RT bytes and reduce the length by RT.
// The lower RT bytes of RS are lost
uint8_t rd = t->operands[0]; // destination vector
uint8_t rs = t->operands[4]; // RS = source vector
uint8_t rt = t->operands[5]; // RT indicates length
uint8_t * source = (uint8_t*)t->vectors.buf() + rs*t->MaxVectorLength; // address of RS data
uint8_t * destination = (uint8_t*)t->vectors.buf() + rd*t->MaxVectorLength; // address of RD data
uint32_t sourceLength = t->vectorLength[rs]; // length of source vector
uint64_t shiftCount = t->registers[rt]; // value of RT = shift count
if (shiftCount > sourceLength) shiftCount = sourceLength; // limit length
uint32_t destinationLength = sourceLength - (uint32_t)shiftCount; // length of destination vector
t->vectorLength[rd] = destinationLength; // set length of destination vector
// copy data from source
if (destinationLength > 0) {
memmove(destination, source + shiftCount, destinationLength);
}
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD. It has already been saved
return 0;
}
static uint64_t shift_up(CThread * t) {
// Shift elements of vector RS up RT elements.
// The lower RT elements of RD will be zero, the upper RT elements of RS are lost.
uint8_t rd = t->operands[0]; // destination vector
uint8_t rs = t->operands[4]; // RS = source vector
uint8_t rt = t->operands[5]; // RT indicates length
uint8_t * source = (uint8_t*)t->vectors.buf() + rs * t->MaxVectorLength; // address of RS data
uint8_t * destination = (uint8_t*)t->vectors.buf() + rd * t->MaxVectorLength; // address of RD data
uint8_t dsizelog = dataSizeTableLog[t->operandType]; // log2(elementsize)
uint64_t shiftCount = t->registers[rt] << dsizelog; // value of TS = shift count, elements
if (shiftCount > t->MaxVectorLength) shiftCount = t->MaxVectorLength; // limit length
uint32_t sourceLength = t->vectorLength[rs]; // length of source vector
t->vectorLength[rd] = sourceLength; // set length of destination vector to the same as source vector
// copy from source
if (sourceLength > shiftCount) {
memmove(destination + shiftCount, source, size_t(sourceLength - shiftCount));
}
// set lower part of destination to zero
memset(destination, 0, size_t(shiftCount));
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD. It has already been saved
return 0;
}
static uint64_t shift_down(CThread * t) {
// Shift elements of vector RS down RT elements.
// The upper RT elements of RD will be zero, the lower RT elements of RS are lost.
uint8_t rd = t->operands[0]; // destination vector
uint8_t rs = t->operands[4]; // RS = source vector
uint8_t rt = t->operands[5]; // RT indicates length
uint8_t * source = (uint8_t*)t->vectors.buf() + rs*t->MaxVectorLength; // address of RS data
uint8_t * destination = (uint8_t*)t->vectors.buf() + rd*t->MaxVectorLength; // address of RD data
uint32_t sourceLength = t->vectorLength[rs]; // length of source vector
uint8_t dsizelog = dataSizeTableLog[t->operandType]; // log2(elementsize)
uint64_t shiftCount = t->registers[rt] << dsizelog; // value of RT = shift count, elements
if (shiftCount > sourceLength) shiftCount = sourceLength; // limit length
t->vectorLength[rd] = sourceLength; // set length of destination vector
if (sourceLength > shiftCount) { // copy data from source
memmove(destination, source + shiftCount, size_t(sourceLength - shiftCount));
}
if (shiftCount > 0) { // set the rest to zero
memset(destination + sourceLength - shiftCount, 0, size_t(shiftCount));
}
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD. It has already been saved
return 0;
}
/*
static uint64_t rotate_up (CThread * t) {
// Rotate vector RT up one element.
uint8_t rd = t->operands[0]; // destination vector
uint8_t rt = t->operands[5]; // RT = source vector
//uint8_t rs = t->operands[4]; // RS indicates length
int8_t * source = t->vectors.buf() + rt*t->MaxVectorLength; // address of RT data
int8_t * destination = t->vectors.buf() + rd*t->MaxVectorLength; // address of RD data
//uint64_t length = t->registers[rs]; // value of RS = vector length
//if (length > t->MaxVectorLength) length = t->MaxVectorLength; // limit length
uint32_t sourceLength = t->vectorLength[rt]; // length of source vector
uint32_t length = sourceLength;
if (rd == rt) {
// source and destination are the same. Make a temporary copy of source to avoid overwriting
memcpy(t->tempBuffer, source, length);
source = t->tempBuffer;
}
if (length > sourceLength) { // reading beyond the end of the source vector. make sure the rest is zero
memset(source + sourceLength, 0, size_t(length - sourceLength));
}
uint32_t elementSize = dataSizeTable[t->operandType]; // size of each element
if (elementSize > length) elementSize = (uint32_t)length;
t->vectorLength[rd] = (uint32_t)length; // set length of destination vector
memcpy(destination, source + length - elementSize, elementSize); // copy top element to bottom
memcpy(destination + elementSize, source, size_t(length - elementSize)); // copy the rest
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD. It has already been saved
return 0;
}
static uint64_t rotate_down (CThread * t) {
// Rotate vector RT down one element.
uint8_t rd = t->operands[0]; // destination vector
uint8_t rt = t->operands[5]; // RT = source vector
//uint8_t rs = t->operands[4]; // RS indicates length
int8_t * source = t->vectors.buf() + rt*t->MaxVectorLength; // address of RT data
int8_t * destination = t->vectors.buf() + rd*t->MaxVectorLength; // address of RD data
//uint64_t length = t->registers[rs]; // value of RS = vector length
uint32_t sourceLength = t->vectorLength[rt]; // length of source vector
uint32_t length = sourceLength;
//if (length > t->MaxVectorLength) length = t->MaxVectorLength; // limit length
if (rd == rt) {
// source and destination are the same. Make a temporary copy of source to avoid overwriting
memcpy(t->tempBuffer, source, length);
source = t->tempBuffer;
}
if (length > sourceLength) { // reading beyond the end of the source vector. make sure the rest is zero
memset(source + sourceLength, 0, size_t(length - sourceLength));
}
uint32_t elementSize = dataSizeTable[t->operandType]; // size of each element
if (elementSize > length) elementSize = (uint32_t)length;
t->vectorLength[rd] = (uint32_t)length; // set length of destination vector
memcpy(destination, source + elementSize, size_t(length - elementSize)); // copy down
memcpy(destination + length - elementSize, source, elementSize); // copy the bottom element to top
t->vect = 4; // stop vector loop
t->running = 2; // don't save RD. It has already been saved