| 1 | //===----------------------------------------------------------------------===// |
| 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 | /// \file |
| 9 | /// Implementation of the log10p1bf16 function. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1BF16_H |
| 14 | #define LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1BF16_H |
| 15 | |
| 16 | #include "hdr/errno_macros.h" |
| 17 | #include "hdr/fenv_macros.h" |
| 18 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 19 | #include "src/__support/FPUtil/FPBits.h" |
| 20 | #include "src/__support/FPUtil/bfloat16.h" |
| 21 | #include "src/__support/FPUtil/cast.h" |
| 22 | #include "src/__support/FPUtil/multiply_add.h" |
| 23 | #include "src/__support/FPUtil/polyeval.h" |
| 24 | #include "src/__support/macros/attributes.h" |
| 25 | #include "src/__support/macros/config.h" |
| 26 | #include "src/__support/macros/optimization.h" |
| 27 | |
| 28 | namespace LIBC_NAMESPACE_DECL { |
| 29 | namespace math { |
| 30 | |
| 31 | namespace log10p1bf16_internal { |
| 32 | // TODO: Reuse the lookup tables from expxf16_utils.h when they can be |
| 33 | // shared with the bfloat16 implementation. See |
| 34 | // https://github.com/llvm/llvm-project/pull/184283. |
| 35 | |
| 36 | // The top five mantissa bits select F = 1 + f * 2^-5, where f is in [0, 31]. |
| 37 | // BFloat16 has seven explicit mantissa bits, so the table uses five of those |
| 38 | // bits for lookup and the remaining two bits to refine the residual. |
| 39 | // Generated by Sollya with the following commands: |
| 40 | // > display = hexadecimal; |
| 41 | // > for f from 0 to 31 do print(round(log10(1 + f * 2^-5), SG, RN)); |
| 42 | LIBC_INLINE_VAR constexpr float LOG10F_F[32] = { |
| 43 | 0x0p+0f, 0x1.b5e908p-7f, 0x1.af5f92p-6f, 0x1.3ed11ap-5f, |
| 44 | 0x1.a30a9ep-5f, 0x1.02428cp-4f, 0x1.31b306p-4f, 0x1.5fe804p-4f, |
| 45 | 0x1.8cf184p-4f, 0x1.b8de4ep-4f, 0x1.e3bc1ap-4f, 0x1.06cbd6p-3f, |
| 46 | 0x1.1b3e72p-3f, 0x1.2f3b6ap-3f, 0x1.42c7e8p-3f, 0x1.55e8c6p-3f, |
| 47 | 0x1.68a288p-3f, 0x1.7af974p-3f, 0x1.8cf184p-3f, 0x1.9e8e7cp-3f, |
| 48 | 0x1.afd3e4p-3f, 0x1.c0c514p-3f, 0x1.d1653p-3f, 0x1.e1b734p-3f, |
| 49 | 0x1.f1bdeep-3f, 0x1.00be06p-2f, 0x1.087a08p-2f, 0x1.101432p-2f, |
| 50 | 0x1.178da6p-2f, 0x1.1ee778p-2f, 0x1.2622bp-2f, 0x1.2d404cp-2f, |
| 51 | }; |
| 52 | |
| 53 | // ONE_OVER_F_F[f] stores the reciprocal of the same representative F. It is |
| 54 | // used to form the residual v = M * (1 / F) - 1. |
| 55 | // Generated by Sollya with the following commands: |
| 56 | // > display = hexadecimal; |
| 57 | // > for f from 0 to 31 do print(round(1 / (1 + f * 2^-5), SG, RN)); |
| 58 | LIBC_INLINE_VAR constexpr float ONE_OVER_F_F[32] = { |
| 59 | 0x1p+0f, 0x1.f07c2p-1f, 0x1.e1e1e2p-1f, 0x1.d41d42p-1f, |
| 60 | 0x1.c71c72p-1f, 0x1.bacf92p-1f, 0x1.af286cp-1f, 0x1.a41a42p-1f, |
| 61 | 0x1.99999ap-1f, 0x1.8f9c18p-1f, 0x1.861862p-1f, 0x1.7d05f4p-1f, |
| 62 | 0x1.745d18p-1f, 0x1.6c16c2p-1f, 0x1.642c86p-1f, 0x1.5c9882p-1f, |
| 63 | 0x1.555556p-1f, 0x1.4e5e0ap-1f, 0x1.47ae14p-1f, 0x1.414142p-1f, |
| 64 | 0x1.3b13b2p-1f, 0x1.3521dp-1f, 0x1.2f684cp-1f, 0x1.29e412p-1f, |
| 65 | 0x1.24924ap-1f, 0x1.1f7048p-1f, 0x1.1a7b96p-1f, 0x1.15b1e6p-1f, |
| 66 | 0x1.111112p-1f, 0x1.0c9714p-1f, 0x1.08421p-1f, 0x1.041042p-1f, |
| 67 | }; |
| 68 | |
| 69 | // Generated by Sollya with the following commands: |
| 70 | // > display = hexadecimal; |
| 71 | // > round(log10(2), SG, RN); |
| 72 | LIBC_INLINE_VAR constexpr float LOG10F_2 = 0x1.344136p-2f; |
| 73 | |
| 74 | } // namespace log10p1bf16_internal |
| 75 | |
| 76 | LIBC_INLINE bfloat16 log10p1bf16(bfloat16 x) { |
| 77 | using namespace log10p1bf16_internal; |
| 78 | using FPBits = fputil::FPBits<bfloat16>; |
| 79 | FPBits x_bits(x); |
| 80 | uint16_t x_u = x_bits.uintval(); |
| 81 | uint16_t x_abs = x_u & 0x7fffU; |
| 82 | float xf = static_cast<float>(x); |
| 83 | |
| 84 | // Handle special values; for |x| <= 0.125 (bits <= 0x3e00), use a direct |
| 85 | // polynomial to avoid cancellation in the range-reduction residual. |
| 86 | if (LIBC_UNLIKELY(x_abs <= 0x3e00U || x_abs >= 0x7f80U)) { |
| 87 | // log10p1(NaN) = NaN |
| 88 | if (x_bits.is_nan()) { |
| 89 | if (x_bits.is_signaling_nan()) { |
| 90 | fputil::raise_except_if_required(FE_INVALID); |
| 91 | return FPBits::quiet_nan().get_val(); |
| 92 | } |
| 93 | return x; |
| 94 | } |
| 95 | |
| 96 | // log10p1(+/-0) = +/-0 |
| 97 | if (x_abs == 0U) |
| 98 | return x; |
| 99 | |
| 100 | // log10p1(+inf) = +inf |
| 101 | if (x_u == 0x7f80U) |
| 102 | return x; |
| 103 | |
| 104 | // log10p1(-inf) = NaN |
| 105 | if (x_u == 0xff80U) { |
| 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 | // For tiny |x|, log10(1 + x) is approximately x / ln(10). |
| 112 | // Avoid subnormal intermediate products in the polynomial evaluation. |
| 113 | if (x_abs < 0x0d80U) |
| 114 | return fputil::cast<bfloat16>(x: xf * 0x1.bcb7b2p-2f); |
| 115 | // Degree-5 minimax polynomial generated by Sollya with the following |
| 116 | // commands: |
| 117 | // > display = hexadecimal; |
| 118 | // > f = log(1 + x) / (x * log(10)); |
| 119 | // > P = fpminimax(f, 4, [|SG, SG, SG, SG, SG|], [-1/8; 1/8]); |
| 120 | // > x * P; |
| 121 | return fputil::cast<bfloat16>( |
| 122 | x: xf * fputil::polyeval(x: xf, a0: 0x1.bcb7b2p-2f, a: -0x1.bcb4cp-3f, |
| 123 | a: 0x1.2875bcp-3f, a: -0x1.c2946ep-4f, a: 0x1.69da2p-4f)); |
| 124 | } |
| 125 | |
| 126 | // x = -1 has bits 0xbf80 and is the domain boundary log10(1 + x) = -inf. |
| 127 | if (LIBC_UNLIKELY(x_u == 0xbf80U)) { |
| 128 | fputil::raise_except_if_required(FE_DIVBYZERO); |
| 129 | return FPBits::inf(sign: Sign::NEG).get_val(); |
| 130 | } |
| 131 | |
| 132 | // log10p1(x) is undefined for x < -1; return NaN and raise FE_INVALID. |
| 133 | if (LIBC_UNLIKELY(x_u > 0xbf80U)) { |
| 134 | fputil::set_errno_if_required(EDOM); |
| 135 | fputil::raise_except_if_required(FE_INVALID); |
| 136 | return FPBits::quiet_nan().get_val(); |
| 137 | } |
| 138 | |
| 139 | // log10(1 + 9) = 1 and log10(1 + 99) = 2 exactly; return them directly. |
| 140 | if (LIBC_UNLIKELY(x_u == 0x4110U)) |
| 141 | return FPBits(static_cast<uint16_t>(0x3f80U)).get_val(); |
| 142 | if (LIBC_UNLIKELY(x_u == 0x42c6U)) |
| 143 | return FPBits(static_cast<uint16_t>(0x4000U)).get_val(); |
| 144 | |
| 145 | // For y = 1 + x, write y = 2^e * M, where M is in [1, 2), and select |
| 146 | // F = 1 + f * 2^-5 from the lookup tables. Then: |
| 147 | // log10(y) = e * log10(2) + log10(F) + log10(M / F). |
| 148 | // The residual v = M / F - 1 is computed using the reciprocal table. |
| 149 | float y = 1.0f + xf; |
| 150 | using FPBitsFloat = fputil::FPBits<float>; |
| 151 | FPBitsFloat y_bits(y); |
| 152 | |
| 153 | int e = y_bits.get_exponent(); |
| 154 | y_bits.set_biased_exponent(FPBitsFloat::EXP_BIAS); |
| 155 | float mant_f = y_bits.get_val(); |
| 156 | |
| 157 | int f = y_bits.get_mantissa() >> (FPBitsFloat::FRACTION_LEN - 5); |
| 158 | float v = fputil::multiply_sub(x: mant_f, y: ONE_OVER_F_F[f], z: 1.0f); |
| 159 | // Degree-3 minimax polynomial generated by Sollya with the following |
| 160 | // commands: |
| 161 | // > display = hexadecimal; |
| 162 | // > P = fpminimax(log10(1 + x) / x, 2, [|SG, SG, SG|], [-2^-5; 2^-5]); |
| 163 | // > x * P; |
| 164 | float log10p1_d_over_f = |
| 165 | v * fputil::polyeval(x: v, a0: 0x1.bcb7bp-2f, a: -0x1.bce168p-3f, a: 0x1.28acb8p-3f); |
| 166 | float log10_1_mant = LOG10F_F[f] + log10p1_d_over_f; |
| 167 | return fputil::cast<bfloat16>( |
| 168 | x: fputil::multiply_add(x: static_cast<float>(e), y: LOG10F_2, z: log10_1_mant)); |
| 169 | } |
| 170 | |
| 171 | } // namespace math |
| 172 | } // namespace LIBC_NAMESPACE_DECL |
| 173 | |
| 174 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1BF16_H |
| 175 | |