Skip to content

Commit d16fa92

Browse files
committed
Reapply "[libc][math] Refactor exp implementation to header-only in src/__support/math folder." (#148668)
This reverts commit f4630ba.
1 parent 57c3d65 commit d16fa92

File tree

20 files changed

+656
-116
lines changed

20 files changed

+656
-116
lines changed

libc/fuzzing/math/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,42 @@ add_libc_fuzzer(
6262
libc.src.math.nextafterl
6363
)
6464

65+
add_libc_fuzzer(
66+
exp_fuzz
67+
NEED_MPFR
68+
SRCS
69+
exp_fuzz.cpp
70+
DEPENDS
71+
libc.src.math.exp
72+
)
73+
74+
add_libc_fuzzer(
75+
exp10_fuzz
76+
NEED_MPFR
77+
SRCS
78+
exp10_fuzz.cpp
79+
DEPENDS
80+
libc.src.math.exp10
81+
)
82+
83+
add_libc_fuzzer(
84+
exp2_fuzz
85+
NEED_MPFR
86+
SRCS
87+
exp2_fuzz.cpp
88+
DEPENDS
89+
libc.src.math.exp2
90+
)
91+
92+
add_libc_fuzzer(
93+
expm1_fuzz
94+
NEED_MPFR
95+
SRCS
96+
expm1_fuzz.cpp
97+
DEPENDS
98+
libc.src.math.expm1
99+
)
100+
65101
add_libc_fuzzer(
66102
asin_fuzz
67103
NEED_MPFR

libc/fuzzing/math/exp10_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- exp10_fuzz.cpp ----------------------------------------------------===//
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+
/// Fuzzing test for llvm-libc exp10 implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/exp10.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <math.h>
16+
17+
extern "C" int LLVMFuzzerTestOneInput(double x) {
18+
// remove NaN and inf
19+
if (isnan(x) || isinf(x))
20+
return 0;
21+
// signed zeros already tested in unit tests
22+
if (signbit(x) && x == 0.0)
23+
return 0;
24+
mpfr_t input;
25+
mpfr_init2(input, 53);
26+
mpfr_set_d(input, x, MPFR_RNDN);
27+
int output = mpfr_exp10(input, input, MPFR_RNDN);
28+
mpfr_subnormalize(input, output, MPFR_RNDN);
29+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
30+
31+
double result = LIBC_NAMESPACE::exp10(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

libc/fuzzing/math/exp2_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- exp2_fuzz.cpp -----------------------------------------------------===//
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+
/// Fuzzing test for llvm-libc exp2 implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/exp2.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <math.h>
16+
17+
extern "C" int LLVMFuzzerTestOneInput(double x) {
18+
// remove NaN and inf
19+
if (isnan(x) || isinf(x))
20+
return 0;
21+
// signed zeros already tested in unit tests
22+
if (signbit(x) && x == 0.0)
23+
return 0;
24+
mpfr_t input;
25+
mpfr_init2(input, 53);
26+
mpfr_set_d(input, x, MPFR_RNDN);
27+
int output = mpfr_exp2(input, input, MPFR_RNDN);
28+
mpfr_subnormalize(input, output, MPFR_RNDN);
29+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
30+
31+
double result = LIBC_NAMESPACE::exp2(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

libc/fuzzing/math/exp_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- exp_fuzz.cpp ------------------------------------------------------===//
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+
/// Fuzzing test for llvm-libc exp implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/exp.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <math.h>
16+
17+
extern "C" int LLVMFuzzerTestOneInput(double x) {
18+
// remove NaN and inf
19+
if (isnan(x) || isinf(x))
20+
return 0;
21+
// signed zeros already tested in unit tests
22+
if (signbit(x) && x == 0.0)
23+
return 0;
24+
mpfr_t input;
25+
mpfr_init2(input, 53);
26+
mpfr_set_d(input, x, MPFR_RNDN);
27+
int output = mpfr_exp(input, input, MPFR_RNDN);
28+
mpfr_subnormalize(input, output, MPFR_RNDN);
29+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
30+
31+
double result = LIBC_NAMESPACE::exp(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

libc/fuzzing/math/expm1_fuzz.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- expm1_fuzz.cpp ----------------------------------------------------===//
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+
/// Fuzzing test for llvm-libc expm1 implementation.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "src/math/expm1.h"
14+
#include "utils/MPFRWrapper/mpfr_inc.h"
15+
#include <math.h>
16+
17+
extern "C" int LLVMFuzzerTestOneInput(double x) {
18+
// remove NaN and inf
19+
if (isnan(x) || isinf(x))
20+
return 0;
21+
// signed zeros already tested in unit tests
22+
if (signbit(x) && x == 0.0)
23+
return 0;
24+
mpfr_t input;
25+
mpfr_init2(input, 53);
26+
mpfr_set_d(input, x, MPFR_RNDN);
27+
int output = mpfr_expm1(input, input, MPFR_RNDN);
28+
mpfr_subnormalize(input, output, MPFR_RNDN);
29+
double to_compare = mpfr_get_d(input, MPFR_RNDN);
30+
31+
double result = LIBC_NAMESPACE::expm1(x);
32+
33+
if (result != to_compare)
34+
__builtin_trap();
35+
36+
mpfr_clear(input);
37+
return 0;
38+
}

libc/shared/math.h

Lines changed: 1 addition & 1 deletion
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"
@@ -19,6 +20,5 @@
1920
#include "math/ldexpf.h"
2021
#include "math/ldexpf128.h"
2122
#include "math/ldexpf16.h"
22-
#include "math/exp.h"
2323

2424
#endif // LLVM_LIBC_SHARED_MATH_H

libc/src/__support/math/exp.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "src/__support/macros/config.h"
2828
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
2929

30-
3130
namespace LIBC_NAMESPACE_DECL {
3231

3332
using fputil::DoubleDouble;
@@ -68,7 +67,7 @@ namespace {
6867
// Return expm1(dx) / x ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24.
6968
// For |dx| < 2^-13 + 2^-30:
7069
// | output - expm1(dx) / dx | < 2^-51.
71-
static constexpr double poly_approx_d(double dx) {
70+
static double poly_approx_d(double dx) {
7271
// dx^2
7372
double dx2 = dx * dx;
7473
// c0 = 1 + dx / 2
@@ -86,7 +85,7 @@ static constexpr double poly_approx_d(double dx) {
8685
// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^6 / 720
8786
// For |dx| < 2^-13 + 2^-30:
8887
// | output - exp(dx) | < 2^-101
89-
static constexpr DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
88+
static DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
9089
// Taylor polynomial.
9190
constexpr DoubleDouble COEFFS[] = {
9291
{0, 0x1p0}, // 1
@@ -107,7 +106,7 @@ static constexpr DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
107106
// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^7 / 5040
108107
// For |dx| < 2^-13 + 2^-30:
109108
// | output - exp(dx) | < 2^-126.
110-
static constexpr Float128 poly_approx_f128(const Float128 &dx) {
109+
static Float128 poly_approx_f128(const Float128 &dx) {
111110
constexpr Float128 COEFFS_128[]{
112111
{Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
113112
{Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
@@ -128,7 +127,7 @@ static constexpr Float128 poly_approx_f128(const Float128 &dx) {
128127
// Compute exp(x) using 128-bit precision.
129128
// TODO(lntue): investigate triple-double precision implementation for this
130129
// step.
131-
static constexpr Float128 exp_f128(double x, double kd, int idx1, int idx2) {
130+
static Float128 exp_f128(double x, double kd, int idx1, int idx2) {
132131
// Recalculate dx:
133132

134133
double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact
@@ -161,8 +160,8 @@ static constexpr Float128 exp_f128(double x, double kd, int idx1, int idx2) {
161160
}
162161

163162
// Compute exp(x) with double-double precision.
164-
static constexpr DoubleDouble exp_double_double(double x, double kd,
165-
const DoubleDouble &exp_mid) {
163+
static DoubleDouble exp_double_double(double x, double kd,
164+
const DoubleDouble &exp_mid) {
166165
// Recalculate dx:
167166
// dx = x - k * 2^-12 * log(2)
168167
double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact
@@ -185,7 +184,7 @@ static constexpr DoubleDouble exp_double_double(double x, double kd,
185184

186185
// Check for exceptional cases when
187186
// |x| <= 2^-53 or x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9
188-
static constexpr double set_exceptional(double x) {
187+
static double set_exceptional(double x) {
189188
using FPBits = typename fputil::FPBits<double>;
190189
FPBits xbits(x);
191190

@@ -235,7 +234,7 @@ static constexpr double set_exceptional(double x) {
235234

236235
namespace math {
237236

238-
static constexpr double exp(double x) {
237+
static double exp(double x) {
239238
using FPBits = typename fputil::FPBits<double>;
240239
FPBits xbits(x);
241240

libc/src/__support/math/exp_utils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXP_UTILS_H
1010
#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP_UTILS_H
1111

12-
#include "src/__support/CPP/optional.h"
1312
#include "src/__support/CPP/bit.h"
13+
#include "src/__support/CPP/optional.h"
1414
#include "src/__support/FPUtil/FPBits.h"
1515

16-
1716
namespace LIBC_NAMESPACE_DECL {
1817

1918
// Rounding tests for 2^hi * (mid + lo) when the output might be denormal. We
@@ -23,8 +22,8 @@ namespace LIBC_NAMESPACE_DECL {
2322
// So if we scale x up by 2^1022, we can use
2423
// double(1.0 + 2^1022 * x) - 1.0 to test how x is rounded in denormal range.
2524
template <bool SKIP_ZIV_TEST = false>
26-
static constexpr cpp::optional<double>
27-
ziv_test_denorm(int hi, double mid, double lo, double err) {
25+
static constexpr cpp::optional<double> ziv_test_denorm(int hi, double mid,
26+
double lo, double err) {
2827
using FPBits = typename fputil::FPBits<double>;
2928

3029
// Scaling factor = 1/(min normal number) = 2^1022

libc/src/math/generic/common_constants.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
#include "src/__support/FPUtil/triple_double.h"
1313
#include "src/__support/macros/config.h"
14-
#include "src/__support/number_pair.h"
1514
#include "src/__support/math/exp_constants.h"
16-
15+
#include "src/__support/number_pair.h"
1716

1817
namespace LIBC_NAMESPACE_DECL {
1918

libc/src/math/generic/exp.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include "src/__support/math/exp.h"
1111
namespace LIBC_NAMESPACE_DECL {
1212

13-
LLVM_LIBC_FUNCTION(double, exp, (double x)) {
14-
return math::exp(x);
15-
}
13+
LLVM_LIBC_FUNCTION(double, exp, (double x)) { return math::exp(x); }
1614

1715
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)