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