Skip to content

Commit 93383ba

Browse files
committed
[libc][math] Refactor exp2f implementation to header-only in src/__support/math folder.
1 parent 839b91c commit 93383ba

File tree

10 files changed

+80
-53
lines changed

10 files changed

+80
-53
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "math/exp10m1f.h"
4949
#include "math/exp10m1f16.h"
5050
#include "math/exp2.h"
51+
#include "math/exp2f.h"
5152
#include "math/expf.h"
5253
#include "math/expf16.h"
5354
#include "math/frexpf.h"

libc/shared/math/exp2f.h

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

libc/src/__support/math/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,24 @@ add_header_library(
735735
libc.src.errno.errno
736736
)
737737

738+
add_header_library(
739+
exp2f
740+
HDRS
741+
exp2f.h
742+
DEPENDS
743+
.exp10f_utils
744+
libc.src.__support.FPUtil.except_value_utils
745+
libc.src.__support.FPUtil.fenv_impl
746+
libc.src.__support.FPUtil.fp_bits
747+
libc.src.__support.FPUtil.multiply_add
748+
libc.src.__support.FPUtil.nearest_integer
749+
libc.src.__support.FPUtil.polyeval
750+
libc.src.__support.FPUtil.rounding_mode
751+
libc.src.__support.macros.optimization
752+
libc.src.__support.common
753+
libc.src.errno.errno
754+
)
755+
738756
add_header_library(
739757
exp10
740758
HDRS

libc/src/math/generic/exp2f_impl.h renamed to libc/src/__support/math/exp2f.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
//===-- Single-precision 2^x function -------------------------------------===//
1+
//===-- Implementation header for exp2f -------------------------*- C++ -*-===//
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_EXP2F_IMPL_H
10-
#define LLVM_LIBC_SRC_MATH_GENERIC_EXP2F_IMPL_H
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP2F_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP2F_H
1111

12+
#include "exp10f_utils.h"
1213
#include "src/__support/FPUtil/FEnvImpl.h"
1314
#include "src/__support/FPUtil/FPBits.h"
1415
#include "src/__support/FPUtil/PolyEval.h"
@@ -20,12 +21,12 @@
2021
#include "src/__support/macros/config.h"
2122
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
2223
#include "src/__support/macros/properties/cpu_features.h"
23-
#include "src/__support/math/exp10f_utils.h"
2424

2525
namespace LIBC_NAMESPACE_DECL {
26-
namespace generic {
2726

28-
LIBC_INLINE float exp2f(float x) {
27+
namespace math {
28+
29+
LIBC_INLINE static constexpr float exp2f(float x) {
2930
using FPBits = typename fputil::FPBits<float>;
3031
FPBits xbits(x);
3132

@@ -120,8 +121,8 @@ LIBC_INLINE float exp2f(float x) {
120121
// of 2^mid.
121122

122123
// kf = (hi + mid) * 2^5 = round(x * 2^5)
123-
float kf;
124-
int k;
124+
float kf = 0;
125+
int k = 0;
125126
#ifdef LIBC_TARGET_CPU_HAS_NEAREST_INT
126127
kf = fputil::nearest_integer(x * 32.0f);
127128
k = static_cast<int>(kf);
@@ -161,7 +162,8 @@ LIBC_INLINE float exp2f(float x) {
161162
return static_cast<float>(fputil::multiply_add(p, dx_sq * mh, c1 * mh));
162163
}
163164

164-
} // namespace generic
165+
} // namespace math
166+
165167
} // namespace LIBC_NAMESPACE_DECL
166168

167-
#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXP2F_IMPL_H
169+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXP2F_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,32 +1451,14 @@ add_entrypoint_object(
14511451
libc.src.__support.math.exp2
14521452
)
14531453

1454-
add_header_library(
1455-
exp2f_impl
1456-
HDRS
1457-
exp2f_impl.h
1458-
DEPENDS
1459-
libc.src.__support.FPUtil.except_value_utils
1460-
libc.src.__support.FPUtil.fenv_impl
1461-
libc.src.__support.FPUtil.fp_bits
1462-
libc.src.__support.FPUtil.multiply_add
1463-
libc.src.__support.FPUtil.nearest_integer
1464-
libc.src.__support.FPUtil.polyeval
1465-
libc.src.__support.FPUtil.rounding_mode
1466-
libc.src.__support.macros.optimization
1467-
libc.src.__support.math.exp10f_utils
1468-
libc.src.__support.common
1469-
libc.src.errno.errno
1470-
)
1471-
14721454
add_entrypoint_object(
14731455
exp2f
14741456
SRCS
14751457
exp2f.cpp
14761458
HDRS
14771459
../exp2f.h
14781460
DEPENDS
1479-
.exp2f_impl
1461+
libc.src.__support.math.exp2f
14801462
)
14811463

14821464
add_entrypoint_object(
@@ -1659,8 +1641,8 @@ add_entrypoint_object(
16591641
HDRS
16601642
../powf.h
16611643
DEPENDS
1662-
.exp2f_impl
16631644
libc.src.__support.math.exp10f
1645+
libc.src.__support.math.exp2f
16641646
libc.src.__support.CPP.bit
16651647
libc.src.__support.FPUtil.fenv_impl
16661648
libc.src.__support.FPUtil.fp_bits

libc/src/math/generic/exp2f.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#include "src/math/exp2f.h"
1010
#include "src/__support/common.h" // for LLVM_LIBC_FUNCTION
1111
#include "src/__support/macros/config.h"
12-
#include "src/math/generic/exp2f_impl.h"
12+
#include "src/__support/math/exp2f.h"
1313

1414
namespace LIBC_NAMESPACE_DECL {
1515

16-
LLVM_LIBC_FUNCTION(float, exp2f, (float x)) { return generic::exp2f(x); }
16+
LLVM_LIBC_FUNCTION(float, exp2f, (float x)) { return math::exp2f(x); }
1717

1818
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/generic/powf.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
2121
#include "src/__support/math/common_constants.h" // Lookup tables EXP_M1 and EXP_M2.
2222
#include "src/__support/math/exp10f.h" // Speedup for powf(10, y) = exp10f(y)
23+
#include "src/__support/math/exp2f.h" // Speedup for powf(2, y) = exp2f(y)
2324
#include "src/__support/math/exp_constants.h"
2425

25-
#include "exp2f_impl.h" // Speedup for powf(2, y) = exp2f(y)
26-
2726
namespace LIBC_NAMESPACE_DECL {
2827

2928
using fputil::DoubleDouble;
@@ -779,7 +778,7 @@ LLVM_LIBC_FUNCTION(float, powf, (float x, float y)) {
779778
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
780779
case 0x4000'0000: // x = 2.0f
781780
// pow(2, y) = exp2(y)
782-
return generic::exp2f(y);
781+
return math::exp2f(y);
783782
case 0x4120'0000: // x = 10.0f
784783
// pow(10, y) = exp10(y)
785784
return math::exp10f(y);

libc/test/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ add_fp_unittest(
4141
libc.src.__support.math.erff
4242
libc.src.__support.math.exp
4343
libc.src.__support.math.exp2
44+
libc.src.__support.math.exp2f
4445
libc.src.__support.math.exp10
4546
libc.src.__support.math.exp10f
4647
libc.src.__support.math.exp10f16

libc/test/shared/shared_math_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
6262
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::erff(0.0f));
6363
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp10f(0.0f));
6464
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::expf(0.0f));
65+
EXPECT_FP_EQ(0x1p+0f, LIBC_NAMESPACE::shared::exp2f(0.0f));
6566

6667
EXPECT_FP_EQ_ALL_ROUNDING(0.75f,
6768
LIBC_NAMESPACE::shared::frexpf(24.0f, &exponent));

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,22 +2167,6 @@ libc_support_library(
21672167
],
21682168
)
21692169

2170-
libc_support_library(
2171-
name = "exp2f_impl",
2172-
hdrs = ["src/math/generic/exp2f_impl.h"],
2173-
deps = [
2174-
":__support_fputil_except_value_utils",
2175-
":__support_fputil_fma",
2176-
":__support_fputil_multiply_add",
2177-
":__support_fputil_nearest_integer",
2178-
":__support_fputil_polyeval",
2179-
":__support_fputil_rounding_mode",
2180-
":__support_macros_optimization",
2181-
":__support_math_common_constants",
2182-
":__support_math_exp10f_utils",
2183-
],
2184-
)
2185-
21862170
libc_support_library(
21872171
name = "__support_math_acos",
21882172
hdrs = ["src/__support/math/acos.h"],
@@ -2895,6 +2879,22 @@ libc_support_library(
28952879
],
28962880
)
28972881

2882+
libc_support_library(
2883+
name = "__support_math_exp2f",
2884+
hdrs = ["src/__support/math/exp2f.h"],
2885+
deps = [
2886+
":__support_fputil_except_value_utils",
2887+
":__support_fputil_fma",
2888+
":__support_fputil_multiply_add",
2889+
":__support_fputil_nearest_integer",
2890+
":__support_fputil_polyeval",
2891+
":__support_fputil_rounding_mode",
2892+
":__support_macros_optimization",
2893+
":__support_math_common_constants",
2894+
":__support_math_exp10f_utils",
2895+
],
2896+
)
2897+
28982898
libc_support_library(
28992899
name = "__support_math_exp10",
29002900
hdrs = ["src/__support/math/exp10.h"],
@@ -3676,7 +3676,7 @@ libc_math_function(
36763676
libc_math_function(
36773677
name = "exp2f",
36783678
additional_deps = [
3679-
":exp2f_impl",
3679+
":__support_math_exp2f",
36803680
],
36813681
)
36823682

@@ -4511,7 +4511,7 @@ libc_math_function(
45114511
":__support_macros_optimization",
45124512
":__support_math_exp10f",
45134513
":__support_math_common_constants",
4514-
":exp2f_impl",
4514+
":__support_math_exp2f",
45154515
],
45164516
)
45174517

0 commit comments

Comments
 (0)