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