|
| 1 | +//===-- Half-precision 2^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/exp2f16.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, 3> EXP2F16_EXCEPTS = {{ |
| 27 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 28 | + // x = 0x1.714p-11, exp2f16(x) = 0x1p+0 (RZ) |
| 29 | + {0x11c5U, 0x3c00U, 1U, 0U, 1U}, |
| 30 | + // x = -0x1.558p-4, exp2f16(x) = 0x1.e34p-1 (RZ) |
| 31 | + {0xad56U, 0x3b8dU, 1U, 0U, 0U}, |
| 32 | + // x = -0x1.d5cp-4, exp2f16(x) = 0x1.d8cp-1 (RZ) |
| 33 | + {0xaf57U, 0x3b63U, 1U, 0U, 0U}, |
| 34 | +}}; |
| 35 | + |
| 36 | +// Generated by Sollya with the following commands: |
| 37 | +// > display = hexadecimal; |
| 38 | +// > for i from 0 to 7 do printsingle(round(2^(i * 2^-3), SG, RN)); |
| 39 | +static constexpr cpp::array<uint32_t, 8> EXP2_MID_BITS = { |
| 40 | + 0x3f80'0000U, 0x3f8b'95c2U, 0x3f98'37f0U, 0x3fa5'fed7U, |
| 41 | + 0x3fb5'04f3U, 0x3fc5'672aU, 0x3fd7'44fdU, 0x3fea'c0c7U, |
| 42 | +}; |
| 43 | + |
| 44 | +LLVM_LIBC_FUNCTION(float16, exp2f16, (float16 x)) { |
| 45 | + using FPBits = fputil::FPBits<float16>; |
| 46 | + FPBits x_bits(x); |
| 47 | + |
| 48 | + uint16_t x_u = x_bits.uintval(); |
| 49 | + uint16_t x_abs = x_u & 0x7fffU; |
| 50 | + |
| 51 | + // When |x| >= 16, or x is NaN. |
| 52 | + if (LIBC_UNLIKELY(x_abs >= 0x4c00U)) { |
| 53 | + // exp2(NaN) = NaN |
| 54 | + if (x_bits.is_nan()) { |
| 55 | + if (x_bits.is_signaling_nan()) { |
| 56 | + fputil::raise_except_if_required(FE_INVALID); |
| 57 | + return FPBits::quiet_nan().get_val(); |
| 58 | + } |
| 59 | + |
| 60 | + return x; |
| 61 | + } |
| 62 | + |
| 63 | + // When x >= 16. |
| 64 | + if (x_bits.is_pos()) { |
| 65 | + // exp2(+inf) = +inf |
| 66 | + if (x_bits.is_inf()) |
| 67 | + return FPBits::inf().get_val(); |
| 68 | + |
| 69 | + switch (fputil::quick_get_round()) { |
| 70 | + case FE_TONEAREST: |
| 71 | + case FE_UPWARD: |
| 72 | + fputil::set_errno_if_required(ERANGE); |
| 73 | + fputil::raise_except_if_required(FE_OVERFLOW); |
| 74 | + return FPBits::inf().get_val(); |
| 75 | + default: |
| 76 | + return FPBits::max_normal().get_val(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // When x <= -25. |
| 81 | + if (x_u >= 0xce40U) { |
| 82 | + // exp2(-inf) = +0 |
| 83 | + if (x_bits.is_inf()) |
| 84 | + return FPBits::zero().get_val(); |
| 85 | + |
| 86 | + fputil::set_errno_if_required(ERANGE); |
| 87 | + fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); |
| 88 | + |
| 89 | + if (fputil::fenv_is_round_up()) |
| 90 | + return FPBits::min_subnormal().get_val(); |
| 91 | + return FPBits::zero().get_val(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (auto r = EXP2F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 96 | + return r.value(); |
| 97 | + |
| 98 | + // For -25 < x < 16, to compute 2^x, we perform the following range reduction: |
| 99 | + // find hi, mid, lo, such that: |
| 100 | + // x = hi + mid + lo, in which |
| 101 | + // hi is an integer, |
| 102 | + // mid * 2^3 is an integer, |
| 103 | + // -2^(-4) <= lo < 2^(-4). |
| 104 | + // In particular, |
| 105 | + // hi + mid = round(x * 2^3) * 2^(-3). |
| 106 | + // Then, |
| 107 | + // 2^x = 2^(hi + mid + lo) = 2^hi * 2^mid * 2^lo. |
| 108 | + // We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid |
| 109 | + // by adding hi to the exponent field of 2^mid. 2^lo is computed using a |
| 110 | + // degree-3 minimax polynomial generated by Sollya. |
| 111 | + |
| 112 | + float xf = x; |
| 113 | + float kf = fputil::nearest_integer(xf * 0x1.0p+3f); |
| 114 | + int x_hi_mid = static_cast<int>(kf); |
| 115 | + int x_hi = x_hi_mid >> 3; |
| 116 | + int x_mid = x_hi_mid & 0x7; |
| 117 | + // lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x |
| 118 | + float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf); |
| 119 | + |
| 120 | + uint32_t exp2_hi_mid_bits = |
| 121 | + EXP2_MID_BITS[x_mid] + |
| 122 | + static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN); |
| 123 | + float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val(); |
| 124 | + // Degree-3 minimax polynomial generated by Sollya with the following |
| 125 | + // commands: |
| 126 | + // > display = hexadecimal; |
| 127 | + // > P = fpminimax((2^x - 1)/x, 2, [|SG...|], [-2^-4, 2^-4]); |
| 128 | + // > 1 + x * P; |
| 129 | + float exp2_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.62e43p-1f, 0x1.ec0aa6p-3f, |
| 130 | + 0x1.c6b4a6p-5f); |
| 131 | + return static_cast<float16>(exp2_hi_mid * exp2_lo); |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments