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