Skip to content

Commit 7f7b0dc

Browse files
author
Siva Chandra Reddy
committed
[libc] Add implementations of nextafter[f|l] functions.
A differential fuzzer for these functions has also been added. Along the way, a small correction has been done to the normal/subnormal limits of x86 long double values. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D94109
1 parent 993d8ac commit 7f7b0dc

21 files changed

Lines changed: 618 additions & 1 deletion

‎libc/config/linux/aarch64/entrypoints.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ set(TARGET_LIBM_ENTRYPOINTS
8888
libc.src.math.modf
8989
libc.src.math.modff
9090
libc.src.math.modfl
91+
libc.src.math.nextafter
92+
libc.src.math.nextafterf
93+
libc.src.math.nextafterl
9194
libc.src.math.remainderf
9295
libc.src.math.remainder
9396
libc.src.math.remainderl

‎libc/config/linux/x86_64/entrypoints.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ set(TARGET_LIBM_ENTRYPOINTS
144144
libc.src.math.nearbyint
145145
libc.src.math.nearbyintf
146146
libc.src.math.nearbyintl
147+
libc.src.math.nextafter
148+
libc.src.math.nextafterf
149+
libc.src.math.nextafterl
147150
libc.src.math.remainderf
148151
libc.src.math.remainder
149152
libc.src.math.remainderl

‎libc/fuzzing/math/CMakeLists.txt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,15 @@ add_libc_fuzzer(
4848
libc.utils.FPUtil.fputil
4949
libc.utils.CPP.standalone_cpp
5050
)
51+
52+
add_libc_fuzzer(
53+
nextafter_differential_fuzz
54+
SRCS
55+
nextafter_differential_fuzz.cpp
56+
HDRS
57+
TwoInputSingleOutputDiff.h
58+
DEPENDS
59+
libc.src.math.nextafter
60+
libc.src.math.nextafterf
61+
libc.src.math.nextafterl
62+
)

‎libc/fuzzing/math/Compare.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_LIBC_FUZZING_MATH_COMPARE_H
1111

1212
#include "utils/CPP/TypeTraits.h"
13+
#include "utils/FPUtil/FPBits.h"
1314

1415
template <typename T>
1516
__llvm_libc::cpp::EnableIfType<__llvm_libc::cpp::IsFloatingPointType<T>::Value,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- nextafter_differential_fuzz.cpp
2+
//---------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// Differential fuzz test for llvm-libc nextafter implementation.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "fuzzing/math/TwoInputSingleOutputDiff.h"
15+
16+
#include "src/math/nextafter.h"
17+
#include "src/math/nextafterf.h"
18+
#include "src/math/nextafterl.h"
19+
20+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
21+
TwoInputSingleOutputDiff<float, float>(&__llvm_libc::nextafterf,
22+
&::nextafterf, data, size);
23+
TwoInputSingleOutputDiff<double, double>(&__llvm_libc::nextafter,
24+
&::nextafter, data, size);
25+
return 0;
26+
}

‎libc/spec/stdc.td‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ def StdC : StandardSpec<"stdc"> {
394394
FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
395395
FunctionSpec<"nearbyintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
396396
FunctionSpec<"nearbyintl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
397+
398+
FunctionSpec<"nextafterf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
399+
FunctionSpec<"nextafter", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
400+
FunctionSpec<"nextafterl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
397401
]
398402
>;
399403

‎libc/src/math/CMakeLists.txt‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,3 +941,40 @@ add_entrypoint_object(
941941
COMPILE_OPTIONS
942942
-O2
943943
)
944+
945+
add_entrypoint_object(
946+
nextafter
947+
SRCS
948+
nextafter.cpp
949+
HDRS
950+
nextafter.h
951+
DEPENDS
952+
libc.utils.FPUtil.fputil
953+
COMPILE_OPTIONS
954+
-O2
955+
)
956+
957+
add_entrypoint_object(
958+
nextafterf
959+
SRCS
960+
nextafterf.cpp
961+
HDRS
962+
nextafterf.h
963+
DEPENDS
964+
libc.utils.FPUtil.fputil
965+
COMPILE_OPTIONS
966+
-O2
967+
)
968+
969+
add_entrypoint_object(
970+
nextafterl
971+
SRCS
972+
nextafterl.cpp
973+
HDRS
974+
nextafterl.h
975+
DEPENDS
976+
libc.utils.FPUtil.fputil
977+
COMPILE_OPTIONS
978+
-O2
979+
)
980+

‎libc/src/math/nextafter.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of nextafter function ------------------------------===//
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+
#include "src/__support/common.h"
10+
#include "utils/FPUtil/ManipulationFunctions.h"
11+
12+
namespace __llvm_libc {
13+
14+
double LLVM_LIBC_ENTRYPOINT(nextafter)(double x, double y) {
15+
return fputil::nextafter(x, y);
16+
}
17+
18+
} // namespace __llvm_libc

‎libc/src/math/nextafter.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for nextafter ---------------------*- 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_MATH_NEXTAFTER_H
10+
#define LLVM_LIBC_SRC_MATH_NEXTAFTER_H
11+
12+
namespace __llvm_libc {
13+
14+
double nextafter(double x, double y);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_MATH_NEXTAFTER_H

‎libc/src/math/nextafterf.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of nextafterf function -----------------------------===//
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+
#include "src/__support/common.h"
10+
#include "utils/FPUtil/ManipulationFunctions.h"
11+
12+
namespace __llvm_libc {
13+
14+
float LLVM_LIBC_ENTRYPOINT(nextafterf)(float x, float y) {
15+
return fputil::nextafter(x, y);
16+
}
17+
18+
} // namespace __llvm_libc

0 commit comments

Comments
 (0)