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