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