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