blob: 165c45e4fa3b6526ac683eec4712183f5d8d496b [file] [log] [blame]
Marat Dukhanf46f6752020-01-21 11:03:49 -08001// Auto-generated file. Do not edit!
2// Template: src/f32-raddstoreexpminusmax/scalar-lut64-p2.c.in
3// Generator: tools/xngen
4//
5// Copyright 2020 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 <xnnpack/common.h>
13#include <xnnpack/raddstoreexpminusmax.h>
14
15#include <fp16/bitcasts.h>
16
17
18// Note redefine as uint32[] to avoid redundant bitcasts.
19extern XNN_INTERNAL const uint32_t xnn_table_exp2_k_over_64[64];
20
21void xnn_f32_raddstoreexpminusmax_ukernel__scalar_lut64_p2_x4_acc2(
22 size_t elements,
23 const float* input,
24 float* output,
25 float* sum,
26 float vi_max)
27{
28 assert(elements % sizeof(float) == 0);
29
30 const float vmagic_bias = 0x1.800000p23f;
31 // The smallest x for which expf(x) is normalized.
32 const float vdenorm_cutoff = -0x1.5D589Ep6f;
33 const float vlog2e_x64 = 0x1.715476p6f;
34 // Last 13 bits are zeroes
35 const float vminus_ln2_o64_hi = -0x1.630000p-7f;
36 const float vminus_ln2_o64_lo = 0x1.BD0106p-19f;
37
38 const float vc2 = 0x1.FFFF0Ap-2f;
39
40 const uint32_t vindex_mask = UINT32_C(0x3F);
41
42 float vacc0 = 0.0f;
43 float vacc1 = 0.0f;
44 for (; elements >= 4 * sizeof(float); elements -= 4 * sizeof(float)) {
45 // Load 4 inputs at a time.
46 const float vi0 = input[0];
47 const float vi1 = input[1];
48 const float vi2 = input[2];
49 const float vi3 = input[3];
50 input += 4;
51
52 // Subtract maximum input x := i - i_max. This implies x <= 0.
53 const float vx0 = vi0 - vi_max;
54 const float vx1 = vi1 - vi_max;
55 const float vx2 = vi2 - vi_max;
56 const float vx3 = vi3 - vi_max;
57
58 // Compute reduced argument n := round(x * 64 / log(2)).
59 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
60 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
61 // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
62 // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
63 // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
64 // algorithm.
65 float vn0 = vx0 * vlog2e_x64 + vmagic_bias;
66 float vn1 = vx1 * vlog2e_x64 + vmagic_bias;
67 float vn2 = vx2 * vlog2e_x64 + vmagic_bias;
68 float vn3 = vx3 * vlog2e_x64 + vmagic_bias;
69
70 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
71 // i.e. -87.33642 <= x <= 0.0. As n has 6 fractional bits, we split s == 2**(n / 64) = 2**e * 2**(n / 64 - e), where
72 // e := int(n / 64). We create s in two steps:
73 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
74 // fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
75 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
76 // number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
77 // and thus the adjusted exponent is not lower than -126.
78 //
79 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
80 const uint32_t ve0 = (fp32_to_bits(vn0) & UINT32_C(0xFFFFFFC0)) << 17;
81 const uint32_t ve1 = (fp32_to_bits(vn1) & UINT32_C(0xFFFFFFC0)) << 17;
82 const uint32_t ve2 = (fp32_to_bits(vn2) & UINT32_C(0xFFFFFFC0)) << 17;
83 const uint32_t ve3 = (fp32_to_bits(vn3) & UINT32_C(0xFFFFFFC0)) << 17;
84
85 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
86 const uint32_t vidx0 = fp32_to_bits(vn0) & vindex_mask;
87 const uint32_t vidx1 = fp32_to_bits(vn1) & vindex_mask;
88 const uint32_t vidx2 = fp32_to_bits(vn2) & vindex_mask;
89 const uint32_t vidx3 = fp32_to_bits(vn3) & vindex_mask;
90 // Adjust exponent of the value l fetched from the table to get the final s value.
91 const float vs0 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx0] + ve0);
92 const float vs1 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx1] + ve1);
93 const float vs2 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx2] + ve2);
94 const float vs3 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx3] + ve3);
95
96 // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
97 vn0 -= vmagic_bias;
98 vn1 -= vmagic_bias;
99 vn2 -= vmagic_bias;
100 vn3 -= vmagic_bias;
101
102 // Compute reduced argument t := x - n * log(2) / 64.
103 // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
104 float vt0 = vn0 * vminus_ln2_o64_hi + vx0;
105 float vt1 = vn1 * vminus_ln2_o64_hi + vx1;
106 float vt2 = vn2 * vminus_ln2_o64_hi + vx2;
107 float vt3 = vn3 * vminus_ln2_o64_hi + vx3;
108
109 vt0 = vn0 * vminus_ln2_o64_lo + vt0;
110 vt1 = vn1 * vminus_ln2_o64_lo + vt1;
111 vt2 = vn2 * vminus_ln2_o64_lo + vt2;
112 vt3 = vn3 * vminus_ln2_o64_lo + vt3;
113
Marat Dukhan102a7392020-11-20 01:18:10 -0800114 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
Marat Dukhanf46f6752020-01-21 11:03:49 -0800115 float vp0 = vt0 * vc2;
116 float vp1 = vt1 * vc2;
117 float vp2 = vt2 * vc2;
118 float vp3 = vt3 * vc2;
119
120 vp0 = vp0 * vt0 + vt0;
121 vp1 = vp1 * vt1 + vt1;
122 vp2 = vp2 * vt2 + vt2;
123 vp3 = vp3 * vt3 + vt3;
124
125 // Reconstruct the final f value:
126 // f = s * (1 + t * (1 + t * c2))
127 // = s * (1 + t + t * (t * c2))
128 // = s + s * (t + t * (t * c2))
129 // = s + s * p
130 float vf0 = vp0 * vs0 + vs0;
131 float vf1 = vp1 * vs1 + vs1;
132 float vf2 = vp2 * vs2 + vs2;
133 float vf3 = vp3 * vs3 + vs3;
134
135 // For inputs below denormal cutoff, replace output with +0.0f.
136 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
137 if XNN_UNPREDICTABLE(vx0 < vdenorm_cutoff) {
138 vf0 = 0.0f;
139 }
140 if XNN_UNPREDICTABLE(vx1 < vdenorm_cutoff) {
141 vf1 = 0.0f;
142 }
143 if XNN_UNPREDICTABLE(vx2 < vdenorm_cutoff) {
144 vf2 = 0.0f;
145 }
146 if XNN_UNPREDICTABLE(vx3 < vdenorm_cutoff) {
147 vf3 = 0.0f;
148 }
149
150 // Store 4 outputs at a time.
151 output[0] = vf0;
152 output[1] = vf1;
153 output[2] = vf2;
154 output[3] = vf3;
155 output += 4;
156
157 // Accumulate computed exponents.
158 vacc0 += vf0;
159 vacc1 += vf1;
160 vacc0 += vf2;
161 vacc1 += vf3;
162 }
163 // Add up all accumulators to vacc0
164 vacc0 += vacc1;
165
166 float vacc = vacc0;
167 for (; elements >= sizeof(float); elements -= sizeof(float)) {
168 // Load 1 input at a time.
169 const float vi = *input++;
170
171 // Subtract maximum input x := i - i_max. This implies x <= 0.
172 const float vx = vi - vi_max;
173
174 // Compute reduced argument n := round(x * 64 / log(2)).
175 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
176 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
177 // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
178 // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
179 // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
180 // algorithm.
181 float vn = vx * vlog2e_x64 + vmagic_bias;
182
183 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
184 // i.e. -87.33642 <= x <= 0.0. As n has 6 fractional bits, we split s == 2**(n / 64) = 2**e * 2**(n / 64 - e), where
185 // e := int(n / 64). We create s in two steps:
186 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
187 // fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
188 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
189 // number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
190 // and thus the adjusted exponent is not lower than -126.
191 //
192 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
193 const uint32_t ve = (fp32_to_bits(vn) & UINT32_C(0xFFFFFFC0)) << 17;
194
195 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
196 const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
197 // Adjust exponent of the value l fetched from the table to get the final s value.
198 const float vs = fp32_from_bits(xnn_table_exp2_k_over_64[vidx] + ve);
199
200 // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
201 vn -= vmagic_bias;
202
203 // Compute reduced argument t := x - n * log(2) / 64.
204 // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
205 float vt = vn * vminus_ln2_o64_hi + vx;
206 vt = vn * vminus_ln2_o64_lo + vt;
207
Marat Dukhan102a7392020-11-20 01:18:10 -0800208 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
Marat Dukhanf46f6752020-01-21 11:03:49 -0800209 float vp = vt * vc2;
210 vp = vp * vt + vt;
211
212 // Reconstruct the final f value:
213 // f = s * (1 + t * (1 + t * c2))
214 // = s * (1 + t + t * (t * c2))
215 // = s + s * (t + t * (t * c2))
216 // = s + s * p
217 float vf = vp * vs + vs;
218
219 // For inputs below denormal cutoff, replace output with +0.0f.
220 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
221 if XNN_UNPREDICTABLE(vx < vdenorm_cutoff) {
222 vf = 0.0f;
223 }
224
225 // Store 1 output at a time.
226 *output++ = vf;
227
228 // Accumulate computed exponents.
229 vacc += vf;
230 }
231 *sum = vacc;
232}