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