66 lines · c
1//===-- Implementation header for coshf -------------------------*- 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_COSHF_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H11 12#include "sinhfcoshf_utils.h"13#include "src/__support/FPUtil/FEnvImpl.h"14#include "src/__support/FPUtil/FPBits.h"15#include "src/__support/FPUtil/rounding_mode.h"16#include "src/__support/macros/config.h"17#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY18 19namespace LIBC_NAMESPACE_DECL {20 21namespace math {22 23LIBC_INLINE static constexpr float coshf(float x) {24 using namespace sinhfcoshf_internal;25 using FPBits = typename fputil::FPBits<float>;26 27 FPBits xbits(x);28 xbits.set_sign(Sign::POS);29 x = xbits.get_val();30 31 uint32_t x_u = xbits.uintval();32 33 // When |x| >= 90, or x is inf or nan34 if (LIBC_UNLIKELY(x_u >= 0x42b4'0000U || x_u <= 0x3280'0000U)) {35 // |x| <= 2^-2636 if (x_u <= 0x3280'0000U) {37 return 1.0f + x;38 }39 40 if (xbits.is_inf_or_nan())41 return x + FPBits::inf().get_val();42 43 int rounding = fputil::quick_get_round();44 if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO))45 return FPBits::max_normal().get_val();46 47 fputil::set_errno_if_required(ERANGE);48 fputil::raise_except_if_required(FE_OVERFLOW);49 50 return x + FPBits::inf().get_val();51 }52 53 // TODO: We should be able to reduce the latency and reciprocal throughput54 // further by using a low degree (maybe 3-7 ?) minimax polynomial for small55 // but not too small inputs, such as |x| < 2^-2, or |x| < 2^-3.56 57 // cosh(x) = (e^x + e^(-x)) / 2.58 return static_cast<float>(exp_pm_eval</*is_sinh*/ false>(x));59}60 61} // namespace math62 63} // namespace LIBC_NAMESPACE_DECL64 65#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H66