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