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