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