• 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_x4(size_t n,const float * x,float * y,const void * params)18 void xnn_f32_sigmoid_ukernel__neonfma_rr1_p5_nr2recps_x4(
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 >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
41     const float32x4_t vx = vld1q_f32(x); x += 4;
42 
43     // General structure of the algorithm:
44     //           / exp(x) / (1 + exp(x)) if x <= 0
45     //   f[x] :=
46     //           \ 1 - f[-x] if x >= 0
47     //
48     // First we compute f[z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
49     // then replace result with 1 - f[z] if x <= 0.
50     const float32x4_t vz = vabsq_f32(vx);
51 
52     // Compute reduced argument n := round(-z / log(2)).
53     // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
54     // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
55     // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
56     // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
57     // anyway. We fixup the result for such inputs at the very end of the algorithm.
58     float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e);
59 
60     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
61     // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
62     const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23));
63 
64     // Subtract the large number back to get final n := round(-z / log(2)).
65     vn = vsubq_f32(vn, vmagic_bias);
66 
67     // Compute reduced argument -t := -z - n * log(2) = -(z + n * log(2)).
68     float32x4_t vt = vfmaq_f32(vz, vn, vln2);
69 
70     // Compute degree-5 polynomial approxiatmion for exp(-t) on [-log(2)/2, log(2)/2].
71     float32x4_t vp = vfmaq_f32(vc4, vc5, vt);
72     vp = vfmaq_f32(vc3, vp, vt);
73     vp = vfmaq_f32(vc2, vp, vt);
74     vp = vfmaq_f32(vc1, vp, vt);
75 
76     // Reconstruct the exp(-z) value:
77     //   e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
78     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
79     //     = s + (t * s) * p
80     vt = vmulq_f32(vt, vs);
81     float32x4_t ve = vfmaq_f32(vs, vp, vt);
82 
83     // Denominator of the sigmoid fraction: 1.0 + exp(-z)
84     float32x4_t vd = vaddq_f32(ve, vone);
85 
86     // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator.
87     // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
88     // Thus the reciprocal of the denominator never overflows.
89     float32x4_t vr = vrecpeq_f32(vd);
90 
91     vr = vmulq_f32(vr, vrecpsq_f32(vr, vd));
92 
93     vr = vmulq_f32(vr, vrecpsq_f32(vr, vd));
94 
95     // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
96     float32x4_t vf = vmulq_f32(ve, vr);
97 
98     // For inputs below denormal cutoff, replace output with +0.0f.
99     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
100     vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff)));
101 
102     // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
103     const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f));
104     vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf));
105 
106     vst1q_f32(y, vf); y += 4;
107   }
108   if XNN_UNLIKELY(n != 0) {
109     const float32x4_t vx = vld1q_f32(x);
110 
111     // General structure of the algorithm:
112     //           / exp(x) / (1 + exp(x)) if x <= 0
113     //   f[x] :=
114     //           \ 1 - f[-x] if x >= 0
115     //
116     // First we compute f[z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
117     // then replace result with 1 - f[z] if x <= 0.
118     const float32x4_t vz = vabsq_f32(vx);
119 
120     // Compute reduced argument n := round(-z / log(2)).
121     // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
122     // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
123     // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
124     // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
125     // anyway. We fixup the result for such inputs at the very end of the algorithm.
126     float32x4_t vn = vfmaq_f32(vmagic_bias, vz, vminus_log2e);
127 
128     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
129     // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
130     const float32x4_t vs = vreinterpretq_f32_s32(vshlq_n_s32(vreinterpretq_s32_f32(vn), 23));
131 
132     // Subtract the large number back to get final n := round(-z / log(2)).
133     vn = vsubq_f32(vn, vmagic_bias);
134 
135     // Compute reduced argument -t := -z - n * log(2) = -(z + n * log(2)).
136     float32x4_t vt = vfmaq_f32(vz, vn, vln2);
137 
138     // Compute degree-5 polynomial approxiatmion for exp(-t) on [-log(2)/2, log(2)/2].
139     float32x4_t vp = vfmaq_f32(vc4, vc5, vt);
140     vp = vfmaq_f32(vc3, vp, vt);
141     vp = vfmaq_f32(vc2, vp, vt);
142     vp = vfmaq_f32(vc1, vp, vt);
143 
144     // Reconstruct the exp(-z) value:
145     //   e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
146     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
147     //     = s + (t * s) * p
148     vt = vmulq_f32(vt, vs);
149     float32x4_t ve = vfmaq_f32(vs, vp, vt);
150 
151     // Denominator of the sigmoid fraction: 1.0 + exp(-z)
152     float32x4_t vd = vaddq_f32(ve, vone);
153 
154     // Use Newton-Raphson method (2 iterations) to compute reciprocal of denominator.
155     // Note: 1 < d <= 2, because z >= 0.0 and 0 < exp(-z) <= 1.0.
156     // Thus the reciprocal of the denominator never overflows.
157     float32x4_t vr = vrecpeq_f32(vd);
158 
159     vr = vmulq_f32(vr, vrecpsq_f32(vr, vd));
160 
161     vr = vmulq_f32(vr, vrecpsq_f32(vr, vd));
162 
163     // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
164     float32x4_t vf = vmulq_f32(ve, vr);
165 
166     // For inputs below denormal cutoff, replace output with +0.0f.
167     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
168     vf = vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(vf), vcagtq_f32(vx, vdenorm_cutoff)));
169 
170     // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
171     const uint32x4_t vm = vcltq_f32(vx, vmovq_n_f32(0.0f));
172     vf = vbslq_f32(vm, vf, vsubq_f32(vone, vf));
173 
174     float32x2_t vf_lo = vget_low_f32(vf);
175     if (n & (2 * sizeof(float))) {
176       vst1_f32(y, vf_lo); y += 2;
177       vf_lo = vget_high_f32(vf);
178     }
179     if (n & (1 * sizeof(float))) {
180       vst1_lane_f32(y, vf_lo, 0);
181     }
182   }
183 }
184