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