Marat Dukhan | 5e9a91e | 2019-12-22 19:13:03 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | #include <assert.h> |
| 7 | #include <stddef.h> |
| 8 | |
| 9 | #include <xnnpack/common.h> |
| 10 | #include <xnnpack/math-stubs.h> |
| 11 | |
| 12 | #include <fp16/bitcasts.h> |
| 13 | |
| 14 | |
Marat Dukhan | 9dd119a | 2020-11-20 18:20:04 -0800 | [diff] [blame^] | 15 | void xnn_math_f32_expminus__scalar_rr2_p5( |
Marat Dukhan | 5e9a91e | 2019-12-22 19:13:03 -0800 | [diff] [blame] | 16 | size_t n, |
| 17 | const float* input, |
| 18 | float* output) |
| 19 | { |
| 20 | assert(n % sizeof(float) == 0); |
| 21 | |
| 22 | const float vmagic_bias = 0x1.8000FEp23f; |
| 23 | // The smallest x for which expf(x) is normalized. |
| 24 | const float vdenorm_cutoff = -0x1.5D589Ep6f; |
| 25 | const float vlog2e = 0x1.715476p+0f; |
| 26 | // Last 7 bits are zeroes |
| 27 | const float vminus_ln2_hi = -0x1.62E400p-1f; |
| 28 | const float vminus_ln2_lo = -0x1.7F7D1Cp-20f; |
| 29 | |
| 30 | const float vc1 = 0x1.FFFFF6p-1f; |
| 31 | const float vc2 = 0x1.FFFDC6p-2f; |
| 32 | const float vc3 = 0x1.555A80p-3f; |
| 33 | const float vc4 = 0x1.573A1Ap-5f; |
| 34 | const float vc5 = 0x1.0F9F9Cp-7f; |
| 35 | |
| 36 | for (; n != 0; n -= sizeof(float)) { |
| 37 | const float vx = *input++; |
| 38 | |
| 39 | // Compute reduced argument n := round(x / log(2)). |
| 40 | // We do it by adding a large number (magic bias) to the product x * (1/log(2)), which cause rounding of the result |
| 41 | // to an integer, then subtracing the large number back. The trick with adding large number is valid only within |
| 42 | // certain bounds (|x| <= 2**22), but thats ok, because inputs outside of [-87.336540, 0.0] underflow expf(x) |
| 43 | // anyway. We fixup the result for such inputs at the very end of the algorithm. |
| 44 | float vn = vx * vlog2e + vmagic_bias; |
| 45 | |
| 46 | // Create a floating-point number s (scale) such that s == 2**n for inputs which don't cause underflow, i.e. |
| 47 | // -87.33642 <= x <= 0.0, and -126 <= n <= 0 accordingly. |
| 48 | const float vs = fp32_from_bits(fp32_to_bits(vn) << 23); |
| 49 | |
| 50 | // Subtract the large number back to get final n := round(x / log(2)). |
| 51 | vn -= vmagic_bias; |
| 52 | |
| 53 | // Compute reduced argument t := x - n * log(2). |
| 54 | // Use Cody-Waite range reduction method (note two constants to represent log(2)) to improve accuracy. |
| 55 | float vt = vn * vminus_ln2_hi + vx; |
| 56 | vt = vn * vminus_ln2_lo + vt; |
| 57 | |
Marat Dukhan | 102a739 | 2020-11-20 01:18:10 -0800 | [diff] [blame] | 58 | // Compute degree-5 polynomial approximation for exp(t) on [-log(2)/2, log(2)/2]. |
Marat Dukhan | 5e9a91e | 2019-12-22 19:13:03 -0800 | [diff] [blame] | 59 | float vp = vc5 * vt + vc4; |
| 60 | vp = vp * vt + vc3; |
| 61 | vp = vp * vt + vc2; |
| 62 | vp = vp * vt + vc1; |
| 63 | |
| 64 | // Reconstruct the final f value: |
| 65 | // f = s * (1 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5))))) |
| 66 | // = s + (t * s) * (c1 + t * (c2 + t * (c3 + t * (c4 + t * c5)))) |
| 67 | // = s + (t * s) * p |
| 68 | vt *= vs; |
| 69 | float vf = vt * vp + vs; |
| 70 | |
| 71 | // For inputs below denormal cutoff, replace output with +0.0f. |
| 72 | // Note that for NaN inputs, comparison result is false, and outputs are left unchanged. |
| 73 | if XNN_UNPREDICTABLE(vx < vdenorm_cutoff) { |
| 74 | vf = 0.0f; |
| 75 | } |
| 76 | |
| 77 | *output++ = vf; |
| 78 | } |
| 79 | } |