• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-sigmoid/psimd-p5-div.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 <psimd.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/vunary.h>
16 
17 
xnn_f32_sigmoid_ukernel__psimd_p5_div_x8(size_t n,const float * x,float * y,const void * params)18 void xnn_f32_sigmoid_ukernel__psimd_p5_div_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 psimd_f32 vmagic_bias = psimd_splat_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 psimd_f32 vdenorm_cutoff = psimd_splat_f32(0x1.5D589Ep+6f);
30   const psimd_f32 vminus_log2e = psimd_splat_f32(-0x1.715476p+0f);
31   // Last 7 bits are zeroes
32   const psimd_f32 vln2_hi = psimd_splat_f32(0x1.62E400p-1f);
33   const psimd_f32 vln2_lo = psimd_splat_f32(0x1.7F7D1Cp-20f);
34   const psimd_f32 vone = psimd_splat_f32(1.0f);
35 
36   const psimd_f32 vc1 = psimd_splat_f32(-0x1.FFFFF6p-1f);
37   const psimd_f32 vc2 = psimd_splat_f32( 0x1.FFFDC6p-2f);
38   const psimd_f32 vc3 = psimd_splat_f32(-0x1.555A80p-3f);
39   const psimd_f32 vc4 = psimd_splat_f32( 0x1.573A1Ap-5f);
40   const psimd_f32 vc5 = psimd_splat_f32(-0x1.0F9F9Cp-7f);
41 
42   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
43     const psimd_f32 vx0123 = psimd_load_f32(x);
44     const psimd_f32 vx4567 = psimd_load_f32(x + 4);
45     x += 8;
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 psimd_f32 vz0123 = psimd_abs_f32(vx0123);
55     const psimd_f32 vz4567 = psimd_abs_f32(vx4567);
56 
57     // Compute reduced argument n := round(-z / log(2)).
58     // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
59     // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
60     // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
61     // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
62     // anyway. We fixup the result for such inputs at the very end of the algorithm.
63     psimd_f32 vn0123 = psimd_qfma_f32(vmagic_bias, vz0123, vminus_log2e);
64     psimd_f32 vn4567 = psimd_qfma_f32(vmagic_bias, vz4567, 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 psimd_f32 vs0123 = (psimd_f32) ((psimd_u32) vn0123 << 23);
69     const psimd_f32 vs4567 = (psimd_f32) ((psimd_u32) vn4567 << 23);
70 
71     // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number.
72     vn0123 = psimd_sub_f32(vn0123, vmagic_bias);
73     vn4567 = psimd_sub_f32(vn4567, vmagic_bias);
74 
75     // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2).
76     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
77     psimd_f32 vt0123 = psimd_qfma_f32(vz0123, vn0123, vln2_hi);
78     psimd_f32 vt4567 = psimd_qfma_f32(vz4567, vn4567, vln2_hi);
79 
80     vt0123 = psimd_qfma_f32(vt0123, vn0123, vln2_lo);
81     vt4567 = psimd_qfma_f32(vt4567, vn4567, vln2_lo);
82 
83     // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]:
84     //   P5(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
85     psimd_f32 vp0123 = psimd_qfma_f32(vc4, vt0123, vc5);
86     psimd_f32 vp4567 = psimd_qfma_f32(vc4, vt4567, vc5);
87 
88     vp0123 = psimd_qfma_f32(vc3, vt0123, vp0123);
89     vp4567 = psimd_qfma_f32(vc3, vt4567, vp4567);
90 
91     vp0123 = psimd_qfma_f32(vc2, vt0123, vp0123);
92     vp4567 = psimd_qfma_f32(vc2, vt4567, vp4567);
93 
94     vp0123 = psimd_qfma_f32(vc1, vt0123, vp0123);
95     vp4567 = psimd_qfma_f32(vc1, vt4567, vp4567);
96 
97     // Reconstruct the exp(-z) value:
98     //   e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
99     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
100     //     = s + (t * s) * p
101     vt0123 = psimd_mul_f32(vt0123, vs0123);
102     vt4567 = psimd_mul_f32(vt4567, vs4567);
103 
104     const psimd_f32 ve0123 = psimd_qfma_f32(vs0123, vt0123, vp0123);
105     const psimd_f32 ve4567 = psimd_qfma_f32(vs4567, vt4567, vp4567);
106 
107     // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
108     psimd_f32 vf0123 = psimd_div_f32(ve0123, psimd_add_f32(ve0123, vone));
109     psimd_f32 vf4567 = psimd_div_f32(ve4567, psimd_add_f32(ve4567, vone));
110 
111     // For inputs above denormal cutoff, replace output with +0.0f.
112     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
113     vf0123 = psimd_andnotmask_f32(vz0123 > vdenorm_cutoff, vf0123);
114     vf4567 = psimd_andnotmask_f32(vz4567 > vdenorm_cutoff, vf4567);
115 
116     // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
117     vf0123 = psimd_signblend_f32(vx0123, vf0123, psimd_sub_f32(vone, vf0123));
118     vf4567 = psimd_signblend_f32(vx4567, vf4567, psimd_sub_f32(vone, vf4567));
119 
120     psimd_store_f32(y, vf0123);
121     psimd_store_f32(y + 4, vf4567);
122     y += 8;
123   }
124   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
125     const psimd_f32 vx = psimd_load_f32(x);
126     x += 4;
127 
128     // General structure of the algorithm:
129     //           / exp(x) / (1 + exp(x)) if x <= 0
130     //   f[x] :=
131     //           \ 1 - f[-x] if x >= 0
132     //
133     // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
134     // then replace result with 1 - f[-z] if x >= 0.
135     const psimd_f32 vz = psimd_abs_f32(vx);
136 
137     // Compute reduced argument n := round(-z / log(2)).
138     // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
139     // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
140     // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
141     // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
142     // anyway. We fixup the result for such inputs at the very end of the algorithm.
143     psimd_f32 vn = psimd_qfma_f32(vmagic_bias, vz, vminus_log2e);
144 
145     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
146     // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
147     const psimd_f32 vs = (psimd_f32) ((psimd_u32) vn << 23);
148 
149     // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number.
150     vn = psimd_sub_f32(vn, vmagic_bias);
151 
152     // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2).
153     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
154     psimd_f32 vt = psimd_qfma_f32(vz, vn, vln2_hi);
155     vt = psimd_qfma_f32(vt, vn, vln2_lo);
156 
157     // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]:
158     //   P5(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
159     psimd_f32 vp = psimd_qfma_f32(vc4, vt, vc5);
160     vp = psimd_qfma_f32(vc3, vt, vp);
161     vp = psimd_qfma_f32(vc2, vt, vp);
162     vp = psimd_qfma_f32(vc1, vt, vp);
163 
164     // Reconstruct the exp(-z) value:
165     //   e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
166     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
167     //     = s + (t * s) * p
168     vt = psimd_mul_f32(vt, vs);
169     const psimd_f32 ve = psimd_qfma_f32(vs, vt, vp);
170 
171     // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
172     psimd_f32 vf = psimd_div_f32(ve, psimd_add_f32(ve, vone));
173 
174     // For inputs above denormal cutoff, replace output with +0.0f.
175     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
176     vf = psimd_andnotmask_f32(vz > vdenorm_cutoff, vf);
177 
178     // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
179     vf = psimd_signblend_f32(vx, vf, psimd_sub_f32(vone, vf));
180 
181     psimd_store_f32(y, vf);
182     y += 4;
183   }
184   if XNN_UNLIKELY(n != 0) {
185     const psimd_f32 vx = psimd_load_f32(x);
186 
187     // General structure of the algorithm:
188     //           / exp(x) / (1 + exp(x)) if x <= 0
189     //   f[x] :=
190     //           \ 1 - f[-x] if x >= 0
191     //
192     // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
193     // then replace result with 1 - f[-z] if x >= 0.
194     const psimd_f32 vz = psimd_abs_f32(vx);
195 
196     // Compute reduced argument n := round(-z / log(2)).
197     // We do it by adding a large number (magic bias), which cause rounding of result to an integer, then subtracing the
198     // large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
199     // The trick with adding large number is valid only within certain bounds (|x| <= 2**22), but thats ok, because
200     // inputs x outside of [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x)
201     // anyway. We fixup the result for such inputs at the very end of the algorithm.
202     psimd_f32 vn = psimd_qfma_f32(vmagic_bias, vz, vminus_log2e);
203 
204     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
205     // -87.336544 <= -z <= 0.0, and -126 <= n <= 0 accordingly.
206     const psimd_f32 vs = (psimd_f32) ((psimd_u32) vn << 23);
207 
208     // Subtract the large number back to get the final n := round(-z / log(2)) as a floating-point number.
209     vn = psimd_sub_f32(vn, vmagic_bias);
210 
211     // Compute reduced argument t := z + n * log(2). Note that -t = -z - n * log(2).
212     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
213     psimd_f32 vt = psimd_qfma_f32(vz, vn, vln2_hi);
214     vt = psimd_qfma_f32(vt, vn, vln2_lo);
215 
216     // Compute degree-5 polynomial approximation for exp(-t) on [-log(2)/2, log(2)/2]:
217     //   P5(t) = 1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
218     psimd_f32 vp = psimd_qfma_f32(vc4, vt, vc5);
219     vp = psimd_qfma_f32(vc3, vt, vp);
220     vp = psimd_qfma_f32(vc2, vt, vp);
221     vp = psimd_qfma_f32(vc1, vt, vp);
222 
223     // Reconstruct the exp(-z) value:
224     //   e = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
225     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
226     //     = s + (t * s) * p
227     vt = psimd_mul_f32(vt, vs);
228     const psimd_f32 ve = psimd_qfma_f32(vs, vt, vp);
229 
230     // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
231     psimd_f32 vf = psimd_div_f32(ve, psimd_add_f32(ve, vone));
232 
233     // For inputs above denormal cutoff, replace output with +0.0f.
234     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
235     vf = psimd_andnotmask_f32(vz > vdenorm_cutoff, vf);
236 
237     // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
238     vf = psimd_signblend_f32(vx, vf, psimd_sub_f32(vone, vf));
239 
240     if (n & (2 * sizeof(float))) {
241       psimd_store2_f32(y, vf);
242       vf = psimd_concat_hi_f32(vf, vf);
243       y += 2;
244     }
245     if (n & (1 * sizeof(float))) {
246       psimd_store1_f32(y, vf);
247     }
248   }
249 }
250