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