1 // Auto-generated file. Do not edit!
2 // Template: src/f32-sigmoid/scalar-lut2048-p1-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_2048[2048];
21
xnn_f32_sigmoid_ukernel__scalar_lut2048_p1_div_x4(size_t n,const float * x,float * y,const void * params)22 void xnn_f32_sigmoid_ukernel__scalar_lut2048_p1_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_x2048 = -0x1.715476p11f;
35 // Last 18 bits are zeroes
36 const float vln2_o2048_hi = 0x1.600000p-12f;
37 const float vln2_o2048_lo = 0x1.7217F8p-19f;
38 const float vone = 1.0f;
39
40 const float vc1 = -0x1.FFFFFEp-1f;
41
42 const uint32_t vindex_mask = UINT32_C(0x7FF);
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 * 2048 / 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+10 = 1419.5654296875), 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_x2048 + vmagic_bias;
71 float vn1 = vz1 * vminus_log2e_x2048 + vmagic_bias;
72 float vn2 = vz2 * vminus_log2e_x2048 + vmagic_bias;
73 float vn3 = vz3 * vminus_log2e_x2048 + vmagic_bias;
74
75 // Create a floating-point number s (scale) such that s := 2**(n / 2048) for such inputs that sigmoidf(-z) is
76 // normalized, i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**(n / 2048) =
77 // = 2**e * 2**(n / 2048 - e), where e := int(n / 2048). We create s in two steps:
78 // 1. Fetch 2**(n / 2048 - e) = 2**(n % 2048) from table using the 6 low bits of n, as integer.
79 // Note 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 11:19 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) << 12;
86 const uint32_t ve1 = (fp32_to_bits(vn1) & ~vindex_mask) << 12;
87 const uint32_t ve2 = (fp32_to_bits(vn2) & ~vindex_mask) << 12;
88 const uint32_t ve3 = (fp32_to_bits(vn3) & ~vindex_mask) << 12;
89
90 // Use bits 0:11 bits of n, as integer, as an index for table lookup of l := 2**(n % 2048).
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_2048[vidx0] + ve0);
97 const float vs1 = fp32_from_bits(xnn_table_exp2_k_over_2048[vidx1] + ve1);
98 const float vs2 = fp32_from_bits(xnn_table_exp2_k_over_2048[vidx2] + ve2);
99 const float vs3 = fp32_from_bits(xnn_table_exp2_k_over_2048[vidx3] + ve3);
100
101 // Subtract the large number back to get the final n := round(-z * 2048 / 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) / 2048). Note that -t = -z - n * log(2) / 2048.
108 // Use Cody-Waite range reduction method (note two constants to represent log(2) / 2048) to improve accuracy.
109 float vt0 = vn0 * vln2_o2048_hi + vz0;
110 float vt1 = vn1 * vln2_o2048_hi + vz1;
111 float vt2 = vn2 * vln2_o2048_hi + vz2;
112 float vt3 = vn3 * vln2_o2048_hi + vz3;
113
114 vt0 = vn0 * vln2_o2048_lo + vt0;
115 vt1 = vn1 * vln2_o2048_lo + vt1;
116 vt2 = vn2 * vln2_o2048_lo + vt2;
117 vt3 = vn3 * vln2_o2048_lo + vt3;
118
119 // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/4096, log(2)/4096]:
120 // P1(t) = 1 + t * c1
121 const float vp0 = vt0 * vc1;
122 const float vp1 = vt1 * vc1;
123 const float vp2 = vt2 * vc1;
124 const float vp3 = vt3 * vc1;
125
126 // Reconstruct the exp(-z) value:
127 // y = s * (1 + t * c1)
128 // = s + s * (t * c1))
129 // = s + s * p
130 const float vy0 = vp0 * vs0 + vs0;
131 const float vy1 = vp1 * vs1 + vs1;
132 const float vy2 = vp2 * vs2 + vs2;
133 const float vy3 = vp3 * vs3 + vs3;
134
135 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
136 float vf0 = vy0 / (vy0 + vone);
137 float vf1 = vy1 / (vy1 + vone);
138 float vf2 = vy2 / (vy2 + vone);
139 float vf3 = vy3 / (vy3 + vone);
140
141 // For inputs above denormal cutoff, replace output with +0.0f.
142 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
143 if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
144 vf0 = 0.0f;
145 }
146 if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
147 vf1 = 0.0f;
148 }
149 if XNN_UNPREDICTABLE(vz2 > vdenorm_cutoff) {
150 vf2 = 0.0f;
151 }
152 if XNN_UNPREDICTABLE(vz3 > vdenorm_cutoff) {
153 vf3 = 0.0f;
154 }
155
156 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
157 if XNN_UNPREDICTABLE(vx0 > 0.0f) {
158 vf0 = vone - vf0;
159 }
160 if XNN_UNPREDICTABLE(vx1 > 0.0f) {
161 vf1 = vone - vf1;
162 }
163 if XNN_UNPREDICTABLE(vx2 > 0.0f) {
164 vf2 = vone - vf2;
165 }
166 if XNN_UNPREDICTABLE(vx3 > 0.0f) {
167 vf3 = vone - vf3;
168 }
169
170 y[0] = vf0;
171 y[1] = vf1;
172 y[2] = vf2;
173 y[3] = vf3;
174 y += 4;
175 }
176 if XNN_UNLIKELY(n != 0) {
177 do {
178 const float vx = *x++;
179
180 // General structure of the algorithm:
181 // / exp(x) / (1 + exp(x)) if x <= 0
182 // f[x] :=
183 // \ 1 - f[-x] if x >= 0
184 //
185 // First we compute f[-z] := exp(-z) / (1 + exp(-z)) where z = abs(x),
186 // then replace result with 1 - f[-z] if x >= 0.
187 const float vz = fabsf(vx);
188
189 // Compute reduced argument n := round(-z * 2048 / log(2)).
190 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
191 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
192 // The trick with adding large number is valid only within certain bounds (|z * 2048 / log(2)| <= 2**22, i.e.
193 // |z| <= 0x1.62E43p+10 = 1419.5654296875), but that is acceptable, because inputs x outside of
194 // [-87.336544, 17.328678] (i.e. z outsize [0, 87.336544]) underflow or saturate sigmoidf(x). We fixup the result
195 // for such inputs at the very end of the algorithm.
196 float vn = vz * vminus_log2e_x2048 + vmagic_bias;
197
198 // Create a floating-point number s (scale) such that s := 2**(n / 2048) for such inputs that sigmoidf(-z) is
199 // normalized, i.e. 0 <= z <= 87.33642. As n has 11 fractional bits, we split s == 2**(n / 2048) =
200 // = 2**e * 2**(n / 2048 - e), where e := int(n / 2048). We create s in two steps:
201 // 1. Fetch 2**(n / 2048 - e) = 2**(n % 2048) from table using the 6 low bits of n, as integer.
202 // Note that the fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
203 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
204 // number, because for 0 <= z <= 87.33642 (inputs for which sigmoidf(-z) is normalized) we have -126 <= e <= 0,
205 // and thus the adjusted exponent is not lower than -126.
206 //
207 // Extract e from bits 11:19 of n and shift it into bits 23:31 (position of floating-point exponent).
208 const uint32_t ve = (fp32_to_bits(vn) & ~vindex_mask) << 12;
209
210 // Use bits 0:11 bits of n, as integer, as an index for table lookup of l := 2**(n % 2048).
211 const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
212 // Adjust exponent of the value l fetched from the table to get the final s value.
213 const float vs = fp32_from_bits(xnn_table_exp2_k_over_2048[vidx] + ve);
214
215 // Subtract the large number back to get the final n := round(-z * 2048 / log(2)) as a floating-point number.
216 vn -= vmagic_bias;
217
218 // Compute reduced argument t := (z + n * log(2) / 2048). Note that -t = -z - n * log(2) / 2048.
219 // Use Cody-Waite range reduction method (note two constants to represent log(2) / 2048) to improve accuracy.
220 float vt = vn * vln2_o2048_hi + vz;
221 vt = vn * vln2_o2048_lo + vt;
222
223 // Compute degree-1 polynomial approximation for exp(-t) on [-log(2)/4096, log(2)/4096]:
224 // P1(t) = 1 + t * c1
225 const float vp = vt * vc1;
226
227 // Reconstruct the exp(-z) value:
228 // y = s * (1 + t * c1)
229 // = s + s * (t * c1))
230 // = s + s * p
231 const float vy = vp * vs + vs;
232
233 // Reconstruct sigmoid(-z) = exp(-z) / (1.0 + exp(-z))
234 float vf = vy / (vy + vone);
235
236 // For inputs above denormal cutoff, replace output with +0.0f.
237 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
238 if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
239 vf = 0.0f;
240 }
241
242 // Reconstruct sigmoid(x) = x < 0 ? sigmoid(-z) : 1.0 - sigmoid(-z)
243 if XNN_UNPREDICTABLE(vx > 0.0f) {
244 vf = vone - vf;
245 }
246
247 *y++ = vf;
248
249 n -= sizeof(float);
250 } while (n != 0);
251 }
252 }
253