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