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