[libc] Add implementations of nearbyint[f|l].
The implementation is exactly the same as rint* as even rint does not
raise any floating point exceptions currently. [Note that the standards
do not specify that floating point exceptions must be raised - they
leave it up to the implementation to choose to raise FE_INEXACT when
rounding non-integral values.]
Reviewed By: lntue
Differential Revision: https://reviews.llvm.org/D94112
GitOrigin-RevId: 993d8ac5cb935b78fb136c25a7e4bae18852f429
diff --git a/config/linux/x86_64/entrypoints.txt b/config/linux/x86_64/entrypoints.txt
index 8e430af..45571ee 100644
--- a/config/linux/x86_64/entrypoints.txt
+++ b/config/linux/x86_64/entrypoints.txt
@@ -141,6 +141,9 @@
libc.src.math.modf
libc.src.math.modff
libc.src.math.modfl
+ libc.src.math.nearbyint
+ libc.src.math.nearbyintf
+ libc.src.math.nearbyintl
libc.src.math.remainderf
libc.src.math.remainder
libc.src.math.remainderl
diff --git a/spec/stdc.td b/spec/stdc.td
index 0eea8e5..355321c 100644
--- a/spec/stdc.td
+++ b/spec/stdc.td
@@ -390,6 +390,10 @@
FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"truncf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"truncl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+
+ FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
+ FunctionSpec<"nearbyintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
+ FunctionSpec<"nearbyintl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
]
>;
diff --git a/src/math/CMakeLists.txt b/src/math/CMakeLists.txt
index ff9d791..aa4b4a5 100644
--- a/src/math/CMakeLists.txt
+++ b/src/math/CMakeLists.txt
@@ -416,6 +416,42 @@
-O2
)
+add_entrypoint_object(
+ nearbyint
+ SRCS
+ nearbyint.cpp
+ HDRS
+ nearbyint.h
+ DEPENDS
+ libc.utils.FPUtil.fputil
+ COMPILE_OPTIONS
+ -O2
+)
+
+add_entrypoint_object(
+ nearbyintf
+ SRCS
+ nearbyintf.cpp
+ HDRS
+ nearbyintf.h
+ DEPENDS
+ libc.utils.FPUtil.fputil
+ COMPILE_OPTIONS
+ -O2
+)
+
+add_entrypoint_object(
+ nearbyintl
+ SRCS
+ nearbyintl.cpp
+ HDRS
+ nearbyintl.h
+ DEPENDS
+ libc.utils.FPUtil.fputil
+ COMPILE_OPTIONS
+ -O2
+)
+
add_object_library(
exp_utils
HDRS
diff --git a/src/math/nearbyint.cpp b/src/math/nearbyint.cpp
new file mode 100644
index 0000000..0938952
--- /dev/null
+++ b/src/math/nearbyint.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of nearbyint function ------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/NearestIntegerOperations.h"
+
+namespace __llvm_libc {
+
+double LLVM_LIBC_ENTRYPOINT(nearbyint)(double x) {
+ return fputil::roundUsingCurrentRoundingMode(x);
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/nearbyint.h b/src/math/nearbyint.h
new file mode 100644
index 0000000..957a06b
--- /dev/null
+++ b/src/math/nearbyint.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for nearbyint ---------------------*- 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_SRC_MATH_NEARBYINT_H
+#define LLVM_LIBC_SRC_MATH_NEARBYINT_H
+
+namespace __llvm_libc {
+
+double nearbyint(double x);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_NEARBYINT_H
diff --git a/src/math/nearbyintf.cpp b/src/math/nearbyintf.cpp
new file mode 100644
index 0000000..cc19da6
--- /dev/null
+++ b/src/math/nearbyintf.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of nearbyintf function -----------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/NearestIntegerOperations.h"
+
+namespace __llvm_libc {
+
+float LLVM_LIBC_ENTRYPOINT(nearbyintf)(float x) {
+ return fputil::roundUsingCurrentRoundingMode(x);
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/nearbyintf.h b/src/math/nearbyintf.h
new file mode 100644
index 0000000..3793f6b
--- /dev/null
+++ b/src/math/nearbyintf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for nearbyintf --------------------*- 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_SRC_MATH_NEARBYINTF_H
+#define LLVM_LIBC_SRC_MATH_NEARBYINTF_H
+
+namespace __llvm_libc {
+
+float nearbyintf(float x);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_NEARBYINTF_H
diff --git a/src/math/nearbyintl.cpp b/src/math/nearbyintl.cpp
new file mode 100644
index 0000000..5618832
--- /dev/null
+++ b/src/math/nearbyintl.cpp
@@ -0,0 +1,18 @@
+//===-- Implementation of nearbyintl function -----------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "utils/FPUtil/NearestIntegerOperations.h"
+
+namespace __llvm_libc {
+
+long double LLVM_LIBC_ENTRYPOINT(nearbyintl)(long double x) {
+ return fputil::roundUsingCurrentRoundingMode(x);
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/nearbyintl.h b/src/math/nearbyintl.h
new file mode 100644
index 0000000..7029e86
--- /dev/null
+++ b/src/math/nearbyintl.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for nearbyintl --------------------*- 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_SRC_MATH_NEARBYINTL_H
+#define LLVM_LIBC_SRC_MATH_NEARBYINTL_H
+
+namespace __llvm_libc {
+
+long double nearbyintl(long double x);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_NEARBYINTL_H