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