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