Warning: This file is not a C or C++ file. It does not have highlighting.

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

Warning: This file is not a C or C++ file. It does not have highlighting.

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

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