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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
|
/*
The SIMD support package.
SIMD (Single Instruction Multiple Data), is a CPU hardware feature that
introduce special registers and instructions which operate on multiple units
of data at the same time, which enables faster data processing for
applications with heavy computational workloads.
In Odin SIMD is exposed via a special kinds of arrays, called the *SIMD
vectors*. The types of SIMD vectors is written as `#simd [N]T`, where N is a
power of two, and T could be any basic type (integers, floats, etc.). The
documentation of this package will call *SIMD vectors* just *vectors*.
SIMD vectors consist of elements, called *scalar values*, or
*scalars*, each occupying a *lane* of the SIMD vector. In the type declaration,
`N` specifies the amount of lanes, or values, that a vector stores.
This package implements procedures for working with vectors.
*/
package simd
import "base:builtin"
import "base:intrinsics"
import "base:runtime"
/*
Check if SIMD is software-emulated on a target platform.
This value is `true`, when the compile-time target has the hardware support for
at least 128-bit (or wider) SIMD. If the compile-time target lacks the hardware support
for 128-bit SIMD, this value is `false`, and all SIMD operations will likely be
emulated.
*/
HAS_HARDWARE_SIMD :: runtime.HAS_HARDWARE_SIMD
/*
Vector of 16 `u8` lanes (128 bits).
*/
u8x16 :: #simd[16]u8
/*
Vector of 16 `i8` lanes (128 bits).
*/
i8x16 :: #simd[16]i8
/*
Vector of 8 `u16` lanes (128 bits).
*/
u16x8 :: #simd[8]u16
/*
Vector of 8 `i16` lanes (128 bits).
*/
i16x8 :: #simd[8]i16
/*
Vector of 4 `u32` lanes (128 bits).
*/
u32x4 :: #simd[4]u32
/*
Vector of 4 `i32` lanes (128 bits).
*/
i32x4 :: #simd[4]i32
/*
Vector of 2 `u64` lanes (128 bits).
*/
u64x2 :: #simd[2]u64
/*
Vector of 2 `i64` lanes (128 bits).
*/
i64x2 :: #simd[2]i64
/*
Vector of 4 `f32` lanes (128 bits).
*/
f32x4 :: #simd[4]f32
/*
Vector of 2 `f64` lanes (128 bits).
*/
f64x2 :: #simd[2]f64
/*
Vector of 16 `bool` lanes (128 bits).
*/
boolx16 :: #simd[16]bool
/*
Vector of 16 `b8` lanes (128 bits).
*/
b8x16 :: #simd[16]b8
/*
Vector of 8 `b16` lanes (128 bits).
*/
b16x8 :: #simd[8]b16
/*
Vector of 4 `b32` lanes (128 bits).
*/
b32x4 :: #simd[4]b32
/*
Vector of 2 `b64` lanes (128 bits).
*/
b64x2 :: #simd[2]b64
/*
Vector of 32 `u8` lanes (256 bits).
*/
u8x32 :: #simd[32]u8
/*
Vector of 32 `i8` lanes (256 bits).
*/
i8x32 :: #simd[32]i8
/*
Vector of 16 `u16` lanes (256 bits).
*/
u16x16 :: #simd[16]u16
/*
Vector of 16 `i16` lanes (256 bits).
*/
i16x16 :: #simd[16]i16
/*
Vector of 8 `u32` lanes (256 bits).
*/
u32x8 :: #simd[8]u32
/*
Vector of 8 `i32` lanes (256 bits).
*/
i32x8 :: #simd[8]i32
/*
Vector of 4 `u64` lanes (256 bits).
*/
u64x4 :: #simd[4]u64
/*
Vector of 4 `i64` lanes (256 bits).
*/
i64x4 :: #simd[4]i64
/*
Vector of 8 `f32` lanes (256 bits).
*/
f32x8 :: #simd[8]f32
/*
Vector of 4 `f64` lanes (256 bits).
*/
f64x4 :: #simd[4]f64
/*
Vector of 32 `bool` lanes (256 bits).
*/
boolx32 :: #simd[32]bool
/*
Vector of 32 `b8` lanes (256 bits).
*/
b8x32 :: #simd[32]b8
/*
Vector of 16 `b16` lanes (256 bits).
*/
b16x16 :: #simd[16]b16
/*
Vector of 8 `b32` lanes (256 bits).
*/
b32x8 :: #simd[8]b32
/*
Vector of 4 `b64` lanes (256 bits).
*/
b64x4 :: #simd[4]b64
/*
Vector of 64 `u8` lanes (512 bits).
*/
u8x64 :: #simd[64]u8
/*
Vector of 64 `i8` lanes (512 bits).
*/
i8x64 :: #simd[64]i8
/*
Vector of 32 `u16` lanes (512 bits).
*/
u16x32 :: #simd[32]u16
/*
Vector of 32 `i16` lanes (512 bits).
*/
i16x32 :: #simd[32]i16
/*
Vector of 16 `u32` lanes (512 bits).
*/
u32x16 :: #simd[16]u32
/*
Vector of 16 `i32` lanes (512 bits).
*/
i32x16 :: #simd[16]i32
/*
Vector of 8 `u64` lanes (512 bits).
*/
u64x8 :: #simd[8]u64
/*
Vector of 8 `i64` lanes (512 bits).
*/
i64x8 :: #simd[8]i64
/*
Vector of 16 `f32` lanes (512 bits).
*/
f32x16 :: #simd[16]f32
/*
Vector of 8 `f64` lanes (512 bits).
*/
f64x8 :: #simd[8]f64
/*
Vector of 64 `bool` lanes (512 bits).
*/
boolx64 :: #simd[64]bool
/*
Vector of 64 `b8` lanes (512 bits).
*/
b8x64 :: #simd[64]b8
/*
Vector of 32 `b16` lanes (512 bits).
*/
b16x32 :: #simd[32]b16
/*
Vector of 16 `b32` lanes (512 bits).
*/
b32x16 :: #simd[16]b32
/*
Vector of 8 `b64` lanes (512 bits).
*/
b64x8 :: #simd[8]b64
/*
Add SIMD vectors.
This procedure returns a vector, where each lane holds the sum of the
corresponding `a` and `b` vectors' lanes.
Inputs:
- `a`: An integer or a float vector.
- `b`: An integer or a float vector.
Returns:
- A vector that is the sum of two input vectors.
**Operation**:
for i in 0 ..< len(res) {
res[i] = a[i] + b[i]
}
return res
Example:
+-----+-----+-----+-----+
a: | 0 | 1 | 2 | 3 |
+-----+-----+-----+-----+
+-----+-----+-----+-----+
b: | 0 | 1 | 2 | -1 |
+-----+-----+-----+-----+
res:
+-----+-----+-----+-----+
| 0 | 2 | 4 | 2 |
+-----+-----+-----+-----+
*/
add :: intrinsics.simd_add
/*
Subtract SIMD vectors.
This procedure returns a vector, where each lane holds the difference between
the corresponding lanes of the vectors `a` and `b`. The lanes from the vector
`b` are subtracted from the corresponding lanes of the vector `a`.
Inputs:
- `a`: An integer or a float vector to subtract from.
- `b`: An integer or a float vector.
Returns:
- A vector that is the difference of two vectors, `a` - `b`.
**Operation**:
for i in 0 ..< len(res) {
res[i] = a[i] - b[i]
}
return res
Example:
+-----+-----+-----+-----+
a: | 2 | 2 | 2 | 2 |
+-----+-----+-----+-----+
+-----+-----+-----+-----+
b: | 0 | 1 | 2 | 3 |
+-----+-----+-----+-----+
res:
+-----+-----+-----+-----+
| 2 | 1 | 0 | -1 |
+-----+-----+-----+-----+
*/
sub :: intrinsics.simd_sub
/*
Multiply (component-wise) SIMD vectors.
This procedure returns a vector, where each lane holds the product of the
corresponding lanes of the vectors `a` and `b`.
Inputs:
- `a`: An integer or a float vector.
- `b`: An integer or a float vector.
Returns:
- A vector that is the product of two vectors.
**Operation**:
for i in 0 ..< len(res) {
res[i] = a[i] * b[i]
}
return res
Example:
+-----+-----+-----+-----+
a: | 2 | 2 | 2 | 2 |
+-----+-----+-----+-----+
+-----+-----+-----+-----+
b: | 0 | -1 | 2 | -3 |
+-----+-----+-----+-----+
res:
+-----+-----+-----+-----+
| 0 | -2 | 4 | -6 |
+-----+-----+-----+-----+
*/
mul :: intrinsics.simd_mul
/*
Divide SIMD vectors.
This procedure returns a vector, where each lane holds the quotient (result
of division) between the corresponding lanes of the vectors `a` and `b`. Each
lane of the vector `a` is divided by the corresponding lane of the vector `b`.
This operation performs a standard floating-point division for each lane.
Inputs:
- `a`: A float vector.
- `b`: A float vector to divide by.
Returns:
- A vector that is the quotient of two vectors, `a` / `b`.
**Operation**:
for i in 0 ..< len(res) {
res[i] = a[i] / b[i]
}
return res
Example:
+-----+-----+-----+-----+
a: | 2 | 2 | 2 | 2 |
+-----+-----+-----+-----+
+-----+-----+-----+-----+
b: | 0 | -1 | 2 | -3 |
+-----+-----+-----+-----+
res:
+-----+-----+-----+------+
| +∞ | -2 | 1 | -2/3 |
+-----+-----+-----+------+
*/
div :: intrinsics.simd_div
/*
Shift left lanes of a vector.
This procedure returns a vector, such that each lane holds the result of a
shift-left (aka shift-up) operation of the corresponding lane from vector `a` by the shift
amount from the corresponding lane of the vector `b`.
If the shift amount is greater than the bit-width of a lane, the result is `0`
in the corresponding positions of the result.
Inputs:
- `a`: An integer vector of values to shift.
- `b`: An unsigned integer vector of the shift amounts.
Result:
- A vector, where each lane is the lane from `a` shifted left by the amount
specified in the corresponding lane of the vector `b`.
**Operation**:
for i in 0 ..< len(res) {
if b[i] < 8*size_of(a[i]) {
res[i] = a[i] << b[i]
} else {
res[i] = 0
}
}
return res
Example:
// An example for a 4-lane 8-bit signed integer vector `a`.
+-------+-------+-------+-------+
a: | 0x11 | 0x55 | 0x03 | 0xff |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 2 | 1 | 33 | 1 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+--------+
| 0x44 | 0xaa | 0 | 0xfe |
+-------+-------+-------+--------+
*/
shl :: intrinsics.simd_shl
/*
Shift right lanes of a vector.
This procedure returns a vector, such that each lane holds the result of a
shift-right (aka shift-down) operation, of lane from the vector `a` by the shift
amount from the corresponding lane of the vector `b`.
If the shift amount is greater than the bit-width of a lane, the result is `0`
in the corresponding positions of the result.
If the first vector is a vector of signed integers, the arithmetic shift
operation is performed. Otherwise, if the first vector is a vector of unsigned
integers, a logical shift is performed.
Inputs:
- `a`: An integer vector of values to shift.
- `b`: An unsigned integer vector of the shift amounts.
Result:
- A vector, where each lane is the lane from `a` shifted right by the amount
specified in the corresponding lane of the vector `b`.
**Operation**:
for i in 0 ..< len(res) {
if b[i] < 8*size_of(a[i]) {
res[i] = a[i] >> b[i]
} else {
res[i] = 0
}
}
return res
Example:
// An example for a 4-lane 8-bit signed integer vector `a`.
+-------+-------+-------+-------+
a: | 0x11 | 0x55 | 0x03 | 0xff |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 2 | 1 | 33 | 1 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+--------+
| 0x04 | 0x2a | 0 | 0xff |
+-------+-------+-------+--------+
*/
shr :: intrinsics.simd_shr
/*
Shift left lanes of a vector (masked).
This procedure returns a vector, such that each lane holds the result of a
shift-left (aka shift-up) operation, of lane from the vector `a` by the shift
amount from the corresponding lane of the vector `b`.
The shift amount is wrapped (masked) to the bit-width of the lane.
Inputs:
- `a`: An integer vector of values to shift.
- `b`: An unsigned integer vector of the shift amounts.
Result:
- A vector, where each lane is the lane from `a` shifted left by the amount
specified in the corresponding lane of the vector `b`.
**Operation**:
for i in 0 ..< len(res) {
mask := 8*size_of(a[i]) - 1
res[i] = a[i] << (b[i] & mask)
}
return res
Example:
// An example for a 4-lane vector `a` of 8-bit signed integers.
+-------+-------+-------+-------+
a: | 0x11 | 0x55 | 0x03 | 0xff |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 2 | 1 | 33 | 1 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+--------+
| 0x44 | 0xaa | 0x06 | 0xfe |
+-------+-------+-------+--------+
*/
shl_masked :: intrinsics.simd_shl_masked
/*
Shift right lanes of a vector (masked).
This procedure returns a vector, such that each lane holds the result of a
shift-right (aka shift-down) operation, of lane from the vector `a` by the shift
amount from the corresponding lane of the vector `b`.
The shift amount is wrapped (masked) to the bit-width of the lane.
If the first vector is a vector of signed integers, the arithmetic shift
operation is performed. Otherwise, if the first vector is a vector of unsigned
integers, a logical shift is performed.
Inputs:
- `a`: An integer vector of values to shift.
- `b`: An unsigned integer vector of the shift amounts.
Result:
- A vector, where each lane is the lane from `a` shifted right by the amount
specified in the corresponding lane of the vector `b`.
**Operation**:
for i in 0 ..< len(res) {
mask := 8*size_of(a[i]) - 1
res[i] = a[i] >> (b[i] & mask)
}
return res
Example:
// An example for a 4-lane vector `a` of 8-bit signed integers.
+-------+-------+-------+-------+
a: | 0x11 | 0x55 | 0x03 | 0xff |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 2 | 1 | 33 | 1 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+--------+
| 0x04 | 0x2a | 0x01 | 0xff |
+-------+-------+-------+--------+
*/
shr_masked :: intrinsics.simd_shr_masked
/*
Saturated addition of SIMD vectors.
The *saturated sum* is a just like a normal sum, except the treatment of the
result upon overflow or underflow is different. In saturated operations, the
result is not wrapped to the bit-width of the lane, and instead is kept clamped
between the minimum and the maximum values of the lane type.
This procedure returns a vector where each lane is the saturated sum of the
corresponding lanes of vectors `a` and `b`.
Inputs:
- `a`: An integer vector.
- `b`: An integer vector.
Returns:
- The saturated sum of the two vectors.
**Operation**:
for i in 0 ..< len(res) {
switch {
case b[i] >= max(type_of(a[i])) - a[i]: // (overflow of a[i])
res[i] = max(type_of(a[i]))
case b[i] <= min(type_of(a[i])) - a[i]: // (underflow of a[i])
res[i] = min(type_of(a[i]))
} else {
res[i] = a[i] + b[i]
}
}
return res
Example:
// An example for a 4-lane vector `a` of 8-bit signed integers.
+-----+-----+-----+-----+
a: | 0 | 255 | 2 | 3 |
+-----+-----+-----+-----+
+-----+-----+-----+-----+
b: | 1 | 3 | 2 | -1 |
+-----+-----+-----+-----+
res:
+-----+-----+-----+-----+
| 1 | 255 | 4 | 2 |
+-----+-----+-----+-----+
*/
saturating_add :: intrinsics.simd_saturating_add
/*
Saturated subtraction of 2 lanes of vectors.
The *saturated difference* is a just like a normal difference, except the treatment of the
result upon overflow or underflow is different. In saturated operations, the
result is not wrapped to the bit-width of the lane, and instead is kept clamped
between the minimum and the maximum values of the lane type.
This procedure returns a vector where each lane is the saturated difference of
the corresponding lanes of vectors `a` and `b`.
Inputs:
- `a`: An integer vector to subtract from.
- `b`: An integer vector.
Returns:
- The saturated difference of the two vectors.
**Operation**:
for i in 0 ..< len(res) {
switch {
case b[i] >= max(type_of(a[i])) + a[i]: // (overflow of a[i])
res[i] = max(type_of(a[i]))
case b[i] <= min(type_of(a[i])) + a[i]: // (underflow of a[i])
res[i] = min(type_of(a[i]))
} else {
res[i] = a[i] - b[i]
}
}
return res
Example:
// An example for a 4-lane vector `a` of 8-bit signed integers.
+-----+-----+-----+-----+
a: | 0 | 255 | 2 | 3 |
+-----+-----+-----+-----+
+-----+-----+-----+-----+
b: | 3 | 3 | 2 | -1 |
+-----+-----+-----+-----+
res:
+-----+-----+-----+-----+
| 0 | 252 | 0 | 4 |
+-----+-----+-----+-----+
*/
saturating_sub :: intrinsics.simd_saturating_sub
/*
Bitwise AND of vectors.
This procedure returns a vector, such that each lane has the result of a bitwise
AND operation between the corresponding lanes of the vectors `a` and `b`.
Inputs:
- `a`: An integer or a boolean vector.
- `b`: An integer or a boolean vector.
Returns:
- A vector that is the result of the bitwise AND operation between two vectors.
**Operation**:
for i in 0 ..< len(res) {
res[i] = a[i] & b[i]
}
return res
Example:
+------+------+------+------+
a: | 0x11 | 0x33 | 0x55 | 0xaa |
+------+------+------+------+
+------+------+------+------+
b: | 0xff | 0xf0 | 0x0f | 0x00 |
+------+------+------+------+
res:
+------+------+------+------+
| 0x11 | 0x30 | 0x05 | 0x00 |
+------+------+------+------+
*/
bit_and :: intrinsics.simd_bit_and
/*
Bitwise OR of vectors.
This procedure returns a vector, such that each lane has the result of a bitwise
OR operation between the corresponding lanes of the vectors `a` and `b`.
Inputs:
- `a`: An integer or a boolean vector.
- `b`: An integer or a boolean vector.
Returns:
- A vector that is the result of the bitwise OR operation between two vectors.
**Operation**:
for i in 0 ..< len(res) {
res[i] = a[i] | b[i]
}
return res
Example:
+------+------+------+------+
a: | 0x11 | 0x33 | 0x55 | 0xaa |
+------+------+------+------+
+------+------+------+------+
b: | 0xff | 0xf0 | 0x0f | 0x00 |
+------+------+------+------+
res:
+------+------+------+------+
| 0xff | 0xf3 | 0x5f | 0xaa |
+------+------+------+------+
*/
bit_or :: intrinsics.simd_bit_or
/*
Bitwise XOR of vectors.
This procedure returns a vector, such that each lane has the result of a bitwise
XOR operation between the corresponding lanes of the vectors `a` and `b`.
Inputs:
- `a`: An integer or a boolean vector.
- `b`: An integer or a boolean vector.
Returns:
- A vector that is the result of the bitwise XOR operation between two vectors.
**Operation**:
for i in 0 ..< len(res) {
res[i] = a[i] ~ b[i]
}
return res
Example:
+------+------+------+------+
a: | 0x11 | 0x33 | 0x55 | 0xaa |
+------+------+------+------+
+------+------+------+------+
b: | 0xff | 0xf0 | 0x0f | 0x00 |
+------+------+------+------+
res:
+------+------+------+------+
| 0xee | 0xc3 | 0x5a | 0xaa |
+------+------+------+------+
*/
bit_xor :: intrinsics.simd_bit_xor
/*
Bitwise AND NOT of vectors.
This procedure returns a vector, such that each lane has the result of a bitwise
AND NOT operation between the corresponding lanes of the vectors `a` and `b`.
Inputs:
- `a`: An integer or a boolean vector.
- `b`: An integer or a boolean vector.
Returns:
- A vector that is the result of the bitwise AND NOT operation between two vectors.
**Operation**:
for i in 0 ..< len(res) {
res[i] = a[i] &~ b[i]
}
return res
Example:
+------+------+------+------+
a: | 0x11 | 0x33 | 0x55 | 0xaa |
+------+------+------+------+
+------+------+------+------+
b: | 0xff | 0xf0 | 0x0f | 0x00 |
+------+------+------+------+
res:
+------+------+------+------+
| 0x00 | 0x03 | 0x50 | 0xaa |
+------+------+------+------+
*/
bit_and_not :: intrinsics.simd_bit_and_not
/*
Negation of a SIMD vector.
This procedure returns a vector where each lane is the negation of the
corresponding lane in the vector `a`.
Inputs:
- `a`: An integer or a float vector to negate.
Returns:
- The negated version of the vector `a`.
**Operation**:
for i in 0 ..< len(res) {
res[i] = -a[i]
}
return res
Example:
+------+------+------+------+
a: | 0 | 1 | 2 | 3 |
+------+------+------+------+
res:
+------+------+------+------+
| 0 | -1 | -2 | -3 |
+------+------+------+------+
*/
neg :: intrinsics.simd_neg
/*
Absolute value of a SIMD vector.
This procedure returns a vector where each lane has the absolute value of the
corresponding lane in the vector `a`.
Inputs:
- `a`: An integer or a float vector to negate
Returns:
- The absolute value of a vector.
**Operation**:
for i in 0 ..< len(res) {
switch {
case a[i] < 0: res[i] = -a[i]
case a[i] > 0: res[i] = a[i]
case a[i] == 0: res[i] = 0
}
}
return res
Example:
+------+------+------+------+
a: | 0 | -1 | 2 | -3 |
+------+------+------+------+
res:
+------+------+------+------+
| 0 | 1 | 2 | 3 |
+------+------+------+------+
*/
abs :: intrinsics.simd_abs
/*
Minimum of each lane of vectors.
This procedure returns a vector, such that each lane has the minimum value
between the corresponding lanes in vectors `a` and `b`.
Inputs:
- `a`: An integer or a float vector.
- `b`: An integer or a float vector.
Returns:
- A vector containing with minimum values from corresponding lanes of `a` and `b`.
**Operation**:
for i in 0 ..< len(res) {
if a[i] < b[i] {
res[i] = a[i]
} else {
res[i] = b[i]
}
}
return res
Example:
+-----+-----+-----+-----+
a: | 0 | 1 | 2 | 3 |
+-----+-----+-----+-----+
+-----+-----+-----+-----+
b: | 0 | 2 | 1 | -1 |
+-----+-----+-----+-----+
res:
+-----+-----+-----+-----+
| 0 | 1 | 1 | -1 |
+-----+-----+-----+-----+
*/
min :: intrinsics.simd_min
/*
Maximum of each lane of vectors.
This procedure returns a vector, such that each lane has the maximum value
between the corresponding lanes in vectors `a` and `b`.
Inputs:
- `a`: An integer or a float vector.
- `b`: An integer or a float vector.
Returns:
- A vector containing with maximum values from corresponding lanes of `a` and `b`.
**Operation**:
for i in 0 ..< len(res) {
if a[i] > b[i] {
res[i] = a[i]
} else {
res[i] = b[i]
}
}
return res
Example:
+-----+-----+-----+-----+
a: | 0 | 1 | 2 | 3 |
+-----+-----+-----+-----+
+-----+-----+-----+-----+
b: | 0 | 2 | 1 | -1 |
+-----+-----+-----+-----+
res:
+-----+-----+-----+-----+
| 0 | 2 | 2 | 3 |
+-----+-----+-----+-----+
*/
max :: intrinsics.simd_max
/*
Clamp lanes of vector.
This procedure returns a vector, where each lane is the result of the
clamping of the lane from the vector `v` between the values in the corresponding
lanes of vectors `min` and `max`.
Inputs:
- `v`: An integer or a float vector with values to be clamped.
- `min`: An integer or a float vector with minimum bounds.
- `max`: An integer or a float vectoe with maximum bounds.
Returns:
- A vector containing clamped values in each lane.
**Operation**:
for i in 0 ..< len(res) {
val := v[i]
switch {
case val < min: val = min
case val > max: val = max
}
res[i] = val
}
return res
Example:
+-------+-------+-------+-------+
v: | -1 | 0.3 | 1.2 | 1 |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
min: | 0 | 0 | 0 | 0 |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
max: | 1 | 1 | 1 | 1 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+-------+
| 0 | 0.3 | 1 | 1 |
+-------+-------+-------+-------+
*/
clamp :: intrinsics.simd_clamp
/*
Check if lanes of vectors are equal.
This procedure checks each pair of lanes from vectors `a` and `b` for whether
they are equal, and if they are, the corresponding lane of the result vector
will have a value with all bits set (`0xff..ff`). Otherwise the lane of the
result vector will have the value `0`.
Inputs:
- `a`: An integer, a float or a boolean vector.
- `b`: An integer, a float or a boolean vector.
Returns:
- A vector of unsigned integers of the same size as the input vector's lanes,
containing the comparison results for each lane.
**Operation**:
for i in 0 ..< len(res) {
if a[i] == b[i] {
res[i] = max(T)
} else {
res[i] = 0
}
}
return res
Example:
+-------+-------+-------+-------+
a: | 0 | 1 | 2 | 3 |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 0 | 2 | 2 | 2 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+-------+
| 0xff | 0x00 | 0xff | 0x00 |
+-------+-------+-------+-------+
*/
lanes_eq :: intrinsics.simd_lanes_eq
/*
Check if lanes of vectors are not equal.
This procedure checks each pair of lanes from vectors `a` and `b` for whether
they are not equal, and if they are, the corresponding lane of the result
vector will have a value with all bits set (`0xff..ff`). Otherwise the lane of
the result vector will have the value `0`.
Inputs:
- `a`: An integer, a float or a boolean vector.
- `b`: An integer, a float or a boolean vector.
Returns:
- A vector of unsigned integers of the same size as the input vector's lanes,
containing the comparison results for each lane.
**Operation**:
for i in 0 ..< len(res) {
if a[i] != b[i] {
res[i] = unsigned(-1)
} else {
res[i] = 0
}
}
return res
Example:
+-------+-------+-------+-------+
a: | 0 | 1 | 2 | 3 |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 0 | 2 | 2 | 2 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+-------+
| 0x00 | 0xff | 0x00 | 0xff |
+-------+-------+-------+-------+
*/
lanes_ne :: intrinsics.simd_lanes_ne
/*
Check if lanes of a vector are less than another.
This procedure checks each pair of lanes from vectors `a` and `b` for whether
the lane of `a` is less than the lane of `b`, and if so, the corresponding lane
of the result vector will have a value with all bits set (`0xff..ff`). Otherwise
the lane of the result vector will have the value `0`.
Inputs:
- `a`: An integer or a float vector.
- `b`: An integer or a float vector.
Returns:
- A vector of unsigned integers of the same size as the input vector's lanes,
containing the comparison results for each lane.
**Operation**:
for i in 0 ..< len(res) {
if a[i] < b[i] {
res[i] = unsigned(-1)
} else {
res[i] = 0
}
}
return res
Example:
+-------+-------+-------+-------+
a: | 0 | 1 | 2 | 3 |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 0 | 2 | 2 | 2 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+-------+
r: | 0x00 | 0xff | 0x00 | 0x00 |
+-------+-------+-------+-------+
*/
lanes_lt :: intrinsics.simd_lanes_lt
/*
Check if lanes of a vector are less than or equal than another.
SIMD vector.
This procedure checks each pair of lanes from vectors `a` and `b` for whether the
lane of `a` is less than or equal to the lane of `b`, and if so, the
corresponding lane of the result vector will have a value with all bits set
(`0xff..ff`). Otherwise the lane of the result vector will have the value `0`.
Inputs:
- `a`: An integer or a float vector.
- `b`: An integer or a float vector.
Returns:
- A vector of unsigned integers of the same size as the input vector's lanes,
containing the comparison results for each lane.
**Operation**:
for i in 0 ..< len(res) {
if a[i] <= b[i] {
res[i] = unsigned(-1)
} else {
res[i] = 0
}
}
return res
Example:
+-------+-------+-------+-------+
a: | 0 | 1 | 2 | 3 |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 0 | 2 | 2 | 2 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+-------+
| 0xff | 0xff | 0xff | 0x00 |
+-------+-------+-------+-------+
*/
lanes_le :: intrinsics.simd_lanes_le
/*
Check if lanes of a vector are greater than another.
vector.
This procedure checks each pair of lanes from vectors `a` and `b` for whether the
lane of `a` is greater than to the lane of `b`, and if so, the corresponding
lane of the result vector will have a value with all bits set (`0xff..ff`).
Otherwise the lane of the result vector will have the value `0`.
Inputs:
- `a`: An integer or a float vector.
- `b`: An integer or a float vector.
Returns:
- A vector of unsigned integers of the same size as the input vector's lanes,
containing the comparison results for each lane.
**Operation**:
for i in 0 ..< len(res) {
if a[i] > b[i] {
res[i] = unsigned(-1)
} else {
res[i] = 0
}
}
return res
Example:
+-------+-------+-------+-------+
a: | 0 | 1 | 2 | 3 |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 0 | 2 | 2 | 2 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+-------+
| 0x00 | 0x00 | 0x00 | 0xff |
+-------+-------+-------+-------+
*/
lanes_gt :: intrinsics.simd_lanes_gt
/*
Check if lanes of a vector are greater than or equal than another.
SIMD vector.
This procedure checks each pair of lanes from vectors `a` and `b` for whether the
lane of `a` is greater than or equal to the lane of `b`, and if so, the
corresponding lane of the result vector will have a value with all bits set
(`0xff..ff`). Otherwise the lane of the result vector will have the value `0`.
Inputs:
- `a`: An integer or a float vector.
- `b`: An integer or a float vector.
Returns:
- A vector of unsigned integers of the same size as the input vector's lanes,
containing the comparison results for each lane.
**Operation**:
for i in 0 ..< len(res) {
if a[i] >= b[i] {
res[i] = unsigned(-1)
} else {
res[i] = 0
}
}
return res
Example:
+-------+-------+-------+-------+
a: | 0 | 1 | 2 | 3 |
+-------+-------+-------+-------+
+-------+-------+-------+-------+
b: | 0 | 2 | 2 | 2 |
+-------+-------+-------+-------+
res:
+-------+-------+-------+-------+
| 0xff | 0x00 | 0xff | 0xff |
+-------+-------+-------+-------+
*/
lanes_ge :: intrinsics.simd_lanes_ge
/*
Perform a gather load into a vector.
A *gather* operation is memory load operation, that loads values from an vector
of addresses into a single value vector. This can be used to achieve the
following results:
- Accessing every N'th element of an array (strided access)
- Access of elements according to some computed offsets (indexed access).
- Access of elements in a different order (shuffling access).
When used alongside other SIMD procedures in order to compute the offsets
for the `ptr` and `mask` parameters.
Inputs:
- `ptr`: A vector of memory locations. Each pointer points to a single value,
of a SIMD vector's lane type that will be loaded into the vector. Pointer
in this vector can be `nil` or any other invalid value, if the corresponding
value in the `mask` parameter is zero.
- `val`: A vector of values that will be used at corresponding positions
of the result vector, if the corresponding memory location has been
masked out.
- `mask`: A vector of booleans or unsigned integers that determines which memory
locations to read from. If the value at an index has the value true
(lowest bit set), the value at that index will be loaded into the result
vector from the corresponding memory location in the `ptr` vector. Otherwise
the value will be loaded from the `val` vector.
Returns:
- A vector with all values from unmasked indices
loaded from the pointer vector `ptr`, and all values from masked indices loaded
from the value vector `val`.
**Operation**:
for i in 0 ..< len(res) {
if mask[i]&1 == 1 {
res[i] = ptr[i]^
} else {
res[i] = val[i]
}
}
return res
Example:
// Example below loads 2 lanes of values from 2 lanes of float vectors, `v1` and
// `v2`. From each of these vectors we're loading the second value, into the first
// and the third position of the result vector.
// Therefore the `ptrs` argument is initialized such that the first and the third
// value are the addresses of the values that we want to load into the result
// vector, and we'll fill in `nil` for the rest of them. To prevent CPU from
// dereferencing those `nil` addresses we provide the mask that only allows us
// to load valid positions of the `ptrs` array, and the array of defaults which
// will have `127` in each position as the default value.
import "core:fmt"
import "core:simd"
simd_gather_example :: proc() {
v1 := [4] f32 {1, 2, 3, 4};
v2 := [4] f32 {9, 10,11,12};
ptrs := #simd [4]rawptr { &v1[1], nil, &v2[1], nil }
mask := #simd [4]bool { true, false, true, false }
defaults := #simd [4]f32 { 0x7f, 0x7f, 0x7f, 0x7f }
res := simd.gather(ptrs, defaults, mask)
fmt.println(res)
}
Output:
<2, 127, 10, 127>
The first and the third positions came from the `ptrs` array, and the other
2 lanes of from the default vector. The graphic below shows how the values of
the result are decided based on the mask:
+-------------------------------+
mask: | 1 | 0 | 1 | 0 |
+-------------------------------+
| | | `----------------------------.
| | | |
| `---- | ------------------------. |
v v v v
+-------------------------------+ +-------------------+
ptrs: | &m0 | nil | &m2 | nil | vals: | d0 | d1 | d2 | d3 |
+-------------------------------+ +-------------------+
| | | |
| .--- | -------------------------' |
| | | ,-------------------------'
v v v v
+-------------------------------+
result: | m0 | d1 | m2 | d3 |
+-------------------------------+
*/
gather :: intrinsics.simd_gather
/*
Perform a scatter store from a vector.
A *scatter* operation is a memory store operation that stores values from a
vector into multiple memory locations. This operation is effectively the
opposite of the *gather* operation.
Inputs:
- `ptr`: A vector of memory locations. Each masked location will be written
to with a value from the `val` vector. Pointers in this vector can be `nil`
or any other invalid value if the corresponding value in the `mask`
parameter is zero.
- `val`: A vector of values to write to the memory locations.
- `mask`: A vector of booleans or unsigned integers that decides which lanes
get written to memory. If the value of the mask is `true` (the lowest bit
set), the corresponding lane is written into memory. Otherwise it's not
written into memory.
**Operation**:
for i in 0 ..< len(ptr) {
if mask[i]&1 == 1 {
ptr[i]^ = val[i]
}
}
Example:
// Example below writes value `127` to the second element of two different
// vectors. The addresses of store destinations are written to the first and the
// third argument of the `ptr` vector, and the `mask` is set accordingly.
import "core:fmt"
import "core:simd"
simd_scatter_example :: proc() {
v1 := [4] f32 {1, 2, 3, 4};
v2 := [4] f32 {5, 6, 7, 8};
ptrs := #simd [4]rawptr { &v1[1], nil, &v2[1], nil }
mask := #simd [4]bool { true, false, true, false }
vals := #simd [4]f32 { 0x7f, 0x7f, 0x7f, 0x7f }
simd.scatter(ptrs, vals, mask)
fmt.println(v1)
fmt.println(v2)
}
Output:
[1, 127, 3, 4]
[5, 127, 7, 8]
The graphic below shows how the data gets written into memory.
+-------------------+
mask: | 1 | 0 | 1 | 0 |
+-------------------+
| | | |
v X v X
+-------------------+
vals: | d0 | d1 | d2 | d3 |
+-------------------+
| \
v v
+-----------------------+
ptrs: | &m0 | nil | &m2 | nil |
+-----------------------+
*/
scatter :: intrinsics.simd_scatter
/*
Perform a masked load into the vector.
This procedure performs a masked load from memory, into the vector. The `ptr`
argument specifies the base address from which the values of the vector
will be loaded. The mask selects the source for the result vector's lanes. If
the mask for the corresponding lane has the value `true` (lowest bit set), the
result lane is loaded from memory. Otherwise the result lane is loaded from the
corresponding lane of the `val` vector.
Inputs:
- `ptr`: The address of the vector values to load. Masked-off values are not
accessed.
- `val`: The vector of values that will be loaded into the masked slots of the
result vector.
- `mask`: The mask that selects where to load the values from.
Returns:
- The loaded vector. The lanes for which the mask was set are loaded from
memory, and the other lanes are loaded from the `val` vector.
**Operation**:
for i in 0 ..< len(res) {
if mask[i]&1 == 1 {
res[i] = ptr[i]
} else {
res[i] = vals[i]
}
}
return res
Example:
// The following code loads two values from the `src` vector, the first and the
// third value (selected by the mask). The masked-off values are given the value
// of 127 (`0x7f`).
import "core:fmt"
import "core:simd"
simd_masked_load_example :: proc() {
src := [4] f32 {1, 2, 3, 4};
mask := #simd [4]bool { true, false, true, false }
vals := #simd [4]f32 { 0x7f, 0x7f, 0x7f, 0x7f }
res := simd.masked_load(&src, vals, mask)
fmt.println(res)
}
Output:
<1, 127, 3, 127>
The graphic below demonstrates the flow of lanes.
+-------------------------------+
mask: | 1 | 0 | 1 | 0 |
+-------------------------------+
| | | `----------------------------.
| | | |
| `---- | ------------------------. |
ptr v v v v
+---->+-------------------------------+ +-------------------+
| v1 | v2 | v3 | v4 | vals: | d0 | d1 | d2 | d3 |
+-------------------------------+ +-------------------+
| | | |
| .--- | -------------------------' |
| | | ,-------------------------'
v v v v
+-------------------------------+
result: | v1 | d1 | v3 | d3 |
+-------------------------------+
*/
masked_load :: intrinsics.simd_masked_load
/*
Perform a masked store to memory.
This procedure performs a masked store from a vector `val`, into memory at
address `ptr`, with the `mask` deciding which lanes are going to be stored,
and which aren't. If the mask at a corresponding lane has the value `true`
(lowest bit set), the lane is stored into memory. Otherwise the lane is not
stored into memory.
Inputs:
- `ptr`: The base address of the store.
- `val`: The vector to store.
- `mask`: The mask, selecting which lanes of the vector to store into memory.
**Operation**:
for i in 0 ..< len(val) {
if mask[i]&1 == 1 {
ptr[i] = val
}
}
Example:
// Example below stores the value 127 into the first and the third slot of the
// vector `v`.
import "core:fmt"
import "core:simd"
simd_masked_store_example :: proc() {
v := [4] f32 {1, 2, 3, 4};
mask := #simd [4]bool { true, false, true, false }
vals := #simd [4]f32 { 0x7f, 0x7f, 0x7f, 0x7f }
simd.masked_store(&v, vals, mask)
fmt.println(v)
}
Output:
[127, 2, 127, 4]
The graphic below shows the flow of lanes:
+-------------------+
mask: | 1 | 0 | 1 | 0 |
+-------------------+
| | | |
v X v X
+-------------------+
vals: | v0 | v1 | v2 | v3 |
+-------------------+
| \
ptr v v
+--->+-----------------------+
| v0 | ... | v2 | ... |
+-----------------------+
*/
masked_store :: intrinsics.simd_masked_store
/*
Load consecutive scalar values and expand into a vector.
This procedure loads a number of consecutive scalar values from an address,
specified by the `ptr` parameter, and stores them in a result vector, according
to the mask. The number of values read from memory is the number of set bits
in the mask. The lanes for which the mask has the value `true` get the next
consecutive value from memory, otherwise if the mask is `false` for the
lane, its value is filled from the corresponding lane of the `val` parameter.
This procedure acts like `masked_store`, except the values from memory are
read consecutively, and not according to the lanes. The memory values are read
and assigned to the result vector's masked lanes in order of increasing
addresses.
Inputs:
- `ptr`: The pointer to the memory to read from.
- `vals`: The default values for masked-off entries.
- `mask`: The mask that determines which lanes get consecutive memory values.
Returns:
- The result vector, holding masked memory values unmasked default values.
**Operation**:
mem_idx := 0
for i in 0 ..< len(mask) {
if mask[i]&1 == 1 {
res[i] = ptr[mem_idx]
mem_idx += 1
} else {
res[i] = val[i]
}
}
return res
Example:
// The example below loads two values from memory of the vector `v`. Two values in
// the mask are set to `true`, meaning only two memory items will be loaded into
// the result vector. The mask is set to `true` in the first and the third
// position, which specifies that the first memory item will be read into the
// first lane of the result vector, and the second memory item will be read into
// the third lane of the result vector. All the other lanes of the result vector
// will be initialized to the default value `127`.
import "core:fmt"
import "core:simd"
simd_masked_expand_load_example :: proc() {
v := [2] f64 {1, 2};
mask := #simd [4]bool { true, false, true, false }
vals := #simd [4]f64 { 0x7f, 0x7f, 0x7f, 0x7f }
res := simd.masked_expand_load(&v, vals, mask)
fmt.println(res)
}
Output:
<1, 127, 2, 127>
Graphical representation of the operation:
ptr --->+-----------+-----
| m0 | m1 | ...
+-----------+-----
| `--.
v v
+-------------------+ +-------------------+
mask: | 1 | 0 | 1 | 0 | vals: | v0 | v1 | v2 | v3 |
+-------------------+ +-------------------+
| | | |
| .-- | -----------------------' |
| | | ,----------------------------'
v v v v
+-------------------+
result: | m0 | v1 | m1 | v3 |
+-------------------+
*/
masked_expand_load :: intrinsics.simd_masked_expand_load
/*
Store masked values to consecutive memory locations.
This procedure stores values from masked lanes of a vector `val` consecutively
into memory. This operation is the opposite of `masked_expand_load`. The number
of items stored into memory is the number of set bits in the mask. If the value
in a lane of a mask is `true`, that lane is stored into memory. Otherwise
nothing is stored.
Inputs:
- `ptr`: The pointer to the memory of a store.
- `val`: The vector to store into memory.
- `mask`: The mask that selects which values to store into memory.
**Operation**:
mem_idx := 0
for i in 0 ..< len(mask) {
if mask[i]&1 == 1 {
ptr[mem_idx] = val[i]
mem_idx += 1
}
}
Example:
// The code below fills the vector `v` with two values from a 4-element SIMD
// vector, the first and the third value. The items in the mask are set to `true`
// in those lanes.
import "core:fmt"
import "core:simd"
simd_masked_compress_store_example :: proc() {
v := [2] f64 { };
mask := #simd [4]bool { true, false, true, false }
vals := #simd [4]f64 { 1, 2, 3, 4 }
simd.masked_compress_store(&v, vals, mask)
fmt.println(v)
}
Output:
[1, 3]
Graphical representation of the operation:
+-------------------+
mask: | 1 | 0 | 1 | 0 |
+-------------------+
| |
v v
+-------------------+
vals: | v0 | v1 | v2 | v3 |
+-------------------+
| ,--'
ptr v v
+--->+-----------------
| v0 | v2 | ...
+-----------------
*/
masked_compress_store :: intrinsics.simd_masked_compress_store
/*
Extract scalar from a vector's lane.
This procedure returns the scalar from the lane at the specified index of the
vector.
Inputs:
- `a`: The vector to extract from.
- `idx`: The lane index.
Returns:
- The value of the lane at the specified index.
**Operation**:
return a[idx]
*/
extract :: intrinsics.simd_extract
/*
Replace the value in a vector's lane.
This procedure places a scalar value at the lane corresponding to the given index of
the vector.
Inputs:
- `a`: The vector to replace a lane in.
- `idx`: The lane index.
- `elem`: The scalar to place.
Returns:
- Vector with the specified lane replaced.
**Operation**:
a[idx] = elem
*/
replace :: intrinsics.simd_replace
/*
Reduce a vector to a scalar by adding up all the lanes in a bisecting fashion.
This procedure returns a scalar that is the sum of all lanes, calculated by
bisecting the vector into two parts, where the first contains lanes [0, N/2)
and the second contains lanes [N/2, N), and adding the two halves element-wise
to produce N/2 values. This is repeated until only a single element remains.
This order may be faster to compute than the ordered sum for floats, as it can
often be better parallelized.
The order of the sum may be important for accounting for precision errors in
floating-point computation, as floating-point addition is not associative, that
is `(a+b)+c` may not be equal to `a+(b+c)`.
Inputs:
- `v`: The vector to reduce.
Result:
- Sum of all lanes, as a scalar.
**Operation**:
for n > 1 {
n = n / 2
for i in 0 ..< n {
a[i] += a[i+n]
}
}
res := a[0]
Graphical representation of the operation for N=4:
+-----------------------+
| v0 | v1 | v2 | v3 |
+-----------------------+
| | | |
[+]<-- | ---' |
| [+]<--------'
| |
`>[+]<'
|
v
+-----+
result: | y0 |
+-----+
*/
reduce_add_bisect :: intrinsics.simd_reduce_add_bisect
/*
Reduce a vector to a scalar by multiplying up all the lanes in a bisecting fashion.
This procedure returns a scalar that is the product of all lanes, calculated by
bisecting the vector into two parts, where the first contains indices [0, N/2)
and the second contains indices [N/2, N), and multiplying the two halves
together element-wise to produce N/2 values. This is repeated until only a
single element remains. This order may be faster to compute than the ordered
product for floats, as it can often be better parallelized.
The order of the product may be important for accounting for precision errors
in floating-point computation, as floating-point multiplication is not
associative, that is `(a*b)*c` may not be equal to `a*(b*c)`.
Inputs:
- `v`: The vector to reduce.
Result:
- Product of all lanes, as a scalar.
**Operation**:
for n > 1 {
n = n / 2
for i in 0 ..< n {
a[i] *= a[i+n]
}
}
res := a[0]
Graphical representation of the operation for N=4:
+-----------------------+
| v0 | v1 | v2 | v3 |
+-----------------------+
| | | |
[x]<-- | ---' |
| [x]<--------'
| |
`>[x]<'
|
v
+-----+
result: | y0 |
+-----+
*/
reduce_mul_bisect :: intrinsics.simd_reduce_mul_bisect
/*
Reduce a vector to a scalar by adding up all the lanes in an ordered fashion.
This procedure returns a scalar that is the ordered sum of all lanes. The
ordered sum may be important for accounting for precision errors in
floating-point computation, as floating-point addition is not associative,
that is `(a+b)+c` may not be equal to `a+(b+c)`.
Inputs:
- `a`: The vector to reduce.
Result:
- Sum of all lanes, as a scalar.
**Operation**:
res := 0
for i in 0 ..< len(a) {
res += a[i]
}
*/
reduce_add_ordered :: intrinsics.simd_reduce_add_ordered
/*
Reduce a vector to a scalar by multiplying all the lanes in an ordered fashion.
This procedure returns a scalar that is the ordered product of all lanes.
The ordered product may be important for accounting for precision errors in
floating-point computation, as floating-point multiplication is not associative,
that is `(a*b)*c` may not be equal to `a*(b*c)`.
Inputs:
- `a`: The vector to reduce.
Result:
- Product of all lanes, as a scalar.
**Operation**:
res := 1
for i in 0 ..< len(a) {
res *= a[i]
}
*/
reduce_mul_ordered :: intrinsics.simd_reduce_mul_ordered
/*
Reduce a vector to a scalar by adding up all the lanes in a pairwise fashion.
This procedure returns a scalar that is the sum of all lanes, calculated by
adding each even-indexed element with the following odd-indexed element to
produce N/2 values. This is repeated until only a single element remains. This
order is supported by hardware instructions for some types/architectures (e.g.
i16/i32/f32/f64 on x86 SSE, i8/i16/i32/f32 on ARM NEON).
The order of the sum may be important for accounting for precision errors in
floating-point computation, as floating-point addition is not associative, that
is `(a+b)+c` may not be equal to `a+(b+c)`.
Inputs:
- `v`: The vector to reduce.
Result:
- Sum of all lanes, as a scalar.
**Operation**:
for n > 1 {
n = n / 2
for i in 0 ..< n {
a[i] = a[2*i+0] + a[2*i+1]
}
}
res := a[0]
Graphical representation of the operation for N=4:
+-----------------------+
v: | v0 | v1 | v2 | v3 |
+-----------------------+
| | | |
`>[+]<' `>[+]<'
| |
`--->[+]<--'
|
v
+-----+
result: | y0 |
+-----+
*/
reduce_add_pairs :: intrinsics.simd_reduce_add_pairs
/*
Reduce a vector to a scalar by multiplying all the lanes in a pairwise fashion.
This procedure returns a scalar that is the product of all lanes, calculated by
bisecting the vector into two parts, where the first contains lanes [0, N/2)
and the second contains lanes [N/2, N), and multiplying the two halves together
multiplying each even-indexed element with the following odd-indexed element to
produce N/2 values. This is repeated until only a single element remains. This
order may be faster to compute than the ordered product for floats, as it can
often be better parallelized.
The order of the product may be important for accounting for precision errors
in floating-point computation, as floating-point multiplication is not
associative, that is `(a*b)*c` may not be equal to `a*(b*c)`.
Inputs:
- `v`: The vector to reduce.
Result:
- Product of all lanes, as a scalar.
**Operation**:
for n > 1 {
n = n / 2
for i in 0 ..< n {
a[i] = a[2*i+0] * a[2*i+1]
}
}
res := a[0]
Graphical representation of the operation for N=4:
+-----------------------+
v: | v0 | v1 | v2 | v3 |
+-----------------------+
| | | |
`>[x]<' `>[x]<'
| |
`--->[x]<--'
|
v
+-----+
result: | y0 |
+-----+
*/
reduce_mul_pairs :: intrinsics.simd_reduce_mul_pairs
/*
Reduce a vector to a scalar by finding the minimum value between all of the lanes.
This procedure returns a scalar that is the minimum value of all the lanes
in a vector.
Inputs:
- `a`: The vector to reduce.
Result:
- Minimum value of all lanes, as a scalar.
**Operation**:
res := 0
for i in 0 ..< len(a) {
res = min(res, a[i])
}
*/
reduce_min :: intrinsics.simd_reduce_min
/*
Reduce a vector to a scalar by finding the maximum value between all of the lanes.
This procedure returns a scalar that is the maximum value of all the lanes
in a vector.
Inputs:
- `a`: The vector to reduce.
Result:
- Maximum value of all lanes, as a scalar.
**Operation**:
res := 0
for i in 0 ..< len(a) {
res = max(res, a[i])
}
*/
reduce_max :: intrinsics.simd_reduce_max
/*
Reduce a vector to a scalar by performing bitwise AND of all of the lanes.
This procedure returns a scalar that is the result of the bitwise AND operation
between all of the lanes in a vector.
Inputs:
- `a`: The vector to reduce.
Result:
- Bitwise AND of all lanes, as a scalar.
**Operation**:
res := 0
for i in 0 ..< len(a) {
res &= a[i]
}
*/
reduce_and :: intrinsics.simd_reduce_and
/*
Reduce a vector to a scalar by performing bitwise OR of all of the lanes.
This procedure returns a scalar that is the result of the bitwise OR operation
between all of the lanes in a vector.
Inputs:
- `a`: The vector to reduce.
Result:
- Bitwise OR of all lanes, as a scalar.
**Operation**:
res := 0
for i in 0 ..< len(a) {
res |= a[i]
}
*/
reduce_or :: intrinsics.simd_reduce_or
/*
Reduce SIMD vector to a scalar by performing bitwise XOR of all of the lanes.
This procedure returns a scalar that is the result of the bitwise XOR operation
between all of the lanes in a vector.
Inputs:
- `a`: The vector to reduce.
Result:
- Bitwise XOR of all lanes, as a scalar.
**Operation**:
res := 0
for i in 0 ..< len(a) {
res ~= a[i]
}
*/
reduce_xor :: intrinsics.simd_reduce_xor
/*
Reduce SIMD vector to a scalar by performing bitwise OR of all of the lanes.
This procedure returns a scalar that is the result of the bitwise OR operation
between all of the lanes in a vector.
Inputs:
- `a`: The vector to reduce.
Result:
- Bitwise OR of all lanes, as a scalar.
**Operation**:
res := 0
for i in 0 ..< len(a) {
res |= a[i]
}
*/
reduce_any :: intrinsics.simd_reduce_any
/*
Reduce SIMD vector to a scalar by performing bitwise AND of all of the lanes.
This procedure returns a scalar that is the result of the bitwise AND operation
between all of the lanes in a vector.
Inputs:
- `a`: The vector to reduce.
Result:
- Bitwise AND of all lanes, as a scalar.
**Operation**:
res := 0
for i in 0 ..< len(a) {
res &= a[i]
}
*/
reduce_all :: intrinsics.simd_reduce_all
/*
Reorder the lanes of a SIMD vector.
This procedure reorders the lanes of a vector, according to the provided
indices. The number of indices correspond to the number of lanes in the
result vector and must be the same as the number of lanes of the input vector.
Each index specifies, the lane of the scalar from the input vector, which
will be written at the corresponding position of the result vector.
Inputs:
- `x`: The input vector.
- `indices`: The indices of lanes to write to the result vector.
Result:
- Swizzled input vector.
**Operation**:
res = {}
for i in 0 ..< len(indices) {
res[i] = x[indices[i]]
}
return res
Example:
// The example below shows how the indices are used to determine which lanes of the
// input vector get written into the result vector.
import "core:fmt"
import "core:simd"
swizzle_example :: proc() {
x := #simd [4]f32 { 1.5, 2.5, 3.5, 4.5 }
res := simd.swizzle(x, 0, 3, 1, 1)
fmt.println(res)
}
Output:
<1.5, 4.5, 2.5, 2.5>
The graphical representation of the operation is as follows. The `idx` vector in
the picture represents the `indices` parameter:
0 1 2 3
+-----+-----+-----+-----+
x: | 1.5 | 2.5 | 3.5 | 4.5 |
+-----+-----+-----+-----+
^ ^ ^
| | |
| '----. |
| .---- | ---'
| | |
| | +------.
+-----+-----+-----+-----+
idx: | 0 | 3 | 1 | 1 |
+-----+-----+-----+-----+
^ ^ ^ ^
| | | |
+-----+-----+-----+-----+
res: | 1.5 | 3.5 | 2.5 | 2.5 |
+-----+-----+-----+-----+
*/
swizzle :: builtin.swizzle
/*
Extract the set of most-significant bits of a SIMD vector.
This procedure checks the the most-significant bit (MSB) for each lane of vector
and returns the numbers of lanes with the most-significant bit set. This procedure
can be used in conjuction with `lanes_eq` (and other similar procedures) to
count the number of matched lanes by computing the cardinality of the resulting
set.
Inputs:
- `a`: An input vector.
Result:
- A bitset of integers, corresponding to the indexes of the lanes, whose MSBs
are set.
**Operation**:
bits_per_lane = 8*size_of(a[0])
res = bit_set {}
for i in 0 ..< len(a) {
if a[i] & 1<<(bits_per_lane-1) != 0 {
res |= i
}
}
return res
Example:
// Since lanes 0, 1, 4, 7 contain negative numbers, the most significant
// bits for them will be set.
import "core:fmt"
import "core:simd"
simd_extract_msbs_example :: proc() {
v := #simd [8]i32 { -1, -2, +3, +4, -5, +6, +7, -8 }
fmt.println(simd.extract_msbs(v))
}
Output:
bit_set[0..=7]{0, 1, 4, 7}
*/
extract_msbs :: intrinsics.simd_extract_msbs
/*
Extract the set of least-significant bits of a SIMD vector.
This procedure checks the the least-significant bit (LSB) for each lane of vector
and returns the numbers of lanes with the least-significant bit set. This procedure
can be used in conjuction with `lanes_eq` (and other similar procedures) to
count the number of matched lanes by computing the cardinality of the resulting
set.
Inputs:
- `a`: An input vector.
Result:
- A bitset of integers, corresponding to the indexes of the lanes, whose LSBs
are set.
**Operation**:
res = bit_set {}
for i in 0 ..< len(a) {
if a[i] & 1 != 0 {
res |= i
}
}
return res
Example:
// Since lanes 0, 2, 4, 6 contain odd integers, the least significant bits
// for these lanes are set.
import "core:fmt"
import "core:simd"
simd_extract_lsbs_example :: proc() {
v := #simd [8]i32 { -1, -2, +3, +4, -5, +6, +7, -8 }
fmt.println(simd.extract_lsbs(v))
}
Output:
bit_set[0..=7]{0, 2, 4, 6}
*/
extract_lsbs :: intrinsics.simd_extract_lsbs
/*
Reorder the lanes of two SIMD vectors.
This procedure returns a vector, containing the scalars from the lanes of two
vectors, according to the provided indices vector. Each index in the indices
vector specifies, the lane of the scalar from one of the two input vectors,
which will be written at the corresponding position of the result vector. If
the index is within bounds 0 ..< len(A), it corresponds to the indices of the
first input vector. Otherwise the index corresponds to the indices of the second
input vector.
Inputs:
- `a`: The first input vector.
- `b`: The second input vector.
- `indices`: The indices.
Result:
- Input vectors, shuffled according to the indices.
**Operation**:
res = {}
for i in 0 ..< len(indices) {
idx = indices[i];
if idx < len(a) {
res[i] = a[idx]
} else {
res[i] = b[idx]
}
}
return res
Example:
// The example below shows how the indices are used to determine lanes of the
// input vector that are shuffled into the result vector.
import "core:fmt"
import "core:simd"
simd_shuffle_example :: proc() {
a := #simd [4]f32 { 1, 2, 3, 4 }
b := #simd [4]f32 { 5, 6, 7, 8 }
res := simd.shuffle(a, b, 0, 4, 2, 5)
fmt.println(res)
}
Output:
<1, 5, 3, 6>
The graphical representation of the operation is as follows. The `idx` vector in
the picture represents the `indices` parameter:
0 1 2 3 4 5 6 7
+-----+-----+-----+-----+ +-----+-----+-----+-----+
a: | 1 | 2 | 3 | 4 | b: | 5 | 6 | 7 | 8 |
+-----+-----+-----+-----+ +-----+-----+-----+-----+
^ ^ ^ ^
| | | |
| | | |
| .--- | ----------------' |
| | | .-----------------'
+-----+-----+-----+-----+
idx: | 0 | 4 | 2 | 5 |
+-----+-----+-----+-----+
^ ^ ^ ^
| | | |
+-----+-----+-----+-----+
res: | 1 | 5 | 3 | 6 |
+-----+-----+-----+-----+
*/
shuffle :: intrinsics.simd_shuffle
/*
Select values from one of the two vectors.
This procedure returns a vector, which has, on each lane a value from one of the
corresponding lanes in one of the two input vectors based on the `cond`
parameter. On each lane, if the value of the `cond` parameter is `true` (or
non-zero), the result lane will have a value from the `true` input vector,
otherwise the result lane will have a value from the `false` input vector.
Inputs:
- `cond`: The condition vector.
- `true`: The first input vector.
- `false`: The second input vector.
Result:
- The result of selecting values from the two input vectors.
**Operation**:
res = {}
for i in 0 ..< len(cond) {
if cond[i] {
res[i] = true[i]
} else {
res[i] = false[i]
}
}
return res
Example:
// The following example selects values from the two input vectors, `a` and `b`
// into a single vector.
import "core:fmt"
import "core:simd"
simd_select_example :: proc() {
a := #simd [4] f64 { 1,2,3,4 }
b := #simd [4] f64 { 5,6,7,8 }
cond := #simd[4] int { 1, 0, 1, 0 }
fmt.println(simd.select(cond,a,b))
}
Output:
<1, 6, 3, 8>
Graphically, the operation looks as follows. The `t` and `f` represent the
`true` and `false` vectors respectively:
0 1 2 3 0 1 2 3
+-----+-----+-----+-----+ +-----+-----+-----+-----+
t: | 1 | 2 | 3 | 4 | f: | 5 | 6 | 7 | 8 |
+-----+-----+-----+-----+ +-----+-----+-----+-----+
^ ^ ^ ^
| | | |
| | | |
| .--- | ----------------------' |
| | | .-----------------------------'
+-----+-----+-----+-----+
cond: | 1 | 0 | 1 | 0 |
+-----+-----+-----+-----+
^ ^ ^ ^
| | | |
+-----+-----+-----+-----+
res: | 1 | 5 | 3 | 6 |
+-----+-----+-----+-----+
*/
select :: intrinsics.simd_select
/*
Runtime Equivalent to Shuffle.
Performs element-wise table lookups using runtime indices.
Each element in the indices vector selects an element from the table vector.
The indices are automatically masked to prevent out-of-bounds access.
This operation is hardware-accelerated on most platforms when using 8-bit
integer vectors. For other element types or unsupported vector sizes, it
falls back to software emulation.
Inputs:
- `table`: The lookup table vector (should be power-of-2 size for correct masking).
- `indices`: The indices vector (automatically masked to valid range).
Returns:
- A vector where `result[i] = table[indices[i] & (table_size-1)]`.
Operation:
for i in 0 ..< len(indices) {
masked_index := indices[i] & (len(table) - 1)
result[i] = table[masked_index]
}
return result
Implementation:
| Platform | Lane Size | Implementation |
|-------------|-------------------------------------------|---------------------|
| x86-64 | pshufb (16B), vpshufb (32B), AVX512 (64B) | Single vector |
| ARM64 | tbl1 (16B), tbl2 (32B), tbl4 (64B) | Automatic splitting |
| ARM32 | vtbl1 (8B), vtbl2 (16B), vtbl4 (32B) | Automatic splitting |
| WebAssembly | i8x16.swizzle (16B), Emulation (>16B) | Mixed |
| Other | Emulation | Software |
Example:
import "core:simd"
import "core:fmt"
runtime_swizzle_example :: proc() {
table := simd.u8x16{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
indices := simd.u8x16{15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
result := simd.runtime_swizzle(table, indices)
fmt.println(result) // Expected: {15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
}
*/
runtime_swizzle :: intrinsics.simd_runtime_swizzle
/*
Compute the square root of each lane in a SIMD vector.
*/
sqrt :: intrinsics.sqrt
/*
Ceil each lane in a SIMD vector.
*/
ceil :: intrinsics.simd_ceil
/*
Floor each lane in a SIMD vector.
*/
floor :: intrinsics.simd_floor
/*
Truncate each lane in a SIMD vector.
*/
trunc :: intrinsics.simd_trunc
/*
Compute the nearest integer of each lane in a SIMD vector.
*/
nearest :: intrinsics.simd_nearest
/*
Transmute a SIMD vector into an integer vector.
*/
to_bits :: intrinsics.simd_to_bits
/*
Reverse the lanes of a SIMD vector.
This procedure reverses the lanes of a vector, putting last lane in the
first spot, etc. This procedure is equivalent to the following call (for
4-element vectors):
swizzle(a, 3, 2, 1, 0)
*/
lanes_reverse :: intrinsics.simd_lanes_reverse
/*
Rotate the lanes of a SIMD vector left.
This procedure rotates the lanes of a vector, putting the first lane of the
last spot, second lane in the first spot, third lane in the second spot, etc.
For 4-element vectors, this procedure is equvalent to the following:
swizzle(a, 1, 2, 3, 0)
*/
lanes_rotate_left :: intrinsics.simd_lanes_rotate_left
/*
Rotate the lanes of a SIMD vector right.
This procedure rotates the lanes of a SIMD vector, putting the first lane of the
second spot, second lane in the third spot, etc. For 4-element vectors, this
procedure is equvalent to the following:
swizzle(a, 3, 0, 1, 2)
*/
lanes_rotate_right :: intrinsics.simd_lanes_rotate_right
/*
Count the number of set bits in each lane of a SIMD vector.
*/
count_ones :: intrinsics.count_ones
/*
Count the number of unset bits in each lane of a SIMD vector.
*/
count_zeros :: intrinsics.count_zeros
/*
Count the number of trailing unset bits in each lane of a SIMD vector.
*/
count_trailing_zeros :: intrinsics.count_trailing_zeros
/*
Count the number of leading unset bits in each lane of a SIMD vector.
*/
count_leading_zeros :: intrinsics.count_leading_zeros
/*
Reverse the bit pattern of a SIMD vector.
*/
reverse_bits :: intrinsics.reverse_bits
/*
Perform a FMA (Fused multiply-add) operation on each lane of SIMD vectors.
A fused multiply-add is a ternary operation that for three operands, `a`, `b`
and `c` performs the operation `a*b+c`. This operation is a hardware feature
that allows to minimize floating-point error and allow for faster computation.
This procedure performs a FMA operation on each lane of the SIMD vectors.
Inputs:
- `a`: The multiplier
- `b`: The multiplicand
- `c`: The addend
Returns:
- `a*b+c`
**Operation**
res := 0
for i in 0 ..< len(a) {
res[i] = fma(a[i], b[i], c[i])
}
return res
*/
fused_mul_add :: intrinsics.fused_mul_add
/*
Perform a FMA (Fused multiply-add) operation on each lane of SIMD vectors.
A fused multiply-add is a ternary operation that for three operands, `a`, `b`
and `c` performs the operation `a*b+c`. This operation is a hardware feature
that allows to minimize floating-point error and allow for faster computation.
This procedure performs a FMA operation on each lane of the SIMD vectors.
Inputs:
- `a`: The multiplier.
- `b`: The multiplicand.
- `c`: The addend.
Returns:
- `a*b+c`
**Operation**
res := 0
for i in 0 ..< len(a) {
res[i] = fma(a[i], b[i], c[i])
}
return res
*/
fma :: intrinsics.fused_mul_add
/*
Convert pointer to SIMD vector to an array pointer.
*/
to_array_ptr :: #force_inline proc "contextless" (v: ^#simd[$LANES]$E) -> ^[LANES]E {
return (^[LANES]E)(v)
}
/*
Convert SIMD vector to an array.
*/
to_array :: #force_inline proc "contextless" (v: #simd[$LANES]$E) -> [LANES]E {
return transmute([LANES]E)(v)
}
/*
Convert array to SIMD vector.
*/
from_array :: #force_inline proc "contextless" (v: $A/[$LANES]$E) -> #simd[LANES]E {
return transmute(#simd[LANES]E)v
}
/*
Convert slice to SIMD vector.
*/
from_slice :: proc($T: typeid/#simd[$LANES]$E, slice: []E) -> T {
assert(len(slice) >= LANES, "slice length must be a least the number of lanes")
array: [LANES]E
#no_bounds_check for i in 0..<LANES {
array[i] = slice[i]
}
return transmute(T)array
}
/*
Perform binary not operation on a SIMD vector.
This procedure returns a vector where each lane is the result of the binary
NOT operation of the corresponding lane in the vector `a`.
**Operation**:
for i in 0 ..< len(res) {
res[i] = ~a[i]
}
return res
Example:
+------+------+------+------+
a: | 0x00 | 0x50 | 0x80 | 0xff |
+------+------+------+------+
res:
+------+------+------+------+
| 0xff | 0xaf | 0x7f | 0x00 |
+------+------+------+------+
*/
bit_not :: #force_inline proc "contextless" (v: $T/#simd[$LANES]$E) -> T where intrinsics.type_is_integer(E) {
return xor(v, T(~E(0)))
}
/*
Copy the signs from lanes of one SIMD vector into another SIMD vector.
*/
copysign :: #force_inline proc "contextless" (v, sign: $T/#simd[$LANES]$E) -> T where intrinsics.type_is_float(E) {
neg_zero := to_bits(T(-0.0))
sign_bit := to_bits(sign) & neg_zero
magnitude := to_bits(v) &~ neg_zero
return transmute(T)(sign_bit|magnitude)
}
/*
Return signs of SIMD lanes.
This procedure returns a vector, each lane of which contains either +1.0 or
-1.0 depending on the sign of the value in the corresponding lane of the
input vector. If the lane of the input vector has NaN, then the result vector
will contain this NaN value as-is.
*/
signum :: #force_inline proc "contextless" (v: $T/#simd[$LANES]$E) -> T where intrinsics.type_is_float(E) {
is_nan := lanes_ne(v, v)
return select(is_nan, v, copysign(T(1), v))
}
/*
Calculate reciprocals of SIMD lanes.
This procedure returns a vector where each lane is the reciprocal of the
corresponding lane in the vector `a`.
Inputs:
- `a`: An integer or a float vector to negate.
Returns:
- Negated vector.
**Operation**:
for i in 0 ..< len(res) {
res[i] = 1.0 / a[i]
}
return res
Example:
+------+------+------+------+
a: | 2 | 1 | 3 | 5 |
+------+------+------+------+
res:
+------+------+------+------+
| 0.5 | 1 | 0.33 | 0.2 |
+------+------+------+------+
*/
recip :: #force_inline proc "contextless" (v: $T/#simd[$LANES]$E) -> T where intrinsics.type_is_float(E) {
return T(1) / v
}
/*
Create a vector where each lane contains the index of that lane.
Inputs:
- `V`: The type of the vector to create.
Result:
- A vector of the given type, where each lane contains the index of that lane.
**Operation**:
for i in 0 ..< N {
res[i] = i
}
*/
indices :: intrinsics.simd_indices
|