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