| //===----------------------------------------------------------------------===// | |
| // | |
| // 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 | |
| /// Integer-only implementation of exp10f. | |
| /// | |
| //===----------------------------------------------------------------------===// | |
| #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_INTEGER_EVAL_H | |
| #define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_INTEGER_EVAL_H | |
| #include "hdr/fenv_macros.h" | |
| #include "hdr/stdint_proxy.h" | |
| #include "src/__support/CPP/bit.h" | |
| #include "src/__support/FPUtil/FPBits.h" | |
| #include "src/__support/frac64.h" | |
| #include "src/__support/macros/attributes.h" | |
| #include "src/__support/macros/config.h" | |
| #include "src/__support/macros/optimization.h" | |
| #include "src/__support/math/exp2f_integer_utils.h" | |
| namespace LIBC_NAMESPACE_DECL { | |
| namespace math { | |
| namespace static_rounding { | |
| // Constants generated by Sollya with: | |
| // > display = hexadecimal; | |
| // > round(log2(10) * 2^62, 64, RN); | |
| // LSB(LOG2_10) = 2^-62 | |
| LIBC_INLINE_VAR constexpr Frac64 LOG2_10 = Frac64(0xd49a'784b'cd1b'8afe); | |
| // IEEE 754 single-precision bit representations of exact powers 10^1 .. 10^10. | |
| LIBC_INLINE_VAR constexpr uint32_t EXP10_POWERS[10] = { | |
| 0x4120'0000U, // 10^1 | |
| 0x42c8'0000U, // 10^2 | |
| 0x447a'0000U, // 10^3 | |
| 0x461c'4000U, // 10^4 | |
| 0x47c3'5000U, // 10^5 | |
| 0x4974'2400U, // 10^6 | |
| 0x4b18'9680U, // 10^7 | |
| 0x4cbe'bc20U, // 10^8 | |
| 0x4e6e'6b28U, // 10^9 | |
| 0x5015'02f9U, // 10^10 | |
| }; | |
| LIBC_ALWAYS_INLINE uint32_t exp10f_bits(uint32_t x_u, | |
| [[maybe_unused]] int rounding) { | |
| using FPBits = fputil::FPBits<float>; | |
| bool is_neg = (x_u >> 31) != 0; | |
| uint32_t x_abs = x_u & 0x7fff'ffffU; | |
| uint32_t x_e = x_abs >> FPBits::FRACTION_LEN; | |
| // When |x| < 2^-26 (x_e <= 100) or |x| >= 32 (x_e >= 132): | |
| if (LIBC_UNLIKELY(x_e - 101U >= 31U)) { | |
| // |x| < 2^-26 | |
| if (x_e <= 100U) { | |
| #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| if (LIBC_UNLIKELY(rounding != FE_TONEAREST)) { | |
| if (rounding == FE_UPWARD && static_cast<int32_t>(x_u) > 0) // x > +0.0f | |
| return 0x3f80'0001U; // 0x1.000002p+0f (1.0f + 2^-23f) | |
| if ((rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) && | |
| x_u > 0x8000'0000U) // x < -0.0f | |
| return 0x3f7f'ffffU; // 0x1.fffffep-1f (1.0f - 2^-24f) | |
| return 0x3f80'0000U; // 1.0f | |
| } | |
| #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| // 10^x rounds to 1.0f (0x3f80'0000U) or 1.0f - 2^-24f (0x3f7f'ffffU) when | |
| // x <= log10(1 - 2^-25) ~ -0x1.bcb7b2p-27f (0xb25e'5bd9U). | |
| return 0x3f80'0000U - static_cast<uint32_t>(x_u >= 0xb25e'5bd9U); | |
| } | |
| // exp10(nan) = nan | |
| if (x_abs > 0x7f80'0000U) // |x| > +inf (x is NaN) | |
| return x_u | 0x0040'0000U; // quiet NaN | |
| // x >= 0x1.344136p+5f > log10(2^128) or +inf | |
| if (!is_neg && x_u >= 0x421a'209bU) { | |
| #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| if ((rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO) && | |
| x_u < 0x7f80'0000U) // x < +inf | |
| return 0x7f7f'ffffU; // 0x1.fffffep+127f (FLT_MAX) | |
| #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| return 0x7f80'0000U; // +inf | |
| } | |
| // x <= -0x1.693c6cp+5f < log10(2^-150) or -inf | |
| if (x_u >= 0xc234'9e36U) { | |
| #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| if (rounding == FE_UPWARD && x_u < 0xff80'0000U) // x > -inf | |
| return 0x0000'0001U; // 0x1.0p-149f (FLT_TRUE_MIN) | |
| #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| return 0x0000'0000U; // +0.0f | |
| } | |
| } | |
| #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| // Exact outputs when x = 1, 2, ..., 10 in directed rounding modes: | |
| // - (x_u << 12) == 0 filters out inputs with nonzero low 20 fraction bits. | |
| // - 0x3f8 is the high 12 bits of 1.0f, and 0x3f8 + 0x1a = 0x412 is 10.0f. | |
| // - scaled = (8 | (d & 7)) << (d >> 3) computes 8 * x; (scaled & 7) == 0 | |
| // checks that the 3 fractional bits of x are zero, giving n = scaled >> 3. | |
| if (rounding != FE_TONEAREST && LIBC_UNLIKELY((x_u << 12) == 0)) { | |
| uint32_t d = (x_u >> 20) - 0x3f8U; | |
| if (d <= 0x1aU) { | |
| uint32_t scaled = (8U | (d & 7U)) << (d >> 3); | |
| if ((scaled & 7U) == 0U) | |
| return EXP10_POWERS[(scaled >> 3) - 1U]; | |
| } | |
| } | |
| #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY | |
| // Range reduction: | |
| // k = floor(x * log2(10)) | |
| // x * log2(10) = k + u, with u in [0, 1) | |
| // 10^x = 2^(k + u) = 2^k * 2^u | |
| uint64_t u_bits = exp_b_scale(x_u, x_e, LOG2_10, 132U); | |
| return exp2f_eval_bits(u_bits, is_neg, rounding); | |
| } | |
| // Statically rounded, no-except implementation of exp10f using integer-only | |
| // arithmetic. | |
| LIBC_ALWAYS_INLINE float exp10f(float x, [[maybe_unused]] int rounding) { | |
| return cpp::bit_cast<float>( | |
| exp10f_bits(cpp::bit_cast<uint32_t>(x), rounding)); | |
| } | |
| } // namespace static_rounding | |
| namespace integer_eval { | |
| LIBC_ALWAYS_INLINE float exp10f(float x) { | |
| return static_rounding::exp10f(x, FE_TONEAREST); | |
| } | |
| } // namespace integer_eval | |
| } // namespace math | |
| } // namespace LIBC_NAMESPACE_DECL | |
| #endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_INTEGER_EVAL_H |