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