• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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.
19 extern XNN_INTERNAL const uint32_t xnn_table_exp2_k_over_64[64];
20 
xnn_f32_raddstoreexpminusmax_ukernel__scalar_lut64_p2_x2(size_t elements,const float * input,float * output,float * sum,float vi_max)21 void xnn_f32_raddstoreexpminusmax_ukernel__scalar_lut64_p2_x2(
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 vacc0 = 0.0f;
43   for (; elements >= 2 * sizeof(float); elements -= 2 * sizeof(float)) {
44     // Load 2 inputs at a time.
45     const float vi0 = input[0];
46     const float vi1 = input[1];
47     input += 2;
48 
49     // Subtract maximum input x := i - i_max. This implies x <= 0.
50     const float vx0 = vi0 - vi_max;
51     const float vx1 = vi1 - vi_max;
52 
53     // Compute reduced argument n := round(x * 64 / log(2)).
54     // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
55     // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
56     // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
57     // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
58     // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
59     // algorithm.
60     float vn0 = vx0 * vlog2e_x64 + vmagic_bias;
61     float vn1 = vx1 * vlog2e_x64 + vmagic_bias;
62 
63     // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
64     // 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
65     // e := int(n / 64). We create s in two steps:
66     // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
67     //    fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
68     // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
69     //    number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
70     //    and thus the adjusted exponent is not lower than -126.
71     //
72     // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
73     const uint32_t ve0 = (fp32_to_bits(vn0) & UINT32_C(0xFFFFFFC0)) << 17;
74     const uint32_t ve1 = (fp32_to_bits(vn1) & UINT32_C(0xFFFFFFC0)) << 17;
75 
76     // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
77     const uint32_t vidx0 = fp32_to_bits(vn0) & vindex_mask;
78     const uint32_t vidx1 = fp32_to_bits(vn1) & vindex_mask;
79     // Adjust exponent of the value l fetched from the table to get the final s value.
80     const float vs0 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx0] + ve0);
81     const float vs1 = fp32_from_bits(xnn_table_exp2_k_over_64[vidx1] + ve1);
82 
83     // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
84     vn0 -= vmagic_bias;
85     vn1 -= vmagic_bias;
86 
87     // Compute reduced argument t := x - n * log(2) / 64.
88     // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
89     float vt0 = vn0 * vminus_ln2_o64_hi + vx0;
90     float vt1 = vn1 * vminus_ln2_o64_hi + vx1;
91 
92     vt0 = vn0 * vminus_ln2_o64_lo + vt0;
93     vt1 = vn1 * vminus_ln2_o64_lo + vt1;
94 
95     // Compute degree-2 polynomial approxiatmion for exp(t) on [-log(2)/128, log(2)/128].
96     float vp0 = vt0 * vc2;
97     float vp1 = vt1 * vc2;
98 
99     vp0 = vp0 * vt0 + vt0;
100     vp1 = vp1 * vt1 + vt1;
101 
102     // Reconstruct the final f value:
103     //   f = s * (1 + t * (1 + t * c2))
104     //     = s * (1 + t + t * (t * c2))
105     //     = s + s * (t + t * (t * c2))
106     //     = s + s * p
107     float vf0 = vp0 * vs0 + vs0;
108     float vf1 = vp1 * vs1 + vs1;
109 
110     // For inputs below denormal cutoff, replace output with +0.0f.
111     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
112     if XNN_UNPREDICTABLE(vx0 < vdenorm_cutoff) {
113       vf0 = 0.0f;
114     }
115     if XNN_UNPREDICTABLE(vx1 < vdenorm_cutoff) {
116       vf1 = 0.0f;
117     }
118 
119     // Store 2 outputs at a time.
120     output[0] = vf0;
121     output[1] = vf1;
122     output += 2;
123 
124     // Accumulate computed exponents.
125     vacc0 += vf0;
126     vacc0 += vf1;
127   }
128 
129   float vacc = vacc0;
130   for (; elements >= sizeof(float); elements -= sizeof(float)) {
131     // Load 1 input at a time.
132     const float vi = *input++;
133 
134     // Subtract maximum input x := i - i_max. This implies x <= 0.
135     const float vx = vi - vi_max;
136 
137     // Compute reduced argument n := round(x * 64 / log(2)).
138     // We do it by adding a large number (magic bias), which cause rounding of the result to an integer, then subtracing
139     // the large number back. The first addition is combined with multiplication by log2e into a single FMA instruction.
140     // The trick with adding large number is valid only within certain bounds (|x * 64 / log(2)| <= 2**22, i.e.
141     // |x| <= 0x1.62E43p+15 = 45426.09375), but that is acceptable, because inputs outside of [-87.336540, 0.0]
142     // result in denormalized or underflown expf(x). We fixup the result for such inputs at the very end of the
143     // algorithm.
144     float vn = vx * vlog2e_x64 + vmagic_bias;
145 
146     // Create a floating-point number s (scale) such that s := 2**(n / 64) for such inputs that expf(x) is normalized,
147     // 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
148     // e := int(n / 64). We create s in two steps:
149     // 1. Fetch 2**(n / 64 - e) = 2**(n % 64) from the table using the 6 low bits of n, as integer. Note that the
150     //    fetched values are in the [1.0, 2.0) range, i.e. their floating-point exponent is 0.
151     // 2. Adjust fecthed value by addition of e to its floating-point exponent. The result is always a normalized
152     //    number, because for -87.33642 <= x <= 0.0 (inputs for which expf(x) is normalized) we have -126 <= e <= 0,
153     //    and thus the adjusted exponent is not lower than -126.
154     //
155     // Extract e from bits 6:14 of n and shift it into bits 23:31 (position of floating-point exponent).
156     const uint32_t ve = (fp32_to_bits(vn) & UINT32_C(0xFFFFFFC0)) << 17;
157 
158     // Use bits 0:6 bits of n, as integer, as an index for table lookup of l := 2**(n % 64).
159     const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
160     // Adjust exponent of the value l fetched from the table to get the final s value.
161     const float vs = fp32_from_bits(xnn_table_exp2_k_over_64[vidx] + ve);
162 
163     // Subtract the large number back to get final n := round(x * 64 / log(2)) as a floating-point number.
164     vn -= vmagic_bias;
165 
166     // Compute reduced argument t := x - n * log(2) / 64.
167     // Use Cody-Waite range reduction method (note the two constants representing log(2) / 64) to improve accuracy.
168     float vt = vn * vminus_ln2_o64_hi + vx;
169     vt = vn * vminus_ln2_o64_lo + vt;
170 
171     // Compute degree-2 polynomial approxiatmion for exp(t) on [-log(2)/128, log(2)/128].
172     float vp = vt * vc2;
173     vp = vp * vt + vt;
174 
175     // Reconstruct the final f value:
176     //   f = s * (1 + t * (1 + t * c2))
177     //     = s * (1 + t + t * (t * c2))
178     //     = s + s * (t + t * (t * c2))
179     //     = s + s * p
180     float vf = vp * vs + vs;
181 
182     // For inputs below denormal cutoff, replace output with +0.0f.
183     // Note that for NaN inputs, comparison result is false, and outputs are left unchanged.
184     if XNN_UNPREDICTABLE(vx < vdenorm_cutoff) {
185       vf = 0.0f;
186     }
187 
188     // Store 1 output at a time.
189     *output++ = vf;
190 
191     // Accumulate computed exponents.
192     vacc += vf;
193   }
194   *sum = vacc;
195 }
196