blob: d1158a7b10b9c48faa5e542527cb8ae040c6f1b5 [file] [log] [blame]
Marat Dukhan8137e4c2020-01-25 12:56:58 -08001// Auto-generated file. Do not edit!
2// Template: src/f32-raddstoreexpminusmax/neon-lut64-p2.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 <arm_neon.h>
13
14#include <xnnpack/common.h>
15#include <xnnpack/raddstoreexpminusmax.h>
16
17
18extern XNN_INTERNAL const float xnn_table_exp2_k_over_64[64];
19
20void xnn_f32_raddstoreexpminusmax_ukernel__neon_lut64_p2_x12_acc3(
21 size_t elements,
22 const float* input,
23 float* output,
24 float* sum,
Marat Dukhanb2217dd2020-05-28 17:30:28 -070025 float max) XNN_DISABLE_TSAN
Marat Dukhan8137e4c2020-01-25 12:56:58 -080026{
27 assert(elements % sizeof(float) == 0);
28
29 const float32x4_t vmagic_bias = vmovq_n_f32(0x1.800000p23f);
30 // The smallest x for which expf(x) is normalized.
31 const float32x4_t vdenorm_cutoff = vmovq_n_f32(-0x1.5D589Ep6f);
32 const float32x4_t vlog2e_x64 = vmovq_n_f32(0x1.715476p6f);
33 // Last 13 bits are zeroes
34 const float32x4_t vminus_ln2_o64_hi = vmovq_n_f32(-0x1.630000p-7f);
35 const float32x4_t vminus_ln2_o64_lo = vmovq_n_f32(0x1.BD0106p-19f);
36
37 const float32x4_t vc2 = vmovq_n_f32(0x1.FFFF0Ap-2f);
38
39 const int32x4_t vindex_mask = vmovq_n_s32(INT32_C(0x3F));
40
41 const float32x4_t vi_max = vdupq_n_f32(max);
42
43 float32x4_t vacc0 = vmovq_n_f32(0.0f);
44 float32x4_t vacc1 = vmovq_n_f32(0.0f);
45 float32x4_t vacc2 = vmovq_n_f32(0.0f);
46 for (; elements >= 12 * sizeof(float); elements -= 12 * sizeof(float)) {
47 // Load 12 (3x4) inputs at a time.
48 const float32x4_t vi0123 = vld1q_f32(input); input += 4;
49 const float32x4_t vi4567 = vld1q_f32(input); input += 4;
50 const float32x4_t vi89AB = vld1q_f32(input); input += 4;
51
52 // Subtract maximum input x := i - i_max. This implies x <= 0.
53 const float32x4_t vx0123 = vsubq_f32(vi0123, vi_max);
54 const float32x4_t vx4567 = vsubq_f32(vi4567, vi_max);
55 const float32x4_t vx89AB = vsubq_f32(vi89AB, vi_max);
56
57 // Compute reduced argument n := round(x * 64 / log(2)).
58 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
59 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
60 // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
61 // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
62 // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
63 // algorithm.
64 float32x4_t vn0123 = vmlaq_f32(vmagic_bias, vx0123, vlog2e_x64);
65 float32x4_t vn4567 = vmlaq_f32(vmagic_bias, vx4567, vlog2e_x64);
66 float32x4_t vn89AB = vmlaq_f32(vmagic_bias, vx89AB, vlog2e_x64);
67
68 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
69 // i.e. -87.33642 <= x <= 0.0. As n has 6 fractional bits, we split s == 2**(n / 64) = 2**e * 2**(n / 64 - e), where
70 // e := int(n / 64). We create s in two steps:
71 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
72 // fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
73 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
74 // number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
75 // and thus the adjusted exponent is not lower than -126.
76 //
77 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
78 const int32x4_t ve0123 = vshlq_n_s32(vbicq_s32(vreinterpretq_s32_f32(vn0123), vmovq_n_s32(INT32_C(0x3F))), 17);
79 const int32x4_t ve4567 = vshlq_n_s32(vbicq_s32(vreinterpretq_s32_f32(vn4567), vmovq_n_s32(INT32_C(0x3F))), 17);
80 const int32x4_t ve89AB = vshlq_n_s32(vbicq_s32(vreinterpretq_s32_f32(vn89AB), vmovq_n_s32(INT32_C(0x3F))), 17);
81
82 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
83 const uint64x2_t vidx0123 = vreinterpretq_u64_s32(vandq_s32(vreinterpretq_s32_f32(vn0123), vindex_mask));
84 const uint64_t vidx01 = vgetq_lane_u64(vidx0123, 0);
85 const uint64_t vidx23 = vgetq_lane_u64(vidx0123, 1);
86 const uint64x2_t vidx4567 = vreinterpretq_u64_s32(vandq_s32(vreinterpretq_s32_f32(vn4567), vindex_mask));
87 const uint64_t vidx45 = vgetq_lane_u64(vidx4567, 0);
88 const uint64_t vidx67 = vgetq_lane_u64(vidx4567, 1);
89 const uint64x2_t vidx89AB = vreinterpretq_u64_s32(vandq_s32(vreinterpretq_s32_f32(vn89AB), vindex_mask));
90 const uint64_t vidx89 = vgetq_lane_u64(vidx89AB, 0);
91 const uint64_t vidxAB = vgetq_lane_u64(vidx89AB, 1);
92
93 float32x2_t vl01 = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx01]);
94 float32x2_t vl23 = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx23]);
95 float32x2_t vl45 = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx45]);
96 float32x2_t vl67 = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx67]);
97 float32x2_t vl89 = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx89]);
98 float32x2_t vlAB = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidxAB]);
99
100 vl01 = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx01 >> 32)], vl01, 1);
101 vl23 = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx23 >> 32)], vl23, 1);
102 const float32x4_t vl0123 = vcombine_f32(vl01, vl23);
103 vl45 = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx45 >> 32)], vl45, 1);
104 vl67 = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx67 >> 32)], vl67, 1);
105 const float32x4_t vl4567 = vcombine_f32(vl45, vl67);
106 vl89 = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx89 >> 32)], vl89, 1);
107 vlAB = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidxAB >> 32)], vlAB, 1);
108 const float32x4_t vl89AB = vcombine_f32(vl89, vlAB);
109
110 // Adjust exponent of the value l fetched from the table to get the final s value.
111 const float32x4_t vs0123 = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl0123), ve0123));
112 const float32x4_t vs4567 = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl4567), ve4567));
113 const float32x4_t vs89AB = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl89AB), ve89AB));
114
115 // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
116 vn0123 = vsubq_f32(vn0123, vmagic_bias);
117 vn4567 = vsubq_f32(vn4567, vmagic_bias);
118 vn89AB = vsubq_f32(vn89AB, vmagic_bias);
119
120 // Compute reduced argument t := x - n * log(2) / 64.
121 // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
122 float32x4_t vt0123 = vmlaq_f32(vx0123, vn0123, vminus_ln2_o64_hi);
123 float32x4_t vt4567 = vmlaq_f32(vx4567, vn4567, vminus_ln2_o64_hi);
124 float32x4_t vt89AB = vmlaq_f32(vx89AB, vn89AB, vminus_ln2_o64_hi);
125
126 vt0123 = vmlaq_f32(vt0123, vn0123, vminus_ln2_o64_lo);
127 vt4567 = vmlaq_f32(vt4567, vn4567, vminus_ln2_o64_lo);
128 vt89AB = vmlaq_f32(vt89AB, vn89AB, vminus_ln2_o64_lo);
129
Marat Dukhan102a7392020-11-20 01:18:10 -0800130 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
Marat Dukhan8137e4c2020-01-25 12:56:58 -0800131 float32x4_t vp0123 = vmulq_f32(vt0123, vc2);
132 float32x4_t vp4567 = vmulq_f32(vt4567, vc2);
133 float32x4_t vp89AB = vmulq_f32(vt89AB, vc2);
134
135 vp0123 = vmlaq_f32(vt0123, vt0123, vp0123);
136 vp4567 = vmlaq_f32(vt4567, vt4567, vp4567);
137 vp89AB = vmlaq_f32(vt89AB, vt89AB, vp89AB);
138
139 // Reconstruct the final f value:
140 // f = s * (1 + t * (1 + t * c2))
141 // = s * (1 + t + t * (t * c2))
142 // = s + s * (t + t * (t * c2))
143 // = s + s * p
144 float32x4_t vf0123 = vmlaq_f32(vs0123, vs0123, vp0123);
145 float32x4_t vf4567 = vmlaq_f32(vs4567, vs4567, vp4567);
146 float32x4_t vf89AB = vmlaq_f32(vs89AB, vs89AB, vp89AB);
147
148 // For inputs below denormal cutoff, replace output with +0.0f.
149 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
150 vf0123 = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf0123), vcltq_f32(vx0123, vdenorm_cutoff)));
151 vf4567 = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf4567), vcltq_f32(vx4567, vdenorm_cutoff)));
152 vf89AB = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf89AB), vcltq_f32(vx89AB, vdenorm_cutoff)));
153
154 // Store 12 (3x4) outputs at a time.
155 vst1q_f32(output, vf0123); output += 4;
156 vst1q_f32(output, vf4567); output += 4;
157 vst1q_f32(output, vf89AB); output += 4;
158
159 // Accumulate computed exponents.
160 vacc0 = vaddq_f32(vacc0, vf0123);
161 vacc1 = vaddq_f32(vacc1, vf4567);
162 vacc2 = vaddq_f32(vacc2, vf89AB);
163 }
164 // Add up all accumulators to vacc0
165 vacc0 = vaddq_f32(vacc0, vacc1);
166 vacc0 = vaddq_f32(vacc0, vacc2);
167
168 float32x4_t vacc = vacc0;
169 for (; elements >= 4 * sizeof(float); elements -= 4 * sizeof(float)) {
170 // Load 4 inputs at a time.
171 const float32x4_t vi = vld1q_f32(input); input += 4;
172
173 // Subtract maximum input x := i - i_max. This implies x <= 0.
174 const float32x4_t vx = vsubq_f32(vi, vi_max);
175
176 // Compute reduced argument n := round(x * 64 / log(2)).
177 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
178 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
179 // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
180 // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
181 // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
182 // algorithm.
183 float32x4_t vn = vmlaq_f32(vmagic_bias, vx, vlog2e_x64);
184
185 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
186 // i.e. -87.33642 <= x <= 0.0. As n has 6 fractional bits, we split s == 2**(n / 64) = 2**e * 2**(n / 64 - e), where
187 // e := int(n / 64). We create s in two steps:
188 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
189 // fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
190 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
191 // number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
192 // and thus the adjusted exponent is not lower than -126.
193 //
194 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
195 const int32x4_t ve = vshlq_n_s32(vbicq_s32(vreinterpretq_s32_f32(vn), vmovq_n_s32(INT32_C(0x3F))), 17);
196
197 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
198 const uint64x2_t vidx = vreinterpretq_u64_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask));
199 const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0);
200 const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1);
201 float32x2_t vl_lo = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx_lo]);
202 float32x2_t vl_hi = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx_hi]);
203 vl_lo = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx_lo >> 32)], vl_lo, 1);
204 vl_hi = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx_hi >> 32)], vl_hi, 1);
205 const float32x4_t vl = vcombine_f32(vl_lo, vl_hi);
206 // Adjust exponent of the value l fetched from the table to get the final s value.
207 const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve));
208
209 // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
210 vn = vsubq_f32(vn, vmagic_bias);
211
212 // Compute reduced argument t := x - n * log(2) / 64.
213 // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
214 float32x4_t vt = vmlaq_f32(vx, vn, vminus_ln2_o64_hi);
215 vt = vmlaq_f32(vt, vn, vminus_ln2_o64_lo);
216
Marat Dukhan102a7392020-11-20 01:18:10 -0800217 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
Marat Dukhan8137e4c2020-01-25 12:56:58 -0800218 float32x4_t vp = vmulq_f32(vt, vc2);
219 vp = vmlaq_f32(vt, vt, vp);
220
221 // Reconstruct the final f value:
222 // f = s * (1 + t * (1 + t * c2))
223 // = s * (1 + t + t * (t * c2))
224 // = s + s * (t + t * (t * c2))
225 // = s + s * p
226 float32x4_t vf = vmlaq_f32(vs, vs, vp);
227
228 // For inputs below denormal cutoff, replace output with +0.0f.
229 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
230 vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcltq_f32(vx, vdenorm_cutoff)));
231
232 // Store 4 outputs at a time.
233 vst1q_f32(output, vf); output += 4;
234
235 // Accumulate computed exponents.
236 vacc = vaddq_f32(vacc, vf);
237 }
238#if XNN_ARCH_ARM64
239 float vacc_lo = vaddvq_f32(vacc);
240#else
241 float32x2_t vacc_lo = vadd_f32(vget_high_f32(vacc), vget_low_f32(vacc));
242#endif
243 if (elements != 0) {
244 assert(elements >= 1 * sizeof(float));
245 assert(elements <= 3 * sizeof(float));
246 // Load 4 inputs at a time.
247 const float32x4_t vi = vld1q_f32(input); input += 4;
248
249 // Subtract maximum input x := i - i_max. This implies x <= 0.
250 const float32x4_t vx = vsubq_f32(vi, vi_max);
251
252 // Compute reduced argument n := round(x * 64 / log(2)).
253 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
254 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
255 // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
256 // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
257 // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
258 // algorithm.
259 float32x4_t vn = vmlaq_f32(vmagic_bias, vx, vlog2e_x64);
260
261 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
262 // i.e. -87.33642 <= x <= 0.0. As n has 6 fractional bits, we split s == 2**(n / 64) = 2**e * 2**(n / 64 - e), where
263 // e := int(n / 64). We create s in two steps:
264 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
265 // fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
266 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
267 // number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
268 // and thus the adjusted exponent is not lower than -126.
269 //
270 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
271 const int32x4_t ve = vshlq_n_s32(vbicq_s32(vreinterpretq_s32_f32(vn), vmovq_n_s32(INT32_C(0x3F))), 17);
272
273 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
274 const uint64x2_t vidx = vreinterpretq_u64_s32(vandq_s32(vreinterpretq_s32_f32(vn), vindex_mask));
275 const uint64_t vidx_lo = vgetq_lane_u64(vidx, 0);
276 const uint64_t vidx_hi = vgetq_lane_u64(vidx, 1);
277 float32x2_t vl_lo = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx_lo]);
278 float32x2_t vl_hi = vld1_dup_f32(&xnn_table_exp2_k_over_64[(uint32_t) vidx_hi]);
279 vl_lo = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx_lo >> 32)], vl_lo, 1);
280 vl_hi = vld1_lane_f32(&xnn_table_exp2_k_over_64[(uint32_t) (vidx_hi >> 32)], vl_hi, 1);
281 const float32x4_t vl = vcombine_f32(vl_lo, vl_hi);
282 // Adjust exponent of the value l fetched from the table to get the final s value.
283 const float32x4_t vs = vreinterpretq_f32_s32(vaddq_s32(vreinterpretq_s32_f32(vl), ve));
284
285 // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
286 vn = vsubq_f32(vn, vmagic_bias);
287
288 // Compute reduced argument t := x - n * log(2) / 64.
289 // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
290 float32x4_t vt = vmlaq_f32(vx, vn, vminus_ln2_o64_hi);
291 vt = vmlaq_f32(vt, vn, vminus_ln2_o64_lo);
292
Marat Dukhan102a7392020-11-20 01:18:10 -0800293 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
Marat Dukhan8137e4c2020-01-25 12:56:58 -0800294 float32x4_t vp = vmulq_f32(vt, vc2);
295 vp = vmlaq_f32(vt, vt, vp);
296
297 // Reconstruct the final f value:
298 // f = s * (1 + t * (1 + t * c2))
299 // = s * (1 + t + t * (t * c2))
300 // = s + s * (t + t * (t * c2))
301 // = s + s * p
302 float32x4_t vf = vmlaq_f32(vs, vs, vp);
303
304 // For inputs below denormal cutoff, replace output with +0.0f.
305 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
306 vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcltq_f32(vx, vdenorm_cutoff)));
307
308 float32x2_t vf_lo = vget_low_f32(vf);
309 if (elements & (2 * sizeof(float))) {
310 // Store 2 outputs at a time.
311 vst1_f32(output, vf_lo); output += 2;
312
313 // Accumulate 2 computed exponents.
314 #if XNN_ARCH_ARM64
315 vacc_lo += vaddv_f32(vf_lo);
316 #else
317 vacc_lo = vadd_f32(vacc_lo, vf_lo);
318 #endif
319
320 vf_lo = vget_high_f32(vf);
321 }
322 if (elements & (1 * sizeof(float))) {
323 // Store 1 output at a time.
324 vst1_lane_f32(output, vf_lo, 0);
325
326 // Accumulate 1 computed exponent.
327 #if XNN_ARCH_ARM64
328 vacc_lo += vget_lane_f32(vf_lo, 0);
329 #else
330 vacc_lo = vadd_f32(vacc_lo, vreinterpret_f32_u64(vshl_n_u64(vreinterpret_u64_f32(vf_lo), 32)));
331 #endif
332 }
333 }
334 // Reduce 4 elements in the SIMD register
335#if XNN_ARCH_ARM64
336 *sum = vacc_lo;
337#else
338 vst1_lane_f32(sum, vpadd_f32(vacc_lo, vacc_lo), 0);
339#endif
340}