| //===----------------------------------------------------------------------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| /// \file | |
| /// Implementation of the log10p1bf16 function. | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1BF16_H | |
| #define LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1BF16_H | |
| #include "hdr/errno_macros.h" | |
| #include "hdr/fenv_macros.h" | |
| #include "src/__support/FPUtil/FEnvImpl.h" | |
| #include "src/__support/FPUtil/FPBits.h" | |
| #include "src/__support/FPUtil/bfloat16.h" | |
| #include "src/__support/FPUtil/cast.h" | |
| #include "src/__support/FPUtil/multiply_add.h" | |
| #include "src/__support/FPUtil/polyeval.h" | |
| #include "src/__support/macros/attributes.h" | |
| #include "src/__support/macros/config.h" | |
| #include "src/__support/macros/optimization.h" | |
| namespace LIBC_NAMESPACE_DECL { | |
| namespace math { | |
| namespace log10p1bf16_internal { | |
| // TODO: Reuse the lookup tables from expxf16_utils.h when they can be | |
| // shared with the bfloat16 implementation. See | |
| // https://github.com/llvm/llvm-project/pull/184283. | |
| // The top five mantissa bits select F = 1 + f * 2^-5, where f is in [0, 31]. | |
| // BFloat16 has seven explicit mantissa bits, so the table uses five of those | |
| // bits for lookup and the remaining two bits to refine the residual. | |
| // Generated by Sollya with the following commands: | |
| // > display = hexadecimal; | |
| // > for f from 0 to 31 do print(round(log10(1 + f * 2^-5), SG, RN)); | |
| LIBC_INLINE_VAR constexpr float LOG10F_F[32] = { | |
| 0x0p+0f, 0x1.b5e908p-7f, 0x1.af5f92p-6f, 0x1.3ed11ap-5f, | |
| 0x1.a30a9ep-5f, 0x1.02428cp-4f, 0x1.31b306p-4f, 0x1.5fe804p-4f, | |
| 0x1.8cf184p-4f, 0x1.b8de4ep-4f, 0x1.e3bc1ap-4f, 0x1.06cbd6p-3f, | |
| 0x1.1b3e72p-3f, 0x1.2f3b6ap-3f, 0x1.42c7e8p-3f, 0x1.55e8c6p-3f, | |
| 0x1.68a288p-3f, 0x1.7af974p-3f, 0x1.8cf184p-3f, 0x1.9e8e7cp-3f, | |
| 0x1.afd3e4p-3f, 0x1.c0c514p-3f, 0x1.d1653p-3f, 0x1.e1b734p-3f, | |
| 0x1.f1bdeep-3f, 0x1.00be06p-2f, 0x1.087a08p-2f, 0x1.101432p-2f, | |
| 0x1.178da6p-2f, 0x1.1ee778p-2f, 0x1.2622bp-2f, 0x1.2d404cp-2f, | |
| }; | |
| // ONE_OVER_F_F[f] stores the reciprocal of the same representative F. It is | |
| // used to form the residual v = M * (1 / F) - 1. | |
| // Generated by Sollya with the following commands: | |
| // > display = hexadecimal; | |
| // > for f from 0 to 31 do print(round(1 / (1 + f * 2^-5), SG, RN)); | |
| LIBC_INLINE_VAR constexpr float ONE_OVER_F_F[32] = { | |
| 0x1p+0f, 0x1.f07c2p-1f, 0x1.e1e1e2p-1f, 0x1.d41d42p-1f, | |
| 0x1.c71c72p-1f, 0x1.bacf92p-1f, 0x1.af286cp-1f, 0x1.a41a42p-1f, | |
| 0x1.99999ap-1f, 0x1.8f9c18p-1f, 0x1.861862p-1f, 0x1.7d05f4p-1f, | |
| 0x1.745d18p-1f, 0x1.6c16c2p-1f, 0x1.642c86p-1f, 0x1.5c9882p-1f, | |
| 0x1.555556p-1f, 0x1.4e5e0ap-1f, 0x1.47ae14p-1f, 0x1.414142p-1f, | |
| 0x1.3b13b2p-1f, 0x1.3521dp-1f, 0x1.2f684cp-1f, 0x1.29e412p-1f, | |
| 0x1.24924ap-1f, 0x1.1f7048p-1f, 0x1.1a7b96p-1f, 0x1.15b1e6p-1f, | |
| 0x1.111112p-1f, 0x1.0c9714p-1f, 0x1.08421p-1f, 0x1.041042p-1f, | |
| }; | |
| // Generated by Sollya with the following commands: | |
| // > display = hexadecimal; | |
| // > round(log10(2), SG, RN); | |
| LIBC_INLINE_VAR constexpr float LOG10F_2 = 0x1.344136p-2f; | |
| } // namespace log10p1bf16_internal | |
| LIBC_INLINE bfloat16 log10p1bf16(bfloat16 x) { | |
| using namespace log10p1bf16_internal; | |
| using FPBits = fputil::FPBits<bfloat16>; | |
| FPBits x_bits(x); | |
| uint16_t x_u = x_bits.uintval(); | |
| uint16_t x_abs = x_u & 0x7fffU; | |
| float xf = static_cast<float>(x); | |
| // Handle special values; for |x| <= 0.125 (bits <= 0x3e00), use a direct | |
| // polynomial to avoid cancellation in the range-reduction residual. | |
| if (LIBC_UNLIKELY(x_abs <= 0x3e00U || x_abs >= 0x7f80U)) { | |
| // log10p1(NaN) = NaN | |
| if (x_bits.is_nan()) { | |
| if (x_bits.is_signaling_nan()) { | |
| fputil::raise_except_if_required(FE_INVALID); | |
| return FPBits::quiet_nan().get_val(); | |
| } | |
| return x; | |
| } | |
| // log10p1(+/-0) = +/-0 | |
| if (x_abs == 0U) | |
| return x; | |
| // log10p1(+inf) = +inf | |
| if (x_u == 0x7f80U) | |
| return x; | |
| // log10p1(-inf) = NaN | |
| if (x_u == 0xff80U) { | |
| fputil::set_errno_if_required(EDOM); | |
| fputil::raise_except_if_required(FE_INVALID); | |
| return FPBits::quiet_nan().get_val(); | |
| } | |
| // For tiny |x|, log10(1 + x) is approximately x / ln(10). | |
| // Avoid subnormal intermediate products in the polynomial evaluation. | |
| if (x_abs < 0x0d80U) | |
| return fputil::cast<bfloat16>(xf * 0x1.bcb7b2p-2f); | |
| // Degree-5 minimax polynomial generated by Sollya with the following | |
| // commands: | |
| // > display = hexadecimal; | |
| // > f = log(1 + x) / (x * log(10)); | |
| // > P = fpminimax(f, 4, [|SG, SG, SG, SG, SG|], [-1/8; 1/8]); | |
| // > x * P; | |
| return fputil::cast<bfloat16>( | |
| xf * fputil::polyeval(xf, 0x1.bcb7b2p-2f, -0x1.bcb4cp-3f, | |
| 0x1.2875bcp-3f, -0x1.c2946ep-4f, 0x1.69da2p-4f)); | |
| } | |
| // x = -1 has bits 0xbf80 and is the domain boundary log10(1 + x) = -inf. | |
| if (LIBC_UNLIKELY(x_u == 0xbf80U)) { | |
| fputil::raise_except_if_required(FE_DIVBYZERO); | |
| return FPBits::inf(Sign::NEG).get_val(); | |
| } | |
| // log10p1(x) is undefined for x < -1; return NaN and raise FE_INVALID. | |
| if (LIBC_UNLIKELY(x_u > 0xbf80U)) { | |
| fputil::set_errno_if_required(EDOM); | |
| fputil::raise_except_if_required(FE_INVALID); | |
| return FPBits::quiet_nan().get_val(); | |
| } | |
| // log10(1 + 9) = 1 and log10(1 + 99) = 2 exactly; return them directly. | |
| if (LIBC_UNLIKELY(x_u == 0x4110U)) | |
| return FPBits(static_cast<uint16_t>(0x3f80U)).get_val(); | |
| if (LIBC_UNLIKELY(x_u == 0x42c6U)) | |
| return FPBits(static_cast<uint16_t>(0x4000U)).get_val(); | |
| // For y = 1 + x, write y = 2^e * M, where M is in [1, 2), and select | |
| // F = 1 + f * 2^-5 from the lookup tables. Then: | |
| // log10(y) = e * log10(2) + log10(F) + log10(M / F). | |
| // The residual v = M / F - 1 is computed using the reciprocal table. | |
| float y = 1.0f + xf; | |
| using FPBitsFloat = fputil::FPBits<float>; | |
| FPBitsFloat y_bits(y); | |
| int e = y_bits.get_exponent(); | |
| y_bits.set_biased_exponent(FPBitsFloat::EXP_BIAS); | |
| float mant_f = y_bits.get_val(); | |
| int f = y_bits.get_mantissa() >> (FPBitsFloat::FRACTION_LEN - 5); | |
| float v = fputil::multiply_sub(mant_f, ONE_OVER_F_F[f], 1.0f); | |
| // Degree-3 minimax polynomial generated by Sollya with the following | |
| // commands: | |
| // > display = hexadecimal; | |
| // > P = fpminimax(log10(1 + x) / x, 2, [|SG, SG, SG|], [-2^-5; 2^-5]); | |
| // > x * P; | |
| float log10p1_d_over_f = | |
| v * fputil::polyeval(v, 0x1.bcb7bp-2f, -0x1.bce168p-3f, 0x1.28acb8p-3f); | |
| float log10_1_mant = LOG10F_F[f] + log10p1_d_over_f; | |
| return fputil::cast<bfloat16>( | |
| fputil::multiply_add(static_cast<float>(e), LOG10F_2, log10_1_mant)); | |
| } | |
| } // namespace math | |
| } // namespace LIBC_NAMESPACE_DECL | |
| #endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1BF16_H |