Skip to content

[libc][math] Refactor coshf implementation to header-only in src/__support/math folder. #153427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/shared/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "math/cos.h"
#include "math/cosf.h"
#include "math/cosf16.h"
#include "math/coshf.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
Expand Down
23 changes: 23 additions & 0 deletions libc/shared/math/coshf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Shared coshf function -----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SHARED_MATH_COSHF_H
#define LLVM_LIBC_SHARED_MATH_COSHF_H

#include "shared/libc_common.h"
#include "src/__support/math/coshf.h"

namespace LIBC_NAMESPACE_DECL {
namespace shared {

using math::coshf;

} // namespace shared
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SHARED_MATH_COSHF_H
21 changes: 21 additions & 0 deletions libc/src/__support/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,18 @@ add_header_library(
libc.src.__support.macros.properties.types
)

add_header_library(
coshf
HDRS
coshf.h
DEPENDS
.sinhfcoshf_utils
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
)

add_header_library(
erff
HDRS
Expand Down Expand Up @@ -726,3 +738,12 @@ add_header_library(
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.common
)

add_header_library(
sinhfcoshf_utils
HDRS
sinhfcoshf_utils.h
DEPENDS
.exp10f_utils
libc.src.__support.FPUtil.multiply_add
)
65 changes: 65 additions & 0 deletions libc/src/__support/math/coshf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//===-- Implementation header for coshf -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H

#include "sinhfcoshf_utils.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY

namespace LIBC_NAMESPACE_DECL {

namespace math {

LIBC_INLINE static constexpr float coshf(float x) {
using namespace sinhfcoshf_internal;
using FPBits = typename fputil::FPBits<float>;

FPBits xbits(x);
xbits.set_sign(Sign::POS);
x = xbits.get_val();

uint32_t x_u = xbits.uintval();

// When |x| >= 90, or x is inf or nan
if (LIBC_UNLIKELY(x_u >= 0x42b4'0000U || x_u <= 0x3280'0000U)) {
// |x| <= 2^-26
if (x_u <= 0x3280'0000U) {
return 1.0f + x;
}

if (xbits.is_inf_or_nan())
return x + FPBits::inf().get_val();

int rounding = fputil::quick_get_round();
if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO))
return FPBits::max_normal().get_val();

fputil::set_errno_if_required(ERANGE);
fputil::raise_except_if_required(FE_OVERFLOW);

return x + FPBits::inf().get_val();
}

// TODO: We should be able to reduce the latency and reciprocal throughput
// further by using a low degree (maybe 3-7 ?) minimax polynomial for small
// but not too small inputs, such as |x| < 2^-2, or |x| < 2^-3.

// cosh(x) = (e^x + e^(-x)) / 2.
return static_cast<float>(exp_pm_eval</*is_sinh*/ false>(x));
}

} // namespace math

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
//===-- Single-precision general exp/log functions ------------------------===//
//===-- Single-precision general sinhf/coshf functions --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
#define LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINHFCOSHF_UTILS_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_SINHFCOSHF_UTILS_H

#include "common_constants.h"

#include "src/__support/common.h"
#include "src/__support/macros/properties/cpu_features.h"
#include "src/__support/math/acoshf_utils.h"
#include "src/__support/math/exp10f_utils.h"
#include "src/__support/math/exp_utils.h"
#include "exp10f_utils.h"
#include "src/__support/FPUtil/multiply_add.h"

namespace LIBC_NAMESPACE_DECL {

constexpr int LOG_P1_BITS = 6;
constexpr int LOG_P1_SIZE = 1 << LOG_P1_BITS;
namespace math {

namespace sinhfcoshf_internal {

// The function correctly calculates sinh(x) and cosh(x) by calculating exp(x)
// and exp(-x) simultaneously.
Expand Down Expand Up @@ -121,6 +117,10 @@ template <bool is_sinh> LIBC_INLINE double exp_pm_eval(float x) {
return r;
}

} // namespace sinhfcoshf_internal

} // namespace math

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINHFCOSHF_UTILS_H
38 changes: 8 additions & 30 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,6 @@ add_entrypoint_object(
../exp2.h
DEPENDS
.common_constants
.explogxf
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.dyadic_float
Expand All @@ -1357,6 +1356,7 @@ add_entrypoint_object(
libc.src.__support.FPUtil.triple_double
libc.src.__support.integer_literals
libc.src.__support.macros.optimization
libc.src.__support.math.exp_utils
libc.src.errno.errno
)

Expand All @@ -1365,7 +1365,6 @@ add_header_library(
HDRS
exp2f_impl.h
DEPENDS
.explogxf
libc.src.__support.FPUtil.except_value_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
Expand All @@ -1374,6 +1373,7 @@ add_header_library(
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.math.exp10f_utils
libc.src.__support.common
libc.src.errno.errno
)
Expand Down Expand Up @@ -1413,7 +1413,6 @@ add_entrypoint_object(
HDRS
../exp2m1f.h
DEPENDS
.explogxf
libc.src.errno.errno
libc.src.__support.common
libc.src.__support.FPUtil.except_value_utils
Expand All @@ -1424,6 +1423,7 @@ add_entrypoint_object(
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.macros.properties.cpu_features
libc.src.__support.math.exp10f_utils
)

add_entrypoint_object(
Expand Down Expand Up @@ -1488,7 +1488,6 @@ add_entrypoint_object(
HDRS
../exp10m1f.h
DEPENDS
.explogxf
libc.src.errno.errno
libc.src.__support.common
libc.src.__support.FPUtil.except_value_utils
Expand All @@ -1498,6 +1497,7 @@ add_entrypoint_object(
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.math.exp10f_utils
)

add_entrypoint_object(
Expand Down Expand Up @@ -1529,14 +1529,11 @@ add_entrypoint_object(
../expm1.h
DEPENDS
.common_constants
.explogxf
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.dyadic_float
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.FPUtil.triple_double
Expand Down Expand Up @@ -1593,7 +1590,6 @@ add_entrypoint_object(
DEPENDS
.common_constants
.exp2f_impl
.explogxf
libc.src.__support.math.exp10f
libc.src.__support.CPP.bit
libc.src.__support.FPUtil.fenv_impl
Expand Down Expand Up @@ -3905,31 +3901,14 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

#TODO: Add errno include to the hyperbolic functions.
add_header_library(
explogxf
HDRS
explogxf.h
DEPENDS
.common_constants
libc.src.__support.math.exp_utils
libc.src.__support.math.acoshf_utils
libc.src.__support.macros.properties.cpu_features
libc.src.errno.errno
)

add_entrypoint_object(
coshf
SRCS
coshf.cpp
HDRS
../coshf.h
DEPENDS
.explogxf
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.math.coshf
)

add_entrypoint_object(
Expand All @@ -3956,10 +3935,10 @@ add_entrypoint_object(
HDRS
../sinhf.h
DEPENDS
.explogxf
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.math.sinhfcoshf_utils
)

add_entrypoint_object(
Expand All @@ -3973,7 +3952,7 @@ add_entrypoint_object(
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.__support.FPUtil.except_value_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
Expand All @@ -3986,12 +3965,12 @@ add_entrypoint_object(
HDRS
../tanhf.h
DEPENDS
.explogxf
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.polyeval
libc.src.__support.macros.optimization
libc.src.__support.math.exp10f_utils
)

add_entrypoint_object(
Expand Down Expand Up @@ -4022,7 +4001,6 @@ add_entrypoint_object(
HDRS
../acoshf.h
DEPENDS
.explogxf
libc.src.__support.math.acoshf
)

Expand Down
1 change: 0 additions & 1 deletion libc/src/math/generic/acoshf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//

#include "src/math/acoshf.h"

#include "src/__support/math/acoshf.h"

namespace LIBC_NAMESPACE_DECL {
Expand Down
Loading
Loading