1 // Auto-generated file. Do not edit!
2 // Template: src/f32-vsigmoid/avx512f-rr2-lut32-p2-perm2-scalef.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2020 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11
12 #include <immintrin.h>
13
14 #include <xnnpack/common.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/vunary.h>
17
18
xnn_f32_vsigmoid_ukernel__avx512f_rr2_lut32_p2_perm2_scalef_div_x48(size_t n,const float * x,float * y,const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_vsigmoid_ukernel__avx512f_rr2_lut32_p2_perm2_scalef_div_x48(
20 size_t n,
21 const float* x,
22 float* y,
23 const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)])
24 {
25 assert(n % sizeof(float) == 0);
26
27 const __m512i vsign_mask = _mm512_set1_epi32((int) params->avx512_rr2_lut32_p2.sign_mask);
28 const __m512 vmagic_bias = _mm512_set1_ps(params->avx512_rr2_lut32_p2.magic_bias);
29 const __m512 vlog2e = _mm512_set1_ps(params->avx512_rr2_lut32_p2.log2e);
30 const __m512 vtable_lo = _mm512_load_ps(params->avx512_rr2_lut32_p2.table_lo);
31 const __m512 vtable_hi = _mm512_load_ps(params->avx512_rr2_lut32_p2.table_hi);
32 const __m512 vminus_ln2_hi = _mm512_set1_ps(params->avx512_rr2_lut32_p2.minus_ln2_hi);
33 const __m512 vminus_ln2_lo = _mm512_set1_ps(params->avx512_rr2_lut32_p2.minus_ln2_lo);
34 const __m512 vc2 = _mm512_set1_ps(params->avx512_rr2_lut32_p2.c2);
35 const __m512 vc1 = _mm512_set1_ps(params->avx512_rr2_lut32_p2.c1);
36 const __m512 vone = _mm512_set1_ps(params->avx512_rr2_lut32_p2.one);
37
38 for (; n >= 48 * sizeof(float); n -= 48 * sizeof(float)) {
39 const __m512 vx0 = _mm512_loadu_ps(x);
40 const __m512 vx1 = _mm512_loadu_ps(x + 16);
41 const __m512 vx2 = _mm512_loadu_ps(x + 32);
42 x += 48;
43
44 const __m512 vz0 = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx0), vsign_mask));
45 const __m512 vz1 = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx1), vsign_mask));
46 const __m512 vz2 = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx2), vsign_mask));
47
48 __m512 vn0 = _mm512_fmadd_ps(vz0, vlog2e, vmagic_bias);
49 __m512 vn1 = _mm512_fmadd_ps(vz1, vlog2e, vmagic_bias);
50 __m512 vn2 = _mm512_fmadd_ps(vz2, vlog2e, vmagic_bias);
51
52 const __m512 vl0 = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn0), vtable_hi);
53 const __m512 vl1 = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn1), vtable_hi);
54 const __m512 vl2 = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn2), vtable_hi);
55
56 vn0 = _mm512_sub_ps(vn0, vmagic_bias);
57 vn1 = _mm512_sub_ps(vn1, vmagic_bias);
58 vn2 = _mm512_sub_ps(vn2, vmagic_bias);
59
60 __m512 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_hi, vz0);
61 __m512 vt1 = _mm512_fmadd_ps(vn1, vminus_ln2_hi, vz1);
62 __m512 vt2 = _mm512_fmadd_ps(vn2, vminus_ln2_hi, vz2);
63
64 vt0 = _mm512_fmadd_ps(vn0, vminus_ln2_lo, vt0);
65 vt1 = _mm512_fmadd_ps(vn1, vminus_ln2_lo, vt1);
66 vt2 = _mm512_fmadd_ps(vn2, vminus_ln2_lo, vt2);
67
68 __m512 vp0 = _mm512_fmadd_ps(vt0, vc2, vc1);
69 __m512 vp1 = _mm512_fmadd_ps(vt1, vc2, vc1);
70 __m512 vp2 = _mm512_fmadd_ps(vt2, vc2, vc1);
71
72 vt0 = _mm512_mul_ps(vt0, vl0);
73 vt1 = _mm512_mul_ps(vt1, vl1);
74 vt2 = _mm512_mul_ps(vt2, vl2);
75
76 vp0 = _mm512_fmadd_ps(vt0, vp0, vl0);
77 vp1 = _mm512_fmadd_ps(vt1, vp1, vl1);
78 vp2 = _mm512_fmadd_ps(vt2, vp2, vl2);
79
80 const __m512 ve0 = _mm512_scalef_ps(vp0, vn0);
81 const __m512 ve1 = _mm512_scalef_ps(vp1, vn1);
82 const __m512 ve2 = _mm512_scalef_ps(vp2, vn2);
83
84 const __m512 vd0 = _mm512_add_ps(ve0, vone);
85 const __m512 vd1 = _mm512_add_ps(ve1, vone);
86 const __m512 vd2 = _mm512_add_ps(ve2, vone);
87
88 __m512 vf0 = _mm512_div_ps(ve0, vd0);
89 __m512 vf1 = _mm512_div_ps(ve1, vd1);
90 __m512 vf2 = _mm512_div_ps(ve2, vd2);
91
92 vf0 = _mm512_mask_sub_ps(vf0, _mm512_testn_epi32_mask(_mm512_castps_si512(vx0), vsign_mask), vone, vf0);
93 vf1 = _mm512_mask_sub_ps(vf1, _mm512_testn_epi32_mask(_mm512_castps_si512(vx1), vsign_mask), vone, vf1);
94 vf2 = _mm512_mask_sub_ps(vf2, _mm512_testn_epi32_mask(_mm512_castps_si512(vx2), vsign_mask), vone, vf2);
95
96 _mm512_storeu_ps(y, vf0);
97 _mm512_storeu_ps(y + 16, vf1);
98 _mm512_storeu_ps(y + 32, vf2);
99 y += 48;
100 }
101 for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
102 const __m512 vx = _mm512_loadu_ps(x);
103 x += 16;
104
105 const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
106
107 __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
108 const __m512 vl = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn), vtable_hi);
109 vn = _mm512_sub_ps(vn, vmagic_bias);
110
111 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz);
112 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
113
114 __m512 vp = _mm512_fmadd_ps(vt, vc2, vc1);
115 vt = _mm512_mul_ps(vt, vl);
116 vp = _mm512_fmadd_ps(vt, vp, vl);
117
118 const __m512 ve = _mm512_scalef_ps(vp, vn);
119 const __m512 vd = _mm512_add_ps(ve, vone);
120
121 __m512 vf = _mm512_div_ps(ve, vd);
122
123 vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
124
125 _mm512_storeu_ps(y, vf);
126 y += 16;
127 }
128 if XNN_UNLIKELY(n != 0) {
129 assert(n >= 1 * sizeof(float));
130 assert(n <= 15 * sizeof(float));
131
132 // Prepare mask for valid 32-bit elements (depends on n).
133 n >>= 2 /* log2(sizeof(float)) */;
134 const __mmask16 vmask = _cvtu32_mask16((uint16_t) ((uint32_t) (UINT32_C(1) << n) - UINT32_C(1)));
135
136 const __m512 vx = _mm512_maskz_loadu_ps(vmask, x);
137 const __m512 vz = _mm512_castsi512_ps(_mm512_or_epi32(_mm512_castps_si512(vx), vsign_mask));
138
139 __m512 vn = _mm512_fmadd_ps(vz, vlog2e, vmagic_bias);
140 const __m512 vl = _mm512_permutex2var_ps(vtable_lo, _mm512_castps_si512(vn), vtable_hi);
141 vn = _mm512_sub_ps(vn, vmagic_bias);
142
143 __m512 vt = _mm512_fmadd_ps(vn, vminus_ln2_hi, vz);
144 vt = _mm512_fmadd_ps(vn, vminus_ln2_lo, vt);
145
146 __m512 vp = _mm512_fmadd_ps(vt, vc2, vc1);
147 vt = _mm512_mul_ps(vt, vl);
148 vp = _mm512_fmadd_ps(vt, vp, vl);
149
150 const __m512 ve = _mm512_scalef_ps(vp, vn);
151 const __m512 vd = _mm512_add_ps(ve, vone);
152
153 __m512 vf = _mm512_div_ps(ve, vd);
154
155 vf = _mm512_mask_sub_ps(vf, _mm512_testn_epi32_mask(_mm512_castps_si512(vx), vsign_mask), vone, vf);
156
157 _mm512_mask_storeu_ps(y, vmask, vf);
158 }
159 }
160