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