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