• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vsigmoid/sse-rr2-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 <emmintrin.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_vsigmoid_ukernel__sse2_rr2_lut64_p2_div_x8(size_t n,const float * x,float * y,const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS (1)])20 void xnn_f32_vsigmoid_ukernel__sse2_rr2_lut64_p2_div_x8(
21     size_t n,
22     const float* x,
23     float* y,
24     const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
25 {
26   assert(n % sizeof(float) == 0);
27 
28   const __m128 vsign_mask = _mm_load_ps(params->sse2_rr2_lut64_p2.sign_mask);
29   const __m128 vmagic_bias = _mm_load_ps(params->sse2_rr2_lut64_p2.magic_bias);
30   const __m128 vlog2e = _mm_load_ps(params->sse2_rr2_lut64_p2.log2e);
31   const __m128i vindex_mask = _mm_load_si128((const __m128i*) params->sse2_rr2_lut64_p2.index_mask);
32   const __m128 vminus_ln2_hi = _mm_load_ps(params->sse2_rr2_lut64_p2.minus_ln2_hi);
33   const __m128 vminus_ln2_lo = _mm_load_ps(params->sse2_rr2_lut64_p2.minus_ln2_lo);
34   const __m128 vc2 = _mm_load_ps(params->sse2_rr2_lut64_p2.c2);
35   const __m128 vone = _mm_load_ps(params->sse2_rr2_lut64_p2.one);
36   const __m128 vdenorm_cutoff = _mm_load_ps(params->sse2_rr2_lut64_p2.denorm_cutoff);
37 
38   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
39     const __m128 vx0123 = _mm_loadu_ps(x);
40     const __m128 vx4567 = _mm_loadu_ps(x + 4);
41     x += 8;
42 
43     const __m128 vz0123 = _mm_or_ps(vx0123, vsign_mask);
44     const __m128 vz4567 = _mm_or_ps(vx4567, vsign_mask);
45 
46     __m128 vn0123 = _mm_add_ps(_mm_mul_ps(vz0123, vlog2e), vmagic_bias);
47     __m128 vn4567 = _mm_add_ps(_mm_mul_ps(vz4567, vlog2e), vmagic_bias);
48 
49     const __m128i ve0123 = _mm_slli_epi32(_mm_castps_si128(vn0123), 17);
50     const __m128i ve4567 = _mm_slli_epi32(_mm_castps_si128(vn4567), 17);
51 
52     const __m128i vidx0123 = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn0123), vindex_mask), 2);
53     const __m128i vidx4567 = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn4567), vindex_mask), 2);
54 
55     #if XNN_ARCH_X86_64
56       const uint64_t vidx01 = (uint64_t) _mm_cvtsi128_si64(vidx0123);
57       const uint64_t vidx23 = (uint64_t) _mm_cvtsi128_si64(_mm_unpackhi_epi64(vidx0123, vidx0123));
58       const __m128i vl0   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx01)));
59       const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx23)));
60       const __m128i vl1 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx01 >> 32))));
61       const __m128i vl01 = _mm_unpacklo_epi32(vl0, vl1);
62       const __m128i vl3 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx23 >> 32))));
63       const __m128i vl23 = _mm_unpacklo_epi32(vl2, vl3);
64       const __m128i vl0123 = _mm_unpacklo_epi64(vl01, vl23);
65       const uint64_t vidx45 = (uint64_t) _mm_cvtsi128_si64(vidx4567);
66       const uint64_t vidx67 = (uint64_t) _mm_cvtsi128_si64(_mm_unpackhi_epi64(vidx4567, vidx4567));
67       const __m128i vl4   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx45)));
68       const __m128i vl6 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx67)));
69       const __m128i vl5 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx45 >> 32))));
70       const __m128i vl45 = _mm_unpacklo_epi32(vl4, vl5);
71       const __m128i vl7 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx67 >> 32))));
72       const __m128i vl67 = _mm_unpacklo_epi32(vl6, vl7);
73       const __m128i vl4567 = _mm_unpacklo_epi64(vl45, vl67);
74     #else  // !XNN_ARCH_X86_64
75       const uint32_t vidx0 = (uint32_t) _mm_cvtsi128_si32(vidx0123);
76       const uint32_t vidx1 = (uint32_t) _mm_extract_epi16(vidx0123, 2);
77       const uint32_t vidx2 = (uint32_t) _mm_extract_epi16(vidx0123, 4);
78       const uint32_t vidx3 = (uint32_t) _mm_extract_epi16(vidx0123, 6);
79       const __m128i vl0   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx0)));
80       const __m128i vl2 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx2)));
81       const __m128i vl1 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx1)));
82       const __m128i vl01 = _mm_unpacklo_epi32(vl0, vl1);
83       const __m128i vl3 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx3)));
84       const __m128i vl23 = _mm_unpacklo_epi32(vl2, vl3);
85       const __m128i vl0123 = _mm_unpacklo_epi64(vl01, vl23);
86       const uint32_t vidx4 = (uint32_t) _mm_cvtsi128_si32(vidx4567);
87       const uint32_t vidx5 = (uint32_t) _mm_extract_epi16(vidx4567, 2);
88       const uint32_t vidx6 = (uint32_t) _mm_extract_epi16(vidx4567, 4);
89       const uint32_t vidx7 = (uint32_t) _mm_extract_epi16(vidx4567, 6);
90       const __m128i vl4   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx4)));
91       const __m128i vl6 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx6)));
92       const __m128i vl5 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx5)));
93       const __m128i vl45 = _mm_unpacklo_epi32(vl4, vl5);
94       const __m128i vl7 = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + vidx7)));
95       const __m128i vl67 = _mm_unpacklo_epi32(vl6, vl7);
96       const __m128i vl4567 = _mm_unpacklo_epi64(vl45, vl67);
97     #endif  // XNN_ARCH_X86_64
98 
99     const __m128 vs0123 = _mm_castsi128_ps(_mm_add_epi32(vl0123, ve0123));
100     const __m128 vs4567 = _mm_castsi128_ps(_mm_add_epi32(vl4567, ve4567));
101 
102     vn0123 = _mm_sub_ps(vn0123, vmagic_bias);
103     vn4567 = _mm_sub_ps(vn4567, vmagic_bias);
104 
105     __m128 vt0123 = _mm_add_ps(vz0123, _mm_mul_ps(vn0123, vminus_ln2_hi));
106     __m128 vt4567 = _mm_add_ps(vz4567, _mm_mul_ps(vn4567, vminus_ln2_hi));
107 
108     vt0123 = _mm_add_ps(vt0123, _mm_mul_ps(vn0123, vminus_ln2_lo));
109     vt4567 = _mm_add_ps(vt4567, _mm_mul_ps(vn4567, vminus_ln2_lo));
110 
111     __m128 vp0123 = _mm_mul_ps(vt0123, vc2);
112     __m128 vp4567 = _mm_mul_ps(vt4567, vc2);
113 
114     vp0123 = _mm_add_ps(vt0123, _mm_mul_ps(vp0123, vt0123));
115     vp4567 = _mm_add_ps(vt4567, _mm_mul_ps(vp4567, vt4567));
116 
117     const __m128 vy0123 = _mm_add_ps(vs0123, _mm_mul_ps(vs0123, vp0123));
118     const __m128 vy4567 = _mm_add_ps(vs4567, _mm_mul_ps(vs4567, vp4567));
119 
120     __m128 vf0123 = _mm_div_ps(vy0123, _mm_add_ps(vy0123, vone));
121     __m128 vf4567 = _mm_div_ps(vy4567, _mm_add_ps(vy4567, vone));
122 
123     vf0123 = _mm_andnot_ps(_mm_cmplt_ps(vz0123, vdenorm_cutoff), vf0123);
124     vf4567 = _mm_andnot_ps(_mm_cmplt_ps(vz4567, vdenorm_cutoff), vf4567);
125 
126     const __m128 vm0123 = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx0123)));
127     const __m128 vm4567 = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx4567)));
128 
129     vf0123 = _mm_or_ps(_mm_and_ps(vf0123, vm0123), _mm_andnot_ps(vm0123, _mm_sub_ps(vone, vf0123)));
130     vf4567 = _mm_or_ps(_mm_and_ps(vf4567, vm4567), _mm_andnot_ps(vm4567, _mm_sub_ps(vone, vf4567)));
131 
132     _mm_storeu_ps(y, vf0123);
133     _mm_storeu_ps(y + 4, vf4567);
134     y += 8;
135   }
136   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
137     const __m128 vx = _mm_loadu_ps(x);
138     x += 4;
139 
140     const __m128 vz = _mm_or_ps(vx, vsign_mask);
141 
142     __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias);
143     const __m128i ve = _mm_slli_epi32(_mm_castps_si128(vn), 17);
144 
145     const __m128i vidx = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn), vindex_mask), 2);
146     #if XNN_ARCH_X86_64
147       const uint64_t vidx_lo = (uint64_t) _mm_cvtsi128_si64(vidx);
148       const uint64_t vidx_hi = (uint64_t) _mm_cvtsi128_si64(_mm_unpackhi_epi64(vidx, vidx));
149       const __m128i vl_ll   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)));
150       const __m128i vl_hl = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)));
151       const __m128i vl_lh = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32))));
152       const __m128i vl_lo = _mm_unpacklo_epi32(vl_ll, vl_lh);
153       const __m128i vl_hh = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32))));
154       const __m128i vl_hi = _mm_unpacklo_epi32(vl_hl, vl_hh);
155     #else  // !XNN_ARCH_X86_64
156       const __m128i vl_ll = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_cvtsi128_si32(vidx))));
157       const __m128i vl_hl = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 4))));
158       const __m128i vl_lh = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 2))));
159       const __m128i vl_lo = _mm_unpacklo_epi32(vl_ll, vl_lh);
160       const __m128i vl_hh = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 6))));
161       const __m128i vl_hi = _mm_unpacklo_epi32(vl_hl, vl_hh);
162     #endif  // XNN_ARCH_X86_64
163     const __m128i vl = _mm_unpacklo_epi64(vl_lo, vl_hi);
164 
165     const __m128 vs = _mm_castsi128_ps(_mm_add_epi32(vl, ve));
166     vn = _mm_sub_ps(vn, vmagic_bias);
167 
168     __m128 vt = _mm_add_ps(vz, _mm_mul_ps(vn, vminus_ln2_hi));
169     vt = _mm_add_ps(vt, _mm_mul_ps(vn, vminus_ln2_lo));
170 
171     __m128 vp = _mm_mul_ps(vt, vc2);
172     vp = _mm_add_ps(vt, _mm_mul_ps(vp, vt));
173 
174     const __m128 vy = _mm_add_ps(vs, _mm_mul_ps(vs, vp));
175 
176     __m128 vf = _mm_div_ps(vy, _mm_add_ps(vy, vone));
177     vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf);
178     const __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx)));
179     vf = _mm_or_ps(_mm_and_ps(vf, vm), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf)));
180 
181     _mm_storeu_ps(y, vf);
182     y += 4;
183   }
184   if XNN_UNLIKELY(n != 0) {
185     const __m128 vx = _mm_loadu_ps(x);
186 
187     const __m128 vz = _mm_or_ps(vx, vsign_mask);
188 
189     __m128 vn = _mm_add_ps(_mm_mul_ps(vz, vlog2e), vmagic_bias);
190     const __m128i ve = _mm_slli_epi32(_mm_castps_si128(vn), 17);
191 
192     const __m128i vidx = _mm_slli_epi32(_mm_and_si128(_mm_castps_si128(vn), vindex_mask), 2);
193     #if XNN_ARCH_X86_64
194       const uint64_t vidx_lo = (uint64_t) _mm_cvtsi128_si64(vidx);
195       const uint64_t vidx_hi = (uint64_t) _mm_cvtsi128_si64(_mm_unpackhi_epi64(vidx, vidx));
196       const __m128i vl_ll   = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_lo)));
197       const __m128i vl_hl = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) vidx_hi)));
198       const __m128i vl_lh = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_lo >> 32))));
199       const __m128i vl_lo = _mm_unpacklo_epi32(vl_ll, vl_lh);
200       const __m128i vl_hh = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) (vidx_hi >> 32))));
201       const __m128i vl_hi = _mm_unpacklo_epi32(vl_hl, vl_hh);
202     #else  // !XNN_ARCH_X86_64
203       const __m128i vl_ll = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_cvtsi128_si32(vidx))));
204       const __m128i vl_hl = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 4))));
205       const __m128i vl_lh = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 2))));
206       const __m128i vl_lo = _mm_unpacklo_epi32(vl_ll, vl_lh);
207       const __m128i vl_hh = _mm_cvtsi32_si128(*((const int*) ((uintptr_t) xnn_table_exp2minus_k_over_64 + (uint32_t) _mm_extract_epi16(vidx, 6))));
208       const __m128i vl_hi = _mm_unpacklo_epi32(vl_hl, vl_hh);
209     #endif  // XNN_ARCH_X86_64
210     const __m128i vl = _mm_unpacklo_epi64(vl_lo, vl_hi);
211 
212     const __m128 vs = _mm_castsi128_ps(_mm_add_epi32(vl, ve));
213     vn = _mm_sub_ps(vn, vmagic_bias);
214 
215     __m128 vt = _mm_add_ps(vz, _mm_mul_ps(vn, vminus_ln2_hi));
216     vt = _mm_add_ps(vt, _mm_mul_ps(vn, vminus_ln2_lo));
217 
218     __m128 vp = _mm_mul_ps(vt, vc2);
219     vp = _mm_add_ps(vt, _mm_mul_ps(vp, vt));
220 
221     const __m128 vy = _mm_add_ps(vs, _mm_mul_ps(vs, vp));
222 
223     __m128 vf = _mm_div_ps(vy, _mm_add_ps(vy, vone));
224     vf = _mm_andnot_ps(_mm_cmplt_ps(vz, vdenorm_cutoff), vf);
225     const __m128 vm = _mm_castsi128_ps(_mm_cmpgt_epi32(_mm_setzero_si128(), _mm_castps_si128(vx)));
226     vf = _mm_or_ps(_mm_and_ps(vf, vm), _mm_andnot_ps(vm, _mm_sub_ps(vone, vf)));
227 
228     if (n & (2 * sizeof(float))) {
229       _mm_storel_pi((__m64*) y, vf);
230       vf = _mm_movehl_ps(vf, vf);
231       y += 2;
232     }
233     if (n & (1 * sizeof(float))) {
234       _mm_store_ss(y, vf);
235     }
236   }
237 }
238