112 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP2F16_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP2F16_H11 12#include "include/llvm-libc-macros/float16-macros.h"13 14#ifdef LIBC_TYPES_HAS_FLOAT1615 16#include "expxf16_utils.h"17#include "src/__support/FPUtil/FEnvImpl.h"18#include "src/__support/FPUtil/FPBits.h"19#include "src/__support/FPUtil/cast.h"20#include "src/__support/FPUtil/except_value_utils.h"21#include "src/__support/FPUtil/rounding_mode.h"22#include "src/__support/common.h"23#include "src/__support/macros/config.h"24#include "src/__support/macros/optimization.h"25 26namespace LIBC_NAMESPACE_DECL {27 28namespace math {29 30LIBC_INLINE static constexpr float16 exp2f16(float16 x) {31 32#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS33 constexpr fputil::ExceptValues<float16, 3> EXP2F16_EXCEPTS = {{34 // (input, RZ output, RU offset, RD offset, RN offset)35 // x = 0x1.714p-11, exp2f16(x) = 0x1p+0 (RZ)36 {0x11c5U, 0x3c00U, 1U, 0U, 1U},37 // x = -0x1.558p-4, exp2f16(x) = 0x1.e34p-1 (RZ)38 {0xad56U, 0x3b8dU, 1U, 0U, 0U},39 // x = -0x1.d5cp-4, exp2f16(x) = 0x1.d8cp-1 (RZ)40 {0xaf57U, 0x3b63U, 1U, 0U, 0U},41 }};42#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS43 44 using namespace math::expxf16_internal;45 using FPBits = fputil::FPBits<float16>;46 FPBits x_bits(x);47 48 uint16_t x_u = x_bits.uintval();49 uint16_t x_abs = x_u & 0x7fffU;50 51 // When |x| >= 16, or x is NaN.52 if (LIBC_UNLIKELY(x_abs >= 0x4c00U)) {53 // exp2(NaN) = NaN54 if (x_bits.is_nan()) {55 if (x_bits.is_signaling_nan()) {56 fputil::raise_except_if_required(FE_INVALID);57 return FPBits::quiet_nan().get_val();58 }59 60 return x;61 }62 63 // When x >= 16.64 if (x_bits.is_pos()) {65 // exp2(+inf) = +inf66 if (x_bits.is_inf())67 return FPBits::inf().get_val();68 69 switch (fputil::quick_get_round()) {70 case FE_TONEAREST:71 case FE_UPWARD:72 fputil::set_errno_if_required(ERANGE);73 fputil::raise_except_if_required(FE_OVERFLOW);74 return FPBits::inf().get_val();75 default:76 return FPBits::max_normal().get_val();77 }78 }79 80 // When x <= -25.81 if (x_u >= 0xce40U) {82 // exp2(-inf) = +083 if (x_bits.is_inf())84 return FPBits::zero().get_val();85 86 fputil::set_errno_if_required(ERANGE);87 fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);88 89 if (fputil::fenv_is_round_up())90 return FPBits::min_subnormal().get_val();91 return FPBits::zero().get_val();92 }93 }94 95#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS96 if (auto r = EXP2F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))97 return r.value();98#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS99 100 // exp2(x) = exp2(hi + mid) * exp2(lo)101 auto [exp2_hi_mid, exp2_lo] = exp2_range_reduction(x);102 return fputil::cast<float16>(exp2_hi_mid * exp2_lo);103}104 105} // namespace math106 107} // namespace LIBC_NAMESPACE_DECL108 109#endif // LIBC_TYPES_HAS_FLOAT16110 111#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP2F16_H112