[libc-commits] [libc] [libc][math] Add some explicit macro checks to round and roundf. (PR #217822)
via libc-commits
libc-commits at lists.llvm.org
Thu Aug 20 22:49:26 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: lntue
<details>
<summary>Changes</summary>
This will allow users to use them without going through our cmake build checks or forcing the flag `__LIBC_USE_BUILTIN_ROUND`.
---
Full diff: https://github.com/llvm/llvm-project/pull/217822.diff
2 Files Affected:
- (modified) libc/src/__support/math/round.h (+28-1)
- (modified) libc/src/__support/math/roundf.h (+28-1)
``````````diff
diff --git a/libc/src/__support/math/round.h b/libc/src/__support/math/round.h
index 8cbfa3ff79e9a..d35c9f437a630 100644
--- a/libc/src/__support/math/round.h
+++ b/libc/src/__support/math/round.h
@@ -10,13 +10,40 @@
#define LLVM_LIBC_SRC___SUPPORT_MATH_ROUND_H
#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/compiler.h"
+#include "src/__support/macros/properties/cpu_features.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
+// For the following targets, clang will generate rounding instructions
+// by default:
+// - x86-64 with sse4.1 or after
+// - ARM v8
+// - RISC-V
+
+// Notes: gcc does not generate the instructions for x86-64 by default.
+
+// Notes: for x86-64, if `-ffp-model=strict` is set, `__builtin_round` will
+// generate a callback to `round` and it does not look like there is any way for
+// us to detect that just from the pre-defined macros. The only way to really
+// detect the call back is to compile and link with nostdlib.
+// This also affects `__builtin_elementwise_round`, making it behave identical
+// to `__builtin_round`.
+
+// Notes: `__builtin_elementwise_round` is slightly better than
+// `__builtin_round` in that it is not defined for x86-64 pre-SSE4.1, but it
+// still generate callback for ARM version < 8, and for x86-64 with
+// `-ffp-model=strict`.
+
LIBC_INLINE LIBC_CONSTEXPR double round(double x) {
-#if defined(__LIBC_USE_BUILTIN_ROUND) && !defined(LIBC_USE_CONSTEXPR)
+#if __has_builtin(__builtin_round) && !defined(LIBC_USE_CONSTEXPR) && \
+ (defined(__LIBC_USE_BUILTIN_ROUND) || \
+ (defined(LIBC_COMPILER_IS_CLANG) && \
+ defined(LIBC_TARGET_CPU_HAS_FPU_DOUBLE) && \
+ (!defined(__ARM_ARCH) || (__ARM_ARCH >= 8))))
return __builtin_round(x);
#else
return fputil::round(x);
diff --git a/libc/src/__support/math/roundf.h b/libc/src/__support/math/roundf.h
index e20b9d2e7f763..5abe9ebad15b0 100644
--- a/libc/src/__support/math/roundf.h
+++ b/libc/src/__support/math/roundf.h
@@ -10,13 +10,40 @@
#define LLVM_LIBC_SRC___SUPPORT_MATH_ROUNDF_H
#include "src/__support/FPUtil/NearestIntegerOperations.h"
+#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/compiler.h"
+#include "src/__support/macros/properties/cpu_features.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
+// For the following targets, clang will generate rounding instructions
+// by default:
+// - x86-64 with sse4.1 or after
+// - ARM v8
+// - RISC-V
+
+// Notes: gcc does not generate the instructions for x86-64 by default.
+
+// Notes: for x86-64, if `-ffp-model=strict` is set, `__builtin_round` will
+// generate a callback to `round` and it does not look like there is any way for
+// us to detect that just from the pre-defined macros. The only way to really
+// detect the call back is to compile and link with nostdlib.
+// This also affects `__builtin_elementwise_round`, making it behave identical
+// to `__builtin_round`.
+
+// Notes: `__builtin_elementwise_round` is slightly better than
+// `__builtin_round` in that it is not defined for x86-64 pre-SSE4.1, but it
+// still generate callback for ARM version < 8, and for x86-64 with
+// `-ffp-model=strict`.
+
LIBC_INLINE LIBC_CONSTEXPR float roundf(float x) {
-#if defined(__LIBC_USE_BUILTIN_ROUND) && !defined(LIBC_USE_CONSTEXPR)
+#if __has_builtin(__builtin_roundf) && !defined(LIBC_USE_CONSTEXPR) && \
+ (defined(__LIBC_USE_BUILTIN_ROUND) || \
+ (defined(LIBC_COMPILER_IS_CLANG) && \
+ defined(LIBC_TARGET_CPU_HAS_FPU_FLOAT) && \
+ (!defined(__ARM_ARCH) || (__ARM_ARCH >= 8))))
return __builtin_roundf(x);
#else
return fputil::round(x);
``````````
</details>
https://github.com/llvm/llvm-project/pull/217822
More information about the libc-commits
mailing list