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