• 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_x86_rr2_lut16_p3_x12(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_x86_rr2_lut16_p3_x12(
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 >= 12 * sizeof(float); n -= 12 * 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     x += 12;
50 
51     const v128_t vz0123 = wasm_f32x4_mul(vx0123, vprescale);
52     const v128_t vz4567 = wasm_f32x4_mul(vx4567, vprescale);
53     const v128_t vz89AB = wasm_f32x4_mul(vx89AB, vprescale);
54 
55     v128_t vn0123 = wasm_f32x4_add(wasm_f32x4_mul(vz0123, vlog2e), vmagic_bias);
56     v128_t vn4567 = wasm_f32x4_add(wasm_f32x4_mul(vz4567, vlog2e), vmagic_bias);
57     v128_t vn89AB = wasm_f32x4_add(wasm_f32x4_mul(vz89AB, vlog2e), vmagic_bias);
58 
59     const v128_t vidx0123 = wasm_i32x4_shl(wasm_v128_and(vn0123, vindex_mask), 2);
60     const v128_t ven0123 = wasm_i32x4_shl(vn0123, 19);
61     const v128_t vidx4567 = wasm_i32x4_shl(wasm_v128_and(vn4567, vindex_mask), 2);
62     const v128_t ven4567 = wasm_i32x4_shl(vn4567, 19);
63     const v128_t vidx89AB = wasm_i32x4_shl(wasm_v128_and(vn89AB, vindex_mask), 2);
64     const v128_t ven89AB = wasm_i32x4_shl(vn89AB, 19);
65 
66     const uint64_t vidx01 = wasm_i64x2_extract_lane(vidx0123, 0);
67     const uint64_t vidx23 = wasm_i64x2_extract_lane(vidx0123, 1);
68     const float vl0   = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx01));
69     const float vl1 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx01 >> 32)));
70     const float vl2 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx23));
71     const float vl3 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx23 >> 32)));
72     const v128_t vl0123 = wasm_f32x4_make(vl0, vl1, vl2, vl3);
73     const uint64_t vidx45 = wasm_i64x2_extract_lane(vidx4567, 0);
74     const uint64_t vidx67 = wasm_i64x2_extract_lane(vidx4567, 1);
75     const float vl4   = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx45));
76     const float vl5 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx45 >> 32)));
77     const float vl6 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx67));
78     const float vl7 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx67 >> 32)));
79     const v128_t vl4567 = wasm_f32x4_make(vl4, vl5, vl6, vl7);
80     const uint64_t vidx89 = wasm_i64x2_extract_lane(vidx89AB, 0);
81     const uint64_t vidxAB = wasm_i64x2_extract_lane(vidx89AB, 1);
82     const float vl8   = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx89));
83     const float vl9 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx89 >> 32)));
84     const float vlA = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidxAB));
85     const float vlB = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidxAB >> 32)));
86     const v128_t vl89AB = wasm_f32x4_make(vl8, vl9, vlA, vlB);
87 
88     vn0123 = wasm_f32x4_sub(vn0123, vmagic_bias);
89     v128_t vs0123 = wasm_i32x4_add(vl0123, ven0123);
90     vn4567 = wasm_f32x4_sub(vn4567, vmagic_bias);
91     v128_t vs4567 = wasm_i32x4_add(vl4567, ven4567);
92     vn89AB = wasm_f32x4_sub(vn89AB, vmagic_bias);
93     v128_t vs89AB = wasm_i32x4_add(vl89AB, ven89AB);
94 
95     v128_t vt0123 = wasm_f32x4_add(wasm_f32x4_mul(vn0123, vminus_ln2_hi), vz0123);
96     const v128_t vsatm0123 = wasm_f32x4_le(vz0123, vsat_cutoff);
97     v128_t vt4567 = wasm_f32x4_add(wasm_f32x4_mul(vn4567, vminus_ln2_hi), vz4567);
98     const v128_t vsatm4567 = wasm_f32x4_le(vz4567, vsat_cutoff);
99     v128_t vt89AB = wasm_f32x4_add(wasm_f32x4_mul(vn89AB, vminus_ln2_hi), vz89AB);
100     const v128_t vsatm89AB = wasm_f32x4_le(vz89AB, vsat_cutoff);
101 
102     vt0123 = wasm_f32x4_add(wasm_f32x4_mul(vn0123, vminus_ln2_lo), vt0123);
103     vs0123 = wasm_v128_andnot(vs0123, vsatm0123);
104     vt4567 = wasm_f32x4_add(wasm_f32x4_mul(vn4567, vminus_ln2_lo), vt4567);
105     vs4567 = wasm_v128_andnot(vs4567, vsatm4567);
106     vt89AB = wasm_f32x4_add(wasm_f32x4_mul(vn89AB, vminus_ln2_lo), vt89AB);
107     vs89AB = wasm_v128_andnot(vs89AB, vsatm89AB);
108 
109     vt0123 = wasm_v128_andnot(vt0123, vsatm0123);
110     v128_t vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt0123), vc2);
111     vt4567 = wasm_v128_andnot(vt4567, vsatm4567);
112     v128_t vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt4567), vc2);
113     vt89AB = wasm_v128_andnot(vt89AB, vsatm89AB);
114     v128_t vp89AB = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt89AB), vc2);
115 
116     vp0123 = wasm_f32x4_mul(vp0123, vt0123);
117     vp4567 = wasm_f32x4_mul(vp4567, vt4567);
118     vp89AB = wasm_f32x4_mul(vp89AB, vt89AB);
119 
120     vt0123 = wasm_f32x4_mul(vt0123, vs0123);
121     vs0123 = wasm_f32x4_sub(vs0123, vone);
122     vt4567 = wasm_f32x4_mul(vt4567, vs4567);
123     vs4567 = wasm_f32x4_sub(vs4567, vone);
124     vt89AB = wasm_f32x4_mul(vt89AB, vs89AB);
125     vs89AB = wasm_f32x4_sub(vs89AB, vone);
126 
127     vp0123 = wasm_f32x4_add(wasm_f32x4_mul(vp0123, vt0123), vt0123);
128     vp4567 = wasm_f32x4_add(wasm_f32x4_mul(vp4567, vt4567), vt4567);
129     vp89AB = wasm_f32x4_add(wasm_f32x4_mul(vp89AB, vt89AB), vt89AB);
130 
131     const v128_t ve0123 = wasm_f32x4_mul(wasm_f32x4_add(vp0123, vs0123), valpha);
132     const v128_t ve4567 = wasm_f32x4_mul(wasm_f32x4_add(vp4567, vs4567), valpha);
133     const v128_t ve89AB = wasm_f32x4_mul(wasm_f32x4_add(vp89AB, vs89AB), valpha);
134 
135     const v128_t vsignm0123 = wasm_i32x4_shr(vx0123, 31);
136     vx0123 = wasm_f32x4_mul(vx0123, vbeta);
137     const v128_t vsignm4567 = wasm_i32x4_shr(vx4567, 31);
138     vx4567 = wasm_f32x4_mul(vx4567, vbeta);
139     const v128_t vsignm89AB = wasm_i32x4_shr(vx89AB, 31);
140     vx89AB = wasm_f32x4_mul(vx89AB, vbeta);
141 
142     const v128_t vy0123 = wasm_v128_bitselect(ve0123, vx0123, vsignm0123);
143     const v128_t vy4567 = wasm_v128_bitselect(ve4567, vx4567, vsignm4567);
144     const v128_t vy89AB = wasm_v128_bitselect(ve89AB, vx89AB, vsignm89AB);
145 
146     wasm_v128_store(y, vy0123);
147     wasm_v128_store(y + 4, vy4567);
148     wasm_v128_store(y + 8, vy89AB);
149     y += 12;
150   }
151   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
152     v128_t vx = wasm_v128_load(x);
153     x += 4;
154 
155     const v128_t vz = wasm_f32x4_mul(vx, vprescale);
156 
157     v128_t vn = wasm_f32x4_add(wasm_f32x4_mul(vz, vlog2e), vmagic_bias);
158     const v128_t vidx = wasm_i32x4_shl(wasm_v128_and(vn, vindex_mask), 2);
159     const v128_t ven = wasm_i32x4_shl(vn, 19);
160 
161     const uint64_t vidx_lo = wasm_i64x2_extract_lane(vidx, 0);
162     const uint64_t vidx_hi = wasm_i64x2_extract_lane(vidx, 1);
163     const float vl0 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx_lo));
164     const float vl1 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx_lo >> 32)));
165     const float vl2 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx_hi));
166     const float vl3 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx_hi >> 32)));
167     const v128_t vl = wasm_f32x4_make(vl0, vl1, vl2, vl3);
168 
169     v128_t vs = wasm_i32x4_add(vl, ven);
170     vn = wasm_f32x4_sub(vn, vmagic_bias);
171 
172     v128_t vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_hi), vz);
173     const v128_t vsatm = wasm_f32x4_le(vz, vsat_cutoff);
174     vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_lo), vt);
175     vs = wasm_v128_andnot(vs, vsatm);
176     vt = wasm_v128_andnot(vt, vsatm);
177 
178     v128_t vp = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt), vc2);
179     vp = wasm_f32x4_mul(vp, vt);
180 
181     vt = wasm_f32x4_mul(vt, vs);
182     vs = wasm_f32x4_sub(vs, vone);
183     vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vt);
184     const v128_t ve = wasm_f32x4_mul(wasm_f32x4_add(vp, vs), valpha);
185 
186     const v128_t vsignm = wasm_i32x4_shr(vx, 31);
187     vx = wasm_f32x4_mul(vx, vbeta);
188     const v128_t vy = wasm_v128_bitselect(ve, vx, vsignm);
189 
190     wasm_v128_store(y, vy);
191     y += 4;
192   }
193   if XNN_UNLIKELY(n != 0) {
194     v128_t vx = wasm_v128_load(x);
195 
196     const v128_t vz = wasm_f32x4_mul(vx, vprescale);
197 
198     v128_t vn = wasm_f32x4_add(wasm_f32x4_mul(vz, vlog2e), vmagic_bias);
199     const v128_t vidx = wasm_i32x4_shl(wasm_v128_and(vn, vindex_mask), 2);
200     const v128_t ven = wasm_i32x4_shl(vn, 19);
201 
202     const uint64_t vidx_lo = wasm_i64x2_extract_lane(vidx, 0);
203     const uint64_t vidx_hi = wasm_i64x2_extract_lane(vidx, 1);
204     const float vl0 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx_lo));
205     const float vl1 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx_lo >> 32)));
206     const float vl2 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) vidx_hi));
207     const float vl3 = *((const float*) ((uintptr_t) xnn_table_exp2minus_k_over_16 + (uint32_t) (vidx_hi >> 32)));
208     const v128_t vl = wasm_f32x4_make(vl0, vl1, vl2, vl3);
209 
210     v128_t vs = wasm_i32x4_add(vl, ven);
211     vn = wasm_f32x4_sub(vn, vmagic_bias);
212 
213     v128_t vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_hi), vz);
214     const v128_t vsatm = wasm_f32x4_le(vz, vsat_cutoff);
215     vt = wasm_f32x4_add(wasm_f32x4_mul(vn, vminus_ln2_lo), vt);
216     vs = wasm_v128_andnot(vs, vsatm);
217     vt = wasm_v128_andnot(vt, vsatm);
218 
219     v128_t vp = wasm_f32x4_add(wasm_f32x4_mul(vc3, vt), vc2);
220     vp = wasm_f32x4_mul(vp, vt);
221 
222     vt = wasm_f32x4_mul(vt, vs);
223     vs = wasm_f32x4_sub(vs, vone);
224     vp = wasm_f32x4_add(wasm_f32x4_mul(vp, vt), vt);
225     const v128_t ve = wasm_f32x4_mul(wasm_f32x4_add(vp, vs), valpha);
226 
227     const v128_t vsignm = wasm_i32x4_shr(vx, 31);
228     vx = wasm_f32x4_mul(vx, vbeta);
229     v128_t vy = wasm_v128_bitselect(ve, vx, vsignm);
230 
231     if (n & (2 * sizeof(float))) {
232       *((double*) y) = wasm_f64x2_extract_lane(vy, 0);
233       vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3);
234       y += 2;
235     }
236     if (n & (1 * sizeof(float))) {
237       *y = wasm_f32x4_extract_lane(vy, 0);
238     }
239   }
240 }
241