1a8c59750SOverMighty //===-- Implementation of exp2m1f function --------------------------------===// 2a8c59750SOverMighty // 3a8c59750SOverMighty // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4a8c59750SOverMighty // See https://llvm.org/LICENSE.txt for license information. 5a8c59750SOverMighty // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6a8c59750SOverMighty // 7a8c59750SOverMighty //===----------------------------------------------------------------------===// 8a8c59750SOverMighty 9a8c59750SOverMighty #include "src/math/exp2m1f.h" 10a8c59750SOverMighty #include "src/__support/FPUtil/FEnvImpl.h" 11a8c59750SOverMighty #include "src/__support/FPUtil/FPBits.h" 12a8c59750SOverMighty #include "src/__support/FPUtil/PolyEval.h" 13a8c59750SOverMighty #include "src/__support/FPUtil/except_value_utils.h" 14a8c59750SOverMighty #include "src/__support/FPUtil/multiply_add.h" 15a8c59750SOverMighty #include "src/__support/FPUtil/rounding_mode.h" 16a8c59750SOverMighty #include "src/__support/common.h" 17*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 18a8c59750SOverMighty #include "src/__support/macros/optimization.h" 19a8c59750SOverMighty #include "src/__support/macros/properties/cpu_features.h" 20a8c59750SOverMighty #include "src/errno/libc_errno.h" 21a8c59750SOverMighty 22a8c59750SOverMighty #include "explogxf.h" 23a8c59750SOverMighty 24*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 25a8c59750SOverMighty 26a8c59750SOverMighty static constexpr size_t N_EXCEPTS_LO = 8; 27a8c59750SOverMighty 28a8c59750SOverMighty static constexpr fputil::ExceptValues<float, N_EXCEPTS_LO> EXP2M1F_EXCEPTS_LO = 29a8c59750SOverMighty {{ 30a8c59750SOverMighty // (input, RZ output, RU offset, RD offset, RN offset) 31a8c59750SOverMighty // x = 0x1.36dc8ep-36, exp2m1f(x) = 0x1.aef212p-37 (RZ) 32a8c59750SOverMighty {0x2d9b'6e47U, 0x2d57'7909U, 1U, 0U, 0U}, 33a8c59750SOverMighty // x = 0x1.224936p-19, exp2m1f(x) = 0x1.926c0ep-20 (RZ) 34a8c59750SOverMighty {0x3611'249bU, 0x35c9'3607U, 1U, 0U, 1U}, 35a8c59750SOverMighty // x = 0x1.d16d2p-20, exp2m1f(x) = 0x1.429becp-20 (RZ) 36a8c59750SOverMighty {0x35e8'b690U, 0x35a1'4df6U, 1U, 0U, 1U}, 37a8c59750SOverMighty // x = 0x1.17949ep-14, exp2m1f(x) = 0x1.8397p-15 (RZ) 38a8c59750SOverMighty {0x388b'ca4fU, 0x3841'cb80U, 1U, 0U, 1U}, 39a8c59750SOverMighty // x = -0x1.9c3e1ep-38, exp2m1f(x) = -0x1.1dbeacp-38 (RZ) 40a8c59750SOverMighty {0xacce'1f0fU, 0xac8e'df56U, 0U, 1U, 0U}, 41a8c59750SOverMighty // x = -0x1.4d89b4p-32, exp2m1f(x) = -0x1.ce61b6p-33 (RZ) 42a8c59750SOverMighty {0xafa6'c4daU, 0xaf67'30dbU, 0U, 1U, 1U}, 43a8c59750SOverMighty // x = -0x1.a6eac4p-10, exp2m1f(x) = -0x1.24fadap-10 (RZ) 44a8c59750SOverMighty {0xbad3'7562U, 0xba92'7d6dU, 0U, 1U, 1U}, 45a8c59750SOverMighty // x = -0x1.e7526ep-6, exp2m1f(x) = -0x1.4e53dep-6 (RZ) 46a8c59750SOverMighty {0xbcf3'a937U, 0xbca7'29efU, 0U, 1U, 1U}, 47a8c59750SOverMighty }}; 48a8c59750SOverMighty 49a8c59750SOverMighty static constexpr size_t N_EXCEPTS_HI = 3; 50a8c59750SOverMighty 51a8c59750SOverMighty static constexpr fputil::ExceptValues<float, N_EXCEPTS_HI> EXP2M1F_EXCEPTS_HI = 52a8c59750SOverMighty {{ 53a8c59750SOverMighty // (input, RZ output, RU offset, RD offset, RN offset) 54a8c59750SOverMighty // x = 0x1.16a972p-1, exp2m1f(x) = 0x1.d545b2p-2 (RZ) 55a8c59750SOverMighty {0x3f0b'54b9U, 0x3eea'a2d9U, 1U, 0U, 0U}, 56a8c59750SOverMighty // x = -0x1.9f12acp-5, exp2m1f(x) = -0x1.1ab68cp-5 (RZ) 57a8c59750SOverMighty {0xbd4f'8956U, 0xbd0d'5b46U, 0U, 1U, 0U}, 58a8c59750SOverMighty // x = -0x1.de7b9cp-5, exp2m1f(x) = -0x1.4508f4p-5 (RZ) 59a8c59750SOverMighty {0xbd6f'3dceU, 0xbd22'847aU, 0U, 1U, 1U}, 60a8c59750SOverMighty }}; 61a8c59750SOverMighty 62a8c59750SOverMighty LLVM_LIBC_FUNCTION(float, exp2m1f, (float x)) { 63a8c59750SOverMighty using FPBits = fputil::FPBits<float>; 64a8c59750SOverMighty FPBits xbits(x); 65a8c59750SOverMighty 66a8c59750SOverMighty uint32_t x_u = xbits.uintval(); 67a8c59750SOverMighty uint32_t x_abs = x_u & 0x7fff'ffffU; 68a8c59750SOverMighty 69a8c59750SOverMighty // When |x| >= 128, or x is nan, or |x| <= 2^-5 70a8c59750SOverMighty if (LIBC_UNLIKELY(x_abs >= 0x4300'0000U || x_abs <= 0x3d00'0000U)) { 71a8c59750SOverMighty // |x| <= 2^-5 72a8c59750SOverMighty if (x_abs <= 0x3d00'0000U) { 73a8c59750SOverMighty if (auto r = EXP2M1F_EXCEPTS_LO.lookup(x_u); LIBC_UNLIKELY(r.has_value())) 74a8c59750SOverMighty return r.value(); 75a8c59750SOverMighty 76a8c59750SOverMighty // Minimax polynomial generated by Sollya with: 77a8c59750SOverMighty // > display = hexadecimal; 78a8c59750SOverMighty // > fpminimax((2^x - 1)/x, 5, [|D...|], [-2^-5, 2^-5]); 79a8c59750SOverMighty constexpr double COEFFS[] = { 80a8c59750SOverMighty 0x1.62e42fefa39f3p-1, 0x1.ebfbdff82c57bp-3, 0x1.c6b08d6f2d7aap-5, 81a8c59750SOverMighty 0x1.3b2ab6fc92f5dp-7, 0x1.5d897cfe27125p-10, 0x1.43090e61e6af1p-13}; 82a8c59750SOverMighty double xd = x; 83a8c59750SOverMighty double xsq = xd * xd; 84a8c59750SOverMighty double c0 = fputil::multiply_add(xd, COEFFS[1], COEFFS[0]); 85a8c59750SOverMighty double c1 = fputil::multiply_add(xd, COEFFS[3], COEFFS[2]); 86a8c59750SOverMighty double c2 = fputil::multiply_add(xd, COEFFS[5], COEFFS[4]); 87a8c59750SOverMighty double p = fputil::polyeval(xsq, c0, c1, c2); 88a8c59750SOverMighty return static_cast<float>(p * xd); 89a8c59750SOverMighty } 90a8c59750SOverMighty 91a8c59750SOverMighty // x >= 128, or x is nan 92a8c59750SOverMighty if (xbits.is_pos()) { 93a8c59750SOverMighty if (xbits.is_finite()) { 94a8c59750SOverMighty int rounding = fputil::quick_get_round(); 95a8c59750SOverMighty if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) 96a8c59750SOverMighty return FPBits::max_normal().get_val(); 97a8c59750SOverMighty 98a8c59750SOverMighty fputil::set_errno_if_required(ERANGE); 99a8c59750SOverMighty fputil::raise_except_if_required(FE_OVERFLOW); 100a8c59750SOverMighty } 101a8c59750SOverMighty 102a8c59750SOverMighty // x >= 128 and 2^x - 1 rounds to +inf, or x is +inf or nan 103a8c59750SOverMighty return x + FPBits::inf().get_val(); 104a8c59750SOverMighty } 105a8c59750SOverMighty } 106a8c59750SOverMighty 107a8c59750SOverMighty if (LIBC_UNLIKELY(x <= -25.0f)) { 108a8c59750SOverMighty // 2^(-inf) - 1 = -1 109a8c59750SOverMighty if (xbits.is_inf()) 110a8c59750SOverMighty return -1.0f; 111a8c59750SOverMighty // 2^nan - 1 = nan 112a8c59750SOverMighty if (xbits.is_nan()) 113a8c59750SOverMighty return x; 114a8c59750SOverMighty 115a8c59750SOverMighty int rounding = fputil::quick_get_round(); 116a8c59750SOverMighty if (rounding == FE_UPWARD || rounding == FE_TOWARDZERO) 117a8c59750SOverMighty return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f 118a8c59750SOverMighty 119a8c59750SOverMighty fputil::set_errno_if_required(ERANGE); 120a8c59750SOverMighty fputil::raise_except_if_required(FE_UNDERFLOW); 121a8c59750SOverMighty return -1.0f; 122a8c59750SOverMighty } 123a8c59750SOverMighty 124a8c59750SOverMighty if (auto r = EXP2M1F_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value())) 125a8c59750SOverMighty return r.value(); 126a8c59750SOverMighty 127a8c59750SOverMighty // For -25 < x < 128, to compute 2^x, we perform the following range 128a8c59750SOverMighty // reduction: find hi, mid, lo such that: 129a8c59750SOverMighty // x = hi + mid + lo, in which: 130a8c59750SOverMighty // hi is an integer, 131a8c59750SOverMighty // 0 <= mid * 2^5 < 32 is an integer, 132a8c59750SOverMighty // -2^(-6) <= lo <= 2^(-6). 133a8c59750SOverMighty // In particular, 134a8c59750SOverMighty // hi + mid = round(x * 2^5) * 2^(-5). 135a8c59750SOverMighty // Then, 136a8c59750SOverMighty // 2^x = 2^(hi + mid + lo) = 2^hi * 2^mid * 2^lo. 137a8c59750SOverMighty // 2^mid is stored in the lookup table of 32 elements. 138a8c59750SOverMighty // 2^lo is computed using a degree-4 minimax polynomial generated by Sollya. 139a8c59750SOverMighty // We perform 2^hi * 2^mid by simply add hi to the exponent field of 2^mid. 140a8c59750SOverMighty 141a8c59750SOverMighty // kf = (hi + mid) * 2^5 = round(x * 2^5) 142a8c59750SOverMighty float kf; 143a8c59750SOverMighty int k; 144a8c59750SOverMighty #ifdef LIBC_TARGET_CPU_HAS_NEAREST_INT 145a8c59750SOverMighty kf = fputil::nearest_integer(x * 32.0f); 146a8c59750SOverMighty k = static_cast<int>(kf); 147a8c59750SOverMighty #else 148a8c59750SOverMighty constexpr float HALF[2] = {0.5f, -0.5f}; 149a8c59750SOverMighty k = static_cast<int>(fputil::multiply_add(x, 32.0f, HALF[x < 0.0f])); 150a8c59750SOverMighty kf = static_cast<float>(k); 151a8c59750SOverMighty #endif // LIBC_TARGET_CPU_HAS_NEAREST_INT 152a8c59750SOverMighty 153a8c59750SOverMighty // lo = x - (hi + mid) = x - kf * 2^(-5) 154a8c59750SOverMighty double lo = fputil::multiply_add(-0x1.0p-5f, kf, x); 155a8c59750SOverMighty 156a8c59750SOverMighty // hi = floor(kf * 2^(-4)) 157a8c59750SOverMighty // exp2_hi = shift hi to the exponent field of double precision. 158a8c59750SOverMighty int64_t exp2_hi = 159a8c59750SOverMighty static_cast<int64_t>(static_cast<uint64_t>(k >> ExpBase::MID_BITS) 160a8c59750SOverMighty << fputil::FPBits<double>::FRACTION_LEN); 161a8c59750SOverMighty // mh = 2^hi * 2^mid 162a8c59750SOverMighty // mh_bits = bit field of mh 163a8c59750SOverMighty int64_t mh_bits = ExpBase::EXP_2_MID[k & ExpBase::MID_MASK] + exp2_hi; 164a8c59750SOverMighty double mh = fputil::FPBits<double>(static_cast<uint64_t>(mh_bits)).get_val(); 165a8c59750SOverMighty 166a8c59750SOverMighty // Degree-4 polynomial approximating (2^x - 1)/x generated by Sollya with: 167a8c59750SOverMighty // > display = hexadecimal; 168a8c59750SOverMighty // > fpminimax((2^x - 1)/x, 4, [|D...|], [-2^-6, 2^-6]); 169a8c59750SOverMighty constexpr double COEFFS[5] = {0x1.62e42fefa39efp-1, 0x1.ebfbdff8131c4p-3, 170a8c59750SOverMighty 0x1.c6b08d7061695p-5, 0x1.3b2b1bee74b2ap-7, 171a8c59750SOverMighty 0x1.5d88091198529p-10}; 172a8c59750SOverMighty double lo_sq = lo * lo; 173a8c59750SOverMighty double c1 = fputil::multiply_add(lo, COEFFS[0], 1.0); 174a8c59750SOverMighty double c2 = fputil::multiply_add(lo, COEFFS[2], COEFFS[1]); 175a8c59750SOverMighty double c3 = fputil::multiply_add(lo, COEFFS[4], COEFFS[3]); 176a8c59750SOverMighty double exp2_lo = fputil::polyeval(lo_sq, c1, c2, c3); 177a8c59750SOverMighty // 2^x - 1 = 2^(hi + mid + lo) - 1 178a8c59750SOverMighty // = 2^(hi + mid) * 2^lo - 1 179a8c59750SOverMighty // ~ mh * (1 + lo * P(lo)) - 1 180a8c59750SOverMighty // = mh * exp2_lo - 1 181a8c59750SOverMighty return static_cast<float>(fputil::multiply_add(exp2_lo, mh, -1.0)); 182a8c59750SOverMighty } 183a8c59750SOverMighty 184*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 185