|
| 1 | +//===-- Half-precision e^x function ---------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "src/math/expf16.h" |
| 10 | +#include "hdr/errno_macros.h" |
| 11 | +#include "hdr/fenv_macros.h" |
| 12 | +#include "src/__support/CPP/array.h" |
| 13 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | +#include "src/__support/FPUtil/FPBits.h" |
| 15 | +#include "src/__support/FPUtil/PolyEval.h" |
| 16 | +#include "src/__support/FPUtil/except_value_utils.h" |
| 17 | +#include "src/__support/FPUtil/multiply_add.h" |
| 18 | +#include "src/__support/FPUtil/nearest_integer.h" |
| 19 | +#include "src/__support/FPUtil/rounding_mode.h" |
| 20 | +#include "src/__support/common.h" |
| 21 | +#include "src/__support/macros/config.h" |
| 22 | +#include "src/__support/macros/optimization.h" |
| 23 | + |
| 24 | +namespace LIBC_NAMESPACE_DECL { |
| 25 | + |
| 26 | +static constexpr fputil::ExceptValues<float16, 2> EXPF16_EXCEPTS_LO = {{ |
| 27 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 28 | + // x = 0x1.de4p-8, expf16(x) = 0x1.01cp+0 (RZ) |
| 29 | + {0x1f79U, 0x3c07U, 1U, 0U, 0U}, |
| 30 | + // x = 0x1.73cp-6, expf16(x) = 0x1.05cp+0 (RZ) |
| 31 | + {0x25cfU, 0x3c17U, 1U, 0U, 0U}, |
| 32 | +}}; |
| 33 | + |
| 34 | +static constexpr fputil::ExceptValues<float16, 3> EXPF16_EXCEPTS_HI = {{ |
| 35 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 36 | + // x = 0x1.c34p+0, expf16(x) = 0x1.74cp+2 (RZ) |
| 37 | + {0x3f0dU, 0x45d3U, 1U, 0U, 1U}, |
| 38 | + // x = -0x1.488p-5, expf16(x) = 0x1.ebcp-1 (RZ) |
| 39 | + {0xa922U, 0x3bafU, 1U, 0U, 0U}, |
| 40 | + // x = -0x1.55p-5, expf16(x) = 0x1.ebp-1 (RZ) |
| 41 | + {0xa954U, 0x3bacU, 1U, 0U, 0U}, |
| 42 | +}}; |
| 43 | + |
| 44 | +// Generated by Sollya with the following commands: |
| 45 | +// > display = hexadecimal; |
| 46 | +// > for i from -18 to 12 do print(round(exp(i), SG, RN)); |
| 47 | +static constexpr cpp::array<float, 31> EXP_HI = { |
| 48 | + 0x1.05a628p-26f, 0x1.639e32p-25f, 0x1.e355bcp-24f, 0x1.4875cap-22f, |
| 49 | + 0x1.be6c7p-21f, 0x1.2f6054p-19f, 0x1.9c54c4p-18f, 0x1.183542p-16f, |
| 50 | + 0x1.7cd79cp-15f, 0x1.02cf22p-13f, 0x1.5fc21p-12f, 0x1.de16bap-11f, |
| 51 | + 0x1.44e52p-9f, 0x1.b993fep-8f, 0x1.2c155cp-6f, 0x1.97db0cp-5f, |
| 52 | + 0x1.152aaap-3f, 0x1.78b564p-2f, 0x1p+0f, 0x1.5bf0a8p+1f, |
| 53 | + 0x1.d8e64cp+2f, 0x1.415e5cp+4f, 0x1.b4c902p+5f, 0x1.28d38ap+7f, |
| 54 | + 0x1.936dc6p+8f, 0x1.122886p+10f, 0x1.749ea8p+11f, 0x1.fa7158p+12f, |
| 55 | + 0x1.5829dcp+14f, 0x1.d3c448p+15f, 0x1.3de166p+17f, |
| 56 | +}; |
| 57 | + |
| 58 | +// Generated by Sollya with the following commands: |
| 59 | +// > display = hexadecimal; |
| 60 | +// > for i from 0 to 7 do print(round(exp(i * 2^-3), SG, RN)); |
| 61 | +static constexpr cpp::array<float, 8> EXP_MID = { |
| 62 | + 0x1p+0f, 0x1.221604p+0f, 0x1.48b5e4p+0f, 0x1.747a52p+0f, |
| 63 | + 0x1.a61298p+0f, 0x1.de455ep+0f, 0x1.0ef9dcp+1f, 0x1.330e58p+1f, |
| 64 | +}; |
| 65 | + |
| 66 | +LLVM_LIBC_FUNCTION(float16, expf16, (float16 x)) { |
| 67 | + using FPBits = fputil::FPBits<float16>; |
| 68 | + FPBits x_bits(x); |
| 69 | + |
| 70 | + uint16_t x_u = x_bits.uintval(); |
| 71 | + uint16_t x_abs = x_u & 0x7fffU; |
| 72 | + |
| 73 | + // When 0 < |x| <= 2^(-5), or |x| >= 12, or x is NaN. |
| 74 | + if (LIBC_UNLIKELY(x_abs <= 0x2800U || x_abs >= 0x4a00U)) { |
| 75 | + // exp(NaN) = NaN |
| 76 | + if (x_bits.is_nan()) { |
| 77 | + if (x_bits.is_signaling_nan()) { |
| 78 | + fputil::raise_except_if_required(FE_INVALID); |
| 79 | + return FPBits::quiet_nan().get_val(); |
| 80 | + } |
| 81 | + |
| 82 | + return x; |
| 83 | + } |
| 84 | + |
| 85 | + // When x >= 12. |
| 86 | + if (x_bits.is_pos() && x_abs >= 0x4a00U) { |
| 87 | + // exp(+inf) = +inf |
| 88 | + if (x_bits.is_inf()) |
| 89 | + return FPBits::inf().get_val(); |
| 90 | + |
| 91 | + switch (fputil::quick_get_round()) { |
| 92 | + case FE_TONEAREST: |
| 93 | + case FE_UPWARD: |
| 94 | + fputil::set_errno_if_required(ERANGE); |
| 95 | + fputil::raise_except_if_required(FE_OVERFLOW); |
| 96 | + return FPBits::inf().get_val(); |
| 97 | + default: |
| 98 | + return FPBits::max_normal().get_val(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // When x <= -18. |
| 103 | + if (x_u >= 0xcc80U) { |
| 104 | + // exp(-inf) = +0 |
| 105 | + if (x_bits.is_inf()) |
| 106 | + return FPBits::zero().get_val(); |
| 107 | + |
| 108 | + fputil::set_errno_if_required(ERANGE); |
| 109 | + fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); |
| 110 | + |
| 111 | + switch (fputil::quick_get_round()) { |
| 112 | + case FE_UPWARD: |
| 113 | + return FPBits::min_subnormal().get_val(); |
| 114 | + default: |
| 115 | + return FPBits::zero().get_val(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // When 0 < |x| <= 2^(-5). |
| 120 | + if (x_abs <= 0x2800U && !x_bits.is_zero()) { |
| 121 | + if (auto r = EXPF16_EXCEPTS_LO.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 122 | + return r.value(); |
| 123 | + |
| 124 | + float xf = x; |
| 125 | + // Degree-3 minimax polynomial generated by Sollya with the following |
| 126 | + // commands: |
| 127 | + // > display = hexadecimal; |
| 128 | + // > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]); |
| 129 | + // > 1 + x * P; |
| 130 | + float r = |
| 131 | + fputil::polyeval(xf, 0x1p+0f, 0x1p+0f, 0x1.0004p-1f, 0x1.555778p-3f); |
| 132 | + return static_cast<float16>(r); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (auto r = EXPF16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 137 | + return r.value(); |
| 138 | + |
| 139 | + // For -18 < x < 12, to compute exp(x), we perform the following range |
| 140 | + // reduction: find hi, mid, lo, such that: |
| 141 | + // x = hi + mid + lo, in which |
| 142 | + // hi is an integer, |
| 143 | + // mid * 2^3 is an integer, |
| 144 | + // -2^(-4) <= lo < 2^(-4). |
| 145 | + // In particular, |
| 146 | + // hi + mid = round(x * 2^3) * 2^(-3). |
| 147 | + // Then, |
| 148 | + // exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo). |
| 149 | + // We store exp(hi) and exp(mid) in the lookup tables EXP_HI and EXP_MID |
| 150 | + // respectively. exp(lo) is computed using a degree-3 minimax polynomial |
| 151 | + // generated by Sollya. |
| 152 | + |
| 153 | + float xf = static_cast<float>(x); |
| 154 | + float kf = fputil::nearest_integer(xf * 0x1.0p+3f); |
| 155 | + int x_hi_mid = static_cast<int>(kf); |
| 156 | + int x_hi = x_hi_mid >> 3; |
| 157 | + int x_mid = x_hi_mid & 0x7; |
| 158 | + // lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x |
| 159 | + float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf); |
| 160 | + |
| 161 | + float exp_hi = EXP_HI[x_hi + 18]; |
| 162 | + float exp_mid = EXP_MID[x_mid]; |
| 163 | + // Degree-3 minimax polynomial generated by Sollya with the following |
| 164 | + // commands: |
| 165 | + // > display = hexadecimal; |
| 166 | + // > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-4, 2^-4]); |
| 167 | + // > 1 + x * P; |
| 168 | + float exp_lo = |
| 169 | + fputil::polyeval(lo, 0x1p+0f, 0x1p+0f, 0x1.001p-1f, 0x1.555ddep-3f); |
| 170 | + return static_cast<float16>(exp_hi * exp_mid * exp_lo); |
| 171 | +} |
| 172 | + |
| 173 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments