• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-velu/wasmsimd-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 <wasm_simd128.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__wasmsimd_arm_rr2_lut16_p3_x20(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__wasmsimd_arm_rr2_lut16_p3_x20(
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 v128_t vprescale = wasm_v32x4_load_splat(&params->scalar.prescale);
32   const v128_t valpha = wasm_v32x4_load_splat(&params->scalar.alpha);
33   const v128_t vbeta = wasm_v32x4_load_splat(&params->scalar.beta);
34 
35   const v128_t vsat_cutoff = wasm_f32x4_splat(-0x1.154246p+4f);
36   const v128_t vmagic_bias = wasm_f32x4_splat(0x1.800000p19f);
37   const v128_t vlog2e = wasm_f32x4_splat(0x1.715476p+0f);
38   const v128_t vindex_mask = wasm_i32x4_splat(0xF);
39   const v128_t vminus_ln2_hi = wasm_f32x4_splat(-0x1.62E400p-1f);
40   const v128_t vminus_ln2_lo = wasm_f32x4_splat(-0x1.7F7D1Cp-20f);
41   const v128_t vc3 = wasm_f32x4_splat(0x1.55561Cp-3f);
42   const v128_t vc2 = wasm_f32x4_splat(0x1.0001ECp-1f);
43   const v128_t vone = wasm_f32x4_splat(1.0f);
44 
45   for (; n >= 20 * sizeof(float); n -= 20 * sizeof(float)) {
46     v128_t vx0123 = wasm_v128_load(x);
47     v128_t vx4567 = wasm_v128_load(x + 4);
48     v128_t vx89AB = wasm_v128_load(x + 8);
49     v128_t vxCDEF = wasm_v128_load(x + 12);
50     v128_t vxGHIJ = wasm_v128_load(x + 16);
51     x += 20;
52 
53     const v128_t vz0123 = wasm_f32x4_max(wasm_f32x4_mul(vx0123, vprescale), vsat_cutoff);
54     const v128_t vz4567 = wasm_f32x4_max(wasm_f32x4_mul(vx4567, vprescale), vsat_cutoff);
55     const v128_t vz89AB = wasm_f32x4_max(wasm_f32x4_mul(vx89AB, vprescale), vsat_cutoff);
56     const v128_t vzCDEF = wasm_f32x4_max(wasm_f32x4_mul(vxCDEF, vprescale), vsat_cutoff);
57     const v128_t vzGHIJ = wasm_f32x4_max(wasm_f32x4_mul(vxGHIJ, vprescale), vsat_cutoff);
58 
59     v128_t vn0123 = wasm_f32x4_add(wasm_f32x4_mul(vz0123, vlog2e), vmagic_bias);
60     v128_t vn4567 = wasm_f32x4_add(wasm_f32x4_mul(vz4567, vlog2e), vmagic_bias);
61     v128_t vn89AB = wasm_f32x4_add(wasm_f32x4_mul(vz89AB, vlog2e), vmagic_bias);
62     v128_t vnCDEF = wasm_f32x4_add(wasm_f32x4_mul(vzCDEF, vlog2e), vmagic_bias);
63     v128_t vnGHIJ = wasm_f32x4_add(wasm_f32x4_mul(vzGHIJ, vlog2e), vmagic_bias);
64 
65     const v128_t vidx0123 = wasm_i32x4_shl(wasm_v128_and(vn0123, vindex_mask), 2);
66     const v128_t ven0123 = wasm_i32x4_shl(vn0123, 19);
67     const v128_t vidx4567 = wasm_i32x4_shl(wasm_v128_and(vn4567, vindex_mask), 2);
68     const v128_t ven4567 = wasm_i32x4_shl(vn4567, 19);
69     const v128_t vidx89AB = wasm_i32x4_shl(wasm_v128_and(vn89AB, vindex_mask), 2);
70     const v128_t ven89AB = wasm_i32x4_shl(vn89AB, 19);
71     const v128_t vidxCDEF = wasm_i32x4_shl(wasm_v128_and(vnCDEF, vindex_mask), 2);
72     const v128_t venCDEF = wasm_i32x4_shl(vnCDEF, 19);
73     const v128_t vidxGHIJ = wasm_i32x4_shl(wasm_v128_and(vnGHIJ, vindex_mask), 2);
74     const v128_t venGHIJ = wasm_i32x4_shl(vnGHIJ, 19);
75 
76     const uint64_t vidx01 = wasm_i64x2_extract_lane(vidx0123, 0);
77     const uint64_t vidx23 = wasm_i64x2_extract_lane(vidx0123, 1);
78     const float vl0   = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx01));
79     const float vl1 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx01 >> 32)));
80     const float vl2 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx23));
81     const float vl3 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx23 >> 32)));
82     const v128_t vl0123 = wasm_f32x4_make(vl0, vl1, vl2, vl3);
83     const uint64_t vidx45 = wasm_i64x2_extract_lane(vidx4567, 0);
84     const uint64_t vidx67 = wasm_i64x2_extract_lane(vidx4567, 1);
85     const float vl4   = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx45));
86     const float vl5 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx45 >> 32)));
87     const float vl6 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx67));
88     const float vl7 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx67 >> 32)));
89     const v128_t vl4567 = wasm_f32x4_make(vl4, vl5, vl6, vl7);
90     const uint64_t vidx89 = wasm_i64x2_extract_lane(vidx89AB, 0);
91     const uint64_t vidxAB = wasm_i64x2_extract_lane(vidx89AB, 1);
92     const float vl8   = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx89));
93     const float vl9 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx89 >> 32)));
94     const float vlA = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidxAB));
95     const float vlB = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidxAB >> 32)));
96     const v128_t vl89AB = wasm_f32x4_make(vl8, vl9, vlA, vlB);
97     const uint64_t vidxCD = wasm_i64x2_extract_lane(vidxCDEF, 0);
98     const uint64_t vidxEF = wasm_i64x2_extract_lane(vidxCDEF, 1);
99     const float vlC   = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidxCD));
100     const float vlD = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidxCD >> 32)));
101     const float vlE = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidxEF));
102     const float vlF = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidxEF >> 32)));
103     const v128_t vlCDEF = wasm_f32x4_make(vlC, vlD, vlE, vlF);
104     const uint64_t vidxGH = wasm_i64x2_extract_lane(vidxGHIJ, 0);
105     const uint64_t vidxIJ = wasm_i64x2_extract_lane(vidxGHIJ, 1);
106     const float vlG   = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidxGH));
107     const float vlH = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidxGH >> 32)));
108     const float vlI = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidxIJ));
109     const float vlJ = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidxIJ >> 32)));
110     const v128_t vlGHIJ = wasm_f32x4_make(vlG, vlH, vlI, vlJ);
111 
112     vn0123 = wasm_f32x4_sub(vn0123, vmagic_bias);
113     v128_t vs0123 = wasm_i32x4_add(vl0123, ven0123);
114     vn4567 = wasm_f32x4_sub(vn4567, vmagic_bias);
115     v128_t vs4567 = wasm_i32x4_add(vl4567, ven4567);
116     vn89AB = wasm_f32x4_sub(vn89AB, vmagic_bias);
117     v128_t vs89AB = wasm_i32x4_add(vl89AB, ven89AB);
118     vnCDEF = wasm_f32x4_sub(vnCDEF, vmagic_bias);
119     v128_t vsCDEF = wasm_i32x4_add(vlCDEF, venCDEF);
120     vnGHIJ = wasm_f32x4_sub(vnGHIJ, vmagic_bias);
121     v128_t vsGHIJ = wasm_i32x4_add(vlGHIJ, venGHIJ);
122 
123     v128_t vt0123 = wasm_f32x4_add(wasm_f32x4_mul(vn0123, vminus_ln2_hi), vz0123);
124     v128_t vt4567 = wasm_f32x4_add(wasm_f32x4_mul(vn4567, vminus_ln2_hi), vz4567);
125     v128_t vt89AB = wasm_f32x4_add(wasm_f32x4_mul(vn89AB, vminus_ln2_hi), vz89AB);
126     v128_t vtCDEF = wasm_f32x4_add(wasm_f32x4_mul(vnCDEF, vminus_ln2_hi), vzCDEF);
127     v128_t vtGHIJ = wasm_f32x4_add(wasm_f32x4_mul(vnGHIJ, vminus_ln2_hi), vzGHIJ);
128 
129     vt0123 = wasm_f32x4_add(wasm_f32x4_mul(vn0123, vminus_ln2_lo), vt0123);
130     vt4567 = wasm_f32x4_add(wasm_f32x4_mul(vn4567, vminus_ln2_lo), vt4567);
131     vt89AB = wasm_f32x4_add(wasm_f32x4_mul(vn89AB, vminus_ln2_lo), vt89AB);
132     vtCDEF = wasm_f32x4_add(wasm_f32x4_mul(vnCDEF, vminus_ln2_lo), vtCDEF);
133     vtGHIJ = wasm_f32x4_add(wasm_f32x4_mul(vnGHIJ, vminus_ln2_lo), vtGHIJ);
134 
135     v128_t vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt0123), vc2);
136     v128_t vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt4567), vc2);
137     v128_t vp89AB = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt89AB), vc2);
138     v128_t vpCDEF = wasm_f32x4_add(wasm_f32x4_mul(vc3, vtCDEF), vc2);
139     v128_t vpGHIJ = wasm_f32x4_add(wasm_f32x4_mul(vc3, vtGHIJ), vc2);
140 
141     vp0123 = wasm_f32x4_mul(vp0123, vt0123);
142     vp4567 = wasm_f32x4_mul(vp4567, vt4567);
143     vp89AB = wasm_f32x4_mul(vp89AB, vt89AB);
144     vpCDEF = wasm_f32x4_mul(vpCDEF, vtCDEF);
145     vpGHIJ = wasm_f32x4_mul(vpGHIJ, vtGHIJ);
146 
147     vt0123 = wasm_f32x4_mul(vt0123, vs0123);
148     vs0123 = wasm_f32x4_sub(vs0123, vone);
149     vt4567 = wasm_f32x4_mul(vt4567, vs4567);
150     vs4567 = wasm_f32x4_sub(vs4567, vone);
151     vt89AB = wasm_f32x4_mul(vt89AB, vs89AB);
152     vs89AB = wasm_f32x4_sub(vs89AB, vone);
153     vtCDEF = wasm_f32x4_mul(vtCDEF, vsCDEF);
154     vsCDEF = wasm_f32x4_sub(vsCDEF, vone);
155     vtGHIJ = wasm_f32x4_mul(vtGHIJ, vsGHIJ);
156     vsGHIJ = wasm_f32x4_sub(vsGHIJ, vone);
157 
158     vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vp0123, vt0123), vt0123);
159     vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vp4567, vt4567), vt4567);
160     vp89AB = wasm_f32x4_add(wasm_f32x4_mul(vp89AB, vt89AB), vt89AB);
161     vpCDEF = wasm_f32x4_add(wasm_f32x4_mul(vpCDEF, vtCDEF), vtCDEF);
162     vpGHIJ = wasm_f32x4_add(wasm_f32x4_mul(vpGHIJ, vtGHIJ), vtGHIJ);
163 
164     const v128_t ve0123 = wasm_f32x4_mul(wasm_f32x4_add(vp0123, vs0123), valpha);
165     const v128_t ve4567 = wasm_f32x4_mul(wasm_f32x4_add(vp4567, vs4567), valpha);
166     const v128_t ve89AB = wasm_f32x4_mul(wasm_f32x4_add(vp89AB, vs89AB), valpha);
167     const v128_t veCDEF = wasm_f32x4_mul(wasm_f32x4_add(vpCDEF, vsCDEF), valpha);
168     const v128_t veGHIJ = wasm_f32x4_mul(wasm_f32x4_add(vpGHIJ, vsGHIJ), valpha);
169 
170     const v128_t vsignm0123 = wasm_i32x4_shr(vx0123, 31);
171     vx0123 = wasm_f32x4_mul(vx0123, vbeta);
172     const v128_t vsignm4567 = wasm_i32x4_shr(vx4567, 31);
173     vx4567 = wasm_f32x4_mul(vx4567, vbeta);
174     const v128_t vsignm89AB = wasm_i32x4_shr(vx89AB, 31);
175     vx89AB = wasm_f32x4_mul(vx89AB, vbeta);
176     const v128_t vsignmCDEF = wasm_i32x4_shr(vxCDEF, 31);
177     vxCDEF = wasm_f32x4_mul(vxCDEF, vbeta);
178     const v128_t vsignmGHIJ = wasm_i32x4_shr(vxGHIJ, 31);
179     vxGHIJ = wasm_f32x4_mul(vxGHIJ, vbeta);
180 
181     const v128_t vy0123 = wasm_v128_bitselect(ve0123, vx0123, vsignm0123);
182     const v128_t vy4567 = wasm_v128_bitselect(ve4567, vx4567, vsignm4567);
183     const v128_t vy89AB = wasm_v128_bitselect(ve89AB, vx89AB, vsignm89AB);
184     const v128_t vyCDEF = wasm_v128_bitselect(veCDEF, vxCDEF, vsignmCDEF);
185     const v128_t vyGHIJ = wasm_v128_bitselect(veGHIJ, vxGHIJ, vsignmGHIJ);
186 
187     wasm_v128_store(y, vy0123);
188     wasm_v128_store(y + 4, vy4567);
189     wasm_v128_store(y + 8, vy89AB);
190     wasm_v128_store(y + 12, vyCDEF);
191     wasm_v128_store(y + 16, vyGHIJ);
192     y += 20;
193   }
194   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
195     v128_t vx = wasm_v128_load(x);
196     x += 4;
197 
198     const v128_t vz = wasm_f32x4_max(wasm_f32x4_mul(vx, vprescale), vsat_cutoff);
199 
200     v128_t vn = wasm_f32x4_add(wasm_f32x4_mul(vz, vlog2e), vmagic_bias);
201     const v128_t vidx = wasm_i32x4_shl(wasm_v128_and(vn, vindex_mask), 2);
202     const v128_t ven = wasm_i32x4_shl(vn, 19);
203 
204     const uint64_t vidx_lo = wasm_i64x2_extract_lane(vidx, 0);
205     const uint64_t vidx_hi = wasm_i64x2_extract_lane(vidx, 1);
206     const float vl0 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx_lo));
207     const float vl1 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx_lo >> 32)));
208     const float vl2 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx_hi));
209     const float vl3 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx_hi >> 32)));
210     const v128_t vl = wasm_f32x4_make(vl0, vl1, vl2, vl3);
211 
212     v128_t vs = wasm_i32x4_add(vl, ven);
213     vn = wasm_f32x4_sub(vn, vmagic_bias);
214 
215     v128_t vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_hi), vz);
216     vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_lo), vt);
217 
218     v128_t vp = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt), vc2);
219     vp = wasm_f32x4_mul(vp, vt);
220 
221     vt = wasm_f32x4_mul(vt, vs);
222     vs = wasm_f32x4_sub(vs, vone);
223     vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vt);
224     const v128_t ve = wasm_f32x4_mul(wasm_f32x4_add(vp, vs), valpha);
225 
226     const v128_t vsignm = wasm_i32x4_shr(vx, 31);
227     vx = wasm_f32x4_mul(vx, vbeta);
228     const v128_t vy = wasm_v128_bitselect(ve, vx, vsignm);
229 
230     wasm_v128_store(y, vy);
231     y += 4;
232   }
233   if XNN_UNLIKELY(n != 0) {
234     v128_t vx = wasm_v128_load(x);
235 
236     const v128_t vz = wasm_f32x4_max(wasm_f32x4_mul(vx, vprescale), vsat_cutoff);
237 
238     v128_t vn = wasm_f32x4_add(wasm_f32x4_mul(vz, vlog2e), vmagic_bias);
239     const v128_t vidx = wasm_i32x4_shl(wasm_v128_and(vn, vindex_mask), 2);
240     const v128_t ven = wasm_i32x4_shl(vn, 19);
241 
242     const uint64_t vidx_lo = wasm_i64x2_extract_lane(vidx, 0);
243     const uint64_t vidx_hi = wasm_i64x2_extract_lane(vidx, 1);
244     const float vl0 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx_lo));
245     const float vl1 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx_lo >> 32)));
246     const float vl2 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx_hi));
247     const float vl3 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx_hi >> 32)));
248     const v128_t vl = wasm_f32x4_make(vl0, vl1, vl2, vl3);
249 
250     v128_t vs = wasm_i32x4_add(vl, ven);
251     vn = wasm_f32x4_sub(vn, vmagic_bias);
252 
253     v128_t vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_hi), vz);
254     vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_lo), vt);
255 
256     v128_t vp = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt), vc2);
257     vp = wasm_f32x4_mul(vp, vt);
258 
259     vt = wasm_f32x4_mul(vt, vs);
260     vs = wasm_f32x4_sub(vs, vone);
261     vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vt);
262     const v128_t ve = wasm_f32x4_mul(wasm_f32x4_add(vp, vs), valpha);
263 
264     const v128_t vsignm = wasm_i32x4_shr(vx, 31);
265     vx = wasm_f32x4_mul(vx, vbeta);
266     v128_t vy = wasm_v128_bitselect(ve, vx, vsignm);
267 
268     if (n & (2 * sizeof(float))) {
269       *((double*) y) = wasm_f64x2_extract_lane(vy, 0);
270       vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3);
271       y += 2;
272     }
273     if (n & (1 * sizeof(float))) {
274       *y = wasm_f32x4_extract_lane(vy, 0);
275     }
276   }
277 }
278