1 // Auto-generated file. Do not edit!
2 // Template: src/f32-sigmoid/scalar-lut64-p2-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 #include <math.h>
12
13 #include <xnnpack/common.h>
14 #include <xnnpack/vunary.h>
15
16 #include <fp16/bitcasts.h>
17
18
19 // Note redefine as uint32[] to avoid redundant bitcasts.
20 extern XNN_INTERNAL const uint32_t xnn_table_exp2_k_over_64[64];
21
xnn_f32_sigmoid_ukernel__scalar_lut64_p2_div_x4(size_t n,const float * x,float * y,const void * params)22 void xnn_f32_sigmoid_ukernel__scalar_lut64_p2_div_x4(
23 size_t n,
24 const float* x,
25 float* y,
26 const void* params)
27 {
28 assert(n % sizeof(float) == 0);
29
30 const float vmagic_bias = 0x1.800000p23f;
31 // The largest z for which sigmoidf(-z) is normalized.
32 // This number is also the largest z for which expf(-z) is normalized.
33 const float vdenorm_cutoff = 0x1.5D589Ep+6f;
34 const float vminus_log2e_x64 = -0x1.715476p6f;
35 // Last 13 bits are zeroes
36 const float vln2_o64_hi = 0x1.630000p-7f;
37 const float vln2_o64_lo = -0x1.BD0106p-19f;
38 const float vone = 1.0f;
39
40 const float vc2 = 0x1.FFFF0Ap-2f;
41
42 const uint32_t vindex_mask = UINT32_C(0x3F);
43
44 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
45 const float vx0 = x[0];
46 const float vx1 = x[1];
47 const float vx2 = x[2];
48 const float vx3 = x[3];
49 x += 4;
50
51 // General structure of the algorithm:
52 // / exp(x) / (1 + exp(x)) if x <= 0
53 // f[x] :=
54 // \ 1 - f[-x] if x >= 0
55 //
56 // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
57 // then replace result with 1 - f[-z] if x >= 0.
58 const float vz0 = fabsf(vx0);
59 const float vz1 = fabsf(vx1);
60 const float vz2 = fabsf(vx2);
61 const float vz3 = fabsf(vx3);
62
63 // Compute reduced argument n := round(-z * 64 / log(2)).
64 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
65 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
66 // The trick with adding large number is valid only within certain bounds (|z * 2048 / log(2)| <= 2**22, i.e.
67 // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of
68 // [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup the result
69 // for such inputs at the very end of the algorithm.
70 float vn0 = vz0 * vminus_log2e_x64 + vmagic_bias;
71 float vn1 = vz1 * vminus_log2e_x64 + vmagic_bias;
72 float vn2 = vz2 * vminus_log2e_x64 + vmagic_bias;
73 float vn3 = vz3 * vminus_log2e_x64 + vmagic_bias;
74
75 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that sigmoidf(-z) is
76 // normalized, i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**(n / 64) =
77 // = 2**e * 2**(n / 64 - e), where e := int(n / 64). We create s in two steps:
78 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note
79 // that the fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
80 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
81 // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(-z) is normalized) we have -126 <= e <= 0,
82 // and thus the adjusted exponent is not lower than -126.
83 //
84 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
85 const uint32_t ve0 = (fp32_to_bits(vn0) & ~vindex_mask) << 17;
86 const uint32_t ve1 = (fp32_to_bits(vn1) & ~vindex_mask) << 17;
87 const uint32_t ve2 = (fp32_to_bits(vn2) & ~vindex_mask) << 17;
88 const uint32_t ve3 = (fp32_to_bits(vn3) & ~vindex_mask) << 17;
89
90 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
91 const uint32_t vidx0 = fp32_to_bits(vn0) & vindex_mask;
92 const uint32_t vidx1 = fp32_to_bits(vn1) & vindex_mask;
93 const uint32_t vidx2 = fp32_to_bits(vn2) & vindex_mask;
94 const uint32_t vidx3 = fp32_to_bits(vn3) & vindex_mask;
95 // Adjust exponent of the value l fetched from the table to get the final s value.
96 const float vs0 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx0] + ve0);
97 const float vs1 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx1] + ve1);
98 const float vs2 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx2] + ve2);
99 const float vs3 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx3] + ve3);
100
101 // Subtract the large number back to get the final n := round(-z * 64 / log(2)) as a floating-point number.
102 vn0 -= vmagic_bias;
103 vn1 -= vmagic_bias;
104 vn2 -= vmagic_bias;
105 vn3 -= vmagic_bias;
106
107 // Compute reduced argument t := (z + n * log(2) / 64). Note that -t = -z - n * log(2) / 64.
108 // Use Cody-Waite range reduction method (note two constants to represent log(2) / 64) to improve accuracy.
109 float vt0 = vn0 * vln2_o64_hi + vz0;
110 float vt1 = vn1 * vln2_o64_hi + vz1;
111 float vt2 = vn2 * vln2_o64_hi + vz2;
112 float vt3 = vn3 * vln2_o64_hi + vz3;
113
114 vt0 = vn0 * vln2_o64_lo + vt0;
115 vt1 = vn1 * vln2_o64_lo + vt1;
116 vt2 = vn2 * vln2_o64_lo + vt2;
117 vt3 = vn3 * vln2_o64_lo + vt3;
118
119 // Compute degree-2 polynomial approxiatmion for exp(-t) on [-log(2)/128, log(2)/128].
120 // P1(t) = 1 + t * (-1 + t * c2)
121 float vp0 = vt0 * vc2;
122 float vp1 = vt1 * vc2;
123 float vp2 = vt2 * vc2;
124 float vp3 = vt3 * vc2;
125
126 vp0 = vt0 - vp0 * vt0;
127 vp1 = vt1 - vp1 * vt1;
128 vp2 = vt2 - vp2 * vt2;
129 vp3 = vt3 - vp3 * vt3;
130
131 // Reconstruct the final f value:
132 // f = s * (1 + t * (-1 + t * c2))
133 // = s * (1 - t + t * (t * c2))
134 // = s - s * (t - t * (t * c2))
135 // = s - s * p
136 const float vy0 = vs0 - vs0 * vp0;
137 const float vy1 = vs1 - vs1 * vp1;
138 const float vy2 = vs2 - vs2 * vp2;
139 const float vy3 = vs3 - vs3 * vp3;
140
141 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
142 float vf0 = vy0 / (vy0 + vone);
143 float vf1 = vy1 / (vy1 + vone);
144 float vf2 = vy2 / (vy2 + vone);
145 float vf3 = vy3 / (vy3 + vone);
146
147 // For inputs below denormal cutoff, replace output with +0.0f.
148 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
149 if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
150 vf0 = 0.0f;
151 }
152 if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
153 vf1 = 0.0f;
154 }
155 if XNN_UNPREDICTABLE(vz2 > vdenorm_cutoff) {
156 vf2 = 0.0f;
157 }
158 if XNN_UNPREDICTABLE(vz3 > vdenorm_cutoff) {
159 vf3 = 0.0f;
160 }
161
162 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
163 if XNN_UNPREDICTABLE(vx0 > 0.0f) {
164 vf0 = vone - vf0;
165 }
166 if XNN_UNPREDICTABLE(vx1 > 0.0f) {
167 vf1 = vone - vf1;
168 }
169 if XNN_UNPREDICTABLE(vx2 > 0.0f) {
170 vf2 = vone - vf2;
171 }
172 if XNN_UNPREDICTABLE(vx3 > 0.0f) {
173 vf3 = vone - vf3;
174 }
175
176 y[0] = vf0;
177 y[1] = vf1;
178 y[2] = vf2;
179 y[3] = vf3;
180 y += 4;
181 }
182 if XNN_UNLIKELY(n != 0) {
183 do {
184 const float vx = *x++;
185
186 // General structure of the algorithm:
187 // / exp(x) / (1 + exp(x)) if x <= 0
188 // f[x] :=
189 // \ 1 - f[-x] if x >= 0
190 //
191 // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
192 // then replace result with 1 - f[-z] if x >= 0.
193 const float vz = fabsf(vx);
194
195 // Compute reduced argument n := round(-z * 64 / log(2)).
196 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
197 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
198 // The trick with adding large number is valid only within certain bounds (|z * 2048 / log(2)| <= 2**22, i.e.
199 // |z| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs x outside of
200 // [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup the result
201 // for such inputs at the very end of the algorithm.
202 float vn = vz * vminus_log2e_x64 + vmagic_bias;
203
204 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that sigmoidf(-z) is
205 // normalized, i.e. 0 <= z <= 87.33642. As n has 6 fractional bits, we split s == 2**(n / 64) =
206 // = 2**e * 2**(n / 64 - e), where e := int(n / 64). We create s in two steps:
207 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note
208 // that the fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
209 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
210 // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(-z) is normalized) we have -126 <= e <= 0,
211 // and thus the adjusted exponent is not lower than -126.
212 //
213 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
214 const uint32_t ve = (fp32_to_bits(vn) & ~vindex_mask) << 17;
215
216 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
217 const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
218 // Adjust exponent of the value l fetched from the table to get the final s value.
219 const float vs = fp32_from_bits(xnn_table_exp2_k_over_64[vidx] + ve);
220
221 // Subtract the large number back to get the final n := round(-z * 64 / log(2)) as a floating-point number.
222 vn -= vmagic_bias;
223
224 // Compute reduced argument t := (z + n * log(2) / 64). Note that -t = -z - n * log(2) / 64.
225 // Use Cody-Waite range reduction method (note two constants to represent log(2) / 64) to improve accuracy.
226 float vt = vn * vln2_o64_hi + vz;
227 vt = vn * vln2_o64_lo + vt;
228
229 // Compute degree-2 polynomial approxiatmion for exp(-t) on [-log(2)/128, log(2)/128].
230 // P1(t) = 1 + t * (-1 + t * c2)
231 float vp = vt * vc2;
232 vp = vt - vp * vt;
233
234 // Reconstruct the final f value:
235 // f = s * (1 + t * (-1 + t * c2))
236 // = s * (1 - t + t * (t * c2))
237 // = s - s * (t - t * (t * c2))
238 // = s - s * p
239 const float vy = vs - vs * vp;
240
241 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
242 float vf = vy / (vy + vone);
243
244 // For inputs below denormal cutoff, replace output with +0.0f.
245 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
246 if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
247 vf = 0.0f;
248 }
249
250 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
251 if XNN_UNPREDICTABLE(vx > 0.0f) {
252 vf = vone - vf;
253 }
254
255 *y++ = vf;
256
257 n -= sizeof(float);
258 } while (n != 0);
259 }
260 }
261