-
Notifications
You must be signed in to change notification settings - Fork 21
/
simd_utils_avx512_float.h
3636 lines (3178 loc) · 156 KB
/
simd_utils_avx512_float.h
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
/*
* Project : SIMD_Utils
* Version : 0.2.5
* Author : JishinMaster
* Licence : BSD-2
*/
#pragma once
#include <stdint.h>
#include "immintrin.h"
static inline v16sf log10512_ps(v16sf x)
{
v16sf invalid_mask = (v16sf) _mm512_movm_epi32(_mm512_cmp_ps_mask(x, _mm512_setzero_ps(), _CMP_LE_OS));
x = _mm512_max_ps(x, *(v16sf *) _ps512_min_norm_pos); /* cut off denormalized stuff */
/* get the exponent part */
v16sf e = _mm512_getexp_ps(x);
/* keep only the fractional part */
x = _mm512_and_ps(x, *(v16sf *) _ps512_inv_mant_mask);
x = _mm512_or_ps(x, *(v16sf *) _ps512_0p5);
__mmask16 kmask = _mm512_cmp_ps_mask(x, *(v16sf *) _ps512_cephes_SQRTHF, _CMP_LT_OS);
v16sf tmp = x;
#if 1
//instead of doing add 1 then sub 1, dot add 1 on condition, should be faster
__mmask16 knotmask = _knot_mask16(kmask);
e = _mm512_mask_add_ps(e, knotmask, e, *(v16sf *) _ps512_1);
#else
e = _mm512_add_ps(e, *(v16sf *) _ps512_1);
e = _mm512_mask_sub_ps(e, kmask, e, *(v16sf *) _ps512_1);
#endif
x = _mm512_sub_ps(x, *(v16sf *) _ps512_1);
x = _mm512_mask_add_ps(x, kmask, x, tmp);
v16sf z = _mm512_mul_ps(x, x);
v16sf y = _mm512_fmadd_ps_custom(*(v16sf *) _ps512_cephes_log_p0, x, *(v16sf *) _ps512_cephes_log_p1);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p2);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p3);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p4);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p5);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p6);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p7);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p8);
y = _mm512_mul_ps(y, x);
y = _mm512_mul_ps(y, z);
y = _mm512_fnmadd_ps_custom(z, *(v16sf *) _ps512_0p5, y);
// Could it be improved with more parallelism or would it worsen precision?
tmp = _mm512_add_ps(x, y);
z = _mm512_mul_ps(tmp, *(v16sf *) _ps512_cephes_L10EB);
z = _mm512_fmadd_ps_custom(y, *(v16sf *) _ps512_cephes_L10EA, z);
z = _mm512_fmadd_ps_custom(x, *(v16sf *) _ps512_cephes_L10EA, z);
z = _mm512_fmadd_ps_custom(e, *(v16sf *) _ps512_cephes_L102B, z);
x = _mm512_fmadd_ps_custom(e, *(v16sf *) _ps512_cephes_L102A, z);
x = _mm512_or_ps(x, invalid_mask); // negative arg will be NAN
return x;
}
static inline v16sf log2512_ps(v16sf x)
{
v16sf invalid_mask = (v16sf) _mm512_movm_epi32(_mm512_cmp_ps_mask(x, _mm512_setzero_ps(), _CMP_LE_OS));
x = _mm512_max_ps(x, *(v16sf *) _ps512_min_norm_pos); /* cut off denormalized stuff */
/* get the exponent part */
v16sf e = _mm512_getexp_ps(x);
/* keep only the fractional part */
x = _mm512_and_ps(x, *(v16sf *) _ps512_inv_mant_mask);
x = _mm512_or_ps(x, *(v16sf *) _ps512_0p5);
__mmask16 kmask = _mm512_cmp_ps_mask(x, *(v16sf *) _ps512_cephes_SQRTHF, _CMP_LT_OS);
v16sf tmp = x;
#if 1
//instead of doing add 1 then sub 1, dot add 1 on condition, should be faster
__mmask16 knotmask = _knot_mask16(kmask);
e = _mm512_mask_add_ps(e, knotmask, e, *(v16sf *) _ps512_1);
#else
e = _mm512_add_ps(e, *(v16sf *) _ps512_1);
e = _mm512_mask_sub_ps(e, kmask, e, *(v16sf *) _ps512_1);
#endif
x = _mm512_sub_ps(x, *(v16sf *) _ps512_1);
x = _mm512_mask_add_ps(x, kmask, x, tmp);
v16sf z = _mm512_mul_ps(x, x);
v16sf y = _mm512_fmadd_ps_custom(*(v16sf *) _ps512_cephes_log_p0, x, *(v16sf *) _ps512_cephes_log_p1);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p2);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p3);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p4);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p5);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p6);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p7);
y = _mm512_fmadd_ps_custom(y, x, *(v16sf *) _ps512_cephes_log_p8);
y = _mm512_mul_ps(y, x);
y = _mm512_mul_ps(y, z);
y = _mm512_fnmadd_ps_custom(z, *(v16sf *) _ps512_0p5, y);
// Could it be improved with more parallelism or would it worsen precision?
tmp = _mm512_add_ps(y, x);
z = _mm512_mul_ps(y, *(v16sf *) _ps512_cephes_LOG2EA);
z = _mm512_fmadd_ps_custom(x, *(v16sf *) _ps512_cephes_LOG2EA, z);
z = _mm512_add_ps(z, tmp);
x = _mm512_add_ps(z, e);
x = _mm512_or_ps(x, invalid_mask); // negative arg will be NAN
return x;
}
static inline void log10512f(float *src, float *dst, int len)
{
const v16sf invln10f = _mm512_set1_ps((float) INVLN10); //_mm512_broadcast_ss(&invln10f_mask);
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf src_tmp = log512_ps(_mm512_load_ps(src + i));
_mm512_store_ps(dst + i, _mm512_mul_ps(src_tmp, invln10f));
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf src_tmp = log512_ps(_mm512_loadu_ps(src + i));
_mm512_storeu_ps(dst + i, _mm512_mul_ps(src_tmp, invln10f));
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = log10f(src[i]);
}
}
static inline void log10512f_precise(float *src, float *dst, int len)
{
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf src_tmp = log10512_ps(_mm512_load_ps(src + i));
_mm512_store_ps(dst + i, src_tmp);
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf src_tmp = log10512_ps(_mm512_loadu_ps(src + i));
_mm512_storeu_ps(dst + i, src_tmp);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = log10f(src[i]);
}
}
static inline void log2512f(float *src, float *dst, int len)
{
const v16sf invln2f = _mm512_set1_ps((float) INVLN2);
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf src_tmp = log512_ps(_mm512_load_ps(src + i));
_mm512_store_ps(dst + i, _mm512_mul_ps(src_tmp, invln2f));
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf src_tmp = log512_ps(_mm512_loadu_ps(src + i));
_mm512_storeu_ps(dst + i, _mm512_mul_ps(src_tmp, invln2f));
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = log2f(src[i]);
}
}
static inline void log2512f_precise(float *src, float *dst, int len)
{
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf src_tmp = log2512_ps(_mm512_load_ps(src + i));
_mm512_store_ps(dst + i, src_tmp);
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf src_tmp = log2512_ps(_mm512_loadu_ps(src + i));
_mm512_storeu_ps(dst + i, src_tmp);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = log2f(src[i]);
}
}
static inline void ln512f(float *src, float *dst, int len)
{
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_store_ps(dst + i, log512_ps(_mm512_load_ps(src + i)));
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_storeu_ps(dst + i, log512_ps(_mm512_loadu_ps(src + i)));
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = logf(src[i]);
}
}
static inline void exp512f(float *src, float *dst, int len)
{
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_store_ps(dst + i, exp512_ps(_mm512_load_ps(src + i)));
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_storeu_ps(dst + i, exp512_ps(_mm512_loadu_ps(src + i)));
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = expf(src[i]);
}
}
static inline v16sf power_of_two512f(v16si b)
{
return _mm512_cvtepi32_ps(_mm512_sllv_epi32(*(v16si *) _pi32_512_1, b));
}
static inline v16sf cbrt512f_ps(v16sf xx)
{
v16sf e, rem;
//__mmask16 sign;
v16sf x, z;
v16sf sign;
x = xx;
// sign = _mm512_cmp_ps_mask(x, _mm512_setzero_ps(), _CMP_GT_OS);
sign = _mm512_and_ps(xx, *(v16sf *) _ps512_sign_mask);
x = _mm512_and_ps(x, *(v16sf *) _ps512_pos_sign_mask);
z = x;
/* extract power of 2, leaving
* mantissa between 0.5 and 1
*/
// x = frexpf(x, &e);
// solve problem for zero
v16si emm0 = _mm512_srli_epi32(_mm512_castps_si512(x), 23);
emm0 = _mm512_sub_epi32(emm0, *(v16si *) _pi32_512_0x7e);
e = _mm512_cvtepi32_ps(emm0);
x = _mm512_and_ps(x, *(v16sf *) _ps512_inv_mant_mask);
x = _mm512_or_ps(x, *(v16sf *) _ps512_0p5);
/* Approximate cube root of number between .5 and 1,
* peak relative error = 9.2e-6
*/
v16sf tmp;
tmp = _mm512_fmadd_ps_custom(*(v16sf *) _ps512_CBRTF_P0, x, *(v16sf *) _ps512_CBRTF_P1);
tmp = _mm512_fmadd_ps_custom(x, tmp, *(v16sf *) _ps512_CBRTF_P2);
tmp = _mm512_fmadd_ps_custom(x, tmp, *(v16sf *) _ps512_CBRTF_P3);
x = _mm512_fmadd_ps_custom(x, tmp, *(v16sf *) _ps512_CBRTF_P4);
/* exponent divided by 3 */
__mmask16 e_sign = _mm512_cmp_ps_mask(e, _mm512_setzero_ps(), _CMP_GE_OS);
e = _mm512_and_ps(e, *(v16sf *) _ps512_pos_sign_mask);
rem = e;
e = _mm512_mul_ps(e, *(v16sf *) _ps512_0p3);
e = _mm512_roundscale_ps(e, ROUNDTOZERO);
rem = _mm512_fnmadd_ps_custom(*(v16sf *) _ps512_3, e, rem);
v16sf mul_cst1 = _mm512_mask_blend_ps(e_sign, *(v16sf *) _ps512_cephes_invCBRT2, *(v16sf *) _ps512_cephes_CBRT2);
v16sf mul_cst2 = _mm512_mask_blend_ps(e_sign, *(v16sf *) _ps512_cephes_invCBRT4, *(v16sf *) _ps512_cephes_CBRT4);
v16si remi = _mm512_cvtps_epi32(rem); // rem integer
__mmask16 rem1 = _mm512_cmpeq_epi32_mask(remi, _mm512_set1_epi32(1));
__mmask16 rem2 = _mm512_cmpeq_epi32_mask(remi, _mm512_set1_epi32(2));
x = _mm512_mask_mul_ps(x, rem1, x, mul_cst1); // rem==1
x = _mm512_mask_mul_ps(x, rem2, x, mul_cst2); // rem==2
/* multiply by power of 2 */
// x = ldexpf(x, e);
v16sf cst = power_of_two512f(_mm512_cvtps_epi32(e));
// blend sign of e
tmp = _mm512_div_ps(x, cst);
x = _mm512_mask_mul_ps(tmp, e_sign, x, cst);
/* Newton iteration */
// x -= (x - (z / (x * x))) * 0.333333333333;
v16sf tmp2 = _mm512_mul_ps(x, x);
tmp2 = _mm512_div_ps(z, tmp2);
tmp2 = _mm512_sub_ps(x, tmp2);
x = _mm512_fnmadd_ps_custom(tmp2, *(v16sf *) _ps512_0p3, x);
// x = _mm512_mask_blend_ps(sign, _mm512_mul_ps(x, *(v16sf *) _ps512_min1), x);
x = _mm512_xor_ps(x, sign);
return x;
}
static inline void cbrt512f(float *src, float *dst, int len)
{
int stop_len = len / (AVX512_LEN_FLOAT);
stop_len *= (AVX512_LEN_FLOAT);
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf x = _mm512_load_ps(src + i);
v16sf dst_tmp = cbrt512f_ps(x);
_mm512_store_ps(dst + i, dst_tmp);
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf x = _mm512_loadu_ps(src + i);
v16sf dst_tmp = cbrt512f_ps(x);
_mm512_storeu_ps(dst + i, dst_tmp);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = cbrtf(src[i]);
}
}
static inline void fabs512f(float *src, float *dst, int len)
{
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf src_tmp = _mm512_load_ps(src + i);
v16sf src_tmp2 = _mm512_load_ps(src + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_and_ps(*(v16sf *) _ps512_pos_sign_mask, src_tmp);
v16sf dst_tmp2 = _mm512_and_ps(*(v16sf *) _ps512_pos_sign_mask, src_tmp2);
_mm512_store_ps(dst + i, dst_tmp);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf src_tmp = _mm512_loadu_ps(src + i);
v16sf src_tmp2 = _mm512_loadu_ps(src + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_and_ps(*(v16sf *) _ps512_pos_sign_mask, src_tmp);
v16sf dst_tmp2 = _mm512_and_ps(*(v16sf *) _ps512_pos_sign_mask, src_tmp2);
_mm512_storeu_ps(dst + i, dst_tmp);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = fabsf(src[i]);
}
}
static inline void set512f(float *dst, float value, int len)
{
const v16sf tmp = _mm512_set1_ps(value); //_mm512_broadcast_ss(&value); //avx broadcast vs mm_set_ps?
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (isAligned((uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_store_ps(dst + i, tmp);
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_storeu_ps(dst + i, tmp);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = value;
}
}
static inline void zero512f(float *dst, int len)
{
const v16sf tmp = _mm512_setzero_ps();
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (isAligned((uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_store_ps(dst + i, tmp);
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_storeu_ps(dst + i, tmp);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = 0.0f;
}
}
static inline void copy512f(float *src, float *dst, int len)
{
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf src_tmp = _mm512_load_ps(src + i);
v16sf src_tmp2 = _mm512_load_ps(src + i + AVX512_LEN_FLOAT);
_mm512_store_ps(dst + i, src_tmp);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, src_tmp2);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf src_tmp = _mm512_loadu_ps(src + i);
v16sf src_tmp2 = _mm512_loadu_ps(src + i + AVX512_LEN_FLOAT);
_mm512_storeu_ps(dst + i, src_tmp);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, src_tmp2);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = src[i];
}
}
static inline void add512f(float *src1, float *src2, float *dst, int len)
{
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (areAligned3((uintptr_t) (src1), (uintptr_t) (src2), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_load_ps(src1 + i);
v16sf b = _mm512_load_ps(src2 + i);
v16sf a2 = _mm512_load_ps(src1 + i + AVX512_LEN_FLOAT);
v16sf b2 = _mm512_load_ps(src2 + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_add_ps(a, b);
v16sf dst_tmp2 = _mm512_add_ps(a2, b2);
_mm512_store_ps(dst + i, dst_tmp);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(src1 + i);
v16sf b = _mm512_loadu_ps(src2 + i);
v16sf a2 = _mm512_loadu_ps(src1 + i + AVX512_LEN_FLOAT);
v16sf b2 = _mm512_loadu_ps(src2 + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_add_ps(a, b);
v16sf dst_tmp2 = _mm512_add_ps(a2, b2);
_mm512_storeu_ps(dst + i, dst_tmp);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = src1[i] + src2[i];
}
}
static inline void mul512f(float *src1, float *src2, float *dst, int len)
{
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (areAligned3((uintptr_t) (src1), (uintptr_t) (src2), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_load_ps(src1 + i);
v16sf b = _mm512_load_ps(src2 + i);
v16sf a2 = _mm512_load_ps(src1 + i + AVX512_LEN_FLOAT);
v16sf b2 = _mm512_load_ps(src2 + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_mul_ps(a, b);
v16sf dst_tmp2 = _mm512_mul_ps(a2, b2);
_mm512_store_ps(dst + i, dst_tmp);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(src1 + i);
v16sf b = _mm512_loadu_ps(src2 + i);
v16sf a2 = _mm512_loadu_ps(src1 + i + AVX512_LEN_FLOAT);
v16sf b2 = _mm512_loadu_ps(src2 + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_mul_ps(a, b);
v16sf dst_tmp2 = _mm512_mul_ps(a2, b2);
_mm512_storeu_ps(dst + i, dst_tmp);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = src1[i] * src2[i];
}
}
static inline void sub512f(float *src1, float *src2, float *dst, int len)
{
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (areAligned3((uintptr_t) (src1), (uintptr_t) (src2), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_load_ps(src1 + i);
v16sf b = _mm512_load_ps(src2 + i);
v16sf a2 = _mm512_load_ps(src1 + i + AVX512_LEN_FLOAT);
v16sf b2 = _mm512_load_ps(src2 + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_sub_ps(a, b);
v16sf dst_tmp2 = _mm512_sub_ps(a2, b2);
_mm512_store_ps(dst + i, dst_tmp);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(src1 + i);
v16sf b = _mm512_loadu_ps(src2 + i);
v16sf a2 = _mm512_loadu_ps(src1 + i + AVX512_LEN_FLOAT);
v16sf b2 = _mm512_loadu_ps(src2 + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_sub_ps(a, b);
v16sf dst_tmp2 = _mm512_sub_ps(a2, b2);
_mm512_storeu_ps(dst + i, dst_tmp);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = src1[i] - src2[i];
}
}
static inline void addc512f(float *src, float value, float *dst, int len)
{
const v16sf tmp = _mm512_set1_ps(value); //_mm512_broadcast_ss(&value); //avx broadcast vs mm_set_ps?
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_load_ps(src + i);
v16sf a2 = _mm512_load_ps(src + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_add_ps(a, tmp);
v16sf dst_tmp2 = _mm512_add_ps(a2, tmp);
_mm512_store_ps(dst + i, dst_tmp);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(src + i);
v16sf a2 = _mm512_loadu_ps(src + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_add_ps(a, tmp);
v16sf dst_tmp2 = _mm512_add_ps(a2, tmp);
_mm512_storeu_ps(dst + i, dst_tmp);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = src[i] + value;
}
}
static inline void mulc512f(float *src, float value, float *dst, int len)
{
const v16sf tmp = _mm512_set1_ps(value); //_mm512_broadcast_ss(&value); //avx broadcast vs mm_set_ps?
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf src_tmp1 = _mm512_load_ps(src + i);
v16sf src_tmp2 = _mm512_load_ps(src + i + AVX512_LEN_FLOAT);
v16sf dst_tmp1 = _mm512_mul_ps(tmp, src_tmp1);
v16sf dst_tmp2 = _mm512_mul_ps(tmp, src_tmp2);
_mm512_store_ps(dst + i, dst_tmp1);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf src_tmp1 = _mm512_loadu_ps(src + i);
v16sf src_tmp2 = _mm512_loadu_ps(src + i + AVX512_LEN_FLOAT);
v16sf dst_tmp1 = _mm512_mul_ps(tmp, src_tmp1);
v16sf dst_tmp2 = _mm512_mul_ps(tmp, src_tmp2);
_mm512_storeu_ps(dst + i, dst_tmp1);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = src[i] * value;
}
}
static inline void muladd512f(float *_a, float *_b, float *_c, float *dst, int len)
{
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (areAligned2((uintptr_t) (_a), (uintptr_t) (_b), AVX512_LEN_BYTES) &&
areAligned2((uintptr_t) (_c), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_load_ps(_a + i);
v16sf b = _mm512_load_ps(_b + i);
v16sf c = _mm512_load_ps(_c + i);
v16sf a2 = _mm512_load_ps(_a + i + AVX512_LEN_FLOAT);
v16sf b2 = _mm512_load_ps(_b + i + AVX512_LEN_FLOAT);
v16sf c2 = _mm512_load_ps(_c + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_fmadd_ps_custom(a, b, c);
v16sf dst_tmp2 = _mm512_fmadd_ps_custom(a2, b2, c2);
_mm512_store_ps(dst + i, dst_tmp);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(_a + i);
v16sf b = _mm512_loadu_ps(_b + i);
v16sf c = _mm512_loadu_ps(_c + i);
v16sf a2 = _mm512_loadu_ps(_a + i + AVX512_LEN_FLOAT);
v16sf b2 = _mm512_loadu_ps(_b + i + AVX512_LEN_FLOAT);
v16sf c2 = _mm512_loadu_ps(_c + i + AVX512_LEN_FLOAT);
v16sf dst_tmp = _mm512_fmadd_ps_custom(a, b, c);
v16sf dst_tmp2 = _mm512_fmadd_ps_custom(a2, b2, c2);
_mm512_storeu_ps(dst + i, dst_tmp);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, dst_tmp2);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = (_a[i] * _b[i]) + _c[i];
}
}
static inline void mulcadd512f(float *_a, float _b, float *_c, float *dst, int len)
{
v16sf b = _mm512_set1_ps(_b);
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned3((uintptr_t) (_a), (uintptr_t) (_c), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf a = _mm512_load_ps(_a + i);
v16sf c = _mm512_load_ps(_c + i);
_mm512_store_ps(dst + i, _mm512_fmadd_ps_custom(a, b, c));
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(_a + i);
v16sf c = _mm512_loadu_ps(_c + i);
_mm512_storeu_ps(dst + i, _mm512_fmadd_ps_custom(a, b, c));
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = _a[i] * _b + _c[i];
}
}
static inline void mulcaddc512f(float *_a, float _b, float _c, float *dst, int len)
{
v16sf b = _mm512_set1_ps(_b);
v16sf c = _mm512_set1_ps(_c);
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned2((uintptr_t) (_a), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(_a + i);
_mm512_store_ps(dst + i, _mm512_fmadd_ps_custom(a, b, c));
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(_a + i);
_mm512_storeu_ps(dst + i, _mm512_fmadd_ps_custom(a, b, c));
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = _a[i] * _b + _c;
}
}
static inline void muladdc512f(float *_a, float *_b, float _c, float *dst, int len)
{
v16sf c = _mm512_set1_ps(_c);
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned3((uintptr_t) (_a), (uintptr_t) (_b), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf a = _mm512_load_ps(_a + i);
v16sf b = _mm512_load_ps(_b + i);
_mm512_store_ps(dst + i, _mm512_fmadd_ps_custom(a, b, c));
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
v16sf a = _mm512_loadu_ps(_a + i);
v16sf b = _mm512_loadu_ps(_b + i);
_mm512_storeu_ps(dst + i, _mm512_fmadd_ps_custom(a, b, c));
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = _a[i] * _b[i] + _c;
}
}
static inline void div512f(float *src1, float *src2, float *dst, int len)
{
int stop_len = len / AVX512_LEN_FLOAT;
stop_len *= AVX512_LEN_FLOAT;
if (areAligned3((uintptr_t) (src1), (uintptr_t) (src2), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_store_ps(dst + i, _mm512_div_ps(_mm512_load_ps(src1 + i), _mm512_load_ps(src2 + i)));
}
} else {
for (int i = 0; i < stop_len; i += AVX512_LEN_FLOAT) {
_mm512_storeu_ps(dst + i, _mm512_div_ps(_mm512_loadu_ps(src1 + i), _mm512_loadu_ps(src2 + i)));
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = src1[i] / src2[i];
}
}
static inline void vectorSlope512f(float *dst, int len, float offset, float slope)
{
v16sf coef = _mm512_set_ps(15.0f * slope, 14.0f * slope, 13.0f * slope, 12.0f * slope,
11.0f * slope, 10.0f * slope, 9.0f * slope, 8.0f * slope,
7.0f * slope, 6.0f * slope, 5.0f * slope, 4.0f * slope,
3.0f * slope, 2.0f * slope, slope, 0.0f);
v16sf slope32_vec = _mm512_set1_ps(32.0f * slope);
v16sf curVal = _mm512_add_ps(_mm512_set1_ps(offset), coef);
v16sf curVal2 = _mm512_add_ps(_mm512_set1_ps(offset), coef);
curVal2 = _mm512_add_ps(curVal2, _mm512_set1_ps(16.0f * slope));
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
if (len >= 2*AVX512_LEN_FLOAT) {
if (isAligned((uintptr_t) (dst), AVX512_LEN_BYTES)) {
_mm512_store_ps(dst + 0, curVal);
_mm512_store_ps(dst + AVX512_LEN_FLOAT, curVal2);
} else {
_mm512_storeu_ps(dst + 0, curVal);
_mm512_storeu_ps(dst + AVX512_LEN_FLOAT, curVal2);
}
if (isAligned((uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 2 * AVX512_LEN_FLOAT; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
curVal = _mm512_add_ps(curVal, slope32_vec);
_mm512_store_ps(dst + i, curVal);
curVal2 = _mm512_add_ps(curVal2, slope32_vec);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, curVal2);
}
} else {
for (int i = 2 * AVX512_LEN_FLOAT; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
curVal = _mm512_add_ps(curVal, slope32_vec);
_mm512_storeu_ps(dst + i, curVal);
curVal2 = _mm512_add_ps(curVal2, slope32_vec);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, curVal2);
}
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = offset + slope * (float) i;
}
}
static inline void convertInt16ToFloat32_512(int16_t *src, float *dst, int len, int scale_factor)
{
int stop_len = len / (2 * AVX512_LEN_FLOAT);
stop_len *= (2 * AVX512_LEN_FLOAT);
float scale_fact_mult = 1.0f / (float) (1 << scale_factor);
v16sf scale_fact_vec = _mm512_set1_ps(scale_fact_mult);
v16si idx_lo = _mm512_set_epi16(15, 15, 14, 14, 13, 13, 12, 12, 11, 11, 10, 10,
9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0);
v16si idx_hi = _mm512_set_epi16(31, 31, 30, 30, 29, 29, 28, 28, 27, 27, 26, 26, 25, 25, 24, 24,
23, 23, 22, 22, 21, 21, 20, 20, 19, 19, 18, 18, 17, 17, 16, 16);
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16si vec = _mm512_load_si512((__m512i *) (src + i));
v16si low = _mm512_permutexvar_epi16(idx_lo, vec); // low 1 1 2 2 3 3 .. 15 15
v16si high = _mm512_permutexvar_epi16(idx_hi, vec); // high 16 16 17 17 .. 31 31
low = _mm512_srai_epi32(low, 0x10); // make low 1 -1 2 -1 3 -1 4 -4
high = _mm512_srai_epi32(high, 0x10); // make high 5 -1 6 -1 7 -1 8 -1
// convert the vector to float and scale it
v16sf floatlo = _mm512_mul_ps(_mm512_cvtepi32_ps(low), scale_fact_vec);
v16sf floathi = _mm512_mul_ps(_mm512_cvtepi32_ps(high), scale_fact_vec);
_mm512_store_ps(dst + i, floatlo);
_mm512_store_ps(dst + i + AVX512_LEN_FLOAT, floathi);
}
} else {
for (int i = 0; i < stop_len; i += 2 * AVX512_LEN_FLOAT) {
v16si vec = _mm512_loadu_si512((__m512i *) (src + i));
v16si low = _mm512_permutexvar_epi16(idx_lo, vec); // low 1 1 2 2 3 3 .. 15 15
v16si high = _mm512_permutexvar_epi16(idx_hi, vec); // high 16 16 17 17 .. 31 31
low = _mm512_srai_epi32(low, 0x10); // make low 1 -1 2 -1 3 -1 4 -4
high = _mm512_srai_epi32(high, 0x10); // make high 5 -1 6 -1 7 -1 8 -1
// convert the vector to float and scale it
v16sf floatlo = _mm512_mul_ps(_mm512_cvtepi32_ps(low), scale_fact_vec);
v16sf floathi = _mm512_mul_ps(_mm512_cvtepi32_ps(high), scale_fact_vec);
_mm512_storeu_ps(dst + i, floatlo);
_mm512_storeu_ps(dst + i + AVX512_LEN_FLOAT, floathi);
}
}
for (int i = stop_len; i < len; i++) {
dst[i] = (float) src[i] * scale_fact_mult;
}
}
static inline void convertFloat32ToU8_512(float *src, uint8_t *dst, int len, int rounding_mode, int scale_factor)
{
int stop_len = len / (4 * AVX512_LEN_FLOAT);
stop_len *= (4 * AVX512_LEN_FLOAT);
float scale_fact_mult = 1.0f / (float) (1 << scale_factor);
v16sf scale_fact_vec = _mm512_set1_ps(scale_fact_mult);
v16si idx = _mm512_set_epi32(15, 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0);
int _mm_rounding_ori = _MM_GET_ROUNDING_MODE(); // save rounding mode
int rounding_ori = fegetround();
if (rounding_mode == RndZero) {
_MM_SET_ROUNDING_MODE(_MM_ROUND_TOWARD_ZERO); // rounding_vec = ROUNDTOZERO;
fesetround(FE_TOWARDZERO);
} else if (rounding_mode == RndNear){
_MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST); // rounding_vec = ROUNDTONEAREST;
fesetround(FE_TONEAREST);
}
if(rounding_mode != RndFinancial){
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 4 * AVX512_LEN_FLOAT) {
v16sf src_tmp1 = _mm512_load_ps(src + i);
v16sf src_tmp2 = _mm512_load_ps(src + i + AVX512_LEN_FLOAT);
v16sf src_tmp3 = _mm512_load_ps(src + i + 2 * AVX512_LEN_FLOAT);
v16sf src_tmp4 = _mm512_load_ps(src + i + 3 * AVX512_LEN_FLOAT);
v16sf tmp1 = _mm512_mul_ps(src_tmp1, scale_fact_vec);
v16sf tmp2 = _mm512_mul_ps(src_tmp2, scale_fact_vec);
v16sf tmp3 = _mm512_mul_ps(src_tmp3, scale_fact_vec);
v16sf tmp4 = _mm512_mul_ps(src_tmp4, scale_fact_vec);
v16si tmp1_int = _mm512_cvtps_epi32(tmp1);
v16si tmp2_int = _mm512_cvtps_epi32(tmp2);
v16si tmp3_int = _mm512_cvtps_epi32(tmp3);
v16si tmp4_int = _mm512_cvtps_epi32(tmp4);
v16si tmp5 = _mm512_packs_epi32(tmp1_int, tmp2_int);
v16si tmp6 = _mm512_packs_epi32(tmp3_int, tmp4_int);
v16si tmp7 = _mm512_packus_epi16(tmp5, tmp6);
tmp7 = _mm512_permutexvar_epi32(idx, tmp7);
_mm512_store_si512((__m512i *) (dst + i), tmp7);
}
} else {
for (int i = 0; i < stop_len; i += 4 * AVX512_LEN_FLOAT) {
v16sf src_tmp1 = _mm512_loadu_ps(src + i);
v16sf src_tmp2 = _mm512_loadu_ps(src + i + AVX512_LEN_FLOAT);
v16sf src_tmp3 = _mm512_loadu_ps(src + i + 2 * AVX512_LEN_FLOAT);
v16sf src_tmp4 = _mm512_loadu_ps(src + i + 3 * AVX512_LEN_FLOAT);
v16sf tmp1 = _mm512_mul_ps(src_tmp1, scale_fact_vec);
v16sf tmp2 = _mm512_mul_ps(src_tmp2, scale_fact_vec);
v16sf tmp3 = _mm512_mul_ps(src_tmp3, scale_fact_vec);
v16sf tmp4 = _mm512_mul_ps(src_tmp4, scale_fact_vec);
v16si tmp1_int = _mm512_cvtps_epi32(tmp1);
v16si tmp2_int = _mm512_cvtps_epi32(tmp2);
v16si tmp3_int = _mm512_cvtps_epi32(tmp3);
v16si tmp4_int = _mm512_cvtps_epi32(tmp4);
v16si tmp5 = _mm512_packs_epi32(tmp1_int, tmp2_int);
v16si tmp6 = _mm512_packs_epi32(tmp3_int, tmp4_int);
v16si tmp7 = _mm512_packus_epi16(tmp5, tmp6);
tmp7 = _mm512_permutexvar_epi32(idx, tmp7);
_mm512_storeu_si512((__m512i *) (dst + i), tmp7);
}
}
} else{
if (areAligned2((uintptr_t) (src), (uintptr_t) (dst), AVX512_LEN_BYTES)) {
for (int i = 0; i < stop_len; i += 4 * AVX512_LEN_FLOAT) {
v16sf src_tmp1 = _mm512_load_ps(src + i);
v16sf src_tmp2 = _mm512_load_ps(src + i + AVX512_LEN_FLOAT);
v16sf src_tmp3 = _mm512_load_ps(src + i + 2 * AVX512_LEN_FLOAT);
v16sf src_tmp4 = _mm512_load_ps(src + i + 3 * AVX512_LEN_FLOAT);
v16sf tmp1 = _mm512_mul_ps(src_tmp1, scale_fact_vec);
v16sf tmp2 = _mm512_mul_ps(src_tmp2, scale_fact_vec);
v16sf tmp3 = _mm512_mul_ps(src_tmp3, scale_fact_vec);
v16sf tmp4 = _mm512_mul_ps(src_tmp4, scale_fact_vec);
v16sf spe1 = _mm512_and_ps(tmp1, *(v16sf*)_ps512_sign_mask);
spe1 = _mm512_or_ps(spe1,*(v16sf*)_ps512_mid_mask);
tmp1 = _mm512_add_ps(tmp1, spe1);
v16sf spe2 = _mm512_and_ps(tmp2, *(v16sf*)_ps512_sign_mask);
spe2 = _mm512_or_ps(spe2,*(v16sf*)_ps512_mid_mask);
tmp2 = _mm512_add_ps(tmp2, spe2);
v16sf spe3 = _mm512_and_ps(tmp3, *(v16sf*)_ps512_sign_mask);
spe3 = _mm512_or_ps(spe3,*(v16sf*)_ps512_mid_mask);
tmp3 = _mm512_add_ps(tmp3, spe3);
v16sf spe4 = _mm512_and_ps(tmp4, *(v16sf*)_ps512_sign_mask);
spe4 = _mm512_or_ps(spe4,*(v16sf*)_ps512_mid_mask);
tmp4 = _mm512_add_ps(tmp4, spe4);
tmp1 = _mm512_roundscale_ps(tmp1, ROUNDTOZERO);
tmp2 = _mm512_roundscale_ps(tmp2, ROUNDTOZERO);
tmp3 = _mm512_roundscale_ps(tmp3, ROUNDTOZERO);
tmp4 = _mm512_roundscale_ps(tmp4, ROUNDTOZERO);
v16si tmp1_int = _mm512_cvtps_epi32(tmp1);
v16si tmp2_int = _mm512_cvtps_epi32(tmp2);
v16si tmp3_int = _mm512_cvtps_epi32(tmp3);
v16si tmp4_int = _mm512_cvtps_epi32(tmp4);
v16si tmp5 = _mm512_packs_epi32(tmp1_int, tmp2_int);
v16si tmp6 = _mm512_packs_epi32(tmp3_int, tmp4_int);
v16si tmp7 = _mm512_packus_epi16(tmp5, tmp6);
tmp7 = _mm512_permutexvar_epi32(idx, tmp7);
_mm512_store_si512((__m512i *) (dst + i), tmp7);
}
} else {
for (int i = 0; i < stop_len; i += 4 * AVX512_LEN_FLOAT) {
v16sf src_tmp1 = _mm512_loadu_ps(src + i);
v16sf src_tmp2 = _mm512_loadu_ps(src + i + AVX512_LEN_FLOAT);
v16sf src_tmp3 = _mm512_loadu_ps(src + i + 2 * AVX512_LEN_FLOAT);
v16sf src_tmp4 = _mm512_loadu_ps(src + i + 3 * AVX512_LEN_FLOAT);
v16sf tmp1 = _mm512_mul_ps(src_tmp1, scale_fact_vec);
v16sf tmp2 = _mm512_mul_ps(src_tmp2, scale_fact_vec);
v16sf tmp3 = _mm512_mul_ps(src_tmp3, scale_fact_vec);
v16sf tmp4 = _mm512_mul_ps(src_tmp4, scale_fact_vec);
v16sf spe1 = _mm512_and_ps(tmp1, *(v16sf*)_ps512_sign_mask);
spe1 = _mm512_or_ps(spe1,*(v16sf*)_ps512_mid_mask);
tmp1 = _mm512_add_ps(tmp1, spe1);
v16sf spe2 = _mm512_and_ps(tmp2, *(v16sf*)_ps512_sign_mask);
spe2 = _mm512_or_ps(spe2,*(v16sf*)_ps512_mid_mask);
tmp2 = _mm512_add_ps(tmp2, spe2);
v16sf spe3 = _mm512_and_ps(tmp3, *(v16sf*)_ps512_sign_mask);
spe3 = _mm512_or_ps(spe3,*(v16sf*)_ps512_mid_mask);
tmp3 = _mm512_add_ps(tmp3, spe3);
v16sf spe4 = _mm512_and_ps(tmp4, *(v16sf*)_ps512_sign_mask);
spe4 = _mm512_or_ps(spe4,*(v16sf*)_ps512_mid_mask);
tmp4 = _mm512_add_ps(tmp4, spe4);
tmp1 = _mm512_roundscale_ps(tmp1, ROUNDTOZERO);
tmp2 = _mm512_roundscale_ps(tmp2, ROUNDTOZERO);
tmp3 = _mm512_roundscale_ps(tmp3, ROUNDTOZERO);
tmp4 = _mm512_roundscale_ps(tmp4, ROUNDTOZERO);
v16si tmp1_int = _mm512_cvtps_epi32(tmp1);
v16si tmp2_int = _mm512_cvtps_epi32(tmp2);
v16si tmp3_int = _mm512_cvtps_epi32(tmp3);
v16si tmp4_int = _mm512_cvtps_epi32(tmp4);
v16si tmp5 = _mm512_packs_epi32(tmp1_int, tmp2_int);
v16si tmp6 = _mm512_packs_epi32(tmp3_int, tmp4_int);
v16si tmp7 = _mm512_packus_epi16(tmp5, tmp6);
tmp7 = _mm512_permutexvar_epi32(idx, tmp7);
_mm512_storeu_si512((__m512i *) (dst + i), tmp7);
}
}
}
if (rounding_mode == RndFinancial) {
for (int i = stop_len; i < len; i++) {
float tmp = roundf(src[i] * scale_fact_mult);
dst[i] = (uint8_t) (tmp > 255.0f ? 255.0f : tmp); // round to nearest even with round(x/2)*2
}
} else {
// Default round toward zero
for (int i = stop_len; i < len; i++) {
float tmp = rintf(src[i] * scale_fact_mult);
dst[i] = (uint8_t) (tmp > 255.0f ? 255.0f : tmp);
}
_MM_SET_ROUNDING_MODE(_mm_rounding_ori); // restore previous rounding mode