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