1//===-- Implementation header for expm1f16 ----------------------*- 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_EXPM1F16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1F16_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/PolyEval.h"
19#include "src/__support/FPUtil/cast.h"
20#include "src/__support/FPUtil/except_value_utils.h"
21#include "src/__support/FPUtil/multiply_add.h"
22#include "src/__support/FPUtil/rounding_mode.h"
23#include "src/__support/common.h"
24#include "src/__support/macros/config.h"
25#include "src/__support/macros/optimization.h"
26#include "src/__support/math/expxf16_utils.h"
27
28namespace LIBC_NAMESPACE_DECL {
29
30namespace math {
31
32LIBC_INLINE constexpr float16 expm1f16(float16 x) {
33#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
34 constexpr fputil::ExceptValues<float16, 1> EXPM1F16_EXCEPTS_LO = {.values: {
35 // (input, RZ output, RU offset, RD offset, RN offset)
36 // x = 0x1.564p-5, expm1f16(x) = 0x1.5d4p-5 (RZ)
37 {.input: 0x2959U, .rnd_towardzero_result: 0x2975U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
38 }};
39
40#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
41 constexpr size_t N_EXPM1F16_EXCEPTS_HI = 2;
42#else
43 constexpr size_t N_EXPM1F16_EXCEPTS_HI = 3;
44#endif
45
46 constexpr fputil::ExceptValues<float16, N_EXPM1F16_EXCEPTS_HI>
47 EXPM1F16_EXCEPTS_HI = {.values: {
48 // (input, RZ output, RU offset, RD offset, RN offset)
49 // x = 0x1.c34p+0, expm1f16(x) = 0x1.34cp+2 (RZ)
50 {.input: 0x3f0dU, .rnd_towardzero_result: 0x44d3U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U},
51 // x = -0x1.e28p-3, expm1f16(x) = -0x1.adcp-3 (RZ)
52 {.input: 0xb38aU, .rnd_towardzero_result: 0xb2b7U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U},
53#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
54 // x = 0x1.a08p-3, exp10m1f(x) = 0x1.cdcp-3 (RZ)
55 {.input: 0x3282U, .rnd_towardzero_result: 0x3337U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
56#endif
57 }};
58#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
59
60 using namespace math::expxf16_internal;
61 using FPBits = fputil::FPBits<float16>;
62 FPBits x_bits(x);
63
64 uint16_t x_u = x_bits.uintval();
65 uint16_t x_abs = x_u & 0x7fffU;
66
67 // When |x| <= 2^(-3), or |x| >= -11 * log(2), or x is NaN.
68 if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x47a0U)) {
69 // expm1(NaN) = NaN
70 if (x_bits.is_nan()) {
71 if (x_bits.is_signaling_nan()) {
72 fputil::raise_except_if_required(FE_INVALID);
73 return FPBits::quiet_nan().get_val();
74 }
75
76 return x;
77 }
78
79 // expm1(+/-0) = +/-0
80 if (x_abs == 0)
81 return x;
82
83 // When x >= 16 * log(2).
84 if (x_bits.is_pos() && x_abs >= 0x498cU) {
85 // expm1(+inf) = +inf
86 if (x_bits.is_inf())
87 return FPBits::inf().get_val();
88
89#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
90 fputil::set_errno_if_required(ERANGE);
91 fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
92 return FPBits::inf().get_val();
93#else
94 switch (fputil::quick_get_round()) {
95 case FE_TONEAREST:
96 case FE_UPWARD:
97 fputil::set_errno_if_required(ERANGE);
98 fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
99 return FPBits::inf().get_val();
100 default:
101 return FPBits::max_normal().get_val();
102 }
103#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
104 }
105
106 // When x <= -11 * log(2).
107 if (x_u >= 0xc7a0U) {
108 // expm1(-inf) = -1
109 if (x_bits.is_inf())
110 return FPBits::one(sign: Sign::NEG).get_val();
111
112 // When x > -0x1.0ap+3, round(expm1(x), HP, RN) = -1.
113 if (x_u > 0xc828U)
114 return fputil::round_result_slightly_up(
115 value_rn: FPBits::one(sign: Sign::NEG).get_val());
116 // When x <= -0x1.0ap+3, round(expm1(x), HP, RN) = -0x1.ffcp-1.
117 return fputil::round_result_slightly_down(
118 value_rn: fputil::cast<float16>(x: -0x1.ffcp-1));
119 }
120
121 // When 0 < |x| <= 2^(-3).
122 if (x_abs <= 0x3000U && !x_bits.is_zero()) {
123
124#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
125 if (auto r = EXPM1F16_EXCEPTS_LO.lookup(x_bits: x_u);
126 LIBC_UNLIKELY(r.has_value()))
127 return r.value();
128#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
129
130 float xf = x;
131 // Degree-5 minimax polynomial generated by Sollya with the following
132 // commands:
133 // > display = hexadecimal;
134 // > P = fpminimax(expm1(x)/x, 4, [|SG...|], [-2^-3, 2^-3]);
135 // > x * P;
136 return fputil::cast<float16>(
137 x: xf * fputil::polyeval(x: xf, a0: 0x1p+0f, a: 0x1.fffff8p-2f, a: 0x1.555556p-3f,
138 a: 0x1.55905ep-5f, a: 0x1.1124c2p-7f));
139 }
140 }
141
142#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
143 if (auto r = EXPM1F16_EXCEPTS_HI.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value()))
144 return r.value();
145#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
146
147 // exp(x) = exp(hi + mid) * exp(lo)
148 auto [exp_hi_mid, exp_lo] = exp_range_reduction(x);
149 // expm1(x) = exp(hi + mid) * exp(lo) - 1
150 return fputil::cast<float16>(x: fputil::multiply_add(x: exp_hi_mid, y: exp_lo, z: -1.0f));
151}
152
153} // namespace math
154
155} // namespace LIBC_NAMESPACE_DECL
156
157#endif // LIBC_TYPES_HAS_FLOAT16
158
159#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPM1F16_H
160