| 1 | //===-- Half-precision log10(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/log10f16.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_LOG10F16_EXCEPTS = 11; |
| 29 | #else |
| 30 | static constexpr size_t N_LOG10F16_EXCEPTS = 17; |
| 31 | #endif |
| 32 | |
| 33 | static constexpr fputil::ExceptValues<float16, N_LOG10F16_EXCEPTS> |
| 34 | LOG10F16_EXCEPTS = {{ |
| 35 | // (input, RZ output, RU offset, RD offset, RN offset) |
| 36 | // x = 0x1.e3cp-3, log10f16(x) = -0x1.40cp-1 (RZ) |
| 37 | {0x338fU, 0xb903U, 0U, 1U, 0U}, |
| 38 | // x = 0x1.fep-3, log10f16(x) = -0x1.35p-1 (RZ) |
| 39 | {0x33f8U, 0xb8d4U, 0U, 1U, 1U}, |
| 40 | #ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 41 | // x = 0x1.394p-1, log10f16(x) = -0x1.b4cp-3 (RZ) |
| 42 | {0x38e5U, 0xb2d3U, 0U, 1U, 1U}, |
| 43 | #endif |
| 44 | // x = 0x1.ea8p-1, log10f16(x) = -0x1.31p-6 (RZ) |
| 45 | {0x3baaU, 0xa4c4U, 0U, 1U, 1U}, |
| 46 | // x = 0x1.ebp-1, log10f16(x) = -0x1.29cp-6 (RZ) |
| 47 | {0x3bacU, 0xa4a7U, 0U, 1U, 1U}, |
| 48 | // x = 0x1.f3p-1, log10f16(x) = -0x1.6dcp-7 (RZ) |
| 49 | {0x3bccU, 0xa1b7U, 0U, 1U, 1U}, |
| 50 | // x = 0x1.f38p-1, log10f16(x) = -0x1.5f8p-7 (RZ) |
| 51 | #ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 52 | {0x3bceU, 0xa17eU, 0U, 1U, 1U}, |
| 53 | // x = 0x1.fd8p-1, log10f16(x) = -0x1.168p-9 (RZ) |
| 54 | {0x3bf6U, 0x985aU, 0U, 1U, 1U}, |
| 55 | // x = 0x1.ff8p-1, log10f16(x) = -0x1.bccp-12 (RZ) |
| 56 | {0x3bfeU, 0x8ef3U, 0U, 1U, 1U}, |
| 57 | // x = 0x1.374p+0, log10f16(x) = 0x1.5b8p-4 (RZ) |
| 58 | {0x3cddU, 0x2d6eU, 1U, 0U, 1U}, |
| 59 | // x = 0x1.3ecp+1, log10f16(x) = 0x1.958p-2 (RZ) |
| 60 | {0x40fbU, 0x3656U, 1U, 0U, 1U}, |
| 61 | #endif |
| 62 | // x = 0x1.4p+3, log10f16(x) = 0x1p+0 (RZ) |
| 63 | {0x4900U, 0x3c00U, 0U, 0U, 0U}, |
| 64 | // x = 0x1.9p+6, log10f16(x) = 0x1p+1 (RZ) |
| 65 | {0x5640U, 0x4000U, 0U, 0U, 0U}, |
| 66 | // x = 0x1.f84p+6, log10f16(x) = 0x1.0ccp+1 (RZ) |
| 67 | {0x57e1U, 0x4033U, 1U, 0U, 0U}, |
| 68 | // x = 0x1.f4p+9, log10f16(x) = 0x1.8p+1 (RZ) |
| 69 | {0x63d0U, 0x4200U, 0U, 0U, 0U}, |
| 70 | // x = 0x1.388p+13, log10f16(x) = 0x1p+2 (RZ) |
| 71 | {0x70e2U, 0x4400U, 0U, 0U, 0U}, |
| 72 | // x = 0x1.674p+13, log10f16(x) = 0x1.03cp+2 (RZ) |
| 73 | {0x719dU, 0x440fU, 1U, 0U, 0U}, |
| 74 | }}; |
| 75 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 76 | |
| 77 | LLVM_LIBC_FUNCTION(float16, log10f16, (float16 x)) { |
| 78 | using FPBits = fputil::FPBits<float16>; |
| 79 | FPBits x_bits(x); |
| 80 | |
| 81 | uint16_t x_u = x_bits.uintval(); |
| 82 | |
| 83 | // If x <= 0, or x is 1, or x is +inf, or x is NaN. |
| 84 | if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) { |
| 85 | // log10(NaN) = NaN |
| 86 | if (x_bits.is_nan()) { |
| 87 | if (x_bits.is_signaling_nan()) { |
| 88 | fputil::raise_except_if_required(FE_INVALID); |
| 89 | return FPBits::quiet_nan().get_val(); |
| 90 | } |
| 91 | |
| 92 | return x; |
| 93 | } |
| 94 | |
| 95 | // log10(+/-0) = −inf |
| 96 | if ((x_u & 0x7fffU) == 0U) { |
| 97 | fputil::raise_except_if_required(FE_DIVBYZERO); |
| 98 | return FPBits::inf(Sign::NEG).get_val(); |
| 99 | } |
| 100 | |
| 101 | if (x_u == 0x3c00U) |
| 102 | return FPBits::zero().get_val(); |
| 103 | |
| 104 | // When x < 0. |
| 105 | if (x_u > 0x8000U) { |
| 106 | fputil::set_errno_if_required(EDOM); |
| 107 | fputil::raise_except_if_required(FE_INVALID); |
| 108 | return FPBits::quiet_nan().get_val(); |
| 109 | } |
| 110 | |
| 111 | // log10(+inf) = +inf |
| 112 | return FPBits::inf().get_val(); |
| 113 | } |
| 114 | |
| 115 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 116 | if (auto r = LOG10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
| 117 | return r.value(); |
| 118 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 119 | |
| 120 | // To compute log10(x), we perform the following range reduction: |
| 121 | // x = 2^m * 1.mant, |
| 122 | // log10(x) = m * log10(2) + log10(1.mant). |
| 123 | // To compute log10(1.mant), let f be the highest 6 bits including the hidden |
| 124 | // bit, and d be the difference (1.mant - f), i.e., the remaining 5 bits of |
| 125 | // the mantissa, then: |
| 126 | // log10(1.mant) = log10(f) + log10(1.mant / f) |
| 127 | // = log10(f) + log10(1 + d/f) |
| 128 | // since d/f is sufficiently small. |
| 129 | // We store log10(f) and 1/f in the lookup tables LOG10F_F and ONE_OVER_F_F |
| 130 | // respectively. |
| 131 | |
| 132 | int m = -FPBits::EXP_BIAS; |
| 133 | |
| 134 | // When x is subnormal, normalize it. |
| 135 | if ((x_u & FPBits::EXP_MASK) == 0U) { |
| 136 | // Can't pass an integer to fputil::cast directly. |
| 137 | constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN; |
| 138 | x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP)); |
| 139 | x_u = x_bits.uintval(); |
| 140 | m -= FPBits::FRACTION_LEN; |
| 141 | } |
| 142 | |
| 143 | uint16_t mant = x_bits.get_mantissa(); |
| 144 | // Leading 10 - 5 = 5 bits of the mantissa. |
| 145 | int f = mant >> 5; |
| 146 | // Unbiased exponent. |
| 147 | m += x_u >> FPBits::FRACTION_LEN; |
| 148 | |
| 149 | // Set bits to 1.mant instead of 2^m * 1.mant. |
| 150 | x_bits.set_biased_exponent(FPBits::EXP_BIAS); |
| 151 | float mant_f = x_bits.get_val(); |
| 152 | // v = 1.mant * 1/f - 1 = d/f |
| 153 | float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f); |
| 154 | |
| 155 | // Degree-3 minimax polynomial generated by Sollya with the following |
| 156 | // commands: |
| 157 | // > display = hexadecimal; |
| 158 | // > P = fpminimax(log10(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]); |
| 159 | // > x * P; |
| 160 | float log10p1_d_over_f = |
| 161 | v * fputil::polyeval(v, 0x1.bcb7bp-2f, -0x1.bce168p-3f, 0x1.28acb8p-3f); |
| 162 | // log10(1.mant) = log10(f) + log10(1 + d/f) |
| 163 | float log10_1_mant = LOG10F_F[f] + log10p1_d_over_f; |
| 164 | return fputil::cast<float16>( |
| 165 | fputil::multiply_add(static_cast<float>(m), LOG10F_2, log10_1_mant)); |
| 166 | } |
| 167 | |
| 168 | } // namespace LIBC_NAMESPACE_DECL |
| 169 | |