[libc-commits] [libc] [libc][FPUtil] Make hardware helpers usable in constant expressions (PR #242283)
via libc-commits
libc-commits at lists.llvm.org
Fri Nov 13 05:59:55 PST 2026
https://github.com/linisha15 updated https://github.com/llvm/llvm-project/pull/242283
>From e4df7795512f94c44b4dfeaab4cb5c6a705e57e5 Mon Sep 17 00:00:00 2001
From: linisha <linisha232 at gmail.com>
Date: Fri, 13 Nov 2026 19:29:33 +0530
Subject: [PATCH] [libc][FPUtil] Make hardware helpers usable in constant
expressions
Part of #195623 (split 1/2).
---
libc/src/__support/FPUtil/CMakeLists.txt | 2 +
libc/src/__support/FPUtil/FEnvImpl.h | 28 ++++---
libc/src/__support/FPUtil/FMA.h | 43 +++++++----
libc/src/__support/FPUtil/Hypot.h | 10 +--
libc/src/__support/FPUtil/PolyEval.h | 2 +-
libc/src/__support/FPUtil/aarch64/FEnvImpl.h | 31 ++++----
.../FPUtil/aarch64/fenv_darwin_impl.h | 31 ++++----
.../FPUtil/aarch64/nearest_integer.h | 30 +++++++-
libc/src/__support/FPUtil/aarch64/sqrt.h | 31 +++++++-
libc/src/__support/FPUtil/arm/FEnvImpl.h | 32 ++++----
libc/src/__support/FPUtil/arm/sqrt.h | 31 +++++++-
libc/src/__support/FPUtil/double_double.h | 13 ++--
libc/src/__support/FPUtil/dyadic_float.h | 2 +-
libc/src/__support/FPUtil/generic/FMA.h | 8 +-
libc/src/__support/FPUtil/generic/sqrt.h | 16 ++++
libc/src/__support/FPUtil/hexagon/FEnvImpl.h | 29 +++++---
libc/src/__support/FPUtil/multiply_add.h | 41 ++++++----
libc/src/__support/FPUtil/nearest_integer.h | 74 ++++++++++++++-----
libc/src/__support/FPUtil/riscv/FEnvImpl.h | 31 ++++----
libc/src/__support/FPUtil/riscv/sqrt.h | 31 +++++++-
libc/src/__support/FPUtil/sqrt.h | 27 +++++--
libc/src/__support/FPUtil/x86_64/FEnvImpl.h | 11 ++-
.../__support/FPUtil/x86_64/nearest_integer.h | 31 +++++++-
libc/src/__support/FPUtil/x86_64/sqrt.h | 41 +++++++++-
24 files changed, 462 insertions(+), 164 deletions(-)
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index f2d07ab222bab..93c3e6f123915 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -135,6 +135,7 @@ add_header_library(
sqrt.h
DEPENDS
libc.src.__support.common
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.generic.sqrt
libc.src.__support.macros.properties.cpu_features
)
@@ -158,6 +159,7 @@ add_header_library(
multiply_add.h
DEPENDS
libc.src.__support.common
+ libc.src.__support.CPP.type_traits
libc.src.__support.macros.properties.cpu_features
FLAGS
FMA_OPT
diff --git a/libc/src/__support/FPUtil/FEnvImpl.h b/libc/src/__support/FPUtil/FEnvImpl.h
index 6b6929e6978b5..3fc1d38611686 100644
--- a/libc/src/__support/FPUtil/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/FEnvImpl.h
@@ -32,7 +32,8 @@
#undef LIBC_MATH_USE_SYSTEM_FENV
#endif // LIBC_FULL_BUILD
-#if defined(LIBC_MATH_USE_SYSTEM_FENV) && !defined(LIBC_MATH_HAS_NO_EXCEPT)
+#if defined(LIBC_MATH_USE_SYSTEM_FENV) && !defined(LIBC_MATH_HAS_NO_EXCEPT) && \
+ !defined(LIBC_USE_CONSTEXPR)
// Simply call the system libc fenv.h functions, only for those that are used in
// math function implementations.
@@ -76,9 +77,13 @@ LIBC_INLINE int enable_except(int) { return 0; }
LIBC_INLINE int disable_except(int) { return 0; }
-LIBC_INLINE int get_round() {
- LIBC_FENV_ACCESS_ON
- return fegetround();
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int get_round() {
+ if (cpp::is_constant_evaluated()) {
+ return FE_TONEAREST;
+ } else {
+ LIBC_FENV_ACCESS_ON
+ return fegetround();
+ }
}
LIBC_INLINE int set_round(int rounding_mode) {
@@ -101,7 +106,8 @@ LIBC_INLINE int set_env(const fenv_t *env) {
#else // !LIBC_MATH_USE_SYSTEM_FENV
-#if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP)
+#if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP) && \
+ !defined(LIBC_USE_CONSTEXPR)
#if defined(__APPLE__)
#include "src/__support/FPUtil/aarch64/fenv_darwin_impl.h"
#else
@@ -112,14 +118,16 @@ LIBC_INLINE int set_env(const fenv_t *env) {
// the dummy implementations below. Once a proper x86_64 darwin fenv is set up,
// the apple condition here should be removed.
// TODO: fully support fenv for MSVC.
-#elif defined(LIBC_TARGET_ARCH_IS_X86) && !defined(__APPLE__)
+#elif defined(LIBC_TARGET_ARCH_IS_X86) && !defined(__APPLE__) && \
+ !defined(LIBC_USE_CONSTEXPR)
#include "src/__support/FPUtil/x86_64/FEnvImpl.h"
#elif defined(LIBC_TARGET_ARCH_IS_ARM) && defined(__ARM_FP) && \
- !defined(LIBC_COMPILER_IS_MSVC)
+ !defined(LIBC_COMPILER_IS_MSVC) && !defined(LIBC_USE_CONSTEXPR)
#include "src/__support/FPUtil/arm/FEnvImpl.h"
-#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) && defined(__riscv_flen)
+#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) && defined(__riscv_flen) && \
+ !defined(LIBC_USE_CONSTEXPR)
#include "src/__support/FPUtil/riscv/FEnvImpl.h"
-#elif defined(LIBC_TARGET_ARCH_IS_HEXAGON)
+#elif defined(LIBC_TARGET_ARCH_IS_HEXAGON) && !defined(LIBC_USE_CONSTEXPR)
#include "src/__support/FPUtil/hexagon/FEnvImpl.h"
#else
#define LIBC_FPUTIL_HAS_DUMMY_FENV_IMPL
@@ -143,7 +151,7 @@ LIBC_INLINE int enable_except(int) { return 0; }
LIBC_INLINE int disable_except(int) { return 0; }
-LIBC_INLINE int get_round() { return FE_TONEAREST; }
+LIBC_INLINE constexpr int get_round() { return FE_TONEAREST; }
LIBC_INLINE int set_round(int rounding_mode) {
return (rounding_mode == FE_TONEAREST) ? 0 : 1;
diff --git a/libc/src/__support/FPUtil/FMA.h b/libc/src/__support/FPUtil/FMA.h
index fe82afd4c70d6..ef11c68bb4468 100644
--- a/libc/src/__support/FPUtil/FMA.h
+++ b/libc/src/__support/FPUtil/FMA.h
@@ -34,40 +34,51 @@ LIBC_INLINE constexpr OutType fma(InType x, InType y, InType z) {
return generic::fma<OutType>(x, y, z);
}
-#ifdef LIBC_TARGET_CPU_HAS_FMA
+#if defined(LIBC_TARGET_CPU_HAS_FMA) && !defined(LIBC_USE_CONSTEXPR)
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-template <> LIBC_INLINE float fma(float x, float y, float z) {
+template <>
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float fma(float x, float y, float z) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::fma<float>(x, y, z);
+ } else {
#if __has_builtin(__builtin_elementwise_fma)
- return __builtin_elementwise_fma(x, y, z);
+ return __builtin_elementwise_fma(x, y, z);
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
- return _mm_cvtss_f32(
- _mm_fmadd_ss(_mm_set_ss(x), _mm_set_ss(y), _mm_set_ss(z)));
+ return _mm_cvtss_f32(
+ _mm_fmadd_ss(_mm_set_ss(x), _mm_set_ss(y), _mm_set_ss(z)));
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_AARCH64)
- return vget_lane_f32(vfma_f32(vdup_n_f32(z), vdup_n_f32(x), vdup_n_f32(y)),
- 0);
+ return vget_lane_f32(vfma_f32(vdup_n_f32(z), vdup_n_f32(x), vdup_n_f32(y)),
+ 0);
#else
- return __builtin_fmaf(x, y, z);
+ return __builtin_fmaf(x, y, z);
#endif
+ }
}
#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-template <> LIBC_INLINE double fma(double x, double y, double z) {
+template <>
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double fma(double x, double y, double z) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::fma<double>(x, y, z);
+ } else {
#if __has_builtin(__builtin_elementwise_fma)
- return __builtin_elementwise_fma(x, y, z);
+ return __builtin_elementwise_fma(x, y, z);
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
- return _mm_cvtsd_f64(
- _mm_fmadd_sd(_mm_set_sd(x), _mm_set_sd(y), _mm_set_sd(z)));
+ return _mm_cvtsd_f64(
+ _mm_fmadd_sd(_mm_set_sd(x), _mm_set_sd(y), _mm_set_sd(z)));
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_AARCH64)
- return vget_lane_f64(vfma_f64(vdup_n_f64(z), vdup_n_f64(x), vdup_n_f64(y)),
- 0);
+ return vget_lane_f64(vfma_f64(vdup_n_f64(z), vdup_n_f64(x), vdup_n_f64(y)),
+ 0);
#else
- return __builtin_fma(x, y, z);
+ return __builtin_fma(x, y, z);
#endif
+ }
}
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-#endif // LIBC_TARGET_CPU_HAS_FMA
+#endif // defined(LIBC_TARGET_CPU_HAS_FMA) &&
+ // !defined(LIBC_USE_CONSTEXPR)
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/Hypot.h b/libc/src/__support/FPUtil/Hypot.h
index de90705e9a43a..52d6aea0e759b 100644
--- a/libc/src/__support/FPUtil/Hypot.h
+++ b/libc/src/__support/FPUtil/Hypot.h
@@ -109,7 +109,7 @@ template <> struct DoubleLength<uint64_t> {
// - HYPOT(x, y) is NaN if x or y is NaN.
//
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
-LIBC_INLINE T hypot(T x, T y) {
+LIBC_INLINE constexpr T hypot(T x, T y) {
using FPBits_t = FPBits<T>;
using StorageType = typename FPBits<T>::StorageType;
using DStorageType = typename DoubleLength<StorageType>::Type;
@@ -154,8 +154,8 @@ LIBC_INLINE T hypot(T x, T y) {
uint64_t out_exp = a_exp;
StorageType a_mant = a_bits.get_mantissa();
StorageType b_mant = b_bits.get_mantissa();
- DStorageType a_mant_sq, b_mant_sq;
- bool sticky_bits;
+ DStorageType a_mant_sq{}, b_mant_sq{};
+ bool sticky_bits{};
// Add an extra bit to simplify the final rounding bit computation.
constexpr StorageType ONE = StorageType(1) << (FPBits_t::FRACTION_LEN + 1);
@@ -163,8 +163,8 @@ LIBC_INLINE T hypot(T x, T y) {
a_mant <<= 1;
b_mant <<= 1;
- StorageType leading_one;
- int y_mant_width;
+ StorageType leading_one{};
+ int y_mant_width{};
if (a_exp != 0) {
leading_one = ONE;
a_mant |= ONE;
diff --git a/libc/src/__support/FPUtil/PolyEval.h b/libc/src/__support/FPUtil/PolyEval.h
index 55583cca70c3b..6a5d24af48829 100644
--- a/libc/src/__support/FPUtil/PolyEval.h
+++ b/libc/src/__support/FPUtil/PolyEval.h
@@ -48,7 +48,7 @@ polyeval(const T &x, const T &a0, const Ts &...a) {
}
template <typename T, typename... Ts>
-LIBC_INLINE LIBC_CONSTEXPR cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>
+LIBC_INLINE constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>
polyeval(T x, T a0, Ts... a) {
return multiply_add(x, polyeval(x, a...), a0);
}
diff --git a/libc/src/__support/FPUtil/aarch64/FEnvImpl.h b/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
index 18182ed977cce..ee6fb75ed6fe2 100644
--- a/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/aarch64/FEnvImpl.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_FENVIMPL_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_FENVIMPL_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -213,20 +214,24 @@ LIBC_INLINE int raise_except(int excepts) {
return result;
}
-LIBC_INLINE int get_round() {
- uint32_t roundingMode =
- (FEnv::getControlWord() >> FEnv::RoundingControlBitPosition) & 0x3;
- switch (roundingMode) {
- case FEnv::TONEAREST:
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int get_round() {
+ if (cpp::is_constant_evaluated()) {
return FE_TONEAREST;
- case FEnv::DOWNWARD:
- return FE_DOWNWARD;
- case FEnv::UPWARD:
- return FE_UPWARD;
- case FEnv::TOWARDZERO:
- return FE_TOWARDZERO;
- default:
- return -1; // Error value.
+ } else {
+ uint32_t roundingMode =
+ (FEnv::getControlWord() >> FEnv::RoundingControlBitPosition) & 0x3;
+ switch (roundingMode) {
+ case FEnv::TONEAREST:
+ return FE_TONEAREST;
+ case FEnv::DOWNWARD:
+ return FE_DOWNWARD;
+ case FEnv::UPWARD:
+ return FE_UPWARD;
+ case FEnv::TOWARDZERO:
+ return FE_TOWARDZERO;
+ default:
+ return -1; // Error value.
+ }
}
}
diff --git a/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h b/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
index af5fbe592537e..18e63377d18a0 100644
--- a/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
+++ b/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_FENV_DARWIN_IMPL_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_FENV_DARWIN_IMPL_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -290,20 +291,24 @@ LIBC_INLINE int raise_except(int excepts) {
return result;
}
-LIBC_INLINE int get_round() {
- uint32_t rounding_mode =
- (FEnv::get_control_word() >> FEnv::ROUNDING_CONTROL_BIT_POSITION) & 0x3;
- switch (rounding_mode) {
- case FEnv::TONEAREST:
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int get_round() {
+ if (cpp::is_constant_evaluated()) {
return FE_TONEAREST;
- case FEnv::DOWNWARD:
- return FE_DOWNWARD;
- case FEnv::UPWARD:
- return FE_UPWARD;
- case FEnv::TOWARDZERO:
- return FE_TOWARDZERO;
- default:
- return -1; // Error value.
+ } else {
+ uint32_t rounding_mode =
+ (FEnv::get_control_word() >> FEnv::ROUNDING_CONTROL_BIT_POSITION) & 0x3;
+ switch (rounding_mode) {
+ case FEnv::TONEAREST:
+ return FE_TONEAREST;
+ case FEnv::DOWNWARD:
+ return FE_DOWNWARD;
+ case FEnv::UPWARD:
+ return FE_UPWARD;
+ case FEnv::TOWARDZERO:
+ return FE_TOWARDZERO;
+ default:
+ return -1; // Error value.
+ }
}
}
diff --git a/libc/src/__support/FPUtil/aarch64/nearest_integer.h b/libc/src/__support/FPUtil/aarch64/nearest_integer.h
index 9f6a5b97d64e4..628da82ebec43 100644
--- a/libc/src/__support/FPUtil/aarch64/nearest_integer.h
+++ b/libc/src/__support/FPUtil/aarch64/nearest_integer.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_NEAREST_INTEGER_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_NEAREST_INTEGER_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -25,7 +26,11 @@
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
-LIBC_INLINE float nearest_integer(float x) {
+namespace internal {
+
+// These wrappers are kept out of the constexpr functions below because inline
+// assembly is not allowed in a constexpr function before C++20.
+LIBC_INLINE float hardware_nearest_integer(float x) {
#ifdef LIBC_COMPILER_IS_MSVC
return vget_lane_f32(vrndn_f32(vdup_n_f32(x)), 0);
#else
@@ -35,7 +40,7 @@ LIBC_INLINE float nearest_integer(float x) {
#endif
}
-LIBC_INLINE double nearest_integer(double x) {
+LIBC_INLINE double hardware_nearest_integer(double x) {
#ifdef LIBC_COMPILER_IS_MSVC
return vget_lane_f64(vrndn_f64(vdup_n_f64(x)), 0);
#else
@@ -45,6 +50,27 @@ LIBC_INLINE double nearest_integer(double x) {
#endif
}
+} // namespace internal
+
+// generic::nearest_integer is defined in
+// "src/__support/FPUtil/nearest_integer.h", the only header that includes this
+// file.
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float nearest_integer(float x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::nearest_integer(x);
+ } else {
+ return internal::hardware_nearest_integer(x);
+ }
+}
+
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double nearest_integer(double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::nearest_integer(x);
+ } else {
+ return internal::hardware_nearest_integer(x);
+ }
+}
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/aarch64/sqrt.h b/libc/src/__support/FPUtil/aarch64/sqrt.h
index be9cb1f1cea43..390fe8a56bdd0 100644
--- a/libc/src/__support/FPUtil/aarch64/sqrt.h
+++ b/libc/src/__support/FPUtil/aarch64/sqrt.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_SQRT_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_SQRT_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -28,8 +29,12 @@
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
+namespace internal {
+
+// These wrappers are kept out of the constexpr specializations below because
+// inline assembly is not allowed in a constexpr function before C++20.
#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
-template <> LIBC_INLINE float sqrt<float>(float x) {
+LIBC_INLINE float hardware_sqrt(float x) {
#ifdef LIBC_COMPILER_IS_MSVC
return vget_lane_f32(vsqrt_f32(vdup_n_f32(x)), 0);
#else
@@ -41,7 +46,7 @@ template <> LIBC_INLINE float sqrt<float>(float x) {
#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
-template <> LIBC_INLINE double sqrt<double>(double x) {
+LIBC_INLINE double hardware_sqrt(double x) {
#ifdef LIBC_COMPILER_IS_MSVC
return vget_lane_f64(vsqrt_f64(vdup_n_f64(x)), 0);
#else
@@ -52,6 +57,28 @@ template <> LIBC_INLINE double sqrt<double>(double x) {
}
#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+} // namespace internal
+
+#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sqrt<float>(float x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<float>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
+#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
+
+#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sqrt<double>(double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<double>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
+#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/arm/FEnvImpl.h b/libc/src/__support/FPUtil/arm/FEnvImpl.h
index 64ab4a9189b77..3ee1311ac0fe8 100644
--- a/libc/src/__support/FPUtil/arm/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/arm/FEnvImpl.h
@@ -12,6 +12,7 @@
#include "hdr/fenv_macros.h"
#include "hdr/stdint_proxy.h"
#include "hdr/types/fenv_t.h"
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/macros/attributes.h" // For LIBC_INLINE
#include "src/__support/macros/config.h"
@@ -187,21 +188,26 @@ LIBC_INLINE int raise_except(int excepts) {
return result;
}
-LIBC_INLINE int get_round() {
- uint32_t mode = (FEnv::get_fpscr() >> FEnv::RoundingControlBitPosition) & 0x3;
- switch (mode) {
- case FEnv::TONEAREST:
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int get_round() {
+ if (cpp::is_constant_evaluated()) {
return FE_TONEAREST;
- case FEnv::DOWNWARD:
- return FE_DOWNWARD;
- case FEnv::UPWARD:
- return FE_UPWARD;
- case FEnv::TOWARDZERO:
- return FE_TOWARDZERO;
- default:
- return -1; // Error value.
+ } else {
+ uint32_t mode =
+ (FEnv::get_fpscr() >> FEnv::RoundingControlBitPosition) & 0x3;
+ switch (mode) {
+ case FEnv::TONEAREST:
+ return FE_TONEAREST;
+ case FEnv::DOWNWARD:
+ return FE_DOWNWARD;
+ case FEnv::UPWARD:
+ return FE_UPWARD;
+ case FEnv::TOWARDZERO:
+ return FE_TOWARDZERO;
+ default:
+ return -1; // Error value.
+ }
+ return 0;
}
- return 0;
}
LIBC_INLINE int set_round(int mode) {
diff --git a/libc/src/__support/FPUtil/arm/sqrt.h b/libc/src/__support/FPUtil/arm/sqrt.h
index 71803c2ad3156..09cc633ac2547 100644
--- a/libc/src/__support/FPUtil/arm/sqrt.h
+++ b/libc/src/__support/FPUtil/arm/sqrt.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_ARM_SQRT_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_ARM_SQRT_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -23,8 +24,12 @@
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
+namespace internal {
+
+// These wrappers are kept out of the constexpr specializations below because
+// inline assembly is not allowed in a constexpr function before C++20.
#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
-template <> LIBC_INLINE float sqrt<float>(float x) {
+LIBC_INLINE float hardware_sqrt(float x) {
float y;
asm("vsqrt.f32 %0, %1\n\t" : "=w"(y) : "w"(x));
return y;
@@ -32,13 +37,35 @@ template <> LIBC_INLINE float sqrt<float>(float x) {
#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
-template <> LIBC_INLINE double sqrt<double>(double x) {
+LIBC_INLINE double hardware_sqrt(double x) {
double y;
asm("vsqrt.f64 %P0, %P1\n\t" : "=w"(y) : "w"(x));
return y;
}
#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+} // namespace internal
+
+#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sqrt<float>(float x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<float>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
+#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
+
+#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sqrt<double>(double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<double>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
+#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/double_double.h b/libc/src/__support/FPUtil/double_double.h
index 45ce8a10acdac..4cac9f28f382b 100644
--- a/libc/src/__support/FPUtil/double_double.h
+++ b/libc/src/__support/FPUtil/double_double.h
@@ -168,7 +168,7 @@ template <> struct TargetHasFmaInstruction<double> {
// the generated constants to precision <= 51, and splitting it by 2^28 + 1,
// then a * b = r.hi + r.lo is exact for all rounding modes.
template <typename T = double, size_t SPLIT_B = DefaultSplit<T>::VALUE>
-LIBC_INLINE LIBC_CONSTEXPR NumberPair<T> exact_mult(T a, T b) {
+LIBC_INLINE constexpr NumberPair<T> exact_mult(T a, T b) {
NumberPair<T> r;
if constexpr (TargetHasFmaInstruction<T>::VALUE) {
@@ -185,7 +185,7 @@ LIBC_INLINE LIBC_CONSTEXPR NumberPair<T> exact_mult(T a, T b) {
}
template <typename T = double>
-LIBC_INLINE NumberPair<T> quick_mult(T a, const NumberPair<T> &b) {
+LIBC_INLINE constexpr NumberPair<T> quick_mult(T a, const NumberPair<T> &b) {
NumberPair<T> r = exact_mult(a, b.hi);
r.lo = multiply_add(a, b.lo, r.lo);
return r;
@@ -203,9 +203,9 @@ LIBC_INLINE constexpr DoubleDouble quick_mult(const DoubleDouble &a,
// Assuming |c| >= |a * b|.
template <>
-LIBC_INLINE DoubleDouble multiply_add<DoubleDouble>(const DoubleDouble &a,
- const DoubleDouble &b,
- const DoubleDouble &c) {
+LIBC_INLINE constexpr DoubleDouble
+multiply_add<DoubleDouble>(const DoubleDouble &a, const DoubleDouble &b,
+ const DoubleDouble &c) {
return add(c, quick_mult(a, b));
}
@@ -227,7 +227,8 @@ LIBC_INLINE DoubleDouble multiply_add<DoubleDouble>(const DoubleDouble &a,
// as accurate as possible, then the error is bounded by:
// |(ah + al) / (bh + bl) - (rh + rl)| < O(bl/bh) * (2^-52 + al/ah + bl/bh)
template <typename T>
-LIBC_INLINE NumberPair<T> div(const NumberPair<T> &a, const NumberPair<T> &b) {
+LIBC_INLINE constexpr NumberPair<T> div(const NumberPair<T> &a,
+ const NumberPair<T> &b) {
NumberPair<T> r;
T q = T(1) / b.hi;
r.hi = a.hi * q;
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index 130e85d0a4f5f..f09b5253ec5ac 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -407,7 +407,7 @@ template <size_t Bits> struct DyadicFloat {
bool sticky_bit = !(mantissa & sticky_mask).is_zero();
int round_and_sticky = int(round_bit) * 2 + int(sticky_bit);
- T d_lo;
+ T d_lo{};
if (LIBC_UNLIKELY(exp_lo <= 0)) {
// d_lo is denormal, but the output is normal.
diff --git a/libc/src/__support/FPUtil/generic/FMA.h b/libc/src/__support/FPUtil/generic/FMA.h
index 795d8227b4b1e..45437fce109ca 100644
--- a/libc/src/__support/FPUtil/generic/FMA.h
+++ b/libc/src/__support/FPUtil/generic/FMA.h
@@ -18,7 +18,7 @@
#include "src/__support/FPUtil/dyadic_float.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "src/__support/big_int.h"
-#include "src/__support/macros/attributes.h" // LIBC_INLINE
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
@@ -38,7 +38,8 @@ fma(InType x, InType y, InType z);
// TODO(lntue): Implement fmaf that is correctly rounded to all rounding modes.
// The implementation below only is only correct for the default rounding mode,
// round-to-nearest tie-to-even.
-template <> LIBC_INLINE float fma<float>(float x, float y, float z) {
+template <>
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float fma<float>(float x, float y, float z) {
// Product is exact.
double prod = static_cast<double>(x) * static_cast<double>(y);
double z_d = static_cast<double>(z);
@@ -90,7 +91,8 @@ namespace internal {
// Extract the sticky bits and shift the `mantissa` to the right by
// `shift_length`.
template <typename T>
-LIBC_INLINE cpp::enable_if_t<is_unsigned_integral_or_big_int_v<T>, bool>
+LIBC_INLINE constexpr cpp::enable_if_t<is_unsigned_integral_or_big_int_v<T>,
+ bool>
shift_mantissa(int shift_length, T &mant) {
if (shift_length >= cpp::numeric_limits<T>::digits) {
mant = 0;
diff --git a/libc/src/__support/FPUtil/generic/sqrt.h b/libc/src/__support/FPUtil/generic/sqrt.h
index 69087bd93f083..6b1bb350d5a6f 100644
--- a/libc/src/__support/FPUtil/generic/sqrt.h
+++ b/libc/src/__support/FPUtil/generic/sqrt.h
@@ -71,6 +71,8 @@ LIBC_INLINE constexpr void normalize<long double>(int &exponent,
} // namespace internal
+namespace generic {
+
// Correctly rounded IEEE 754 SQRT for all rounding modes.
// Shift-and-add algorithm.
template <typename OutType, typename InType>
@@ -172,6 +174,20 @@ sqrt(InType x) {
}
}
+} // namespace generic
+
+// Targets with a square root instruction specialize this for the supported
+// types in the target specific headers, dispatching to generic::sqrt during
+// constant evaluation.
+template <typename OutType, typename InType>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<OutType> &&
+ cpp::is_floating_point_v<InType> &&
+ sizeof(OutType) <= sizeof(InType),
+ OutType>
+sqrt(InType x) {
+ return generic::sqrt<OutType>(x);
+}
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/hexagon/FEnvImpl.h b/libc/src/__support/FPUtil/hexagon/FEnvImpl.h
index 15d081e54f8c4..197aa69a28256 100644
--- a/libc/src/__support/FPUtil/hexagon/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/hexagon/FEnvImpl.h
@@ -17,6 +17,7 @@
#include "hdr/fenv_macros.h"
#include "hdr/stdint_proxy.h"
#include "hdr/types/fenv_t.h"
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h" // For LIBC_INLINE_ASM
#include "src/__support/macros/config.h" // For LIBC_INLINE
@@ -150,19 +151,23 @@ LIBC_INLINE int raise_except(int excepts) {
return 0;
}
-LIBC_INLINE int get_round() {
- uint32_t rm = (FEnv::get_usr() >> FEnv::RND_SHIFT) & FEnv::RND_MASK;
- switch (rm) {
- case FEnv::RND_TONEAREST:
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int get_round() {
+ if (cpp::is_constant_evaluated()) {
return FE_TONEAREST;
- case FEnv::RND_TOWARDZERO:
- return FE_TOWARDZERO;
- case FEnv::RND_DOWNWARD:
- return FE_DOWNWARD;
- case FEnv::RND_UPWARD:
- return FE_UPWARD;
- default:
- return -1; // Error value.
+ } else {
+ uint32_t rm = (FEnv::get_usr() >> FEnv::RND_SHIFT) & FEnv::RND_MASK;
+ switch (rm) {
+ case FEnv::RND_TONEAREST:
+ return FE_TONEAREST;
+ case FEnv::RND_TOWARDZERO:
+ return FE_TOWARDZERO;
+ case FEnv::RND_DOWNWARD:
+ return FE_DOWNWARD;
+ case FEnv::RND_UPWARD:
+ return FE_UPWARD;
+ default:
+ return -1; // Error value.
+ }
}
}
diff --git a/libc/src/__support/FPUtil/multiply_add.h b/libc/src/__support/FPUtil/multiply_add.h
index f7f30035c7739..03edabb817723 100644
--- a/libc/src/__support/FPUtil/multiply_add.h
+++ b/libc/src/__support/FPUtil/multiply_add.h
@@ -63,40 +63,51 @@ multiply_add(T x, T y, T z) {
// use the generic fma implementation from generic/FMA.h. But currently that
// implementation will use dyadic_float.h , which in turns including this
// multiply_add.h . We will need to break the dependency to enable constexpr
-// for other math functions.
+// for other math functions. Until then, constant evaluation falls back to the
+// non-fused `x * y + z`, exactly like the generic implementation above.
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-LIBC_INLINE float multiply_add(float x, float y, float z) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float multiply_add(float x, float y,
+ float z) {
+ if (cpp::is_constant_evaluated()) {
+ return x * y + z;
+ } else {
#if __has_builtin(__builtin_elementwise_fma)
- return __builtin_elementwise_fma(x, y, z);
+ return __builtin_elementwise_fma(x, y, z);
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
- return _mm_cvtss_f32(
- _mm_fmadd_ss(_mm_set_ss(x), _mm_set_ss(y), _mm_set_ss(z)));
+ return _mm_cvtss_f32(
+ _mm_fmadd_ss(_mm_set_ss(x), _mm_set_ss(y), _mm_set_ss(z)));
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_AARCH64)
- return vget_lane_f32(vfma_f32(vdup_n_f32(z), vdup_n_f32(x), vdup_n_f32(y)),
- 0);
+ return vget_lane_f32(vfma_f32(vdup_n_f32(z), vdup_n_f32(x), vdup_n_f32(y)),
+ 0);
#else
- return __builtin_fmaf(x, y, z);
+ return __builtin_fmaf(x, y, z);
#endif
+ }
}
#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-LIBC_INLINE double multiply_add(double x, double y, double z) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double multiply_add(double x, double y,
+ double z) {
+ if (cpp::is_constant_evaluated()) {
+ return x * y + z;
+ } else {
#if __has_builtin(__builtin_elementwise_fma)
- return __builtin_elementwise_fma(x, y, z);
+ return __builtin_elementwise_fma(x, y, z);
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
- return _mm_cvtsd_f64(
- _mm_fmadd_sd(_mm_set_sd(x), _mm_set_sd(y), _mm_set_sd(z)));
+ return _mm_cvtsd_f64(
+ _mm_fmadd_sd(_mm_set_sd(x), _mm_set_sd(y), _mm_set_sd(z)));
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_AARCH64)
- return vget_lane_f64(vfma_f64(vdup_n_f64(z), vdup_n_f64(x), vdup_n_f64(y)),
- 0);
+ return vget_lane_f64(vfma_f64(vdup_n_f64(z), vdup_n_f64(x), vdup_n_f64(y)),
+ 0);
#else
- return __builtin_fma(x, y, z);
+ return __builtin_fma(x, y, z);
#endif
+ }
}
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
diff --git a/libc/src/__support/FPUtil/nearest_integer.h b/libc/src/__support/FPUtil/nearest_integer.h
index c9d55a2e61af0..aed85900829a7 100644
--- a/libc/src/__support/FPUtil/nearest_integer.h
+++ b/libc/src/__support/FPUtil/nearest_integer.h
@@ -15,28 +15,9 @@
#include "src/__support/macros/properties/architectures.h"
#include "src/__support/macros/properties/cpu_features.h"
-#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 float nearest_integer(float x) { return __builtin_rintf(x); }
-
-LIBC_INLINE double nearest_integer(double x) { return __builtin_rint(x); }
-
-} // namespace fputil
-} // namespace LIBC_NAMESPACE_DECL
-
-#else
-
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
+namespace generic {
// This is a fast implementation for rounding to a nearest integer that.
//
@@ -89,6 +70,59 @@ 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)) && \
+ !defined(LIBC_USE_CONSTEXPR)
+#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)) && \
+ !defined(LIBC_USE_CONSTEXPR)
+#include "aarch64/nearest_integer.h"
+#elif defined(LIBC_TARGET_ARCH_IS_GPU) && !defined(LIBC_USE_CONSTEXPR)
+
+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);
+ }
+}
+
+} // namespace fputil
+} // namespace LIBC_NAMESPACE_DECL
+
+#else
+
+namespace LIBC_NAMESPACE_DECL {
+namespace fputil {
+
+LIBC_INLINE constexpr float nearest_integer(float x) {
+ return generic::nearest_integer(x);
+}
+
+LIBC_INLINE constexpr double nearest_integer(double x) {
+ return generic::nearest_integer(x);
+}
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/riscv/FEnvImpl.h b/libc/src/__support/FPUtil/riscv/FEnvImpl.h
index cb2d2d59fb86c..efbedb974dc56 100644
--- a/libc/src/__support/FPUtil/riscv/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/riscv/FEnvImpl.h
@@ -12,6 +12,7 @@
#include "hdr/fenv_macros.h"
#include "hdr/stdint_proxy.h"
#include "hdr/types/fenv_t.h"
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/macros/attributes.h" // For LIBC_INLINE_ASM
#include "src/__support/macros/config.h" // For LIBC_INLINE
@@ -117,21 +118,25 @@ LIBC_INLINE int raise_except(int excepts) {
return 0;
}
-LIBC_INLINE int get_round() {
- uint32_t rm = FEnv::get_frm();
- switch (rm) {
- case FEnv::TONEAREST:
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int get_round() {
+ if (cpp::is_constant_evaluated()) {
return FE_TONEAREST;
- case FEnv::DOWNWARD:
- return FE_DOWNWARD;
- case FEnv::UPWARD:
- return FE_UPWARD;
- case FEnv::TOWARDZERO:
- return FE_TOWARDZERO;
- default:
- return -1; // Error value.
+ } else {
+ uint32_t rm = FEnv::get_frm();
+ switch (rm) {
+ case FEnv::TONEAREST:
+ return FE_TONEAREST;
+ case FEnv::DOWNWARD:
+ return FE_DOWNWARD;
+ case FEnv::UPWARD:
+ return FE_UPWARD;
+ case FEnv::TOWARDZERO:
+ return FE_TOWARDZERO;
+ default:
+ return -1; // Error value.
+ }
+ return 0;
}
- return 0;
}
LIBC_INLINE int set_round(int mode) {
diff --git a/libc/src/__support/FPUtil/riscv/sqrt.h b/libc/src/__support/FPUtil/riscv/sqrt.h
index 8cff03a9fc73e..9323da033335b 100644
--- a/libc/src/__support/FPUtil/riscv/sqrt.h
+++ b/libc/src/__support/FPUtil/riscv/sqrt.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_RISCV_SQRT_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_RISCV_SQRT_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -23,8 +24,12 @@
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
+namespace internal {
+
+// These wrappers are kept out of the constexpr specializations below because
+// inline assembly is not allowed in a constexpr function before C++20.
#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
-template <> LIBC_INLINE float sqrt<float>(float x) {
+LIBC_INLINE float hardware_sqrt(float x) {
float result;
asm("fsqrt.s %0, %1\n\t" : "=f"(result) : "f"(x));
return result;
@@ -32,13 +37,35 @@ template <> LIBC_INLINE float sqrt<float>(float x) {
#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
-template <> LIBC_INLINE double sqrt<double>(double x) {
+LIBC_INLINE double hardware_sqrt(double x) {
double result;
asm("fsqrt.d %0, %1\n\t" : "=f"(result) : "f"(x));
return result;
}
+#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+
+} // namespace internal
+
+#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sqrt<float>(float x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<float>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
+#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sqrt<double>(double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<double>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
+#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/sqrt.h b/libc/src/__support/FPUtil/sqrt.h
index e8540adc753b7..dd912b7484126 100644
--- a/libc/src/__support/FPUtil/sqrt.h
+++ b/libc/src/__support/FPUtil/sqrt.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_SQRT_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_SQRT_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/properties/architectures.h"
#include "src/__support/macros/properties/cpu_features.h"
@@ -24,22 +25,36 @@ namespace LIBC_NAMESPACE_DECL {
namespace fputil {
#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
-template <> LIBC_INLINE float sqrt<float>(float x) {
- return __builtin_elementwise_sqrt(x);
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sqrt<float>(float x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<float>(x);
+ } else {
+ return __builtin_elementwise_sqrt(x);
+ }
}
#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
-template <> LIBC_INLINE double sqrt<double>(double x) {
- return __builtin_elementwise_sqrt(x);
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sqrt<double>(double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<double>(x);
+ } else {
+ return __builtin_elementwise_sqrt(x);
+ }
}
#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
// Use 80-bit long double instruction on x86.
// https://godbolt.org/z/oWEaj6hxK
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
-template <> LIBC_INLINE long double sqrt<long double>(long double x) {
- return __builtin_elementwise_sqrt(x);
+template <>
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT long double
+sqrt<long double>(long double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<long double>(x);
+ } else {
+ return __builtin_elementwise_sqrt(x);
+ }
}
#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80
diff --git a/libc/src/__support/FPUtil/x86_64/FEnvImpl.h b/libc/src/__support/FPUtil/x86_64/FEnvImpl.h
index 703c305293dfd..c46ab6a76a813 100644
--- a/libc/src/__support/FPUtil/x86_64/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/x86_64/FEnvImpl.h
@@ -13,6 +13,7 @@
#include "hdr/stdint_proxy.h"
#include "hdr/types/fenv_t.h"
#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
@@ -113,9 +114,13 @@ LIBC_INLINE static int disable_except(int excepts) {
return internal::get_macro_from_exception_status(old_excepts);
}
-LIBC_INLINE static int get_round() {
- uint16_t rounding_mode = sse::get_round();
- return internal::get_macro_from_rounding_control(rounding_mode);
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT static int get_round() {
+ if (cpp::is_constant_evaluated()) {
+ return FE_TONEAREST;
+ } else {
+ uint16_t rounding_mode = sse::get_round();
+ return internal::get_macro_from_rounding_control(rounding_mode);
+ }
}
LIBC_INLINE static int set_round(int rounding_mode) {
diff --git a/libc/src/__support/FPUtil/x86_64/nearest_integer.h b/libc/src/__support/FPUtil/x86_64/nearest_integer.h
index a011e7321e43d..b91625fca5ec2 100644
--- a/libc/src/__support/FPUtil/x86_64/nearest_integer.h
+++ b/libc/src/__support/FPUtil/x86_64/nearest_integer.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_NEAREST_INTEGER_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -28,6 +29,8 @@
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
+namespace internal {
+
// The legacy SSE4.1 `roundss` and `roundsd` are 2-operand instructions that
// preserve the upper bits of the destination XMM register (unlike AVX's
// 3-operand `vroundss` and `vroundsd`, where `_mm_round_ss(xmm, xmm, ...)`
@@ -39,7 +42,10 @@ namespace fputil {
// assembly with `"+x"(x)` forces identical source and destination registers
// without the extra lane-zeroing instructions required by `_mm_round_ps` /
// `_mm_round_pd`.
-LIBC_INLINE float nearest_integer(float x) {
+//
+// These wrappers are kept out of the constexpr functions below because inline
+// assembly is not allowed in a constexpr function before C++20.
+LIBC_INLINE float hardware_nearest_integer(float x) {
#if defined(__AVX__) || defined(LIBC_COMPILER_IS_MSVC)
__m128 xmm = _mm_set_ss(x); // NOLINT
__m128 ymm =
@@ -52,7 +58,7 @@ LIBC_INLINE float nearest_integer(float x) {
#endif
}
-LIBC_INLINE double nearest_integer(double x) {
+LIBC_INLINE double hardware_nearest_integer(double x) {
#if defined(__AVX__) || defined(LIBC_COMPILER_IS_MSVC)
__m128d xmm = _mm_set_sd(x); // NOLINT
__m128d ymm =
@@ -65,6 +71,27 @@ LIBC_INLINE double nearest_integer(double x) {
#endif
}
+} // namespace internal
+
+// generic::nearest_integer is defined in
+// "src/__support/FPUtil/nearest_integer.h", the only header that includes this
+// file.
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float nearest_integer(float x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::nearest_integer(x);
+ } else {
+ return internal::hardware_nearest_integer(x);
+ }
+}
+
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double nearest_integer(double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::nearest_integer(x);
+ } else {
+ return internal::hardware_nearest_integer(x);
+ }
+}
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/FPUtil/x86_64/sqrt.h b/libc/src/__support/FPUtil/x86_64/sqrt.h
index 9c8545c1b8fd7..2c35dc40ff091 100644
--- a/libc/src/__support/FPUtil/x86_64/sqrt.h
+++ b/libc/src/__support/FPUtil/x86_64/sqrt.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_SQRT_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_SQRT_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
@@ -28,7 +29,11 @@
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
-template <> LIBC_INLINE float sqrt<float>(float x) {
+namespace internal {
+
+// These wrappers are kept out of the constexpr specializations below because
+// inline assembly is not allowed in a constexpr function before C++20.
+LIBC_INLINE float hardware_sqrt(float x) {
#ifdef LIBC_COMPILER_IS_MSVC
return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(x)));
#else
@@ -38,7 +43,7 @@ template <> LIBC_INLINE float sqrt<float>(float x) {
#endif
}
-template <> LIBC_INLINE double sqrt<double>(double x) {
+LIBC_INLINE double hardware_sqrt(double x) {
#ifdef LIBC_COMPILER_IS_MSVC
__m128d xmm = _mm_set_sd(x);
return _mm_cvtsd_f64(_mm_sqrt_sd(xmm, xmm));
@@ -50,7 +55,7 @@ template <> LIBC_INLINE double sqrt<double>(double x) {
}
#ifdef LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
-template <> LIBC_INLINE long double sqrt<long double>(long double x) {
+LIBC_INLINE long double hardware_sqrt(long double x) {
#ifdef LIBC_COMPILER_IS_MSVC
__m128d xmm = _mm_set_sd(static_cast<double>(x));
return _mm_cvtsd_f64(_mm_sqrt_sd(xmm, xmm));
@@ -61,12 +66,40 @@ template <> LIBC_INLINE long double sqrt<long double>(long double x) {
#endif
}
#else
-template <> LIBC_INLINE long double sqrt<long double>(long double x) {
+LIBC_INLINE long double hardware_sqrt(long double x) {
__asm__ __volatile__("fsqrt" : "+t"(x));
return x;
}
#endif
+} // namespace internal
+
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sqrt<float>(float x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<float>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
+
+template <> LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sqrt<double>(double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<double>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
+
+template <>
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT long double
+sqrt<long double>(long double x) {
+ if (cpp::is_constant_evaluated()) {
+ return generic::sqrt<long double>(x);
+ } else {
+ return internal::hardware_sqrt(x);
+ }
+}
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
More information about the libc-commits
mailing list