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_x1(size_t elements,const float * input,float * output,float * sum,float vi_max)18 void xnn_f32_raddstoreexpminusmax_ukernel__scalar_p5_x1(
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 vacc = 0.0f;
42 for (; elements >= sizeof(float); elements -= sizeof(float)) {
43 // Load 1 input at a time.
44 const float vi = *input++;
45
46 // Subtract maximum input x := i - i_max. This implies x <= 0.
47 const float vx = vi - vi_max;
48
49 // Compute reduced argument n := round(x / log(2)).
50 // We do it by adding a large number (magic bias) to the product x * (1/log(2)), which cause rounding of the result
51 // to an integer, then subtracing the large number back. The trick with adding large number is valid only within
52 // certain bounds (|x| <= 2**22), but thats ok, because inputs outside of [-87.336540, 0.0] underflow expf(x)
53 // anyway. We fixup the result for such inputs at the very end of the algorithm.
54 float vn = vx * vlog2e + vmagic_bias;
55
56 // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e.
57 // -87.33642 <= x <= 0.0, and -126 <= n <= 0 accordingly.
58 const float vs = fp32_from_bits(fp32_to_bits(vn) << 23);
59
60 // Subtract the large number back to get final n := round(x / log(2)).
61 vn -= vmagic_bias;
62
63 // Compute reduced argument t := x - n * log(2).
64 // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy.
65 float vt = vn * vminus_ln2_hi + vx;
66 vt = vn * vminus_ln2_lo + vt;
67
68 // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2].
69 float vp = vc5 * vt + vc4;
70 vp = vp * vt + vc3;
71 vp = vp * vt + vc2;
72 vp = vp * vt + vc1;
73
74 // Reconstruct the final f value:
75 // f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))))
76 // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))
77 // = s + (t * s) * p
78 vt *= vs;
79 float vf = vt * vp + vs;
80
81 // For inputs below denormal cutoff, replace output with +0.0f.
82 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
83 if XNN_UNPREDICTABLE(vx < vdenorm_cutoff) {
84 vf = 0.0f;
85 }
86
87 // Store 1 output at a time.
88 *output++ = vf;
89
90 // Accumulate computed exponents.
91 vacc += vf;
92 }
93 *sum = vacc;
94 }
95