Skip to content

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

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
merged 3 commits into from
Jul 15, 2025
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 @@ -11,6 +11,7 @@

#include "libc_common.h"

#include "math/exp.h"
#include "math/expf.h"
#include "math/expf16.h"
#include "math/frexpf.h"
Expand Down
23 changes: 23 additions & 0 deletions libc/shared/math/exp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- Shared exp 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_EXP_H
#define LLVM_LIBC_SHARED_MATH_EXP_H

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

namespace LIBC_NAMESPACE_DECL {
namespace shared {

using math::exp;

} // namespace shared
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SHARED_MATH_EXP_H
2 changes: 1 addition & 1 deletion libc/src/__support/FPUtil/multiply_add.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ multiply_add(const T &x, const T &y, const T &z) {
}

template <typename T>
LIBC_INLINE cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>
LIBC_INLINE static constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>
multiply_add(T x, T y, T z) {
return x * y + z;
}
Expand Down
4 changes: 2 additions & 2 deletions libc/src/__support/FPUtil/nearest_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace fputil {
// Notice that for AARCH64 and x86-64 with SSE4.2 support, we will use their
// corresponding rounding instruction instead. And in those cases, the results
// are rounded to the nearest integer, tie-to-even.
LIBC_INLINE float nearest_integer(float x) {
LIBC_INLINE static constexpr float nearest_integer(float x) {
if (x < 0x1p24f && x > -0x1p24f) {
float r = x < 0 ? (x - 0x1.0p23f) + 0x1.0p23f : (x + 0x1.0p23f) - 0x1.0p23f;
float diff = x - r;
Expand All @@ -56,7 +56,7 @@ LIBC_INLINE float nearest_integer(float x) {
return x;
}

LIBC_INLINE double nearest_integer(double x) {
LIBC_INLINE static constexpr double nearest_integer(double x) {
if (x < 0x1p53 && x > -0x1p53) {
double r = x < 0 ? (x - 0x1.0p52) + 0x1.0p52 : (x + 0x1.0p52) - 0x1.0p52;
double diff = x - r;
Expand Down
39 changes: 39 additions & 0 deletions libc/src/__support/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,42 @@ add_header_library(
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
)

add_header_library(
exp_constants
HDRS
exp_constants.h
DEPENDS
libc.src.__support.FPUtil.triple_double
)

add_header_library(
exp_utils
HDRS
exp_utils.h
DEPENDS
libc.src.__support.CPP.optional
libc.src.__support.CPP.bit
libc.src.__support.FPUtil.fp_bits
)

add_header_library(
exp
HDRS
exp.h
DEPENDS
.exp_constants
.exp_utils
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
libc.src.__support.integer_literals
libc.src.__support.macros.optimization
)
Loading
Loading