1 | //===-- Half-precision e^x 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/expf16.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/rounding_mode.h" |
19 | #include "src/__support/common.h" |
20 | #include "src/__support/macros/config.h" |
21 | #include "src/__support/macros/optimization.h" |
22 | |
23 | namespace LIBC_NAMESPACE_DECL { |
24 | |
25 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
26 | static constexpr fputil::ExceptValues<float16, 2> EXPF16_EXCEPTS_LO = {{ |
27 | // (input, RZ output, RU offset, RD offset, RN offset) |
28 | // x = 0x1.de4p-8, expf16(x) = 0x1.01cp+0 (RZ) |
29 | {0x1f79U, 0x3c07U, 1U, 0U, 0U}, |
30 | // x = 0x1.73cp-6, expf16(x) = 0x1.05cp+0 (RZ) |
31 | {0x25cfU, 0x3c17U, 1U, 0U, 0U}, |
32 | }}; |
33 | |
34 | static constexpr fputil::ExceptValues<float16, 3> EXPF16_EXCEPTS_HI = {{ |
35 | // (input, RZ output, RU offset, RD offset, RN offset) |
36 | // x = 0x1.c34p+0, expf16(x) = 0x1.74cp+2 (RZ) |
37 | {0x3f0dU, 0x45d3U, 1U, 0U, 1U}, |
38 | // x = -0x1.488p-5, expf16(x) = 0x1.ebcp-1 (RZ) |
39 | {0xa922U, 0x3bafU, 1U, 0U, 0U}, |
40 | // x = -0x1.55p-5, expf16(x) = 0x1.ebp-1 (RZ) |
41 | {0xa954U, 0x3bacU, 1U, 0U, 0U}, |
42 | }}; |
43 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
44 | |
45 | LLVM_LIBC_FUNCTION(float16, expf16, (float16 x)) { |
46 | using FPBits = fputil::FPBits<float16>; |
47 | FPBits x_bits(x); |
48 | |
49 | uint16_t x_u = x_bits.uintval(); |
50 | uint16_t x_abs = x_u & 0x7fffU; |
51 | |
52 | // When 0 < |x| <= 2^(-5), or |x| >= 12, or x is NaN. |
53 | if (LIBC_UNLIKELY(x_abs <= 0x2800U || x_abs >= 0x4a00U)) { |
54 | // exp(NaN) = NaN |
55 | if (x_bits.is_nan()) { |
56 | if (x_bits.is_signaling_nan()) { |
57 | fputil::raise_except_if_required(FE_INVALID); |
58 | return FPBits::quiet_nan().get_val(); |
59 | } |
60 | |
61 | return x; |
62 | } |
63 | |
64 | // When x >= 12. |
65 | if (x_bits.is_pos() && x_abs >= 0x4a00U) { |
66 | // exp(+inf) = +inf |
67 | if (x_bits.is_inf()) |
68 | return FPBits::inf().get_val(); |
69 | |
70 | switch (fputil::quick_get_round()) { |
71 | case FE_TONEAREST: |
72 | case FE_UPWARD: |
73 | fputil::set_errno_if_required(ERANGE); |
74 | fputil::raise_except_if_required(FE_OVERFLOW); |
75 | return FPBits::inf().get_val(); |
76 | default: |
77 | return FPBits::max_normal().get_val(); |
78 | } |
79 | } |
80 | |
81 | // When x <= -18. |
82 | if (x_u >= 0xcc80U) { |
83 | // exp(-inf) = +0 |
84 | if (x_bits.is_inf()) |
85 | return FPBits::zero().get_val(); |
86 | |
87 | fputil::set_errno_if_required(ERANGE); |
88 | fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); |
89 | |
90 | switch (fputil::quick_get_round()) { |
91 | case FE_UPWARD: |
92 | return FPBits::min_subnormal().get_val(); |
93 | default: |
94 | return FPBits::zero().get_val(); |
95 | } |
96 | } |
97 | |
98 | // When 0 < |x| <= 2^(-5). |
99 | if (x_abs <= 0x2800U && !x_bits.is_zero()) { |
100 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
101 | if (auto r = EXPF16_EXCEPTS_LO.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
102 | return r.value(); |
103 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
104 | |
105 | float xf = x; |
106 | // Degree-3 minimax polynomial generated by Sollya with the following |
107 | // commands: |
108 | // > display = hexadecimal; |
109 | // > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]); |
110 | // > 1 + x * P; |
111 | return fputil::cast<float16>( |
112 | fputil::polyeval(xf, 0x1p+0f, 0x1p+0f, 0x1.0004p-1f, 0x1.555778p-3f)); |
113 | } |
114 | } |
115 | |
116 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
117 | if (auto r = EXPF16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
118 | return r.value(); |
119 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
120 | |
121 | // exp(x) = exp(hi + mid) * exp(lo) |
122 | auto [exp_hi_mid, exp_lo] = exp_range_reduction(x); |
123 | return fputil::cast<float16>(exp_hi_mid * exp_lo); |
124 | } |
125 | |
126 | } // namespace LIBC_NAMESPACE_DECL |
127 | |