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/FPBits.h"
18#include "src/__support/FPUtil/except_value_utils.h"
19#include "src/__support/FPUtil/multiply_add.h"
20#include "src/__support/FPUtil/rounding_mode.h"
21#include "src/__support/macros/attributes.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
46LIBC_COLD LIBC_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 return fputil::set_overflow<float>();
58 // x is +inf or nan
59 return x + FPBits::inf().get_val();
60 }
61
62 // When |x| <= log10(2)*2^-6
63 if (LIBC_UNLIKELY(x_abs <= 0x3b9a'209bU))
64 return static_cast<float>(Exp10Base::powb_lo(dx: x));
65
66 // Exceptional value.
67 if (LIBC_UNLIKELY(x_u == 0x3d14'd956U)) { // x = 0x1.29b2acp-5f
68#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
69 if (fputil::fenv_is_round_up())
70 return 0x1.1657c4p+0f;
71#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
72 }
73
74 // Exact outputs when x = 1, 2, ..., 10.
75 // Quick check mask: 0x800f'ffffU = ~(bits of 1.0f | ... | bits of 10.0f)
76 if (LIBC_UNLIKELY((x_u & 0x800f'ffffU) == 0)) {
77 switch (x_u) {
78 case 0x3f800000U: // x = 1.0f
79 return 10.0f;
80 case 0x40000000U: // x = 2.0f
81 return 100.0f;
82 case 0x40400000U: // x = 3.0f
83 return 1'000.0f;
84 case 0x40800000U: // x = 4.0f
85 return 10'000.0f;
86 case 0x40a00000U: // x = 5.0f
87 return 100'000.0f;
88 case 0x40c00000U: // x = 6.0f
89 return 1'000'000.0f;
90 case 0x40e00000U: // x = 7.0f
91 return 10'000'000.0f;
92 case 0x41000000U: // x = 8.0f
93 return 100'000'000.0f;
94 case 0x41100000U: // x = 9.0f
95 return 1'000'000'000.0f;
96 case 0x41200000U: // x = 10.0f
97 return 10'000'000'000.0f;
98 }
99 }
100
101 // When |x| >= log10(2^128), or x is nan
102 if (LIBC_UNLIKELY(x_abs >= 0x421a'209bU)) {
103 // When x < log10(2^-150) or nan
104 if (x_u > 0xc234'9e35U) {
105 // exp(-Inf) = 0
106 if (xbits.is_inf())
107 return 0.0f;
108 // exp(nan) = nan
109 if (xbits.is_nan())
110 return x;
111 return fputil::set_underflow<float>();
112 }
113 }
114
115 return exp10f_mid(x);
116}
117
118LIBC_INLINE float exp10f(float x) {
119 using FPBits = fputil::FPBits<float>;
120 FPBits xbits(x);
121
122 uint32_t x_u = xbits.uintval();
123 uint32_t x_abs = x_u & 0x7fff'ffffU;
124
125 if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
126 if (LIBC_UNLIKELY(x_u == 0xb25e'5bd9U)) { // x = -0x1.bcb7b2p-27f
127#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
128 return 0x1.fffffep-1f;
129#else // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
130 if (fputil::fenv_is_round_to_nearest())
131 return 0x1.fffffep-1f;
132#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
133 }
134 // |x| < 2^-25
135 // 10^x ~ 1 + log(10) * x
136 return fputil::multiply_add(x, y: 0x1.26bb1cp+1f, z: 1.0f);
137 }
138
139 if (LIBC_LIKELY(
140 x_abs > 0x3b9a'209bU &&
141 (x_u < 0x421a'209bU || (xbits.is_neg() && x_u <= 0xc234'9e35U)) &&
142 x_u != 0x3d14'd956U && (x_u & 0x800f'ffffU) != 0))
143 return exp10f_mid(x);
144
145 return exp10f_slow(x);
146}
147
148} // namespace double_eval
149} // namespace math
150} // namespace LIBC_NAMESPACE_DECL
151
152#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP10F_DOUBLE_EVAL_H
153