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///
9/// \file
10/// Double-precision implementation of exp10f.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_DOUBLE_EVAL_H
15#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_DOUBLE_EVAL_H
16
17#include "src/__support/FPUtil/FEnvImpl.h"
18#include "src/__support/FPUtil/FPBits.h"
19#include "src/__support/FPUtil/multiply_add.h"
20#include "src/__support/FPUtil/nearest_integer.h"
21#include "src/__support/FPUtil/rounding_mode.h"
22#include "src/__support/macros/config.h"
23#include "src/__support/macros/optimization.h"
24#include "src/__support/math/exp10f_utils.h"
25
26namespace LIBC_NAMESPACE_DECL {
27namespace math {
28namespace double_eval {
29
30LIBC_INLINE float exp10f_mid(float x) {
31 // Range reduction: 10^x = 2^(mid + hi) * 10^lo
32 auto rr = exp_b_range_reduc<Exp10Base>(x);
33
34 // The low part is approximated by a degree-5 minimax polynomial.
35 using fputil::multiply_add;
36 double lo2 = rr.lo * rr.lo;
37 double c0 = multiply_add(x: rr.lo, y: Exp10Base::COEFFS[0], z: 1.0);
38 double c1 = multiply_add(x: rr.lo, y: Exp10Base::COEFFS[2], z: Exp10Base::COEFFS[1]);
39 double c2 = multiply_add(x: rr.lo, y: Exp10Base::COEFFS[4], z: Exp10Base::COEFFS[3]);
40 double p = multiply_add(x: lo2, y: c2, z: c1);
41 // 10^x = 2^(mid + hi) * 10^lo
42 // ~ mh * (1 + COEFFS[0] * lo + ... + COEFFS[4] * lo^5)
43 return static_cast<float>(multiply_add(x: p, y: lo2 * rr.mh, z: c0 * rr.mh));
44}
45
46[[gnu::cold, gnu::noinline]] LIBC_INLINE float exp10f_slow(float x) {
47 using FPBits = fputil::FPBits<float>;
48 FPBits xbits(x);
49
50 uint32_t x_u = xbits.uintval();
51 uint32_t x_abs = x_u & 0x7fff'ffffU;
52
53 // x >= log10(2^128), or positive NaN.
54 if (LIBC_UNLIKELY(x_u >= 0x421a'209bU && x_u < 0x8000'0000U)) {
55 // x is finite.
56 if (x_u < 0x7f80'0000U) {
57#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
58 int rounding = fputil::quick_get_round();
59 if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
60 return FPBits::max_normal().get_val();
61#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
62
63 fputil::set_errno_if_required(ERANGE);
64 fputil::raise_except_if_required(FE_OVERFLOW);
65 }
66 // x is +inf or nan
67 return x + FPBits::inf().get_val();
68 }
69
70 // When |x| <= log10(2)*2^-6
71 if (LIBC_UNLIKELY(x_abs <= 0x3b9a'209bU))
72 return static_cast<float>(Exp10Base::powb_lo(dx: x));
73
74 // Exceptional value.
75 if (LIBC_UNLIKELY(x_u == 0x3d14'd956U)) { // x = 0x1.29b2acp-5f
76#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
77 if (fputil::fenv_is_round_up())
78 return 0x1.1657c4p+0f;
79#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
80 }
81
82 // Exact outputs when x = 1, 2, ..., 10.
83 // Quick check mask: 0x800f'ffffU = ~(bits of 1.0f | ... | bits of 10.0f)
84 if (LIBC_UNLIKELY((x_u & 0x800f'ffffU) == 0)) {
85 switch (x_u) {
86 case 0x3f800000U: // x = 1.0f
87 return 10.0f;
88 case 0x40000000U: // x = 2.0f
89 return 100.0f;
90 case 0x40400000U: // x = 3.0f
91 return 1'000.0f;
92 case 0x40800000U: // x = 4.0f
93 return 10'000.0f;
94 case 0x40a00000U: // x = 5.0f
95 return 100'000.0f;
96 case 0x40c00000U: // x = 6.0f
97 return 1'000'000.0f;
98 case 0x40e00000U: // x = 7.0f
99 return 10'000'000.0f;
100 case 0x41000000U: // x = 8.0f
101 return 100'000'000.0f;
102 case 0x41100000U: // x = 9.0f
103 return 1'000'000'000.0f;
104 case 0x41200000U: // x = 10.0f
105 return 10'000'000'000.0f;
106 }
107 }
108
109 // When |x| >= log10(2^128), or x is nan
110 if (LIBC_UNLIKELY(x_abs >= 0x421a'209bU)) {
111 // When x < log10(2^-150) or nan
112 if (x_u > 0xc234'9e35U) {
113 // exp(-Inf) = 0
114 if (xbits.is_inf())
115 return 0.0f;
116 // exp(nan) = nan
117 if (xbits.is_nan())
118 return x;
119#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
120 if (fputil::fenv_is_round_up())
121 return FPBits::min_subnormal().get_val();
122#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
123 fputil::set_errno_if_required(ERANGE);
124 fputil::raise_except_if_required(FE_UNDERFLOW);
125 return 0.0f;
126 }
127 }
128
129 return exp10f_mid(x);
130}
131
132LIBC_INLINE float exp10f(float x) {
133 using FPBits = fputil::FPBits<float>;
134 FPBits xbits(x);
135
136 uint32_t x_u = xbits.uintval();
137 uint32_t x_abs = x_u & 0x7fff'ffffU;
138
139 if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
140 if (LIBC_UNLIKELY(x_u == 0xb25e'5bd9U)) { // x = -0x1.bcb7b2p-27f
141#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
142 return 0x1.fffffep-1f;
143#else // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
144 if (fputil::fenv_is_round_to_nearest())
145 return 0x1.fffffep-1f;
146#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
147 }
148 // |x| < 2^-25
149 // 10^x ~ 1 + log(10) * x
150 return fputil::multiply_add(x, y: 0x1.26bb1cp+1f, z: 1.0f);
151 }
152
153 if (LIBC_LIKELY(
154 x_abs > 0x3b9a'209bU &&
155 (x_u < 0x421a'209bU || (xbits.is_neg() && x_u <= 0xc234'9e35U)) &&
156 x_u != 0x3d14'd956U && (x_u & 0x800f'ffffU) != 0))
157 return exp10f_mid(x);
158
159 return exp10f_slow(x);
160}
161
162} // namespace double_eval
163} // namespace math
164} // namespace LIBC_NAMESPACE_DECL
165
166#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_DOUBLE_EVAL_H
167