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

source code of libc/src/math/generic/exp10f_impl.h