1 // Auto-generated file. Do not edit!
2 // Template: src/f32-raddstoreexpminusmax/scalar-rr2-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/math.h>
14 #include <xnnpack/raddstoreexpminusmax.h>
15
16
17 // Note redefine as uint32[] to avoid redundant bitcasts.
18 extern XNN_INTERNAL const uint32_t xnn_table_exp2_k_over_64[64];
19
xnn_f32_raddstoreexpminusmax_ukernel__scalar_rr2_lut64_p2_x4_acc4(size_t elements,const float * input,const float * max,float * output,float * sum,const union xnn_f32_expminus_params params[restrict XNN_MIN_ELEMENTS (1)])20 void xnn_f32_raddstoreexpminusmax_ukernel__scalar_rr2_lut64_p2_x4_acc4(
21 size_t elements,
22 const float* input,
23 const float* max,
24 float* output,
25 float* sum,
26 const union xnn_f32_expminus_params params[restrict XNN_MIN_ELEMENTS(1)])
27 {
28 assert(elements % sizeof(float) == 0);
29
30 const float vi_max = *max;
31 const float vlog2e = params->scalar_rr2_lut64_p2.log2e;
32 const float vmagic_bias = params->scalar_rr2_lut64_p2.magic_bias;
33 const uint32_t vindex_mask = UINT32_C(0x3F);
34 const float vminus_ln2_hi = params->scalar_rr2_lut64_p2.minus_ln2_hi;
35 const float vminus_ln2_lo = params->scalar_rr2_lut64_p2.minus_ln2_lo;
36 const float vc2 = params->scalar_rr2_lut64_p2.c2;
37 const float vdenorm_cutoff = params->scalar_rr2_lut64_p2.denorm_cutoff;
38
39 float vacc0 = 0.0f;
40 float vacc1 = 0.0f;
41 float vacc2 = 0.0f;
42 float vacc3 = 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 + vmagic_bias;
65 float vn1 = vx1 * vlog2e + vmagic_bias;
66 float vn2 = vx2 * vlog2e + vmagic_bias;
67 float vn3 = vx3 * vlog2e + 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 = (float_as_uint32(vn0) & UINT32_C(0xFFFFFFC0)) << 17;
80 const uint32_t ve1 = (float_as_uint32(vn1) & UINT32_C(0xFFFFFFC0)) << 17;
81 const uint32_t ve2 = (float_as_uint32(vn2) & UINT32_C(0xFFFFFFC0)) << 17;
82 const uint32_t ve3 = (float_as_uint32(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 = float_as_uint32(vn0) & vindex_mask;
86 const uint32_t vidx1 = float_as_uint32(vn1) & vindex_mask;
87 const uint32_t vidx2 = float_as_uint32(vn2) & vindex_mask;
88 const uint32_t vidx3 = float_as_uint32(vn3) & vindex_mask;
89 // Adjust exponent of the value l fetched from the table to get the final s value.
90 const float vs0 = uint32_as_float(xnn_table_exp2_k_over_64[vidx0] + ve0);
91 const float vs1 = uint32_as_float(xnn_table_exp2_k_over_64[vidx1] + ve1);
92 const float vs2 = uint32_as_float(xnn_table_exp2_k_over_64[vidx2] + ve2);
93 const float vs3 = uint32_as_float(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_hi + vx0;
104 float vt1 = vn1 * vminus_ln2_hi + vx1;
105 float vt2 = vn2 * vminus_ln2_hi + vx2;
106 float vt3 = vn3 * vminus_ln2_hi + vx3;
107
108 vt0 = vn0 * vminus_ln2_lo + vt0;
109 vt1 = vn1 * vminus_ln2_lo + vt1;
110 vt2 = vn2 * vminus_ln2_lo + vt2;
111 vt3 = vn3 * vminus_ln2_lo + vt3;
112
113 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
114 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 vacc1 += vf1;
159 vacc2 += vf2;
160 vacc3 += vf3;
161 }
162 // Add up all accumulators to vacc0
163 vacc0 += vacc1;
164 vacc2 += vacc3;
165 vacc0 += vacc2;
166
167 float vacc = vacc0;
168 for (; elements >= sizeof(float); elements -= sizeof(float)) {
169 // Load 1 input at a time.
170 const float vi = *input++;
171
172 // Subtract maximum input x := i - i_max. This implies x <= 0.
173 const float vx = vi - vi_max;
174
175 // Compute reduced argument n := round(x * 64 / log(2)).
176 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
177 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
178 // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
179 // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
180 // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
181 // algorithm.
182 float vn = vx * vlog2e + vmagic_bias;
183
184 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
185 // 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
186 // e := int(n / 64). We create s in two steps:
187 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
188 // fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
189 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
190 // number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
191 // and thus the adjusted exponent is not lower than -126.
192 //
193 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
194 const uint32_t ve = (float_as_uint32(vn) & UINT32_C(0xFFFFFFC0)) << 17;
195
196 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
197 const uint32_t vidx = float_as_uint32(vn) & vindex_mask;
198 // Adjust exponent of the value l fetched from the table to get the final s value.
199 const float vs = uint32_as_float(xnn_table_exp2_k_over_64[vidx] + ve);
200
201 // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
202 vn -= vmagic_bias;
203
204 // Compute reduced argument t := x - n * log(2) / 64.
205 // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
206 float vt = vn * vminus_ln2_hi + vx;
207 vt = vn * vminus_ln2_lo + vt;
208
209 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
210 float vp = vt * vc2;
211 vp = vp * vt + vt;
212
213 // Reconstruct the final f value:
214 // f = s * (1 + t * (1 + t * c2))
215 // = s * (1 + t + t * (t * c2))
216 // = s + s * (t + t * (t * c2))
217 // = s + s * p
218 float vf = vp * vs + vs;
219
220 // For inputs below denormal cutoff, replace output with +0.0f.
221 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
222 if XNN_UNPREDICTABLE(vx < vdenorm_cutoff) {
223 vf = 0.0f;
224 }
225
226 // Store 1 output at a time.
227 *output++ = vf;
228
229 // Accumulate computed exponents.
230 vacc += vf;
231 }
232 *sum = vacc;
233 }
234