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