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

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