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