-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathzmath.zig
4571 lines (4158 loc) · 170 KB
/
zmath.zig
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
// ==============================================================================
//
// SIMD math library for game developers
// https://github.com/michal-z/zig-gamedev/tree/main/libs/zmath
//
// Should work on all OSes supported by Zig. Works on x86_64 and ARM.
// Provides ~140 optimized routines and ~70 extensive tests.
// Can be used with any graphics API.
//
// zmath uses row-major matrices, row vectors (each row vector is stored in a SIMD register).
// Handedness is determined by which function version is used (Rh vs. Lh),
// otherwise the function works with either left-handed or right-handed view coordinates.
//
// const va = f32x4(1.0, 2.0, 3.0, 1.0);
// const vb = f32x4(-1.0, 1.0, -1.0, 1.0);
// const v0 = va + vb - f32x4(0.0, 1.0, 0.0, 1.0) * f32x4s(3.0);
// const v1 = cross3(va, vb) + f32x4(1.0, 1.0, 1.0, 1.0);
// const v2 = va + dot3(va, vb) / v1; // dotN() returns scalar replicated on all vector components
//
// const m = rotationX(math.pi * 0.25);
// const v = f32x4(...);
// const v0 = mul(v, m); // 'v' treated as a row vector
// const v1 = mul(m, v); // 'v' treated as a column vector
// const f = m[row][column];
//
// const b = va < vb;
// if (all(b, 0)) { ... } // '0' means check all vector components; if all are 'true'
// if (all(b, 3)) { ... } // '3' means check first three vector components; if all first three are 'true'
// if (any(b, 0)) { ... } // '0' means check all vector components; if any is 'true'
// if (any(b, 3)) { ... } // '3' means check first three vector components; if any from first three is 'true'
//
// var v4 = load(mem[0..], F32x4, 0);
// var v8 = load(mem[100..], F32x8, 0);
// var v16 = load(mem[200..], F32x16, 0);
//
// var camera_position = [3]f32{ 1.0, 2.0, 3.0 };
// var cam_pos = loadArr3(camera_position);
// ...
// storeArr3(&camera_position, cam_pos);
//
// v4 = sin(v4); // SIMDx4
// v8 = cos(v8); // .x86_64 -> 2 x SIMDx4, .x86_64+avx+fma -> SIMDx8
// v16 = atan(v16); // .x86_64 -> 4 x SIMDx4, .x86_64+avx+fma -> 2 x SIMDx8, .x86_64+avx512f -> SIMDx16
//
// store(mem[0..], v4, 0);
// store(mem[100..], v8, 0);
// store(mem[200..], v16, 0);
//
// ------------------------------------------------------------------------------
// 1. Initialization functions
// ------------------------------------------------------------------------------
//
// f32x4(e0: f32, e1: f32, e2: f32, e3: f32) F32x4
// f32x8(e0: f32, e1: f32, e2: f32, e3: f32, e4: f32, e5: f32, e6: f32, e7: f32) F32x8
// f32x16(e0: f32, e1: f32, e2: f32, e3: f32, e4: f32, e5: f32, e6: f32, e7: f32,
// e8: f32, e9: f32, ea: f32, eb: f32, ec: f32, ed: f32, ee: f32, ef: f32) F32x16
//
// f32x4s(e0: f32) F32x4
// f32x8s(e0: f32) F32x8
// f32x16s(e0: f32) F32x16
//
// boolx4(e0: bool, e1: bool, e2: bool, e3: bool) Boolx4
// boolx8(e0: bool, e1: bool, e2: bool, e3: bool, e4: bool, e5: bool, e6: bool, e7: bool) Boolx8
// boolx16(e0: bool, e1: bool, e2: bool, e3: bool, e4: bool, e5: bool, e6: bool, e7: bool,
// e8: bool, e9: bool, ea: bool, eb: bool, ec: bool, ed: bool, ee: bool, ef: bool) Boolx16
//
// load(mem: []const f32, comptime T: type, comptime len: u32) T
// store(mem: []f32, v: anytype, comptime len: u32) void
//
// loadArr2(arr: [2]f32) F32x4
// loadArr2zw(arr: [2]f32, z: f32, w: f32) F32x4
// loadArr3(arr: [3]f32) F32x4
// loadArr3w(arr: [3]f32, w: f32) F32x4
// loadArr4(arr: [4]f32) F32x4
//
// storeArr2(arr: *[2]f32, v: F32x4) void
// storeArr3(arr: *[3]f32, v: F32x4) void
// storeArr4(arr: *[4]f32, v: F32x4) void
//
// arr3Ptr(ptr: anytype) *const [3]f32
// arrNPtr(ptr: anytype) [*]const f32
//
// splat(comptime T: type, value: f32) T
// splatInt(comptime T: type, value: u32) T
//
// ------------------------------------------------------------------------------
// 2. Functions that work on all vector components (F32xN = F32x4 or F32x8 or F32x16)
// ------------------------------------------------------------------------------
//
// all(vb: anytype, comptime len: u32) bool
// any(vb: anytype, comptime len: u32) bool
//
// isNearEqual(v0: F32xN, v1: F32xN, epsilon: F32xN) BoolxN
// isNan(v: F32xN) BoolxN
// isInf(v: F32xN) BoolxN
// isInBounds(v: F32xN, bounds: F32xN) BoolxN
//
// andInt(v0: F32xN, v1: F32xN) F32xN
// andNotInt(v0: F32xN, v1: F32xN) F32xN
// orInt(v0: F32xN, v1: F32xN) F32xN
// norInt(v0: F32xN, v1: F32xN) F32xN
// xorInt(v0: F32xN, v1: F32xN) F32xN
//
// minFast(v0: F32xN, v1: F32xN) F32xN
// maxFast(v0: F32xN, v1: F32xN) F32xN
// min(v0: F32xN, v1: F32xN) F32xN
// max(v0: F32xN, v1: F32xN) F32xN
// round(v: F32xN) F32xN
// floor(v: F32xN) F32xN
// trunc(v: F32xN) F32xN
// ceil(v: F32xN) F32xN
// clamp(v0: F32xN, v1: F32xN) F32xN
// clampFast(v0: F32xN, v1: F32xN) F32xN
// saturate(v: F32xN) F32xN
// saturateFast(v: F32xN) F32xN
// lerp(v0: F32xN, v1: F32xN, t: f32) F32xN
// lerpV(v0: F32xN, v1: F32xN, t: F32xN) F32xN
// lerpInverse(v0: F32xN, v1: F32xN, t: f32) F32xN
// lerpInverseV(v0: F32xN, v1: F32xN, t: F32xN) F32xN
// mapLinear(v: F32xN, min1: f32, max1: f32, min2: f32, max2: f32) F32xN
// mapLinearV(v: F32xN, min1: F32xN, max1: F32xN, min2: F32xN, max2: F32xN) F32xN
// sqrt(v: F32xN) F32xN
// abs(v: F32xN) F32xN
// mod(v0: F32xN, v1: F32xN) F32xN
// modAngle(v: F32xN) F32xN
// mulAdd(v0: F32xN, v1: F32xN, v2: F32xN) F32xN
// select(mask: BoolxN, v0: F32xN, v1: F32xN)
// sin(v: F32xN) F32xN
// cos(v: F32xN) F32xN
// sincos(v: F32xN) [2]F32xN
// asin(v: F32xN) F32xN
// acos(v: F32xN) F32xN
// atan(v: F32xN) F32xN
// atan2(vy: F32xN, vx: F32xN) F32xN
// cmulSoa(re0: F32xN, im0: F32xN, re1: F32xN, im1: F32xN) [2]F32xN
//
// ------------------------------------------------------------------------------
// 3. 2D, 3D, 4D vector functions
// ------------------------------------------------------------------------------
//
// swizzle(v: Vec, c, c, c, c) Vec (comptime c = .x | .y | .z | .w)
// dot2(v0: Vec, v1: Vec) F32x4
// dot3(v0: Vec, v1: Vec) F32x4
// dot4(v0: Vec, v1: Vec) F32x4
// cross3(v0: Vec, v1: Vec) Vec
// lengthSq2(v: Vec) F32x4
// lengthSq3(v: Vec) F32x4
// lengthSq4(v: Vec) F32x4
// length2(v: Vec) F32x4
// length3(v: Vec) F32x4
// length4(v: Vec) F32x4
// normalize2(v: Vec) Vec
// normalize3(v: Vec) Vec
// normalize4(v: Vec) Vec
//
// vecToArr2(v: Vec) [2]f32
// vecToArr3(v: Vec) [3]f32
// vecToArr4(v: Vec) [4]f32
//
// ------------------------------------------------------------------------------
// 4. Matrix functions
// ------------------------------------------------------------------------------
//
// identity() Mat
// mul(m0: Mat, m1: Mat) Mat
// mul(s: f32, m: Mat) Mat
// mul(m: Mat, s: f32) Mat
// mul(v: Vec, m: Mat) Vec
// mul(m: Mat, v: Vec) Vec
// transpose(m: Mat) Mat
// rotationX(angle: f32) Mat
// rotationY(angle: f32) Mat
// rotationZ(angle: f32) Mat
// translation(x: f32, y: f32, z: f32) Mat
// translationV(v: Vec) Mat
// scaling(x: f32, y: f32, z: f32) Mat
// scalingV(v: Vec) Mat
// lookToLh(eyepos: Vec, eyedir: Vec, updir: Vec) Mat
// lookAtLh(eyepos: Vec, focuspos: Vec, updir: Vec) Mat
// lookToRh(eyepos: Vec, eyedir: Vec, updir: Vec) Mat
// lookAtRh(eyepos: Vec, focuspos: Vec, updir: Vec) Mat
// perspectiveFovLh(fovy: f32, aspect: f32, near: f32, far: f32) Mat
// perspectiveFovRh(fovy: f32, aspect: f32, near: f32, far: f32) Mat
// perspectiveFovLhGl(fovy: f32, aspect: f32, near: f32, far: f32) Mat
// perspectiveFovRhGl(fovy: f32, aspect: f32, near: f32, far: f32) Mat
// orthographicLh(w: f32, h: f32, near: f32, far: f32) Mat
// orthographicRh(w: f32, h: f32, near: f32, far: f32) Mat
// orthographicLhGl(w: f32, h: f32, near: f32, far: f32) Mat
// orthographicRhGl(w: f32, h: f32, near: f32, far: f32) Mat
// orthographicOffCenterLh(left: f32, right: f32, top: f32, bottom: f32, near: f32, far: f32) Mat
// orthographicOffCenterRh(left: f32, right: f32, top: f32, bottom: f32, near: f32, far: f32) Mat
// orthographicOffCenterLhGl(left: f32, right: f32, top: f32, bottom: f32, near: f32, far: f32) Mat
// orthographicOffCenterRhGl(left: f32, right: f32, top: f32, bottom: f32, near: f32, far: f32) Mat
// determinant(m: Mat) F32x4
// inverse(m: Mat) Mat
// inverseDet(m: Mat, det: ?*F32x4) Mat
// matToQuat(m: Mat) Quat
// matFromAxisAngle(axis: Vec, angle: f32) Mat
// matFromNormAxisAngle(axis: Vec, angle: f32) Mat
// matFromQuat(quat: Quat) Mat
// matFromRollPitchYaw(pitch: f32, yaw: f32, roll: f32) Mat
// matFromRollPitchYawV(angles: Vec) Mat
// matFromArr(arr: [16]f32) Mat
//
// loadMat(mem: []const f32) Mat
// loadMat43(mem: []const f32) Mat
// loadMat34(mem: []const f32) Mat
// storeMat(mem: []f32, m: Mat) void
// storeMat43(mem: []f32, m: Mat) void
// storeMat34(mem: []f32, m: Mat) void
//
// matToArr(m: Mat) [16]f32
// matToArr43(m: Mat) [12]f32
// matToArr34(m: Mat) [12]f32
//
// ------------------------------------------------------------------------------
// 5. Quaternion functions
// ------------------------------------------------------------------------------
//
// qmul(q0: Quat, q1: Quat) Quat
// qidentity() Quat
// conjugate(quat: Quat) Quat
// inverse(q: Quat) Quat
// rotate(q: Quat, v: Vec) Vec
// slerp(q0: Quat, q1: Quat, t: f32) Quat
// slerpV(q0: Quat, q1: Quat, t: F32x4) Quat
// quatToMat(quat: Quat) Mat
// quatToAxisAngle(quat: Quat, axis: *Vec, angle: *f32) void
// quatFromMat(m: Mat) Quat
// quatFromAxisAngle(axis: Vec, angle: f32) Quat
// quatFromNormAxisAngle(axis: Vec, angle: f32) Quat
// quatFromRollPitchYaw(pitch: f32, yaw: f32, roll: f32) Quat
// quatFromRollPitchYawV(angles: Vec) Quat
//
// ------------------------------------------------------------------------------
// 6. Color functions
// ------------------------------------------------------------------------------
//
// adjustSaturation(color: F32x4, saturation: f32) F32x4
// adjustContrast(color: F32x4, contrast: f32) F32x4
// rgbToHsl(rgb: F32x4) F32x4
// hslToRgb(hsl: F32x4) F32x4
// rgbToHsv(rgb: F32x4) F32x4
// hsvToRgb(hsv: F32x4) F32x4
// rgbToSrgb(rgb: F32x4) F32x4
// srgbToRgb(srgb: F32x4) F32x4
//
// ------------------------------------------------------------------------------
// X. Misc functions
// ------------------------------------------------------------------------------
//
// linePointDistance(linept0: Vec, linept1: Vec, pt: Vec) F32x4
// sin(v: f32) f32
// cos(v: f32) f32
// sincos(v: f32) [2]f32
// asin(v: f32) f32
// acos(v: f32) f32
//
// fftInitUnityTable(unitytable: []F32x4) void
// fft(re: []F32x4, im: []F32x4, unitytable: []const F32x4) void
// ifft(re: []F32x4, im: []const F32x4, unitytable: []const F32x4) void
//
// ==============================================================================
// Fundamental types
pub const F32x4 = @Vector(4, f32);
pub const F32x8 = @Vector(8, f32);
pub const F32x16 = @Vector(16, f32);
pub const Boolx4 = @Vector(4, bool);
pub const Boolx8 = @Vector(8, bool);
pub const Boolx16 = @Vector(16, bool);
// "Higher-level" aliases
pub const Vec = F32x4;
pub const Mat = [4]F32x4;
pub const Quat = F32x4;
const builtin = @import("builtin");
const std = @import("std");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const cpu_arch = builtin.cpu.arch;
const has_avx = if (cpu_arch == .x86_64) std.Target.x86.featureSetHas(builtin.cpu.features, .avx) else false;
const has_avx512f = if (cpu_arch == .x86_64) std.Target.x86.featureSetHas(builtin.cpu.features, .avx512f) else false;
const has_fma = if (cpu_arch == .x86_64) std.Target.x86.featureSetHas(builtin.cpu.features, .fma) else false;
// ------------------------------------------------------------------------------
//
// 1. Initialization functions
//
// ------------------------------------------------------------------------------
pub inline fn f32x4(e0: f32, e1: f32, e2: f32, e3: f32) F32x4 {
return .{ e0, e1, e2, e3 };
}
pub inline fn f32x8(e0: f32, e1: f32, e2: f32, e3: f32, e4: f32, e5: f32, e6: f32, e7: f32) F32x8 {
return .{ e0, e1, e2, e3, e4, e5, e6, e7 };
}
// zig fmt: off
pub inline fn f32x16(
e0: f32, e1: f32, e2: f32, e3: f32, e4: f32, e5: f32, e6: f32, e7: f32,
e8: f32, e9: f32, ea: f32, eb: f32, ec: f32, ed: f32, ee: f32, ef: f32) F32x16 {
return .{ e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef };
}
// zig fmt: on
pub inline fn f32x4s(e0: f32) F32x4 {
return splat(F32x4, e0);
}
pub inline fn f32x8s(e0: f32) F32x8 {
return splat(F32x8, e0);
}
pub inline fn f32x16s(e0: f32) F32x16 {
return splat(F32x16, e0);
}
pub inline fn boolx4(e0: bool, e1: bool, e2: bool, e3: bool) Boolx4 {
return .{ e0, e1, e2, e3 };
}
pub inline fn boolx8(e0: bool, e1: bool, e2: bool, e3: bool, e4: bool, e5: bool, e6: bool, e7: bool) Boolx8 {
return .{ e0, e1, e2, e3, e4, e5, e6, e7 };
}
// zig fmt: off
pub inline fn boolx16(
e0: bool, e1: bool, e2: bool, e3: bool, e4: bool, e5: bool, e6: bool, e7: bool,
e8: bool, e9: bool, ea: bool, eb: bool, ec: bool, ed: bool, ee: bool, ef: bool) Boolx16 {
return .{ e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef };
}
// zig fmt: on
pub inline fn veclen(comptime T: type) comptime_int {
return @typeInfo(T).vector.len;
}
pub inline fn splat(comptime T: type, value: f32) T {
return @splat(value);
}
pub inline fn splatInt(comptime T: type, value: u32) T {
return @splat(@bitCast(value));
}
pub fn load(mem: []const f32, comptime T: type, comptime len: u32) T {
var v = splat(T, 0.0);
const loop_len = if (len == 0) veclen(T) else len;
comptime var i: u32 = 0;
inline while (i < loop_len) : (i += 1) {
v[i] = mem[i];
}
return v;
}
test "zmath.load" {
const a = [7]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
var ptr = &a;
var i: u32 = 0;
const v0 = load(a[i..], F32x4, 2);
try expectVecEqual(v0, F32x4{ 1.0, 2.0, 0.0, 0.0 });
i += 2;
const v1 = load(a[i .. i + 2], F32x4, 2);
try expectVecEqual(v1, F32x4{ 3.0, 4.0, 0.0, 0.0 });
const v2 = load(a[5..7], F32x4, 2);
try expectVecEqual(v2, F32x4{ 6.0, 7.0, 0.0, 0.0 });
const v3 = load(ptr[1..], F32x4, 2);
try expectVecEqual(v3, F32x4{ 2.0, 3.0, 0.0, 0.0 });
i += 1;
const v4 = load(ptr[i .. i + 2], F32x4, 2);
try expectVecEqual(v4, F32x4{ 4.0, 5.0, 0.0, 0.0 });
}
pub fn store(mem: []f32, v: anytype, comptime len: u32) void {
const T = @TypeOf(v);
const loop_len = if (len == 0) veclen(T) else len;
comptime var i: u32 = 0;
inline while (i < loop_len) : (i += 1) {
mem[i] = v[i];
}
}
test "zmath.store" {
var a = [7]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
const v = load(a[1..], F32x4, 3);
store(a[2..], v, 4);
try expect(a[0] == 1.0);
try expect(a[1] == 2.0);
try expect(a[2] == 2.0);
try expect(a[3] == 3.0);
try expect(a[4] == 4.0);
try expect(a[5] == 0.0);
}
pub inline fn loadArr2(arr: [2]f32) F32x4 {
return f32x4(arr[0], arr[1], 0.0, 0.0);
}
pub inline fn loadArr2zw(arr: [2]f32, z: f32, w: f32) F32x4 {
return f32x4(arr[0], arr[1], z, w);
}
pub inline fn loadArr3(arr: [3]f32) F32x4 {
return f32x4(arr[0], arr[1], arr[2], 0.0);
}
pub inline fn loadArr3w(arr: [3]f32, w: f32) F32x4 {
return f32x4(arr[0], arr[1], arr[2], w);
}
pub inline fn loadArr4(arr: [4]f32) F32x4 {
return f32x4(arr[0], arr[1], arr[2], arr[3]);
}
pub inline fn storeArr2(arr: *[2]f32, v: F32x4) void {
arr.* = .{ v[0], v[1] };
}
pub inline fn storeArr3(arr: *[3]f32, v: F32x4) void {
arr.* = .{ v[0], v[1], v[2] };
}
pub inline fn storeArr4(arr: *[4]f32, v: F32x4) void {
arr.* = .{ v[0], v[1], v[2], v[3] };
}
pub inline fn arr3Ptr(ptr: anytype) *const [3]f32 {
comptime assert(@typeInfo(@TypeOf(ptr)) == .pointer);
const T = std.meta.Child(@TypeOf(ptr));
comptime assert(T == F32x4);
return @as(*const [3]f32, @ptrCast(ptr));
}
pub inline fn arrNPtr(ptr: anytype) [*]const f32 {
comptime assert(@typeInfo(@TypeOf(ptr)) == .pointer);
const T = std.meta.Child(@TypeOf(ptr));
comptime assert(T == Mat or T == F32x4 or T == F32x8 or T == F32x16);
return @as([*]const f32, @ptrCast(ptr));
}
test "zmath.arrNPtr" {
{
const mat = identity();
const f32ptr = arrNPtr(&mat);
try expect(f32ptr[0] == 1.0);
try expect(f32ptr[5] == 1.0);
try expect(f32ptr[10] == 1.0);
try expect(f32ptr[15] == 1.0);
}
{
const v8 = f32x8s(1.0);
const f32ptr = arrNPtr(&v8);
try expect(f32ptr[1] == 1.0);
try expect(f32ptr[7] == 1.0);
}
}
test "zmath.loadArr" {
{
const camera_position = [3]f32{ 1.0, 2.0, 3.0 };
const simd_reg = loadArr3(camera_position);
try expectVecEqual(simd_reg, f32x4(1.0, 2.0, 3.0, 0.0));
}
{
const camera_position = [3]f32{ 1.0, 2.0, 3.0 };
const simd_reg = loadArr3w(camera_position, 1.0);
try expectVecEqual(simd_reg, f32x4(1.0, 2.0, 3.0, 1.0));
}
}
pub inline fn vecToArr2(v: Vec) [2]f32 {
return .{ v[0], v[1] };
}
pub inline fn vecToArr3(v: Vec) [3]f32 {
return .{ v[0], v[1], v[2] };
}
pub inline fn vecToArr4(v: Vec) [4]f32 {
return .{ v[0], v[1], v[2], v[3] };
}
// ------------------------------------------------------------------------------
//
// 2. Functions that work on all vector components (F32xN = F32x4 or F32x8 or F32x16)
//
// ------------------------------------------------------------------------------
pub fn all(vb: anytype, comptime len: u32) bool {
const T = @TypeOf(vb);
if (len > veclen(T)) {
@compileError("zmath.all(): 'len' is greater than vector len of type " ++ @typeName(T));
}
const loop_len = if (len == 0) veclen(T) else len;
const ab: [veclen(T)]bool = vb;
comptime var i: u32 = 0;
var result = true;
inline while (i < loop_len) : (i += 1) {
result = result and ab[i];
}
return result;
}
test "zmath.all" {
try expect(all(boolx8(true, true, true, true, true, false, true, false), 5) == true);
try expect(all(boolx8(true, true, true, true, true, false, true, false), 6) == false);
try expect(all(boolx8(true, true, true, true, false, false, false, false), 4) == true);
try expect(all(boolx4(true, true, true, false), 3) == true);
try expect(all(boolx4(true, true, true, false), 1) == true);
try expect(all(boolx4(true, false, false, false), 1) == true);
try expect(all(boolx4(false, true, false, false), 1) == false);
try expect(all(boolx8(true, true, true, true, true, false, true, false), 0) == false);
try expect(all(boolx4(false, true, false, false), 0) == false);
try expect(all(boolx4(true, true, true, true), 0) == true);
}
pub fn any(vb: anytype, comptime len: u32) bool {
const T = @TypeOf(vb);
if (len > veclen(T)) {
@compileError("zmath.any(): 'len' is greater than vector len of type " ++ @typeName(T));
}
const loop_len = if (len == 0) veclen(T) else len;
const ab: [veclen(T)]bool = vb;
comptime var i: u32 = 0;
var result = false;
inline while (i < loop_len) : (i += 1) {
result = result or ab[i];
}
return result;
}
test "zmath.any" {
try expect(any(boolx8(true, true, true, true, true, false, true, false), 0) == true);
try expect(any(boolx8(false, false, false, true, true, false, true, false), 3) == false);
try expect(any(boolx8(false, false, false, false, false, true, false, false), 4) == false);
}
pub inline fn isNearEqual(
v0: anytype,
v1: anytype,
epsilon: anytype,
) @Vector(veclen(@TypeOf(v0)), bool) {
const T = @TypeOf(v0, v1, epsilon);
const delta = v0 - v1;
const temp = maxFast(delta, splat(T, 0.0) - delta);
return temp <= epsilon;
}
test "zmath.isNearEqual" {
{
const v0 = f32x4(1.0, 2.0, -3.0, 4.001);
const v1 = f32x4(1.0, 2.1, 3.0, 4.0);
const b = isNearEqual(v0, v1, splat(F32x4, 0.01));
try expect(@reduce(.And, b == boolx4(true, false, false, true)));
}
{
const v0 = f32x8(1.0, 2.0, -3.0, 4.001, 1.001, 2.3, -0.0, 0.0);
const v1 = f32x8(1.0, 2.1, 3.0, 4.0, -1.001, 2.1, 0.0, 0.0);
const b = isNearEqual(v0, v1, splat(F32x8, 0.01));
try expect(@reduce(.And, b == boolx8(true, false, false, true, false, false, true, true)));
}
try expect(all(isNearEqual(
splat(F32x4, math.inf(f32)),
splat(F32x4, math.inf(f32)),
splat(F32x4, 0.0001),
), 0) == false);
try expect(all(isNearEqual(
splat(F32x4, -math.inf(f32)),
splat(F32x4, math.inf(f32)),
splat(F32x4, 0.0001),
), 0) == false);
try expect(all(isNearEqual(
splat(F32x4, -math.inf(f32)),
splat(F32x4, -math.inf(f32)),
splat(F32x4, 0.0001),
), 0) == false);
try expect(all(isNearEqual(
splat(F32x4, -math.nan(f32)),
splat(F32x4, math.inf(f32)),
splat(F32x4, 0.0001),
), 0) == false);
}
pub inline fn isNan(
v: anytype,
) @Vector(veclen(@TypeOf(v)), bool) {
return v != v;
}
test "zmath.isNan" {
{
const v0 = f32x4(math.inf(f32), math.nan(f32), math.nan(f32), 7.0);
const b = isNan(v0);
try expect(@reduce(.And, b == boolx4(false, true, true, false)));
}
{
const v0 = f32x8(0, math.nan(f32), 0, 0, math.inf(f32), math.nan(f32), math.snan(f32), 7.0);
const b = isNan(v0);
try expect(@reduce(.And, b == boolx8(false, true, false, false, false, true, true, false)));
}
}
pub inline fn isInf(
v: anytype,
) @Vector(veclen(@TypeOf(v)), bool) {
const T = @TypeOf(v);
return abs(v) == splat(T, math.inf(f32));
}
test "zmath.isInf" {
{
const v0 = f32x4(math.inf(f32), math.nan(f32), math.snan(f32), 7.0);
const b = isInf(v0);
try expect(@reduce(.And, b == boolx4(true, false, false, false)));
}
{
const v0 = f32x8(0, math.inf(f32), 0, 0, math.inf(f32), math.nan(f32), math.snan(f32), 7.0);
const b = isInf(v0);
try expect(@reduce(.And, b == boolx8(false, true, false, false, true, false, false, false)));
}
}
pub inline fn isInBounds(
v: anytype,
bounds: anytype,
) @Vector(veclen(@TypeOf(v)), bool) {
const T = @TypeOf(v, bounds);
const Tu = @Vector(veclen(T), u1);
const Tr = @Vector(veclen(T), bool);
// 2 x cmpleps, xorps, load, andps
const b0 = v <= bounds;
const b1 = (bounds * splat(T, -1.0)) <= v;
const b0u = @as(Tu, @bitCast(b0));
const b1u = @as(Tu, @bitCast(b1));
return @as(Tr, @bitCast(b0u & b1u));
}
test "zmath.isInBounds" {
{
const v0 = f32x4(0.5, -2.0, -1.0, 1.9);
const v1 = f32x4(-1.6, -2.001, -1.0, 1.9);
const bounds = f32x4(1.0, 2.0, 1.0, 2.0);
const b0 = isInBounds(v0, bounds);
const b1 = isInBounds(v1, bounds);
try expect(@reduce(.And, b0 == boolx4(true, true, true, true)));
try expect(@reduce(.And, b1 == boolx4(false, false, true, true)));
}
{
const v0 = f32x8(2.0, 1.0, 2.0, 1.0, 0.5, -2.0, -1.0, 1.9);
const bounds = f32x8(1.0, 1.0, 1.0, math.inf(f32), 1.0, math.nan(f32), 1.0, 2.0);
const b0 = isInBounds(v0, bounds);
try expect(@reduce(.And, b0 == boolx8(false, true, false, true, true, false, true, true)));
}
}
pub inline fn andInt(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
const T = @TypeOf(v0, v1);
const Tu = @Vector(veclen(T), u32);
const v0u = @as(Tu, @bitCast(v0));
const v1u = @as(Tu, @bitCast(v1));
return @as(T, @bitCast(v0u & v1u)); // andps
}
test "zmath.andInt" {
{
const v0 = f32x4(0, @as(f32, @bitCast(~@as(u32, 0))), 0, @as(f32, @bitCast(~@as(u32, 0))));
const v1 = f32x4(1.0, 2.0, 3.0, math.inf(f32));
const v = andInt(v0, v1);
try expect(v[3] == math.inf(f32));
try expectVecEqual(v, f32x4(0.0, 2.0, 0.0, math.inf(f32)));
}
{
const v0 = f32x8(0, 0, 0, 0, 0, @as(f32, @bitCast(~@as(u32, 0))), 0, @as(f32, @bitCast(~@as(u32, 0))));
const v1 = f32x8(0, 0, 0, 0, 1.0, 2.0, 3.0, math.inf(f32));
const v = andInt(v0, v1);
try expect(v[7] == math.inf(f32));
try expectVecEqual(v, f32x8(0, 0, 0, 0, 0.0, 2.0, 0.0, math.inf(f32)));
}
}
pub inline fn andNotInt(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
const T = @TypeOf(v0, v1);
const Tu = @Vector(veclen(T), u32);
const v0u = @as(Tu, @bitCast(v0));
const v1u = @as(Tu, @bitCast(v1));
return @as(T, @bitCast(~v0u & v1u)); // andnps
}
test "zmath.andNotInt" {
{
const v0 = f32x4(1.0, 2.0, 3.0, 4.0);
const v1 = f32x4(0, @as(f32, @bitCast(~@as(u32, 0))), 0, @as(f32, @bitCast(~@as(u32, 0))));
const v = andNotInt(v1, v0);
try expectVecEqual(v, f32x4(1.0, 0.0, 3.0, 0.0));
}
{
const v0 = f32x8(0, 0, 0, 0, 1.0, 2.0, 3.0, 4.0);
const v1 = f32x8(0, 0, 0, 0, 0, @as(f32, @bitCast(~@as(u32, 0))), 0, @as(f32, @bitCast(~@as(u32, 0))));
const v = andNotInt(v1, v0);
try expectVecEqual(v, f32x8(0, 0, 0, 0, 1.0, 0.0, 3.0, 0.0));
}
}
pub inline fn orInt(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
const T = @TypeOf(v0, v1);
const Tu = @Vector(veclen(T), u32);
const v0u = @as(Tu, @bitCast(v0));
const v1u = @as(Tu, @bitCast(v1));
return @as(T, @bitCast(v0u | v1u)); // orps
}
test "zmath.orInt" {
{
const v0 = f32x4(0, @as(f32, @bitCast(~@as(u32, 0))), 0, 0);
const v1 = f32x4(1.0, 2.0, 3.0, 4.0);
const v = orInt(v0, v1);
try expect(v[0] == 1.0);
try expect(@as(u32, @bitCast(v[1])) == ~@as(u32, 0));
try expect(v[2] == 3.0);
try expect(v[3] == 4.0);
}
{
const v0 = f32x8(0, 0, 0, 0, 0, @as(f32, @bitCast(~@as(u32, 0))), 0, 0);
const v1 = f32x8(0, 0, 0, 0, 1.0, 2.0, 3.0, 4.0);
const v = orInt(v0, v1);
try expect(v[4] == 1.0);
try expect(@as(u32, @bitCast(v[5])) == ~@as(u32, 0));
try expect(v[6] == 3.0);
try expect(v[7] == 4.0);
}
}
pub inline fn norInt(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
const T = @TypeOf(v0, v1);
const Tu = @Vector(veclen(T), u32);
const v0u = @as(Tu, @bitCast(v0));
const v1u = @as(Tu, @bitCast(v1));
return @as(T, @bitCast(~(v0u | v1u))); // por, pcmpeqd, pxor
}
pub inline fn xorInt(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
const T = @TypeOf(v0, v1);
const Tu = @Vector(veclen(T), u32);
const v0u = @as(Tu, @bitCast(v0));
const v1u = @as(Tu, @bitCast(v1));
return @as(T, @bitCast(v0u ^ v1u)); // xorps
}
test "zmath.xorInt" {
{
const v0 = f32x4(1.0, @as(f32, @bitCast(~@as(u32, 0))), 0, 0);
const v1 = f32x4(1.0, 0, 0, 0);
const v = xorInt(v0, v1);
try expect(v[0] == 0.0);
try expect(@as(u32, @bitCast(v[1])) == ~@as(u32, 0));
try expect(v[2] == 0.0);
try expect(v[3] == 0.0);
}
{
const v0 = f32x8(0, 0, 0, 0, 1.0, @as(f32, @bitCast(~@as(u32, 0))), 0, 0);
const v1 = f32x8(0, 0, 0, 0, 1.0, 0, 0, 0);
const v = xorInt(v0, v1);
try expect(v[4] == 0.0);
try expect(@as(u32, @bitCast(v[5])) == ~@as(u32, 0));
try expect(v[6] == 0.0);
try expect(v[7] == 0.0);
}
}
pub inline fn minFast(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
return select(v0 < v1, v0, v1); // minps
}
test "zmath.minFast" {
{
const v0 = f32x4(1.0, 3.0, 2.0, 7.0);
const v1 = f32x4(2.0, 1.0, 4.0, math.inf(f32));
const v = minFast(v0, v1);
try expectVecEqual(v, f32x4(1.0, 1.0, 2.0, 7.0));
}
{
const v0 = f32x4(1.0, math.nan(f32), 5.0, math.snan(f32));
const v1 = f32x4(2.0, 1.0, 4.0, math.inf(f32));
const v = minFast(v0, v1);
try expect(v[0] == 1.0);
try expect(v[1] == 1.0);
try expect(!math.isNan(v[1]));
try expect(v[2] == 4.0);
try expect(v[3] == math.inf(f32));
try expect(!math.isNan(v[3]));
}
}
pub inline fn maxFast(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
return select(v0 > v1, v0, v1); // maxps
}
test "zmath.maxFast" {
{
const v0 = f32x4(1.0, 3.0, 2.0, 7.0);
const v1 = f32x4(2.0, 1.0, 4.0, math.inf(f32));
const v = maxFast(v0, v1);
try expectVecEqual(v, f32x4(2.0, 3.0, 4.0, math.inf(f32)));
}
{
const v0 = f32x4(1.0, math.nan(f32), 5.0, math.snan(f32));
const v1 = f32x4(2.0, 1.0, 4.0, math.inf(f32));
const v = maxFast(v0, v1);
try expect(v[0] == 2.0);
try expect(v[1] == 1.0);
try expect(v[2] == 5.0);
try expect(v[3] == math.inf(f32));
try expect(!math.isNan(v[3]));
}
}
pub inline fn min(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
// This will handle inf & nan
return @min(v0, v1); // minps, cmpunordps, andps, andnps, orps
}
test "zmath.min" {
// Calling math.inf causes test to fail!
if (builtin.target.os.tag == .macos and builtin.target.cpu.arch == .aarch64) return error.SkipZigTest;
{
const v0 = f32x4(1.0, 3.0, 2.0, 7.0);
const v1 = f32x4(2.0, 1.0, 4.0, math.inf(f32));
const v = min(v0, v1);
try expectVecEqual(v, f32x4(1.0, 1.0, 2.0, 7.0));
}
{
const v0 = f32x8(0, 0, -2.0, 0, 1.0, 3.0, 2.0, 7.0);
const v1 = f32x8(0, 1.0, 0, 0, 2.0, 1.0, 4.0, math.inf(f32));
const v = min(v0, v1);
try expectVecEqual(v, f32x8(0.0, 0.0, -2.0, 0.0, 1.0, 1.0, 2.0, 7.0));
}
{
const v0 = f32x4(1.0, math.nan(f32), 5.0, math.snan(f32));
const v1 = f32x4(2.0, 1.0, 4.0, math.inf(f32));
const v = min(v0, v1);
try expect(v[0] == 1.0);
try expect(v[1] == 1.0);
try expect(!math.isNan(v[1]));
try expect(v[2] == 4.0);
try expect(v[3] == math.inf(f32));
try expect(!math.isNan(v[3]));
}
{
const v0 = f32x4(-math.inf(f32), math.inf(f32), math.inf(f32), math.snan(f32));
const v1 = f32x4(math.snan(f32), -math.inf(f32), math.snan(f32), math.nan(f32));
const v = min(v0, v1);
try expect(v[0] == -math.inf(f32));
try expect(v[1] == -math.inf(f32));
try expect(v[2] == math.inf(f32));
try expect(!math.isNan(v[2]));
try expect(math.isNan(v[3]));
try expect(!math.isInf(v[3]));
}
}
pub inline fn max(v0: anytype, v1: anytype) @TypeOf(v0, v1) {
// This will handle inf & nan
return @max(v0, v1); // maxps, cmpunordps, andps, andnps, orps
}
test "zmath.max" {
// Calling math.inf causes test to fail!
if (builtin.target.os.tag == .macos and builtin.target.cpu.arch == .aarch64) return error.SkipZigTest;
{
const v0 = f32x4(1.0, 3.0, 2.0, 7.0);
const v1 = f32x4(2.0, 1.0, 4.0, math.inf(f32));
const v = max(v0, v1);
try expectVecEqual(v, f32x4(2.0, 3.0, 4.0, math.inf(f32)));
}
{
const v0 = f32x8(0, 0, -2.0, 0, 1.0, 3.0, 2.0, 7.0);
const v1 = f32x8(0, 1.0, 0, 0, 2.0, 1.0, 4.0, math.inf(f32));
const v = max(v0, v1);
try expectVecEqual(v, f32x8(0.0, 1.0, 0.0, 0.0, 2.0, 3.0, 4.0, math.inf(f32)));
}
{
const v0 = f32x4(1.0, math.nan(f32), 5.0, math.snan(f32));
const v1 = f32x4(2.0, 1.0, 4.0, math.inf(f32));
const v = max(v0, v1);
try expect(v[0] == 2.0);
try expect(v[1] == 1.0);
try expect(v[2] == 5.0);
try expect(v[3] == math.inf(f32));
try expect(!math.isNan(v[3]));
}
{
const v0 = f32x4(-math.inf(f32), math.inf(f32), math.inf(f32), math.snan(f32));
const v1 = f32x4(math.snan(f32), -math.inf(f32), math.snan(f32), math.nan(f32));
const v = max(v0, v1);
try expect(v[0] == -math.inf(f32));
try expect(v[1] == math.inf(f32));
try expect(v[2] == math.inf(f32));
try expect(!math.isNan(v[2]));
try expect(math.isNan(v[3]));
try expect(!math.isInf(v[3]));
}
}
pub fn round(v: anytype) @TypeOf(v) {
const T = @TypeOf(v);
if (cpu_arch == .x86_64 and has_avx) {
if (T == F32x4) {
return asm ("vroundps $0, %%xmm0, %%xmm0"
: [ret] "={xmm0}" (-> T),
: [v] "{xmm0}" (v),
);
} else if (T == F32x8) {
return asm ("vroundps $0, %%ymm0, %%ymm0"
: [ret] "={ymm0}" (-> T),
: [v] "{ymm0}" (v),
);
} else if (T == F32x16 and has_avx512f) {
return asm ("vrndscaleps $0, %%zmm0, %%zmm0"
: [ret] "={zmm0}" (-> T),
: [v] "{zmm0}" (v),
);
} else if (T == F32x16 and !has_avx512f) {
const arr: [16]f32 = v;
var ymm0 = @as(F32x8, arr[0..8].*);
var ymm1 = @as(F32x8, arr[8..16].*);
ymm0 = asm ("vroundps $0, %%ymm0, %%ymm0"
: [ret] "={ymm0}" (-> F32x8),
: [v] "{ymm0}" (ymm0),
);
ymm1 = asm ("vroundps $0, %%ymm1, %%ymm1"
: [ret] "={ymm1}" (-> F32x8),
: [v] "{ymm1}" (ymm1),
);
return @shuffle(f32, ymm0, ymm1, [16]i32{ 0, 1, 2, 3, 4, 5, 6, 7, -1, -2, -3, -4, -5, -6, -7, -8 });
}
} else {
const sign = andInt(v, splatNegativeZero(T));
const magic = orInt(splatNoFraction(T), sign);
var r1 = v + magic;
r1 = r1 - magic;
const r2 = abs(v);
const mask = r2 <= splatNoFraction(T);
return select(mask, r1, v);
}
}
test "zmath.round" {
{
try expect(all(round(splat(F32x4, math.inf(f32))) == splat(F32x4, math.inf(f32)), 0));
try expect(all(round(splat(F32x4, -math.inf(f32))) == splat(F32x4, -math.inf(f32)), 0));
try expect(all(isNan(round(splat(F32x4, math.nan(f32)))), 0));
try expect(all(isNan(round(splat(F32x4, -math.nan(f32)))), 0));
try expect(all(isNan(round(splat(F32x4, math.snan(f32)))), 0));
try expect(all(isNan(round(splat(F32x4, -math.snan(f32)))), 0));
}
{
const v = round(f32x16(1.1, -1.1, -1.5, 1.5, 2.1, 2.8, 2.9, 4.1, 5.8, 6.1, 7.9, 8.9, 10.1, 11.2, 12.7, 13.1));
try expectVecApproxEqAbs(
v,
f32x16(1.0, -1.0, -2.0, 2.0, 2.0, 3.0, 3.0, 4.0, 6.0, 6.0, 8.0, 9.0, 10.0, 11.0, 13.0, 13.0),
0.0,
);
}
var v = round(f32x4(1.1, -1.1, -1.5, 1.5));
try expectVecEqual(v, f32x4(1.0, -1.0, -2.0, 2.0));
const v1 = f32x4(-10_000_000.1, -math.inf(f32), 10_000_001.5, math.inf(f32));
v = round(v1);
try expect(v[3] == math.inf(f32));
try expectVecEqual(v, f32x4(-10_000_000.1, -math.inf(f32), 10_000_001.5, math.inf(f32)));
const v2 = f32x4(-math.snan(f32), math.snan(f32), math.nan(f32), -math.inf(f32));
v = round(v2);
try expect(math.isNan(v2[0]));
try expect(math.isNan(v2[1]));
try expect(math.isNan(v2[2]));
try expect(v2[3] == -math.inf(f32));
const v3 = f32x4(1001.5, -201.499, -10000.99, -101.5);
v = round(v3);
try expectVecEqual(v, f32x4(1002.0, -201.0, -10001.0, -102.0));
const v4 = f32x4(-1_388_609.9, 1_388_609.5, 1_388_109.01, 2_388_609.5);
v = round(v4);
try expectVecEqual(v, f32x4(-1_388_610.0, 1_388_610.0, 1_388_109.0, 2_388_610.0));
var f: f32 = -100.0;
var i: u32 = 0;
while (i < 100) : (i += 1) {
const vr = round(splat(F32x4, f));
const fr = @round(splat(F32x4, f));
const vr8 = round(splat(F32x8, f));
const fr8 = @round(splat(F32x8, f));
const vr16 = round(splat(F32x16, f));
const fr16 = @round(splat(F32x16, f));
try expectVecEqual(vr, fr);
try expectVecEqual(vr8, fr8);
try expectVecEqual(vr16, fr16);
f += 0.12345 * @as(f32, @floatFromInt(i));
}
}
pub fn trunc(v: anytype) @TypeOf(v) {
const T = @TypeOf(v);
if (cpu_arch == .x86_64 and has_avx) {
if (T == F32x4) {
return asm ("vroundps $3, %%xmm0, %%xmm0"
: [ret] "={xmm0}" (-> T),
: [v] "{xmm0}" (v),
);
} else if (T == F32x8) {
return asm ("vroundps $3, %%ymm0, %%ymm0"
: [ret] "={ymm0}" (-> T),
: [v] "{ymm0}" (v),
);
} else if (T == F32x16 and has_avx512f) {
return asm ("vrndscaleps $3, %%zmm0, %%zmm0"
: [ret] "={zmm0}" (-> T),
: [v] "{zmm0}" (v),
);
} else if (T == F32x16 and !has_avx512f) {
const arr: [16]f32 = v;
var ymm0 = @as(F32x8, arr[0..8].*);
var ymm1 = @as(F32x8, arr[8..16].*);
ymm0 = asm ("vroundps $3, %%ymm0, %%ymm0"
: [ret] "={ymm0}" (-> F32x8),
: [v] "{ymm0}" (ymm0),
);
ymm1 = asm ("vroundps $3, %%ymm1, %%ymm1"
: [ret] "={ymm1}" (-> F32x8),