1//===-- Half-precision 10^x - 1 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#include "src/math/exp10m1f16.h"
10#include "expxf16.h"
11#include "hdr/errno_macros.h"
12#include "hdr/fenv_macros.h"
13#include "src/__support/FPUtil/FEnvImpl.h"
14#include "src/__support/FPUtil/FPBits.h"
15#include "src/__support/FPUtil/PolyEval.h"
16#include "src/__support/FPUtil/cast.h"
17#include "src/__support/FPUtil/except_value_utils.h"
18#include "src/__support/FPUtil/multiply_add.h"
19#include "src/__support/FPUtil/rounding_mode.h"
20#include "src/__support/common.h"
21#include "src/__support/macros/config.h"
22#include "src/__support/macros/optimization.h"
23#include "src/__support/macros/properties/cpu_features.h"
24
25namespace LIBC_NAMESPACE_DECL {
26
27#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
28static constexpr fputil::ExceptValues<float16, 3> EXP10M1F16_EXCEPTS_LO = {{
29 // (input, RZ output, RU offset, RD offset, RN offset)
30 // x = 0x1.5c4p-4, exp10m1f16(x) = 0x1.bacp-3 (RZ)
31 {0x2d71U, 0x32ebU, 1U, 0U, 0U},
32 // x = -0x1.5ep-13, exp10m1f16(x) = -0x1.92cp-12 (RZ)
33 {0x8978U, 0x8e4bU, 0U, 1U, 0U},
34 // x = -0x1.e2p-10, exp10m1f16(x) = -0x1.14cp-8 (RZ)
35 {0x9788U, 0x9c53U, 0U, 1U, 0U},
36}};
37
38#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
39static constexpr size_t N_EXP10M1F16_EXCEPTS_HI = 3;
40#else
41static constexpr size_t N_EXP10M1F16_EXCEPTS_HI = 6;
42#endif
43
44static constexpr fputil::ExceptValues<float16, N_EXP10M1F16_EXCEPTS_HI>
45 EXP10M1F16_EXCEPTS_HI = {{
46 // (input, RZ output, RU offset, RD offset, RN offset)
47 // x = 0x1.8f4p-2, exp10m1f16(x) = 0x1.744p+0 (RZ)
48 {0x363dU, 0x3dd1U, 1U, 0U, 0U},
49 // x = 0x1.95cp-2, exp10m1f16(x) = 0x1.7d8p+0 (RZ)
50 {0x3657U, 0x3df6U, 1U, 0U, 0U},
51 // x = 0x1.d04p-2, exp10m1f16(x) = 0x1.d7p+0 (RZ)
52 {0x3741U, 0x3f5cU, 1U, 0U, 1U},
53#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
54 // x = 0x1.0cp+1, exp10m1f16(x) = 0x1.ec4p+6 (RZ)
55 {0x4030U, 0x57b1U, 1U, 0U, 1U},
56 // x = 0x1.1b8p+1, exp10m1f16(x) = 0x1.45cp+7 (RZ)
57 {0x406eU, 0x5917U, 1U, 0U, 1U},
58 // x = 0x1.2f4p+2, exp10m1f16(x) = 0x1.ab8p+15 (RZ)
59 {0x44bdU, 0x7aaeU, 1U, 0U, 1U},
60#endif
61 }};
62#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
63
64LLVM_LIBC_FUNCTION(float16, exp10m1f16, (float16 x)) {
65 using FPBits = fputil::FPBits<float16>;
66 FPBits x_bits(x);
67
68 uint16_t x_u = x_bits.uintval();
69 uint16_t x_abs = x_u & 0x7fffU;
70
71 // When |x| <= 2^(-3), or |x| >= 11 * log10(2), or x is NaN.
72 if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x429fU)) {
73 // exp10m1(NaN) = NaN
74 if (x_bits.is_nan()) {
75 if (x_bits.is_signaling_nan()) {
76 fputil::raise_except_if_required(FE_INVALID);
77 return FPBits::quiet_nan().get_val();
78 }
79
80 return x;
81 }
82
83 // When x >= 16 * log10(2).
84 if (x_u >= 0x44d1U && x_bits.is_pos()) {
85 // exp10m1(+inf) = +inf
86 if (x_bits.is_inf())
87 return FPBits::inf().get_val();
88
89 switch (fputil::quick_get_round()) {
90 case FE_TONEAREST:
91 case FE_UPWARD:
92 fputil::set_errno_if_required(ERANGE);
93 fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
94 return FPBits::inf().get_val();
95 default:
96 return FPBits::max_normal().get_val();
97 }
98 }
99
100 // When x < -11 * log10(2).
101 if (x_u > 0xc29fU) {
102 // exp10m1(-inf) = -1
103 if (x_bits.is_inf())
104 return FPBits::one(Sign::NEG).get_val();
105
106 // When x >= -0x1.ce4p+1, round(10^x - 1, HP, RN) = -0x1.ffcp-1.
107 if (x_u <= 0xc339U) {
108 return fputil::round_result_slightly_down(
109 fputil::cast<float16>(-0x1.ffcp-1));
110 }
111
112 // When x < -0x1.ce4p+1, round(10^x - 1, HP, RN) = -1.
113 switch (fputil::quick_get_round()) {
114 case FE_TONEAREST:
115 case FE_DOWNWARD:
116 return FPBits::one(Sign::NEG).get_val();
117 default:
118 return fputil::cast<float16>(-0x1.ffcp-1);
119 }
120 }
121
122 // When |x| <= 2^(-3).
123 if (x_abs <= 0x3000U) {
124 if (LIBC_UNLIKELY(x_abs == 0))
125 return x;
126
127#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
128 if (auto r = EXP10M1F16_EXCEPTS_LO.lookup(x_u);
129 LIBC_UNLIKELY(r.has_value()))
130 return r.value();
131#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
132
133 float xf = x;
134 // Degree-5 minimax polynomial generated by Sollya with the following
135 // commands:
136 // > display = hexadecimal;
137 // > P = fpminimax((10^x - 1)/x, 4, [|SG...|], [-2^-3, 2^-3]);
138 // > x * P;
139 return fputil::cast<float16>(
140 xf * fputil::polyeval(xf, 0x1.26bb1cp+1f, 0x1.5351c8p+1f,
141 0x1.04704p+1f, 0x1.2ce084p+0f, 0x1.14a6bep-1f));
142 }
143 }
144
145 // When x is 1, 2, or 3. These are hard-to-round cases with exact results.
146 // 10^4 - 1 = 9'999 is not exactly representable as a float16, but luckily the
147 // polynomial approximation gives the correct result for x = 4 in all
148 // rounding modes.
149 if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) {
150 switch (x_u) {
151 case 0x3c00U: // x = 1.0f16
152 return fputil::cast<float16>(9.0);
153 case 0x4000U: // x = 2.0f16
154 return fputil::cast<float16>(99.0);
155 case 0x4200U: // x = 3.0f16
156 return fputil::cast<float16>(999.0);
157 }
158 }
159
160#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
161 if (auto r = EXP10M1F16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
162 return r.value();
163#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
164
165 // exp10(x) = exp2((hi + mid) * log2(10)) * exp10(lo)
166 auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x);
167 // exp10m1(x) = exp2((hi + mid) * log2(lo)) * exp10(lo) - 1
168 return fputil::cast<float16>(
169 fputil::multiply_add(exp2_hi_mid, exp10_lo, -1.0f));
170}
171
172} // namespace LIBC_NAMESPACE_DECL
173

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libc/src/math/generic/exp10m1f16.cpp