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