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