xref: /aosp_15_r20/external/XNNPACK/src/f32-raddstoreexpminusmax/gen/scalar-rr2-lut64-p2-x4-acc2.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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_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)])20 void xnn_f32_raddstoreexpminusmax_ukernel__scalar_rr2_lut64_p2_x4_acc2(
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   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 = (float_as_uint32(vn0) & UINT32_C(0xFFFFFFC0)) << 17;
78     const uint32_t ve1 = (float_as_uint32(vn1) & UINT32_C(0xFFFFFFC0)) << 17;
79     const uint32_t ve2 = (float_as_uint32(vn2) & UINT32_C(0xFFFFFFC0)) << 17;
80     const uint32_t ve3 = (float_as_uint32(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 = float_as_uint32(vn0) & vindex_mask;
84     const uint32_t vidx1 = float_as_uint32(vn1) & vindex_mask;
85     const uint32_t vidx2 = float_as_uint32(vn2) & vindex_mask;
86     const uint32_t vidx3 = float_as_uint32(vn3) & vindex_mask;
87     // Adjust exponent of the value l fetched from the table to get the final s value.
88     const float vs0 = uint32_as_float(xnn_table_exp2_k_over_64[vidx0] + ve0);
89     const float vs1 = uint32_as_float(xnn_table_exp2_k_over_64[vidx1] + ve1);
90     const float vs2 = uint32_as_float(xnn_table_exp2_k_over_64[vidx2] + ve2);
91     const float vs3 = uint32_as_float(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     vacc1 += vf1;
157     vacc0 += vf2;
158     vacc1 += vf3;
159   }
160   // Add up all accumulators to vacc0
161   vacc0 += vacc1;
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 + 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 = (float_as_uint32(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 = float_as_uint32(vn) & vindex_mask;
194     // Adjust exponent of the value l fetched from the table to get the final s value.
195     const float vs = uint32_as_float(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_hi + vx;
203     vt = vn * vminus_ln2_lo + vt;
204 
205     // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
206     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 }
230