1//===-- Implementation header for exp2m1f16 ----------------------*- 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_EXP2M1F16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP2M1F16_H
11
12#include "include/llvm-libc-macros/float16-macros.h"
13
14#ifdef LIBC_TYPES_HAS_FLOAT16
15
16#include "src/__support/FPUtil/FEnvImpl.h"
17#include "src/__support/FPUtil/FPBits.h"
18#include "src/__support/FPUtil/cast.h"
19#include "src/__support/FPUtil/except_value_utils.h"
20#include "src/__support/FPUtil/multiply_add.h"
21#include "src/__support/FPUtil/polyeval.h"
22#include "src/__support/FPUtil/rounding_mode.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h"
25#include "src/__support/macros/properties/cpu_features.h"
26#include "src/__support/math/expxf16_utils.h"
27
28namespace LIBC_NAMESPACE_DECL {
29
30namespace math {
31
32LIBC_INLINE constexpr float16 exp2m1f16(float16 x) {
33#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
34 constexpr fputil::ExceptValues<float16, 7> EXP2M1F16_EXCEPTS_LO = {.values: {
35 // (input, RZ output, RU offset, RD offset, RN offset)
36 // Subnormal output straddling the upward rounding boundary.
37 {.input: 0x8461U, .rnd_towardzero_result: 0x8308U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U},
38 // x = 0x1.cf4p-13, exp2m1f16(x) = 0x1.41p-13 (RZ)
39 {.input: 0x0b3dU, .rnd_towardzero_result: 0x0904U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
40 // x = 0x1.4fcp-12, exp2m1f16(x) = 0x1.d14p-13 (RZ)
41 {.input: 0x0d3fU, .rnd_towardzero_result: 0x0b45U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
42 // x = 0x1.63p-11, exp2m1f16(x) = 0x1.ec4p-12 (RZ)
43 {.input: 0x118cU, .rnd_towardzero_result: 0x0fb1U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
44 // x = 0x1.6fp-7, exp2m1f16(x) = 0x1.fe8p-8 (RZ)
45 {.input: 0x21bcU, .rnd_towardzero_result: 0x1ffaU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
46 // x = -0x1.c6p-10, exp2m1f16(x) = -0x1.3a8p-10 (RZ)
47 {.input: 0x9718U, .rnd_towardzero_result: 0x94eaU, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 0U},
48 // x = -0x1.cfcp-10, exp2m1f16(x) = -0x1.414p-10 (RZ)
49 {.input: 0x973fU, .rnd_towardzero_result: 0x9505U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 0U},
50 }};
51
52#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
53 constexpr size_t N_EXP2M1F16_EXCEPTS_HI = 6;
54#else
55 constexpr size_t N_EXP2M1F16_EXCEPTS_HI = 7;
56#endif
57
58 constexpr fputil::ExceptValues<float16, N_EXP2M1F16_EXCEPTS_HI>
59 EXP2M1F16_EXCEPTS_HI = {.values: {
60 // (input, RZ output, RU offset, RD offset, RN offset)
61 // x = 0x1.e58p-3, exp2m1f16(x) = 0x1.6dcp-3 (RZ)
62 {.input: 0x3396U, .rnd_towardzero_result: 0x31b7U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
63#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
64 // x = 0x1.2e8p-2, exp2m1f16(x) = 0x1.d14p-3 (RZ)
65 {.input: 0x34baU, .rnd_towardzero_result: 0x3345U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
66#endif
67 // x = 0x1.ad8p-2, exp2m1f16(x) = 0x1.598p-2 (RZ)
68 {.input: 0x36b6U, .rnd_towardzero_result: 0x3566U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
69#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
70 // x = 0x1.edcp-2, exp2m1f16(x) = 0x1.964p-2 (RZ)
71 {0x37b7U, 0x3659U, 1U, 0U, 1U},
72#endif
73 // x = -0x1.804p-3, exp2m1f16(x) = -0x1.f34p-4 (RZ)
74 {.input: 0xb201U, .rnd_towardzero_result: 0xafcdU, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U},
75 // x = -0x1.f3p-3, exp2m1f16(x) = -0x1.3e4p-3 (RZ)
76 {.input: 0xb3ccU, .rnd_towardzero_result: 0xb0f9U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 0U},
77 // x = -0x1.294p-1, exp2m1f16(x) = -0x1.53p-2 (RZ)
78 {.input: 0xb8a5U, .rnd_towardzero_result: 0xb54cU, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U},
79#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
80 // x = -0x1.a34p-1, exp2m1f16(x) = -0x1.bb4p-2 (RZ)
81 {.input: 0xba8dU, .rnd_towardzero_result: 0xb6edU, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U},
82#endif
83 }};
84#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
85
86 using namespace math::expxf16_internal;
87 using FPBits = fputil::FPBits<float16>;
88 FPBits x_bits(x);
89
90 uint16_t x_u = x_bits.uintval();
91 uint16_t x_abs = x_u & 0x7fffU;
92
93 // When |x| <= 2^(-3), or |x| >= 11, or x is NaN.
94 if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x4980U)) {
95 // exp2m1(NaN) = NaN
96 if (x_bits.is_nan()) {
97 if (x_bits.is_signaling_nan()) {
98 fputil::raise_except_if_required(FE_INVALID);
99 return FPBits::quiet_nan().get_val();
100 }
101
102 return x;
103 }
104
105 // When x >= 16.
106 if (x_u >= 0x4c00 && x_bits.is_pos()) {
107 // exp2m1(+inf) = +inf
108 if (x_bits.is_inf())
109 return FPBits::inf().get_val();
110
111 return fputil::set_overflow<float16>();
112 }
113
114 // When x < -11.
115 if (x_u > 0xc980U) {
116 // exp2m1(-inf) = -1
117 if (x_bits.is_inf())
118 return FPBits::one(sign: Sign::NEG).get_val();
119
120 // When -12 < x < -11, round(2^x - 1, HP, RN) = -0x1.ffcp-1.
121 if (x_u < 0xca00U)
122 return fputil::round_result_slightly_down(
123 value_rn: fputil::cast<float16>(x: -0x1.ffcp-1));
124
125 // When x <= -12, round(2^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#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
145 if (auto r = EXP2M1F16_EXCEPTS_LO.lookup(x_bits: x_u);
146 LIBC_UNLIKELY(r.has_value())) {
147 if (FPBits(r.value()).is_subnormal()) {
148 fputil::set_errno_if_required(ERANGE);
149 fputil::raise_underflow_except_if_required<float16>();
150 }
151 return r.value();
152 }
153#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
154
155 float xf = x;
156 // Degree-5 minimax polynomial generated by Sollya with the following
157 // commands:
158 // > display = hexadecimal;
159 // > P = fpminimax((2^x - 1)/x, 4, [|SG...|], [-2^-3, 2^-3]);
160 // > x * P;
161 return fputil::cast<float16>(
162 x: xf * fputil::polyeval(x: xf, a0: 0x1.62e43p-1f, a: 0x1.ebfbdep-3f,
163 a: 0x1.c6af88p-5f, a: 0x1.3b45d6p-7f,
164 a: 0x1.641e7cp-10f));
165 }
166 }
167
168#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
169 if (auto r = EXP2M1F16_EXCEPTS_HI.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value()))
170 return r.value();
171#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
172
173 // exp2(x) = exp2(hi + mid) * exp2(lo)
174 auto [exp2_hi_mid, exp2_lo] = exp2_range_reduction(x);
175 // exp2m1(x) = exp2(hi + mid) * exp2(lo) - 1
176 return fputil::cast<float16>(
177 x: fputil::multiply_sub(x: exp2_hi_mid, y: exp2_lo, z: 1.0f));
178}
179
180} // namespace math
181
182} // namespace LIBC_NAMESPACE_DECL
183
184#endif // LIBC_TYPES_HAS_FLOAT16
185
186#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP2M1F16_H
187