• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-sigmoid/sse-lut64-p2-div.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 <smmintrin.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/vunary.h>
16 
17 
18 extern XNN_INTERNAL const float xnn_table_exp2minus_k_over_64[64];
19 
xnn_f32_sigmoid_ukernel__sse41_lut64_p2_div_x12(size_t n,const float * x,float * y,const void * params)20 void xnn_f32_sigmoid_ukernel__sse41_lut64_p2_div_x12(
21     size_t n,
22     const float* x,
23     float* y,
24     const void* params) XNN_DISABLE_TSAN
25 {
26   assert(n % sizeof(float) == 0);
27 
28   const __m128 vsign_mask = _mm_set1_ps(-0.0f);
29   const __m128 vmagic_bias = _mm_set1_ps(0x1.800000p17f);
30   const __m128 vlog2e = _mm_set1_ps(0x1.715476p0f);
31   const __m128i vindex_mask = _mm_set1_epi32(INT32_C(0x3F));
32   const __m128 vminus_ln2_hi = _mm_set1_ps(-0x1.630000p-1f);
33   const __m128 vminus_ln2_lo = _mm_set1_ps(0x1.BD0106p-13f);
34   const __m128 vc2 = _mm_set1_ps(0x1.FFFF0Ap-2f);
35   const __m128 vone = _mm_set1_ps(1.0f);
36   const __m128 vdenorm_cutoff = _mm_set1_ps(-0x1.5D589Ep+6f);
37 
38   for (; n >= 12 * sizeof(float); n -= 12 * sizeof(float)) {
39     const __m128 vx0123 = _mm_loadu_ps(x);
40     const __m128 vx4567 = _mm_loadu_ps(x + 4);
41     const __m128 vx89AB = _mm_loadu_ps(x + 8);
42     x += 12;
43 
44     const __m128 vz0123 = _mm_or_ps(vx0123, vsign_mask);
45     const __m128 vz4567 = _mm_or_ps(vx4567, vsign_mask);
46     const __m128 vz89AB = _mm_or_ps(vx89AB, vsign_mask);
47 
48     __m128 vn0123 = _mm_add_ps(_mm_mul_ps(vz0123, vlog2e), vmagic_bias);
49     __m128 vn4567 = _mm_add_ps(_mm_mul_ps(vz4567, vlog2e), vmagic_bias);
50     __m128 vn89AB = _mm_add_ps(_mm_mul_ps(vz89AB, vlog2e), vmagic_bias);
51 
52     const __m128i ve0123 = _mm_slli_epi32(_mm_castps_si128(vn0123), 17);
53     const __m128i ve4567 = _mm_slli_epi32(_mm_castps_si128(vn4567), 17);
54     const __m128i ve89AB = _mm_slli_epi32(_mm_castps_si128(vn89AB), 17);
55 
56     const __m128i vidx0123 = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn0123), vindex_mask), 2);
57     const __m128i vidx4567 = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn4567), vindex_mask), 2);
58     const __m128i vidx89AB = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn89AB), vindex_mask), 2);
59 
60     #if XNN_ARCH_X86_64
61       const uint64_t vidx01 = (uint64_t) _mm_cvtsi128_si64(vidx0123);
62       const uint64_t vidx23 = (uint64_t) _mm_extract_epi64(vidx0123, 1);
63       const __m128i vl0   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx01)));
64       const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx23)));
65       const __m128i vl01 = _mm_insert_epi32(vl0, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx01 >> 32))), 1);
66       const __m128i vl23 = _mm_insert_epi32(vl2, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx23 >> 32))), 1);
67       const __m128i vl0123 = _mm_unpacklo_epi64(vl01, vl23);
68       const uint64_t vidx45 = (uint64_t) _mm_cvtsi128_si64(vidx4567);
69       const uint64_t vidx67 = (uint64_t) _mm_extract_epi64(vidx4567, 1);
70       const __m128i vl4   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx45)));
71       const __m128i vl6 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx67)));
72       const __m128i vl45 = _mm_insert_epi32(vl4, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx45 >> 32))), 1);
73       const __m128i vl67 = _mm_insert_epi32(vl6, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx67 >> 32))), 1);
74       const __m128i vl4567 = _mm_unpacklo_epi64(vl45, vl67);
75       const uint64_t vidx89 = (uint64_t) _mm_cvtsi128_si64(vidx89AB);
76       const uint64_t vidxAB = (uint64_t) _mm_extract_epi64(vidx89AB, 1);
77       const __m128i vl8   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx89)));
78       const __m128i vlA = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidxAB)));
79       const __m128i vl89 = _mm_insert_epi32(vl8, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx89 >> 32))), 1);
80       const __m128i vlAB = _mm_insert_epi32(vlA, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidxAB >> 32))), 1);
81       const __m128i vl89AB = _mm_unpacklo_epi64(vl89, vlAB);
82     #else  // !XNN_ARCH_X86_64
83       const uint32_t vidx0 = (uint32_t) _mm_cvtsi128_si32(vidx0123);
84       const uint32_t vidx1 = (uint32_t) _mm_extract_epi16(vidx0123, 2);
85       const uint32_t vidx2 = (uint32_t) _mm_extract_epi16(vidx0123, 4);
86       const uint32_t vidx3 = (uint32_t) _mm_extract_epi16(vidx0123, 6);
87       const __m128i vl0   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx0)));
88       const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx2)));
89       const __m128i vl01 = _mm_insert_epi32(vl0, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx1)), 1);
90       const __m128i vl23 = _mm_insert_epi32(vl2, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx3)), 1);
91       const __m128i vl0123 = _mm_unpacklo_epi64(vl01, vl23);
92       const uint32_t vidx4 = (uint32_t) _mm_cvtsi128_si32(vidx4567);
93       const uint32_t vidx5 = (uint32_t) _mm_extract_epi16(vidx4567, 2);
94       const uint32_t vidx6 = (uint32_t) _mm_extract_epi16(vidx4567, 4);
95       const uint32_t vidx7 = (uint32_t) _mm_extract_epi16(vidx4567, 6);
96       const __m128i vl4   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx4)));
97       const __m128i vl6 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx6)));
98       const __m128i vl45 = _mm_insert_epi32(vl4, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx5)), 1);
99       const __m128i vl67 = _mm_insert_epi32(vl6, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx7)), 1);
100       const __m128i vl4567 = _mm_unpacklo_epi64(vl45, vl67);
101       const uint32_t vidx8 = (uint32_t) _mm_cvtsi128_si32(vidx89AB);
102       const uint32_t vidx9 = (uint32_t) _mm_extract_epi16(vidx89AB, 2);
103       const uint32_t vidxA = (uint32_t) _mm_extract_epi16(vidx89AB, 4);
104       const uint32_t vidxB = (uint32_t) _mm_extract_epi16(vidx89AB, 6);
105       const __m128i vl8   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx8)));
106       const __m128i vlA = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidxA)));
107       const __m128i vl89 = _mm_insert_epi32(vl8, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx9)), 1);
108       const __m128i vlAB = _mm_insert_epi32(vlA, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidxB)), 1);
109       const __m128i vl89AB = _mm_unpacklo_epi64(vl89, vlAB);
110     #endif  // XNN_ARCH_X86_64
111 
112     const __m128 vs0123 = _mm_castsi128_ps(_mm_add_epi32(vl0123, ve0123));
113     const __m128 vs4567 = _mm_castsi128_ps(_mm_add_epi32(vl4567, ve4567));
114     const __m128 vs89AB = _mm_castsi128_ps(_mm_add_epi32(vl89AB, ve89AB));
115 
116     vn0123 = _mm_sub_ps(vn0123, vmagic_bias);
117     vn4567 = _mm_sub_ps(vn4567, vmagic_bias);
118     vn89AB = _mm_sub_ps(vn89AB, vmagic_bias);
119 
120     __m128 vt0123 = _mm_add_ps(vz0123, _mm_mul_ps(vn0123, vminus_ln2_hi));
121     __m128 vt4567 = _mm_add_ps(vz4567, _mm_mul_ps(vn4567, vminus_ln2_hi));
122     __m128 vt89AB = _mm_add_ps(vz89AB, _mm_mul_ps(vn89AB, vminus_ln2_hi));
123 
124     vt0123 = _mm_add_ps(vt0123, _mm_mul_ps(vn0123, vminus_ln2_lo));
125     vt4567 = _mm_add_ps(vt4567, _mm_mul_ps(vn4567, vminus_ln2_lo));
126     vt89AB = _mm_add_ps(vt89AB, _mm_mul_ps(vn89AB, vminus_ln2_lo));
127 
128     __m128 vp0123 = _mm_mul_ps(vt0123, vc2);
129     __m128 vp4567 = _mm_mul_ps(vt4567, vc2);
130     __m128 vp89AB = _mm_mul_ps(vt89AB, vc2);
131 
132     vp0123 = _mm_add_ps(vt0123, _mm_mul_ps(vp0123, vt0123));
133     vp4567 = _mm_add_ps(vt4567, _mm_mul_ps(vp4567, vt4567));
134     vp89AB = _mm_add_ps(vt89AB, _mm_mul_ps(vp89AB, vt89AB));
135 
136     const __m128 vy0123 = _mm_add_ps(vs0123, _mm_mul_ps(vs0123, vp0123));
137     const __m128 vy4567 = _mm_add_ps(vs4567, _mm_mul_ps(vs4567, vp4567));
138     const __m128 vy89AB = _mm_add_ps(vs89AB, _mm_mul_ps(vs89AB, vp89AB));
139 
140     __m128 vf0123 = _mm_div_ps(vy0123, _mm_add_ps(vy0123, vone));
141     __m128 vf4567 = _mm_div_ps(vy4567, _mm_add_ps(vy4567, vone));
142     __m128 vf89AB = _mm_div_ps(vy89AB, _mm_add_ps(vy89AB, vone));
143 
144     vf0123 = _mm_andnot_ps(_mm_cmplt_ps(vz0123, vdenorm_cutoff), vf0123);
145     vf4567 = _mm_andnot_ps(_mm_cmplt_ps(vz4567, vdenorm_cutoff), vf4567);
146     vf89AB = _mm_andnot_ps(_mm_cmplt_ps(vz89AB, vdenorm_cutoff), vf89AB);
147 
148     vf0123 = _mm_blendv_ps(_mm_sub_ps(vone, vf0123), vf0123, vx0123);
149     vf4567 = _mm_blendv_ps(_mm_sub_ps(vone, vf4567), vf4567, vx4567);
150     vf89AB = _mm_blendv_ps(_mm_sub_ps(vone, vf89AB), vf89AB, vx89AB);
151 
152     _mm_storeu_ps(y, vf0123);
153     _mm_storeu_ps(y + 4, vf4567);
154     _mm_storeu_ps(y + 8, vf89AB);
155     y += 12;
156   }
157   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
158     const __m128 vx = _mm_loadu_ps(x);
159     x += 4;
160 
161     const __m128 vz = _mm_or_ps(vx, vsign_mask);
162 
163     __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias);
164     const __m128i ve = _mm_slli_epi32(_mm_castps_si128(vn), 17);
165 
166     const __m128i vidx = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn), vindex_mask), 2);
167     #if XNN_ARCH_X86_64
168       const uint64_t vidx_lo = (uint64_t) _mm_cvtsi128_si64(vidx);
169       const uint64_t vidx_hi = (uint64_t) _mm_extract_epi64(vidx, 1);
170       const __m128i vl_ll   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)));
171       const __m128i vl_hl = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)));
172       const __m128i vl_lo = _mm_insert_epi32(vl_ll, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32))), 1);
173       const __m128i vl_hi = _mm_insert_epi32(vl_hl, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32))), 1);
174     #else  // !XNN_ARCH_X86_64
175       const __m128i vl_ll = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_cvtsi128_si32(vidx))));
176       const __m128i vl_hl = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 4))));
177       const __m128i vl_lo = _mm_insert_epi32(vl_ll, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 2))), 1);
178       const __m128i vl_hi = _mm_insert_epi32(vl_hl, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 6))), 1);
179     #endif  // XNN_ARCH_X86_64
180     const __m128i vl = _mm_unpacklo_epi64(vl_lo, vl_hi);
181 
182     const __m128 vs = _mm_castsi128_ps(_mm_add_epi32(vl, ve));
183     vn = _mm_sub_ps(vn, vmagic_bias);
184 
185     __m128 vt = _mm_add_ps(vz, _mm_mul_ps(vn, vminus_ln2_hi));
186     vt = _mm_add_ps(vt, _mm_mul_ps(vn, vminus_ln2_lo));
187 
188     __m128 vp = _mm_mul_ps(vt, vc2);
189     vp = _mm_add_ps(vt, _mm_mul_ps(vp, vt));
190 
191     const __m128 vy = _mm_add_ps(vs, _mm_mul_ps(vs, vp));
192 
193     __m128 vf = _mm_div_ps(vy, _mm_add_ps(vy, vone));
194     vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf);
195     vf = _mm_blendv_ps(_mm_sub_ps(vone, vf), vf, vx);
196 
197     _mm_storeu_ps(y, vf);
198     y += 4;
199   }
200   if XNN_UNLIKELY(n != 0) {
201     const __m128 vx = _mm_loadu_ps(x);
202 
203     const __m128 vz = _mm_or_ps(vx, vsign_mask);
204 
205     __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias);
206     const __m128i ve = _mm_slli_epi32(_mm_castps_si128(vn), 17);
207 
208     const __m128i vidx = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn), vindex_mask), 2);
209     #if XNN_ARCH_X86_64
210       const uint64_t vidx_lo = (uint64_t) _mm_cvtsi128_si64(vidx);
211       const uint64_t vidx_hi = (uint64_t) _mm_extract_epi64(vidx, 1);
212       const __m128i vl_ll   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)));
213       const __m128i vl_hl = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)));
214       const __m128i vl_lo = _mm_insert_epi32(vl_ll, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32))), 1);
215       const __m128i vl_hi = _mm_insert_epi32(vl_hl, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32))), 1);
216     #else  // !XNN_ARCH_X86_64
217       const __m128i vl_ll = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_cvtsi128_si32(vidx))));
218       const __m128i vl_hl = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 4))));
219       const __m128i vl_lo = _mm_insert_epi32(vl_ll, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 2))), 1);
220       const __m128i vl_hi = _mm_insert_epi32(vl_hl, *((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 6))), 1);
221     #endif  // XNN_ARCH_X86_64
222     const __m128i vl = _mm_unpacklo_epi64(vl_lo, vl_hi);
223 
224     const __m128 vs = _mm_castsi128_ps(_mm_add_epi32(vl, ve));
225     vn = _mm_sub_ps(vn, vmagic_bias);
226 
227     __m128 vt = _mm_add_ps(vz, _mm_mul_ps(vn, vminus_ln2_hi));
228     vt = _mm_add_ps(vt, _mm_mul_ps(vn, vminus_ln2_lo));
229 
230     __m128 vp = _mm_mul_ps(vt, vc2);
231     vp = _mm_add_ps(vt, _mm_mul_ps(vp, vt));
232 
233     const __m128 vy = _mm_add_ps(vs, _mm_mul_ps(vs, vp));
234 
235     __m128 vf = _mm_div_ps(vy, _mm_add_ps(vy, vone));
236     vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf);
237     vf = _mm_blendv_ps(_mm_sub_ps(vone, vf), vf, vx);
238 
239     if (n & (2 * sizeof(float))) {
240       _mm_storel_pi((__m64*) y, vf);
241       vf = _mm_movehl_ps(vf, vf);
242       y += 2;
243     }
244     if (n & (1 * sizeof(float))) {
245       _mm_store_ss(y, vf);
246     }
247   }
248 }
249