| 1 | //===-- Half-precision log(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/logf16.h" |
| 10 | #include "expxf16.h" |
| 11 | #include "hdr/errno_macros.h" |
| 12 | #include "hdr/fenv_macros.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/cast.h" |
| 17 | #include "src/__support/FPUtil/except_value_utils.h" |
| 18 | #include "src/__support/FPUtil/multiply_add.h" |
| 19 | #include "src/__support/common.h" |
| 20 | #include "src/__support/macros/config.h" |
| 21 | #include "src/__support/macros/optimization.h" |
| 22 | #include "src/__support/macros/properties/cpu_features.h" |
| 23 | |
| 24 | namespace LIBC_NAMESPACE_DECL { |
| 25 | |
| 26 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 27 | #ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 28 | static constexpr size_t N_LOGF16_EXCEPTS = 5; |
| 29 | #else |
| 30 | static constexpr size_t N_LOGF16_EXCEPTS = 11; |
| 31 | #endif |
| 32 | |
| 33 | static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS> |
| 34 | LOGF16_EXCEPTS = {{ |
| 35 | // (input, RZ output, RU offset, RD offset, RN offset) |
| 36 | #ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 37 | // x = 0x1.61cp-13, logf16(x) = -0x1.16p+3 (RZ) |
| 38 | {0x0987U, 0xc858U, 0U, 1U, 0U}, |
| 39 | // x = 0x1.f2p-12, logf16(x) = -0x1.e98p+2 (RZ) |
| 40 | {0x0fc8U, 0xc7a6U, 0U, 1U, 1U}, |
| 41 | #endif |
| 42 | // x = 0x1.4d4p-9, logf16(x) = -0x1.7e4p+2 (RZ) |
| 43 | {0x1935U, 0xc5f9U, 0U, 1U, 0U}, |
| 44 | // x = 0x1.5ep-8, logf16(x) = -0x1.4ecp+2 (RZ) |
| 45 | {0x1d78U, 0xc53bU, 0U, 1U, 0U}, |
| 46 | #ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 47 | // x = 0x1.fdp-1, logf16(x) = -0x1.81p-8 (RZ) |
| 48 | {0x3bf4U, 0x9e04U, 0U, 1U, 1U}, |
| 49 | // x = 0x1.fep-1, logf16(x) = -0x1.008p-8 (RZ) |
| 50 | {0x3bf8U, 0x9c02U, 0U, 1U, 0U}, |
| 51 | #endif |
| 52 | // x = 0x1.ffp-1, logf16(x) = -0x1.004p-9 (RZ) |
| 53 | {0x3bfcU, 0x9801U, 0U, 1U, 0U}, |
| 54 | // x = 0x1.ff8p-1, logf16(x) = -0x1p-10 (RZ) |
| 55 | {0x3bfeU, 0x9400U, 0U, 1U, 1U}, |
| 56 | #ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 57 | // x = 0x1.4c4p+1, logf16(x) = 0x1.e84p-1 (RZ) |
| 58 | {0x4131U, 0x3ba1U, 1U, 0U, 1U}, |
| 59 | #else |
| 60 | // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ) |
| 61 | {0x45d4U, 0x3f0dU, 1U, 0U, 0U}, |
| 62 | // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ) |
| 63 | {0x45d4U, 0x3f0dU, 1U, 0U, 0U}, |
| 64 | // x = 0x1.d5p+9, logf16(x) = 0x1.b5cp+2 (RZ) |
| 65 | {0x6354U, 0x46d7U, 1U, 0U, 1U}, |
| 66 | #endif |
| 67 | }}; |
| 68 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 69 | |
| 70 | LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) { |
| 71 | using FPBits = fputil::FPBits<float16>; |
| 72 | FPBits x_bits(x); |
| 73 | |
| 74 | uint16_t x_u = x_bits.uintval(); |
| 75 | |
| 76 | // If x <= 0, or x is 1, or x is +inf, or x is NaN. |
| 77 | if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) { |
| 78 | // log(NaN) = NaN |
| 79 | if (x_bits.is_nan()) { |
| 80 | if (x_bits.is_signaling_nan()) { |
| 81 | fputil::raise_except_if_required(FE_INVALID); |
| 82 | return FPBits::quiet_nan().get_val(); |
| 83 | } |
| 84 | |
| 85 | return x; |
| 86 | } |
| 87 | |
| 88 | // log(+/-0) = −inf |
| 89 | if ((x_u & 0x7fffU) == 0U) { |
| 90 | fputil::raise_except_if_required(FE_DIVBYZERO); |
| 91 | return FPBits::inf(Sign::NEG).get_val(); |
| 92 | } |
| 93 | |
| 94 | if (x_u == 0x3c00U) |
| 95 | return FPBits::zero().get_val(); |
| 96 | |
| 97 | // When x < 0. |
| 98 | if (x_u > 0x8000U) { |
| 99 | fputil::set_errno_if_required(EDOM); |
| 100 | fputil::raise_except_if_required(FE_INVALID); |
| 101 | return FPBits::quiet_nan().get_val(); |
| 102 | } |
| 103 | |
| 104 | // log(+inf) = +inf |
| 105 | return FPBits::inf().get_val(); |
| 106 | } |
| 107 | |
| 108 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 109 | if (auto r = LOGF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 110 | return r.value(); |
| 111 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 112 | |
| 113 | // To compute log(x), we perform the following range reduction: |
| 114 | // x = 2^m * 1.mant, |
| 115 | // log(x) = m * log(2) + log(1.mant). |
| 116 | // To compute log(1.mant), let f be the highest 6 bits including the hidden |
| 117 | // bit, and d be the difference (1.mant - f), i.e., the remaining 5 bits of |
| 118 | // the mantissa, then: |
| 119 | // log(1.mant) = log(f) + log(1.mant / f) |
| 120 | // = log(f) + log(1 + d/f) |
| 121 | // since d/f is sufficiently small. |
| 122 | // We store log(f) and 1/f in the lookup tables LOGF_F and ONE_OVER_F_F |
| 123 | // respectively. |
| 124 | |
| 125 | int m = -FPBits::EXP_BIAS; |
| 126 | |
| 127 | // When x is subnormal, normalize it. |
| 128 | if ((x_u & FPBits::EXP_MASK) == 0U) { |
| 129 | // Can't pass an integer to fputil::cast directly. |
| 130 | constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN; |
| 131 | x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP)); |
| 132 | x_u = x_bits.uintval(); |
| 133 | m -= FPBits::FRACTION_LEN; |
| 134 | } |
| 135 | |
| 136 | uint16_t mant = x_bits.get_mantissa(); |
| 137 | // Leading 10 - 5 = 5 bits of the mantissa. |
| 138 | int f = mant >> 5; |
| 139 | // Unbiased exponent. |
| 140 | m += x_u >> FPBits::FRACTION_LEN; |
| 141 | |
| 142 | // Set bits to 1.mant instead of 2^m * 1.mant. |
| 143 | x_bits.set_biased_exponent(FPBits::EXP_BIAS); |
| 144 | float mant_f = x_bits.get_val(); |
| 145 | // v = 1.mant * 1/f - 1 = d/f |
| 146 | float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f); |
| 147 | |
| 148 | // Degree-3 minimax polynomial generated by Sollya with the following |
| 149 | // commands: |
| 150 | // > display = hexadecimal; |
| 151 | // > P = fpminimax(log(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]); |
| 152 | // > x * P; |
| 153 | float log1p_d_over_f = |
| 154 | v * fputil::polyeval(v, 0x1p+0f, -0x1.001804p-1f, 0x1.557ef6p-2f); |
| 155 | // log(1.mant) = log(f) + log(1 + d/f) |
| 156 | float log_1_mant = LOGF_F[f] + log1p_d_over_f; |
| 157 | return fputil::cast<float16>( |
| 158 | fputil::multiply_add(static_cast<float>(m), LOGF_2, log_1_mant)); |
| 159 | } |
| 160 | |
| 161 | } // namespace LIBC_NAMESPACE_DECL |
| 162 | |