[libc-commits] [libc] [libc][FPUtil] Make hardware helpers usable in constant expressions (PR #242283)

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Sun Nov 15 12:50:50 PST 2026


================
@@ -89,6 +70,56 @@ LIBC_INLINE constexpr double nearest_integer(double x) {
   return x;
 }
 
+} // namespace generic
+} // namespace fputil
+} // namespace LIBC_NAMESPACE_DECL
+
+// The target specific headers below define fputil::nearest_integer using the
+// rounding instructions of the target, and dispatch to generic::nearest_integer
+// during constant evaluation.
+#if (defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE4_2))
+#include "x86_64/nearest_integer.h"
+#elif (defined(LIBC_TARGET_ARCH_IS_AARCH64) &&                                 \
+       defined(LIBC_TARGET_CPU_HAS_FPU_FLOAT) &&                               \
+       defined(LIBC_TARGET_CPU_HAS_FPU_DOUBLE))
+#include "aarch64/nearest_integer.h"
+#elif defined(LIBC_TARGET_ARCH_IS_GPU)
+
+namespace LIBC_NAMESPACE_DECL {
+namespace fputil {
+
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float nearest_integer(float x) {
+  if (cpp::is_constant_evaluated()) {
+    return generic::nearest_integer(x);
+  } else {
+    return __builtin_rintf(x);
+  }
+}
+
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double nearest_integer(double x) {
+  if (cpp::is_constant_evaluated()) {
+    return generic::nearest_integer(x);
+  } else {
+    return __builtin_rint(x);
+  }
+}
----------------
bassiounix wrote:

I think we should leave the GPU utils without `constexpr`

cc @jhuber6 @lntue 

https://github.com/llvm/llvm-project/pull/242283


More information about the libc-commits mailing list