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