Skip to content

Commit d969ec9

Browse files
authored
[libc][math] Refactor exp implementation to header-only in src/__support/math folder. (#148761)
- **[libc][math] Refactor exp implementation to header-only in src/__support/math folder.** - **Reapply "[libc][math] Refactor exp implementation to header-only in src/__support/math folder." (#148668)**
1 parent 55f1b91 commit d969ec9

File tree

15 files changed

+813
-681
lines changed

15 files changed

+813
-681
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "libc_common.h"
1313

14+
#include "math/exp.h"
1415
#include "math/expf.h"
1516
#include "math/expf16.h"
1617
#include "math/frexpf.h"

libc/shared/math/exp.h

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

libc/src/__support/FPUtil/multiply_add.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ multiply_add(const T &x, const T &y, const T &z) {
2929
}
3030

3131
template <typename T>
32-
LIBC_INLINE cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>
32+
LIBC_INLINE static constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>
3333
multiply_add(T x, T y, T z) {
3434
return x * y + z;
3535
}

libc/src/__support/FPUtil/nearest_integer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace fputil {
4040
// Notice that for AARCH64 and x86-64 with SSE4.2 support, we will use their
4141
// corresponding rounding instruction instead. And in those cases, the results
4242
// are rounded to the nearest integer, tie-to-even.
43-
LIBC_INLINE float nearest_integer(float x) {
43+
LIBC_INLINE static constexpr float nearest_integer(float x) {
4444
if (x < 0x1p24f && x > -0x1p24f) {
4545
float r = x < 0 ? (x - 0x1.0p23f) + 0x1.0p23f : (x + 0x1.0p23f) - 0x1.0p23f;
4646
float diff = x - r;
@@ -56,7 +56,7 @@ LIBC_INLINE float nearest_integer(float x) {
5656
return x;
5757
}
5858

59-
LIBC_INLINE double nearest_integer(double x) {
59+
LIBC_INLINE static constexpr double nearest_integer(double x) {
6060
if (x < 0x1p53 && x > -0x1p53) {
6161
double r = x < 0 ? (x - 0x1.0p52) + 0x1.0p52 : (x + 0x1.0p52) - 0x1.0p52;
6262
double diff = x - r;

libc/src/__support/math/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,42 @@ add_header_library(
110110
DEPENDS
111111
libc.src.__support.FPUtil.manipulation_functions
112112
)
113+
114+
add_header_library(
115+
exp_constants
116+
HDRS
117+
exp_constants.h
118+
DEPENDS
119+
libc.src.__support.FPUtil.triple_double
120+
)
121+
122+
add_header_library(
123+
exp_utils
124+
HDRS
125+
exp_utils.h
126+
DEPENDS
127+
libc.src.__support.CPP.optional
128+
libc.src.__support.CPP.bit
129+
libc.src.__support.FPUtil.fp_bits
130+
)
131+
132+
add_header_library(
133+
exp
134+
HDRS
135+
exp.h
136+
DEPENDS
137+
.exp_constants
138+
.exp_utils
139+
libc.src.__support.CPP.bit
140+
libc.src.__support.CPP.optional
141+
libc.src.__support.FPUtil.dyadic_float
142+
libc.src.__support.FPUtil.fenv_impl
143+
libc.src.__support.FPUtil.fp_bits
144+
libc.src.__support.FPUtil.multiply_add
145+
libc.src.__support.FPUtil.nearest_integer
146+
libc.src.__support.FPUtil.polyeval
147+
libc.src.__support.FPUtil.rounding_mode
148+
libc.src.__support.FPUtil.triple_double
149+
libc.src.__support.integer_literals
150+
libc.src.__support.macros.optimization
151+
)

0 commit comments

Comments
 (0)