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