1 // Auto-generated file. Do not edit!
2 // Template: src/f32-raddstoreexpminusmax/scalar-rr2-lut64-p2.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
17 // Note redefine as uint32[] to avoid redundant bitcasts.
18 extern XNN_INTERNAL const uint32_t xnn_table_exp2_k_over_64[64];
19
xnn_f32_raddstoreexpminusmax_ukernel__scalar_rr2_lut64_p2_x1(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)])20 void xnn_f32_raddstoreexpminusmax_ukernel__scalar_rr2_lut64_p2_x1(
21 size_t elements,
22 const float* input,
23 const float* max,
24 float* output,
25 float* sum,
26 const union xnn_f32_expminus_params params[restrict XNN_MIN_ELEMENTS(1)])
27 {
28 assert(elements % sizeof(float) == 0);
29
30 const float vi_max = *max;
31 const float vlog2e = params->scalar_rr2_lut64_p2.log2e;
32 const float vmagic_bias = params->scalar_rr2_lut64_p2.magic_bias;
33 const uint32_t vindex_mask = UINT32_C(0x3F);
34 const float vminus_ln2_hi = params->scalar_rr2_lut64_p2.minus_ln2_hi;
35 const float vminus_ln2_lo = params->scalar_rr2_lut64_p2.minus_ln2_lo;
36 const float vc2 = params->scalar_rr2_lut64_p2.c2;
37 const float vdenorm_cutoff = params->scalar_rr2_lut64_p2.denorm_cutoff;
38
39 float vacc = 0.0f;
40 for (; elements >= sizeof(float); elements -= sizeof(float)) {
41 // Load 1 input at a time.
42 const float vi = *input++;
43
44 // Subtract maximum input x := i - i_max. This implies x <= 0.
45 const float vx = vi - vi_max;
46
47 // Compute reduced argument n := round(x * 64 / log(2)).
48 // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
49 // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
50 // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
51 // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
52 // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
53 // algorithm.
54 float vn = vx * vlog2e + vmagic_bias;
55
56 // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
57 // i.e. -87.33642 <= x <= 0.0. As n has 6 fractional bits, we split s == 2**(n / 64) = 2**e * 2**(n / 64 - e), where
58 // e := int(n / 64). We create s in two steps:
59 // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
60 // fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
61 // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
62 // number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
63 // and thus the adjusted exponent is not lower than -126.
64 //
65 // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
66 const uint32_t ve = (float_as_uint32(vn) & UINT32_C(0xFFFFFFC0)) << 17;
67
68 // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
69 const uint32_t vidx = float_as_uint32(vn) & vindex_mask;
70 // Adjust exponent of the value l fetched from the table to get the final s value.
71 const float vs = uint32_as_float(xnn_table_exp2_k_over_64[vidx] + ve);
72
73 // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
74 vn -= vmagic_bias;
75
76 // Compute reduced argument t := x - n * log(2) / 64.
77 // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
78 float vt = vn * vminus_ln2_hi + vx;
79 vt = vn * vminus_ln2_lo + vt;
80
81 // Compute degree-2 polynomial approximation for exp(t) on [-log(2)/128, log(2)/128].
82 float vp = vt * vc2;
83 vp = vp * vt + vt;
84
85 // Reconstruct the final f value:
86 // f = s * (1 + t * (1 + t * c2))
87 // = s * (1 + t + t * (t * c2))
88 // = s + s * (t + t * (t * c2))
89 // = s + s * p
90 float vf = vp * vs + vs;
91
92 // For inputs below denormal cutoff, replace output with +0.0f.
93 // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
94 if XNN_UNPREDICTABLE(vx < vdenorm_cutoff) {
95 vf = 0.0f;
96 }
97
98 // Store 1 output at a time.
99 *output++ = vf;
100
101 // Accumulate computed exponents.
102 vacc += vf;
103 }
104 *sum = vacc;
105 }
106