Skip to content

Commit 88597da

Browse files
committed
[libc][math] Refactor coshf implementation to header-only in src/__support/math folder.
1 parent 170acc2 commit 88597da

File tree

19 files changed

+174
-137
lines changed

19 files changed

+174
-137
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "math/cos.h"
3636
#include "math/cosf.h"
3737
#include "math/cosf16.h"
38+
#include "math/coshf.h"
3839
#include "math/erff.h"
3940
#include "math/exp.h"
4041
#include "math/exp10.h"

libc/shared/math/coshf.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Shared coshf function -----------------------------------*- 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_SHARED_MATH_COSHF_H
10+
#define LLVM_LIBC_SHARED_MATH_COSHF_H
11+
12+
#include "shared/libc_common.h"
13+
#include "src/__support/math/coshf.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace shared {
17+
18+
using math::coshf;
19+
20+
} // namespace shared
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SHARED_MATH_COSHF_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,18 @@ add_header_library(
407407
libc.src.__support.macros.properties.types
408408
)
409409

410+
add_header_library(
411+
coshf
412+
HDRS
413+
coshf.h
414+
DEPENDS
415+
.sinhfcoshf_utils
416+
libc.src.__support.FPUtil.fp_bits
417+
libc.src.__support.FPUtil.multiply_add
418+
libc.src.__support.FPUtil.rounding_mode
419+
libc.src.__support.macros.optimization
420+
)
421+
410422
add_header_library(
411423
erff
412424
HDRS
@@ -726,3 +738,12 @@ add_header_library(
726738
libc.src.__support.FPUtil.nearest_integer
727739
libc.src.__support.common
728740
)
741+
742+
add_header_library(
743+
sinhfcoshf_utils
744+
HDRS
745+
sinhfcoshf_utils.h
746+
DEPENDS
747+
.exp10f_utils
748+
libc.src.__support.FPUtil.multiply_add
749+
)

libc/src/__support/math/coshf.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H
11+
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_UNLIKELY
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
namespace math {
22+
23+
LIBC_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 nan
34+
if (LIBC_UNLIKELY(x_u >= 0x42b4'0000U || x_u <= 0x3280'0000U)) {
35+
// |x| <= 2^-26
36+
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 throughput
54+
// further by using a low degree (maybe 3-7 ?) minimax polynomial for small
55+
// 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 math
62+
63+
} // namespace LIBC_NAMESPACE_DECL
64+
65+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H

libc/src/math/generic/explogxf.h renamed to libc/src/__support/math/sinhfcoshf_utils.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
//===-- Single-precision general exp/log functions ------------------------===//
1+
//===-- Single-precision general sinhf/coshf functions --------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
10-
#define LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINHFCOSHF_UTILS_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_SINHFCOSHF_UTILS_H
1111

12-
#include "common_constants.h"
13-
14-
#include "src/__support/common.h"
15-
#include "src/__support/macros/properties/cpu_features.h"
16-
#include "src/__support/math/acoshf_utils.h"
17-
#include "src/__support/math/exp10f_utils.h"
18-
#include "src/__support/math/exp_utils.h"
12+
#include "exp10f_utils.h"
13+
#include "src/__support/FPUtil/multiply_add.h"
1914

2015
namespace LIBC_NAMESPACE_DECL {
2116

22-
constexpr int LOG_P1_BITS = 6;
23-
constexpr int LOG_P1_SIZE = 1 << LOG_P1_BITS;
17+
namespace math {
18+
19+
namespace sinhfcoshf_internal {
2420

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

120+
} // namespace sinhfcoshf_internal
121+
122+
} // namespace math
123+
124124
} // namespace LIBC_NAMESPACE_DECL
125125

126-
#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPLOGXF_H
126+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINHFCOSHF_UTILS_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,6 @@ add_entrypoint_object(
13441344
../exp2.h
13451345
DEPENDS
13461346
.common_constants
1347-
.explogxf
13481347
libc.src.__support.CPP.bit
13491348
libc.src.__support.CPP.optional
13501349
libc.src.__support.FPUtil.dyadic_float
@@ -1357,6 +1356,7 @@ add_entrypoint_object(
13571356
libc.src.__support.FPUtil.triple_double
13581357
libc.src.__support.integer_literals
13591358
libc.src.__support.macros.optimization
1359+
libc.src.__support.math.exp_utils
13601360
libc.src.errno.errno
13611361
)
13621362

@@ -1365,7 +1365,6 @@ add_header_library(
13651365
HDRS
13661366
exp2f_impl.h
13671367
DEPENDS
1368-
.explogxf
13691368
libc.src.__support.FPUtil.except_value_utils
13701369
libc.src.__support.FPUtil.fenv_impl
13711370
libc.src.__support.FPUtil.fp_bits
@@ -1374,6 +1373,7 @@ add_header_library(
13741373
libc.src.__support.FPUtil.polyeval
13751374
libc.src.__support.FPUtil.rounding_mode
13761375
libc.src.__support.macros.optimization
1376+
libc.src.__support.math.exp10f_utils
13771377
libc.src.__support.common
13781378
libc.src.errno.errno
13791379
)
@@ -1413,7 +1413,6 @@ add_entrypoint_object(
14131413
HDRS
14141414
../exp2m1f.h
14151415
DEPENDS
1416-
.explogxf
14171416
libc.src.errno.errno
14181417
libc.src.__support.common
14191418
libc.src.__support.FPUtil.except_value_utils
@@ -1424,6 +1423,7 @@ add_entrypoint_object(
14241423
libc.src.__support.FPUtil.rounding_mode
14251424
libc.src.__support.macros.optimization
14261425
libc.src.__support.macros.properties.cpu_features
1426+
libc.src.__support.math.exp10f_utils
14271427
)
14281428

14291429
add_entrypoint_object(
@@ -1488,7 +1488,6 @@ add_entrypoint_object(
14881488
HDRS
14891489
../exp10m1f.h
14901490
DEPENDS
1491-
.explogxf
14921491
libc.src.errno.errno
14931492
libc.src.__support.common
14941493
libc.src.__support.FPUtil.except_value_utils
@@ -1498,6 +1497,7 @@ add_entrypoint_object(
14981497
libc.src.__support.FPUtil.polyeval
14991498
libc.src.__support.FPUtil.rounding_mode
15001499
libc.src.__support.macros.optimization
1500+
libc.src.__support.math.exp10f_utils
15011501
)
15021502

15031503
add_entrypoint_object(
@@ -1529,14 +1529,11 @@ add_entrypoint_object(
15291529
../expm1.h
15301530
DEPENDS
15311531
.common_constants
1532-
.explogxf
15331532
libc.src.__support.CPP.bit
1534-
libc.src.__support.CPP.optional
15351533
libc.src.__support.FPUtil.dyadic_float
15361534
libc.src.__support.FPUtil.fenv_impl
15371535
libc.src.__support.FPUtil.fp_bits
15381536
libc.src.__support.FPUtil.multiply_add
1539-
libc.src.__support.FPUtil.nearest_integer
15401537
libc.src.__support.FPUtil.polyeval
15411538
libc.src.__support.FPUtil.rounding_mode
15421539
libc.src.__support.FPUtil.triple_double
@@ -1593,7 +1590,6 @@ add_entrypoint_object(
15931590
DEPENDS
15941591
.common_constants
15951592
.exp2f_impl
1596-
.explogxf
15971593
libc.src.__support.math.exp10f
15981594
libc.src.__support.CPP.bit
15991595
libc.src.__support.FPUtil.fenv_impl
@@ -3905,31 +3901,14 @@ add_entrypoint_object(
39053901
libc.src.__support.FPUtil.nearest_integer_operations
39063902
)
39073903

3908-
#TODO: Add errno include to the hyperbolic functions.
3909-
add_header_library(
3910-
explogxf
3911-
HDRS
3912-
explogxf.h
3913-
DEPENDS
3914-
.common_constants
3915-
libc.src.__support.math.exp_utils
3916-
libc.src.__support.math.acoshf_utils
3917-
libc.src.__support.macros.properties.cpu_features
3918-
libc.src.errno.errno
3919-
)
3920-
39213904
add_entrypoint_object(
39223905
coshf
39233906
SRCS
39243907
coshf.cpp
39253908
HDRS
39263909
../coshf.h
39273910
DEPENDS
3928-
.explogxf
3929-
libc.src.__support.FPUtil.fp_bits
3930-
libc.src.__support.FPUtil.multiply_add
3931-
libc.src.__support.FPUtil.rounding_mode
3932-
libc.src.__support.macros.optimization
3911+
libc.src.__support.math.coshf
39333912
)
39343913

39353914
add_entrypoint_object(
@@ -3956,10 +3935,10 @@ add_entrypoint_object(
39563935
HDRS
39573936
../sinhf.h
39583937
DEPENDS
3959-
.explogxf
39603938
libc.src.__support.FPUtil.fp_bits
39613939
libc.src.__support.FPUtil.rounding_mode
39623940
libc.src.__support.macros.optimization
3941+
libc.src.__support.math.sinhfcoshf_utils
39633942
)
39643943

39653944
add_entrypoint_object(
@@ -3973,7 +3952,7 @@ add_entrypoint_object(
39733952
libc.hdr.errno_macros
39743953
libc.hdr.fenv_macros
39753954
libc.src.__support.FPUtil.except_value_utils
3976-
libc.src.__support.FPUtil.fenv_impl
3955+
libc.src.__support.FPUtil.fenv_impl
39773956
libc.src.__support.FPUtil.fp_bits
39783957
libc.src.__support.FPUtil.rounding_mode
39793958
libc.src.__support.macros.optimization
@@ -3986,12 +3965,12 @@ add_entrypoint_object(
39863965
HDRS
39873966
../tanhf.h
39883967
DEPENDS
3989-
.explogxf
39903968
libc.src.__support.FPUtil.fp_bits
39913969
libc.src.__support.FPUtil.rounding_mode
39923970
libc.src.__support.FPUtil.multiply_add
39933971
libc.src.__support.FPUtil.polyeval
39943972
libc.src.__support.macros.optimization
3973+
libc.src.__support.math.exp10f_utils
39953974
)
39963975

39973976
add_entrypoint_object(
@@ -4022,7 +4001,6 @@ add_entrypoint_object(
40224001
HDRS
40234002
../acoshf.h
40244003
DEPENDS
4025-
.explogxf
40264004
libc.src.__support.math.acoshf
40274005
)
40284006

libc/src/math/generic/acoshf.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/acoshf.h"
10-
1110
#include "src/__support/math/acoshf.h"
1211

1312
namespace LIBC_NAMESPACE_DECL {

0 commit comments

Comments
 (0)