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