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