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