xref: /aosp_15_r20/external/XNNPACK/src/f32-raddstoreexpminusmax/gen/scalar-rr2-p5-x4-acc2.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-raddstoreexpminusmax/scalar-rr2-p5.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 
xnn_f32_raddstoreexpminusmax_ukernel__scalar_rr2_p5_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)])17 void xnn_f32_raddstoreexpminusmax_ukernel__scalar_rr2_p5_x4_acc2(
18     size_t elements,
19     const float* input,
20     const float* max,
21     float* output,
22     float* sum,
23     const union xnn_f32_expminus_params params[restrict XNN_MIN_ELEMENTS(1)])
24 {
25   assert(elements % sizeof(float) == 0);
26 
27   const float vi_max = *max;
28   const float vlog2e = params->scalar_rr2_p5.log2e;
29   const float vmagic_bias = params->scalar_rr2_p5.magic_bias;
30   const float vminus_ln2_hi = params->scalar_rr2_p5.minus_ln2_hi;
31   const float vminus_ln2_lo = params->scalar_rr2_p5.minus_ln2_lo;
32   const float vc5 = params->scalar_rr2_p5.c5;
33   const float vc4 = params->scalar_rr2_p5.c4;
34   const float vc3 = params->scalar_rr2_p5.c3;
35   const float vc2 = params->scalar_rr2_p5.c2;
36   const float vc1 = params->scalar_rr2_p5.c1;
37   const float vdenorm_cutoff = params->scalar_rr2_p5.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 / log(2)).
56     // We do it by adding a large number (magic bias) to the product x * (1/log(2)), which cause rounding of the result
57     // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
58     // certain bounds (|x| <= 2**22), but that's ok, because inputs outside of [-87.336540, 0.0] underflow expf(x)
59     // anyway. We fixup the result for such inputs at the very end of the algorithm.
60     float vn0 = vx0 * vlog2e + vmagic_bias;
61     float vn1 = vx1 * vlog2e + vmagic_bias;
62     float vn2 = vx2 * vlog2e + vmagic_bias;
63     float vn3 = vx3 * vlog2e + vmagic_bias;
64 
65     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
66     // -87.33642 <= x <= 0.0, and -126 <= n <= 0 accordingly.
67     const float vs0 = uint32_as_float(float_as_uint32(vn0) << 23);
68     const float vs1 = uint32_as_float(float_as_uint32(vn1) << 23);
69     const float vs2 = uint32_as_float(float_as_uint32(vn2) << 23);
70     const float vs3 = uint32_as_float(float_as_uint32(vn3) << 23);
71 
72     // Subtract the large number back to get final n := round(x / log(2)).
73     vn0 -= vmagic_bias;
74     vn1 -= vmagic_bias;
75     vn2 -= vmagic_bias;
76     vn3 -= vmagic_bias;
77 
78     // Compute reduced argument t := x - n * log(2).
79     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
80     float vt0 = vn0 * vminus_ln2_hi + vx0;
81     float vt1 = vn1 * vminus_ln2_hi + vx1;
82     float vt2 = vn2 * vminus_ln2_hi + vx2;
83     float vt3 = vn3 * vminus_ln2_hi + vx3;
84 
85     vt0 = vn0 * vminus_ln2_lo + vt0;
86     vt1 = vn1 * vminus_ln2_lo + vt1;
87     vt2 = vn2 * vminus_ln2_lo + vt2;
88     vt3 = vn3 * vminus_ln2_lo + vt3;
89 
90     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
91     float vp0 = vc5 * vt0 + vc4;
92     float vp1 = vc5 * vt1 + vc4;
93     float vp2 = vc5 * vt2 + vc4;
94     float vp3 = vc5 * vt3 + vc4;
95 
96     vp0 = vp0 * vt0 + vc3;
97     vp1 = vp1 * vt1 + vc3;
98     vp2 = vp2 * vt2 + vc3;
99     vp3 = vp3 * vt3 + vc3;
100 
101     vp0 = vp0 * vt0 + vc2;
102     vp1 = vp1 * vt1 + vc2;
103     vp2 = vp2 * vt2 + vc2;
104     vp3 = vp3 * vt3 + vc2;
105 
106     vp0 = vp0 * vt0 + vc1;
107     vp1 = vp1 * vt1 + vc1;
108     vp2 = vp2 * vt2 + vc1;
109     vp3 = vp3 * vt3 + vc1;
110 
111     // Reconstruct the final f value:
112     //   f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
113     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
114     //     = s + (t * s) * p
115     vt0 *= vs0;
116     vt1 *= vs1;
117     vt2 *= vs2;
118     vt3 *= vs3;
119 
120     float vf0 = vt0 * vp0 + vs0;
121     float vf1 = vt1 * vp1 + vs1;
122     float vf2 = vt2 * vp2 + vs2;
123     float vf3 = vt3 * vp3 + vs3;
124 
125     // For inputs below denormal cutoff, replace output with +0.0f.
126     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
127     if XNN_UNPREDICTABLE(vx0 < vdenorm_cutoff) {
128       vf0 = 0.0f;
129     }
130     if XNN_UNPREDICTABLE(vx1 < vdenorm_cutoff) {
131       vf1 = 0.0f;
132     }
133     if XNN_UNPREDICTABLE(vx2 < vdenorm_cutoff) {
134       vf2 = 0.0f;
135     }
136     if XNN_UNPREDICTABLE(vx3 < vdenorm_cutoff) {
137       vf3 = 0.0f;
138     }
139 
140     // Store 4 outputs at a time.
141     output[0] = vf0;
142     output[1] = vf1;
143     output[2] = vf2;
144     output[3] = vf3;
145     output += 4;
146 
147     // Accumulate computed exponents.
148     vacc0 += vf0;
149     vacc1 += vf1;
150     vacc0 += vf2;
151     vacc1 += vf3;
152   }
153   // Add up all accumulators to vacc0
154   vacc0 += vacc1;
155 
156   float vacc = vacc0;
157   for (; elements >= sizeof(float); elements -= sizeof(float)) {
158     // Load 1 input at a time.
159     const float vi = *input++;
160 
161     // Subtract maximum input x := i - i_max. This implies x <= 0.
162     const float vx = vi - vi_max;
163 
164     // Compute reduced argument n := round(x / log(2)).
165     // We do it by adding a large number (magic bias) to the product x * (1/log(2)), which cause rounding of the result
166     // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
167     // certain bounds (|x| <= 2**22), but that's ok, because inputs outside of [-87.336540, 0.0] underflow expf(x)
168     // anyway. We fixup the result for such inputs at the very end of the algorithm.
169     float vn = vx * vlog2e + vmagic_bias;
170 
171     // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
172     // -87.33642 <= x <= 0.0, and -126 <= n <= 0 accordingly.
173     const float vs = uint32_as_float(float_as_uint32(vn) << 23);
174 
175     // Subtract the large number back to get final n := round(x / log(2)).
176     vn -= vmagic_bias;
177 
178     // Compute reduced argument t := x - n * log(2).
179     // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
180     float vt = vn * vminus_ln2_hi + vx;
181     vt = vn * vminus_ln2_lo + vt;
182 
183     // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
184     float vp = vc5 * vt + vc4;
185     vp = vp * vt + vc3;
186     vp = vp * vt + vc2;
187     vp = vp * vt + vc1;
188 
189     // Reconstruct the final f value:
190     //   f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
191     //     = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
192     //     = s + (t * s) * p
193     vt *= vs;
194     float vf = vt * vp + vs;
195 
196     // For inputs below denormal cutoff, replace output with +0.0f.
197     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
198     if XNN_UNPREDICTABLE(vx < vdenorm_cutoff) {
199       vf = 0.0f;
200     }
201 
202     // Store 1 output at a time.
203     *output++ = vf;
204 
205     // Accumulate computed exponents.
206     vacc += vf;
207   }
208   *sum = vacc;
209 }
210