1 | //===-- Half-precision 2^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/exp2f16.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/cast.h" |
16 | #include "src/__support/FPUtil/except_value_utils.h" |
17 | #include "src/__support/FPUtil/rounding_mode.h" |
18 | #include "src/__support/common.h" |
19 | #include "src/__support/macros/config.h" |
20 | #include "src/__support/macros/optimization.h" |
21 | |
22 | namespace LIBC_NAMESPACE_DECL { |
23 | |
24 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
25 | static constexpr fputil::ExceptValues<float16, 3> EXP2F16_EXCEPTS = {{ |
26 | // (input, RZ output, RU offset, RD offset, RN offset) |
27 | // x = 0x1.714p-11, exp2f16(x) = 0x1p+0 (RZ) |
28 | {0x11c5U, 0x3c00U, 1U, 0U, 1U}, |
29 | // x = -0x1.558p-4, exp2f16(x) = 0x1.e34p-1 (RZ) |
30 | {0xad56U, 0x3b8dU, 1U, 0U, 0U}, |
31 | // x = -0x1.d5cp-4, exp2f16(x) = 0x1.d8cp-1 (RZ) |
32 | {0xaf57U, 0x3b63U, 1U, 0U, 0U}, |
33 | }}; |
34 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
35 | |
36 | LLVM_LIBC_FUNCTION(float16, exp2f16, (float16 x)) { |
37 | using FPBits = fputil::FPBits<float16>; |
38 | FPBits x_bits(x); |
39 | |
40 | uint16_t x_u = x_bits.uintval(); |
41 | uint16_t x_abs = x_u & 0x7fffU; |
42 | |
43 | // When |x| >= 16, or x is NaN. |
44 | if (LIBC_UNLIKELY(x_abs >= 0x4c00U)) { |
45 | // exp2(NaN) = NaN |
46 | if (x_bits.is_nan()) { |
47 | if (x_bits.is_signaling_nan()) { |
48 | fputil::raise_except_if_required(FE_INVALID); |
49 | return FPBits::quiet_nan().get_val(); |
50 | } |
51 | |
52 | return x; |
53 | } |
54 | |
55 | // When x >= 16. |
56 | if (x_bits.is_pos()) { |
57 | // exp2(+inf) = +inf |
58 | if (x_bits.is_inf()) |
59 | return FPBits::inf().get_val(); |
60 | |
61 | switch (fputil::quick_get_round()) { |
62 | case FE_TONEAREST: |
63 | case FE_UPWARD: |
64 | fputil::set_errno_if_required(ERANGE); |
65 | fputil::raise_except_if_required(FE_OVERFLOW); |
66 | return FPBits::inf().get_val(); |
67 | default: |
68 | return FPBits::max_normal().get_val(); |
69 | } |
70 | } |
71 | |
72 | // When x <= -25. |
73 | if (x_u >= 0xce40U) { |
74 | // exp2(-inf) = +0 |
75 | if (x_bits.is_inf()) |
76 | return FPBits::zero().get_val(); |
77 | |
78 | fputil::set_errno_if_required(ERANGE); |
79 | fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT); |
80 | |
81 | if (fputil::fenv_is_round_up()) |
82 | return FPBits::min_subnormal().get_val(); |
83 | return FPBits::zero().get_val(); |
84 | } |
85 | } |
86 | |
87 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
88 | if (auto r = EXP2F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value())) |
89 | return r.value(); |
90 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
91 | |
92 | // exp2(x) = exp2(hi + mid) * exp2(lo) |
93 | auto [exp2_hi_mid, exp2_lo] = exp2_range_reduction(x); |
94 | return fputil::cast<float16>(exp2_hi_mid * exp2_lo); |
95 | } |
96 | |
97 | } // namespace LIBC_NAMESPACE_DECL |
98 | |