blob: 4e85372bd4b5b327f150aa85f67181e7476fa2bf [file] [log] [blame]
Marat Dukhan8d3c07e2020-01-02 01:20:59 -08001// Auto-generated file. Do not edit!
2// Template: src/f32-sigmoid/neon-p5.c.in
3// Generator: tools/xngen
4//
5// Copyright 2019 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/vunary.h>
16
17
18void xnn_f32_sigmoid_ukernel__neonfma_p5_nr1recps1fma_x20(
19 size_t n,
20 const float* x,
21 float* y,
22 const void* params)
23{
24 assert(n % sizeof(float) == 0);
25
26 const float32x4_t vmagic_bias = vmovq_n_f32(0x1.8000FEp23f);
27 // The largest z for which sigmoidf(-z) is normalized.
28 // This number is also the largest z for which expf(-z) is normalized.
29 const float32x4_t vdenorm_cutoff = vmovq_n_f32(0x1.5D589Ep+6f);
30 const float32x4_t vminus_log2e = vmovq_n_f32(-0x1.715476p+0f);
31 const float32x4_t vln2_hi = vmovq_n_f32(0x1.62E43p-1f);
32 const float32x4_t vln2_lo = vmovq_n_f32(-0x1.05C61p-29f);
33 const float32x4_t vone = vmovq_n_f32(1.0f);
34
35 const float32x4_t vc1 = vmovq_n_f32(-0x1.FFFFF6p-1f);
36 const float32x4_t vc2 = vmovq_n_f32(0x1.FFFDC6p-2f);
37 const float32x4_t vc3 = vmovq_n_f32(-0x1.555A80p-3f);
38 const float32x4_t vc4 = vmovq_n_f32(0x1.573A1Ap-5f);
39 const float32x4_t vc5 = vmovq_n_f32(-0x1.0F9F9Cp-7f);
40
41 for (; n >= 20 * sizeof(float); n -= 20 * sizeof(float)) {
42 const float32x4_t vx0123 = vld1q_f32(x); x += 4;
43 const float32x4_t vx4567 = vld1q_f32(x); x += 4;
44 const float32x4_t vx89AB = vld1q_f32(x); x += 4;
45 const float32x4_t vxCDEF = vld1q_f32(x); x += 4;
46 const float32x4_t vxGHIJ = vld1q_f32(x); x += 4;
47
48 // General structure of the algorithm:
49 // / exp(x) / (1 + exp(x)) if x <= 0
50 // f[x] :=
51 // \ 1 - f[-x] if x >= 0
52 //
53 // First we compute f[z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
54 // then replace result with 1 - f[z] if x >= 0.
55 const float32x4_t vz0123 = vabsq_f32(vx0123);
56 const float32x4_t vz4567 = vabsq_f32(vx4567);
57 const float32x4_t vz89AB = vabsq_f32(vx89AB);
58 const float32x4_t vzCDEF = vabsq_f32(vxCDEF);
59 const float32x4_t vzGHIJ = vabsq_f32(vxGHIJ);
60
61 // Compute reduced argument n := round(-z / log(2)).
62 // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
63 // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
64 // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
65 // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
66 // anyway. We fixup the result for such inputs at the very end of the algorithm.
67 float32x4_t vn0123 = vfmaq_f32(vmagic_bias, vz0123, vminus_log2e);
68 float32x4_t vn4567 = vfmaq_f32(vmagic_bias, vz4567, vminus_log2e);
69 float32x4_t vn89AB = vfmaq_f32(vmagic_bias, vz89AB, vminus_log2e);
70 float32x4_t vnCDEF = vfmaq_f32(vmagic_bias, vzCDEF, vminus_log2e);
71 float32x4_t vnGHIJ = vfmaq_f32(vmagic_bias, vzGHIJ, vminus_log2e);
72
73 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
74 // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
75 const float32x4_t vs0123 = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn0123), 23));
76 const float32x4_t vs4567 = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn4567), 23));
77 const float32x4_t vs89AB = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn89AB), 23));
78 const float32x4_t vsCDEF = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vnCDEF), 23));
79 const float32x4_t vsGHIJ = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vnGHIJ), 23));
80
81 // Subtract the large number back to get final n := round(-z / log(2)).
82 vn0123 = vsubq_f32(vn0123, vmagic_bias);
83 vn4567 = vsubq_f32(vn4567, vmagic_bias);
84 vn89AB = vsubq_f32(vn89AB, vmagic_bias);
85 vnCDEF = vsubq_f32(vnCDEF, vmagic_bias);
86 vnGHIJ = vsubq_f32(vnGHIJ, vmagic_bias);
87
88 // Compute reduced argument -t := -z - n * log(2) = -(z + n * log(2)).
89 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
90 float32x4_t vt0123 = vfmaq_f32(vz0123, vn0123, vln2_hi);
91 float32x4_t vt4567 = vfmaq_f32(vz4567, vn4567, vln2_hi);
92 float32x4_t vt89AB = vfmaq_f32(vz89AB, vn89AB, vln2_hi);
93 float32x4_t vtCDEF = vfmaq_f32(vzCDEF, vnCDEF, vln2_hi);
94 float32x4_t vtGHIJ = vfmaq_f32(vzGHIJ, vnGHIJ, vln2_hi);
95
96 vt0123 = vfmaq_f32(vt0123, vn0123, vln2_lo);
97 vt4567 = vfmaq_f32(vt4567, vn4567, vln2_lo);
98 vt89AB = vfmaq_f32(vt89AB, vn89AB, vln2_lo);
99 vtCDEF = vfmaq_f32(vtCDEF, vnCDEF, vln2_lo);
100 vtGHIJ = vfmaq_f32(vtGHIJ, vnGHIJ, vln2_lo);
101
102 // Compute degree-5 polynomial approxiatmion for exp(-t) on [-log(2)/2, log(2)/2].
103 float32x4_t vp0123 = vfmaq_f32(vc4, vc5, vt0123);
104 float32x4_t vp4567 = vfmaq_f32(vc4, vc5, vt4567);
105 float32x4_t vp89AB = vfmaq_f32(vc4, vc5, vt89AB);
106 float32x4_t vpCDEF = vfmaq_f32(vc4, vc5, vtCDEF);
107 float32x4_t vpGHIJ = vfmaq_f32(vc4, vc5, vtGHIJ);
108
109 vp0123 = vfmaq_f32(vc3, vp0123, vt0123);
110 vp4567 = vfmaq_f32(vc3, vp4567, vt4567);
111 vp89AB = vfmaq_f32(vc3, vp89AB, vt89AB);
112 vpCDEF = vfmaq_f32(vc3, vpCDEF, vtCDEF);
113 vpGHIJ = vfmaq_f32(vc3, vpGHIJ, vtGHIJ);
114
115 vp0123 = vfmaq_f32(vc2, vp0123, vt0123);
116 vp4567 = vfmaq_f32(vc2, vp4567, vt4567);
117 vp89AB = vfmaq_f32(vc2, vp89AB, vt89AB);
118 vpCDEF = vfmaq_f32(vc2, vpCDEF, vtCDEF);
119 vpGHIJ = vfmaq_f32(vc2, vpGHIJ, vtGHIJ);
120
121 vp0123 = vfmaq_f32(vc1, vp0123, vt0123);
122 vp4567 = vfmaq_f32(vc1, vp4567, vt4567);
123 vp89AB = vfmaq_f32(vc1, vp89AB, vt89AB);
124 vpCDEF = vfmaq_f32(vc1, vpCDEF, vtCDEF);
125 vpGHIJ = vfmaq_f32(vc1, vpGHIJ, vtGHIJ);
126
127 // Reconstruct the exp(-z) value:
128 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
129 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
130 // = s + (t * s) * p
131 vt0123 = vmulq_f32(vt0123, vs0123);
132 vt4567 = vmulq_f32(vt4567, vs4567);
133 vt89AB = vmulq_f32(vt89AB, vs89AB);
134 vtCDEF = vmulq_f32(vtCDEF, vsCDEF);
135 vtGHIJ = vmulq_f32(vtGHIJ, vsGHIJ);
136
137 float32x4_t ve0123 = vfmaq_f32(vs0123, vp0123, vt0123);
138 float32x4_t ve4567 = vfmaq_f32(vs4567, vp4567, vt4567);
139 float32x4_t ve89AB = vfmaq_f32(vs89AB, vp89AB, vt89AB);
140 float32x4_t veCDEF = vfmaq_f32(vsCDEF, vpCDEF, vtCDEF);
141 float32x4_t veGHIJ = vfmaq_f32(vsGHIJ, vpGHIJ, vtGHIJ);
142
143 // Denominator of the sigmoid fraction: 1.0 + exp(-z)
144 float32x4_t vd0123 = vaddq_f32(ve0123, vone);
145 float32x4_t vd4567 = vaddq_f32(ve4567, vone);
146 float32x4_t vd89AB = vaddq_f32(ve89AB, vone);
147 float32x4_t vdCDEF = vaddq_f32(veCDEF, vone);
148 float32x4_t vdGHIJ = vaddq_f32(veGHIJ, vone);
149
150 // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator.
151 // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
152 // Thus the reciprocal of the denominator never overflows.
153 float32x4_t vr0123 = vrecpeq_f32(vd0123);
154 float32x4_t vr4567 = vrecpeq_f32(vd4567);
155 float32x4_t vr89AB = vrecpeq_f32(vd89AB);
156 float32x4_t vrCDEF = vrecpeq_f32(vdCDEF);
157 float32x4_t vrGHIJ = vrecpeq_f32(vdGHIJ);
158
159 vr0123 = vmulq_f32(vr0123, vrecpsq_f32(vr0123, vd0123));
160 vr4567 = vmulq_f32(vr4567, vrecpsq_f32(vr4567, vd4567));
161 vr89AB = vmulq_f32(vr89AB, vrecpsq_f32(vr89AB, vd89AB));
162 vrCDEF = vmulq_f32(vrCDEF, vrecpsq_f32(vrCDEF, vdCDEF));
163 vrGHIJ = vmulq_f32(vrGHIJ, vrecpsq_f32(vrGHIJ, vdGHIJ));
164
165 vr0123 = vfmaq_f32(vr0123, vr0123, vfmsq_f32(vone, vr0123, vd0123));
166 vr4567 = vfmaq_f32(vr4567, vr4567, vfmsq_f32(vone, vr4567, vd4567));
167 vr89AB = vfmaq_f32(vr89AB, vr89AB, vfmsq_f32(vone, vr89AB, vd89AB));
168 vrCDEF = vfmaq_f32(vrCDEF, vrCDEF, vfmsq_f32(vone, vrCDEF, vdCDEF));
169 vrGHIJ = vfmaq_f32(vrGHIJ, vrGHIJ, vfmsq_f32(vone, vrGHIJ, vdGHIJ));
170
171 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
172 float32x4_t vf0123 = vmulq_f32(ve0123, vr0123);
173 float32x4_t vf4567 = vmulq_f32(ve4567, vr4567);
174 float32x4_t vf89AB = vmulq_f32(ve89AB, vr89AB);
175 float32x4_t vfCDEF = vmulq_f32(veCDEF, vrCDEF);
176 float32x4_t vfGHIJ = vmulq_f32(veGHIJ, vrGHIJ);
177
178 // For inputs below denormal cutoff, replace output with +0.0f.
179 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
180 vf0123 = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf0123), vcagtq_f32(vx0123, vdenorm_cutoff)));
181 vf4567 = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf4567), vcagtq_f32(vx4567, vdenorm_cutoff)));
182 vf89AB = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf89AB), vcagtq_f32(vx89AB, vdenorm_cutoff)));
183 vfCDEF = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vfCDEF), vcagtq_f32(vxCDEF, vdenorm_cutoff)));
184 vfGHIJ = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vfGHIJ), vcagtq_f32(vxGHIJ, vdenorm_cutoff)));
185
186 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
187 const uint32x4_t vm0123 = vcltq_f32(vx0123, vmovq_n_f32(0.0f));
188 const uint32x4_t vm4567 = vcltq_f32(vx4567, vmovq_n_f32(0.0f));
189 const uint32x4_t vm89AB = vcltq_f32(vx89AB, vmovq_n_f32(0.0f));
190 const uint32x4_t vmCDEF = vcltq_f32(vxCDEF, vmovq_n_f32(0.0f));
191 const uint32x4_t vmGHIJ = vcltq_f32(vxGHIJ, vmovq_n_f32(0.0f));
192
193 vf0123 = vbslq_f32(vm0123, vf0123, vsubq_f32(vone, vf0123));
194 vf4567 = vbslq_f32(vm4567, vf4567, vsubq_f32(vone, vf4567));
195 vf89AB = vbslq_f32(vm89AB, vf89AB, vsubq_f32(vone, vf89AB));
196 vfCDEF = vbslq_f32(vmCDEF, vfCDEF, vsubq_f32(vone, vfCDEF));
197 vfGHIJ = vbslq_f32(vmGHIJ, vfGHIJ, vsubq_f32(vone, vfGHIJ));
198
199 vst1q_f32(y, vf0123); y += 4;
200 vst1q_f32(y, vf4567); y += 4;
201 vst1q_f32(y, vf89AB); y += 4;
202 vst1q_f32(y, vfCDEF); y += 4;
203 vst1q_f32(y, vfGHIJ); y += 4;
204 }
205 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
206 const float32x4_t vx = vld1q_f32(x); x += 4;
207
208 // General structure of the algorithm:
209 // / exp(x) / (1 + exp(x)) if x <= 0
210 // f[x] :=
211 // \ 1 - f[-x] if x >= 0
212 //
213 // First we compute f[z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
214 // then replace result with 1 - f[z] if x <= 0.
215 const float32x4_t vz = vabsq_f32(vx);
216
217 // Compute reduced argument n := round(-z / log(2)).
218 // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
219 // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
220 // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
221 // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
222 // anyway. We fixup the result for such inputs at the very end of the algorithm.
223 float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e);
224
225 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
226 // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
227 const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23));
228
229 // Subtract the large number back to get final n := round(-z / log(2)).
230 vn = vsubq_f32(vn, vmagic_bias);
231
232 // Compute reduced argument -t := -z - n * log(2) = -(z + n * log(2)).
233 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
234 float32x4_t vt = vfmaq_f32(vz, vn, vln2_hi);
235 vt = vfmaq_f32(vt, vn, vln2_lo);
236
237 // Compute degree-5 polynomial approxiatmion for exp(-t) on [-log(2)/2, log(2)/2].
238 float32x4_t vp = vfmaq_f32(vc4, vc5, vt);
239 vp = vfmaq_f32(vc3, vp, vt);
240 vp = vfmaq_f32(vc2, vp, vt);
241 vp = vfmaq_f32(vc1, vp, vt);
242
243 // Reconstruct the exp(-z) value:
244 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
245 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
246 // = s + (t * s) * p
247 vt = vmulq_f32(vt, vs);
248 float32x4_t ve = vfmaq_f32(vs, vp, vt);
249
250 // Denominator of the sigmoid fraction: 1.0 + exp(-z)
251 float32x4_t vd = vaddq_f32(ve, vone);
252
253 // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator.
254 // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
255 // Thus the reciprocal of the denominator never overflows.
256 float32x4_t vr = vrecpeq_f32(vd);
257
258 vr = vmulq_f32(vr, vrecpsq_f32(vr, vd));
259
260 vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd));
261
262 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
263 float32x4_t vf = vmulq_f32(ve, vr);
264
265 // For inputs below denormal cutoff, replace output with +0.0f.
266 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
267 vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff)));
268
269 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
270 const uint32x4_t vm = vcltq_f32(vx, vmovq_n_s32(0.0f));
271 vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf));
272
273 vst1q_f32(y, vf); y += 4;
274 }
275 if XNN_UNLIKELY(n != 0) {
276 const float32x4_t vx = vld1q_f32(x);
277
278 // General structure of the algorithm:
279 // / exp(x) / (1 + exp(x)) if x <= 0
280 // f[x] :=
281 // \ 1 - f[-x] if x >= 0
282 //
283 // First we compute f[z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
284 // then replace result with 1 - f[z] if x <= 0.
285 const float32x4_t vz = vabsq_f32(vx);
286
287 // Compute reduced argument n := round(-z / log(2)).
288 // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
289 // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
290 // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
291 // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
292 // anyway. We fixup the result for such inputs at the very end of the algorithm.
293 float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e);
294
295 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
296 // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
297 const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23));
298
299 // Subtract the large number back to get final n := round(-z / log(2)).
300 vn = vsubq_f32(vn, vmagic_bias);
301
302 // Compute reduced argument -t := -z - n * log(2) = -(z + n * log(2)).
303 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
304 float32x4_t vt = vfmaq_f32(vz, vn, vln2_hi);
305 vt = vfmaq_f32(vt, vn, vln2_lo);
306
307 // Compute degree-5 polynomial approxiatmion for exp(-t) on [-log(2)/2, log(2)/2].
308 float32x4_t vp = vfmaq_f32(vc4, vc5, vt);
309 vp = vfmaq_f32(vc3, vp, vt);
310 vp = vfmaq_f32(vc2, vp, vt);
311 vp = vfmaq_f32(vc1, vp, vt);
312
313 // Reconstruct the exp(-z) value:
314 // e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
315 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
316 // = s + (t * s) * p
317 vt = vmulq_f32(vt, vs);
318 float32x4_t ve = vfmaq_f32(vs, vp, vt);
319
320 // Denominator of the sigmoid fraction: 1.0 + exp(-z)
321 float32x4_t vd = vaddq_f32(ve, vone);
322
323 // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator.
324 // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
325 // Thus the reciprocal of the denominator never overflows.
326 float32x4_t vr = vrecpeq_f32(vd);
327
328 vr = vmulq_f32(vr, vrecpsq_f32(vr, vd));
329
330 vr = vfmaq_f32(vr, vr, vfmsq_f32(vone, vr, vd));
331
332 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
333 float32x4_t vf = vmulq_f32(ve, vr);
334
335 // For inputs below denormal cutoff, replace output with +0.0f.
336 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
337 vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff)));
338
339 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
340 const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f));
341 vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf));
342
343 float32x2_t vf_lo = vget_low_f32(vf);
344 if (n & (2 * sizeof(float))) {
345 vst1_f32(y, vf_lo); y += 2;
346 vf_lo = vget_high_f32(vf);
347 }
348 if (n & (1 * sizeof(float))) {
349 vst1_lane_f32(y, vf_lo, 0);
350 }
351 }
352}