[libc-commits] [libc] Constexpr math annot (PR #242285)
via libc-commits
libc-commits at lists.llvm.org
Fri Nov 13 14:01:00 PST 2026
https://github.com/linisha15 updated https://github.com/llvm/llvm-project/pull/242285
>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 1/3] [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
>From 70b2df6639575f375b9185d06853f9a42e7d64e6 Mon Sep 17 00:00:00 2001
From: linisha <linisha232 at gmail.com>
Date: Fri, 13 Nov 2026 19:30:16 +0530
Subject: [PATCH 2/3] [libc][math] Make shared math functions constexpr by
default
Defers pow/powf (upstream pow_utils uses volatile for the inexact path).
Part of #195623 (split 2/2).
---
libc/src/__support/math/CMakeLists.txt | 19 ++
libc/src/__support/math/acos.h | 2 +-
libc/src/__support/math/acosbf16.h | 2 +-
libc/src/__support/math/acosf.h | 2 +-
libc/src/__support/math/acoshbf16.h | 2 +-
libc/src/__support/math/acoshf.h | 2 +-
libc/src/__support/math/acoshf_utils.h | 4 +-
libc/src/__support/math/acospif.h | 2 +-
libc/src/__support/math/asin.h | 2 +-
libc/src/__support/math/asin_utils.h | 21 +-
libc/src/__support/math/asinbf16.h | 2 +-
libc/src/__support/math/asinf.h | 2 +-
libc/src/__support/math/asinhf.h | 2 +-
libc/src/__support/math/asinpi.h | 2 +-
libc/src/__support/math/asinpif.h | 2 +-
libc/src/__support/math/asinpif16.h | 2 +-
libc/src/__support/math/atan.h | 2 +-
libc/src/__support/math/atan2.h | 2 +-
libc/src/__support/math/atan2f.h | 9 +-
libc/src/__support/math/atan2f128.h | 2 +-
libc/src/__support/math/atan2f16.h | 2 +-
libc/src/__support/math/atan2f_float.h | 4 +-
libc/src/__support/math/atan_utils.h | 12 +-
libc/src/__support/math/atanbf16.h | 2 +-
libc/src/__support/math/atanf.h | 2 +-
libc/src/__support/math/atanf_float.h | 4 +-
libc/src/__support/math/atanhf.h | 2 +-
libc/src/__support/math/atanhf16.h | 2 +-
libc/src/__support/math/atanpif16.h | 2 +-
libc/src/__support/math/bf16add.h | 2 +-
libc/src/__support/math/bf16addf.h | 2 +-
libc/src/__support/math/bf16addf128.h | 2 +-
libc/src/__support/math/bf16addl.h | 2 +-
libc/src/__support/math/bf16div.h | 2 +-
libc/src/__support/math/bf16divf.h | 2 +-
libc/src/__support/math/bf16divf128.h | 2 +-
libc/src/__support/math/bf16divl.h | 2 +-
libc/src/__support/math/bf16fma.h | 2 +-
libc/src/__support/math/bf16fmaf.h | 2 +-
libc/src/__support/math/bf16fmaf128.h | 2 +-
libc/src/__support/math/bf16fmal.h | 3 +-
libc/src/__support/math/bf16mul.h | 2 +-
libc/src/__support/math/bf16mulf.h | 2 +-
libc/src/__support/math/bf16mulf128.h | 2 +-
libc/src/__support/math/bf16mull.h | 2 +-
libc/src/__support/math/cbrt.h | 12 +-
libc/src/__support/math/cbrtbf16.h | 2 +-
libc/src/__support/math/cbrtf.h | 2 +-
libc/src/__support/math/ceil.h | 9 +-
libc/src/__support/math/ceilf.h | 9 +-
libc/src/__support/math/ceilf16.h | 9 +-
libc/src/__support/math/ceill.h | 4 +-
libc/src/__support/math/copysign.h | 2 +-
libc/src/__support/math/copysignf.h | 2 +-
libc/src/__support/math/copysignf16.h | 9 +-
libc/src/__support/math/cos.h | 4 +-
libc/src/__support/math/cos_integer_eval.h | 2 +-
libc/src/__support/math/cosf_double_eval.h | 2 +-
libc/src/__support/math/cosf_float_eval.h | 2 +-
libc/src/__support/math/cosh_double_eval.h | 5 +-
libc/src/__support/math/coshf.h | 2 +-
libc/src/__support/math/cospif.h | 2 +-
libc/src/__support/math/daddl.h | 2 +-
libc/src/__support/math/dfmaf128.h | 2 +-
libc/src/__support/math/dfmal.h | 3 +-
libc/src/__support/math/dsubf128.h | 2 +-
libc/src/__support/math/dsubl.h | 2 +-
libc/src/__support/math/erfcf16.h | 8 +-
libc/src/__support/math/erff.h | 2 +-
libc/src/__support/math/erff16.h | 2 +-
libc/src/__support/math/exp10_double_eval.h | 2 +-
libc/src/__support/math/exp10f16_utils.h | 3 +-
libc/src/__support/math/exp10f_double_eval.h | 4 +-
libc/src/__support/math/exp10f_float_eval.h | 2 +-
libc/src/__support/math/exp10f_utils.h | 4 +-
libc/src/__support/math/exp10m1f.h | 2 +-
libc/src/__support/math/exp2_accurate_128.h | 10 +-
libc/src/__support/math/exp2_double_eval.h | 2 +-
libc/src/__support/math/exp2bf16.h | 2 +-
libc/src/__support/math/exp2f_double_eval.h | 2 +-
libc/src/__support/math/exp2f_float_eval.h | 2 +-
libc/src/__support/math/exp2f_float_utils.h | 4 +-
libc/src/__support/math/exp2f_integer_eval.h | 9 +-
libc/src/__support/math/exp2f_integer_utils.h | 4 +-
libc/src/__support/math/exp2m1f.h | 2 +-
libc/src/__support/math/exp_double_eval.h | 2 +-
libc/src/__support/math/exp_double_utils.h | 6 +-
libc/src/__support/math/exp_utils.h | 13 +-
libc/src/__support/math/expbf16.h | 2 +-
libc/src/__support/math/expf16_utils.h | 3 +-
libc/src/__support/math/expf_double_eval.h | 2 +-
libc/src/__support/math/expf_float_eval.h | 6 +-
libc/src/__support/math/expf_float_utils.h | 10 +-
libc/src/__support/math/expf_integer_eval.h | 9 +-
libc/src/__support/math/expm1.h | 21 +-
libc/src/__support/math/expm1f.h | 2 +-
libc/src/__support/math/expxf16_utils.h | 3 +-
libc/src/__support/math/f16div.h | 2 +-
libc/src/__support/math/f16divf.h | 2 +-
libc/src/__support/math/f16divf128.h | 2 +-
libc/src/__support/math/f16divl.h | 2 +-
libc/src/__support/math/f16fma.h | 2 +-
libc/src/__support/math/f16fmaf.h | 2 +-
libc/src/__support/math/f16fmaf128.h | 2 +-
libc/src/__support/math/f16fmal.h | 3 +-
libc/src/__support/math/f16mul.h | 2 +-
libc/src/__support/math/f16mulf.h | 2 +-
libc/src/__support/math/f16mulf128.h | 2 +-
libc/src/__support/math/f16mull.h | 2 +-
libc/src/__support/math/f16sqrtf128.h | 4 +-
libc/src/__support/math/f16sub.h | 2 +-
libc/src/__support/math/f16subf.h | 2 +-
libc/src/__support/math/f16subf128.h | 2 +-
libc/src/__support/math/f16subl.h | 2 +-
libc/src/__support/math/floor.h | 9 +-
libc/src/__support/math/floorf.h | 9 +-
libc/src/__support/math/floorf16.h | 9 +-
libc/src/__support/math/fma.h | 2 +-
libc/src/__support/math/fmabf16.h | 2 +-
libc/src/__support/math/fmaf.h | 2 +-
libc/src/__support/math/fmaf16.h | 2 +-
libc/src/__support/math/fmaxf.h | 2 +-
libc/src/__support/math/fmul.h | 2 +-
libc/src/__support/math/gamma_util.h | 4 +-
libc/src/__support/math/hypot.h | 8 +-
libc/src/__support/math/hypotbf16.h | 2 +-
libc/src/__support/math/hypotf.h | 4 +-
libc/src/__support/math/hypotf16.h | 2 +-
libc/src/__support/math/isnan.h | 2 +-
libc/src/__support/math/isnanf.h | 2 +-
libc/src/__support/math/isnanf128.h | 2 +-
libc/src/__support/math/isnanf80.h | 2 +-
libc/src/__support/math/isnanl.h | 2 +-
libc/src/__support/math/lgammaf.h | 6 +-
libc/src/__support/math/lgammaf16.h | 4 +-
libc/src/__support/math/log.h | 5 +-
libc/src/__support/math/log10.h | 5 +-
libc/src/__support/math/log10f16.h | 2 +-
libc/src/__support/math/log10f_double_eval.h | 2 +-
libc/src/__support/math/log10p1f16.h | 2 +-
libc/src/__support/math/log1p.h | 6 +-
libc/src/__support/math/log1pf_double_eval.h | 2 +-
libc/src/__support/math/log2.h | 5 +-
libc/src/__support/math/log2f16.h | 2 +-
libc/src/__support/math/log2f_double_eval.h | 2 +-
libc/src/__support/math/log2p1f16.h | 2 +-
libc/src/__support/math/log_bf16.h | 2 +-
libc/src/__support/math/logf16.h | 2 +-
libc/src/__support/math/logf_double_eval.h | 2 +-
libc/src/__support/math/nextafterl.h | 3 +-
libc/src/__support/math/nextdownl.h | 2 +-
libc/src/__support/math/nexttoward.h | 2 +-
libc/src/__support/math/nexttowardbf16.h | 2 +-
libc/src/__support/math/nexttowardf.h | 2 +-
libc/src/__support/math/nexttowardf16.h | 3 +-
libc/src/__support/math/nexttowardl.h | 3 +-
libc/src/__support/math/nextupl.h | 2 +-
libc/src/__support/math/pow.h | 2 +-
libc/src/__support/math/pow_fast.h | 2 +-
libc/src/__support/math/powf_double_eval.h | 7 +-
libc/src/__support/math/powf_float_eval.h | 2 +-
libc/src/__support/math/powf_utils.h | 10 +-
libc/src/__support/math/range_reduction.h | 7 +-
.../math/range_reduction_double_common.h | 10 +-
.../math/range_reduction_double_fma.h | 3 +-
.../math/range_reduction_double_nofma.h | 3 +-
libc/src/__support/math/range_reduction_fma.h | 7 +-
libc/src/__support/math/rint.h | 9 +-
libc/src/__support/math/rintf.h | 9 +-
libc/src/__support/math/rintf16.h | 10 +-
libc/src/__support/math/round.h | 9 +-
libc/src/__support/math/roundeven.h | 9 +-
libc/src/__support/math/roundevenf.h | 9 +-
libc/src/__support/math/roundevenf16.h | 9 +-
libc/src/__support/math/roundf.h | 9 +-
libc/src/__support/math/roundf16.h | 9 +-
libc/src/__support/math/rsqrtf.h | 2 +-
libc/src/__support/math/sin.h | 4 +-
libc/src/__support/math/sin_integer_eval.h | 2 +-
libc/src/__support/math/sincos.h | 3 +-
libc/src/__support/math/sincos_eval.h | 8 +-
.../src/__support/math/sincos_integer_utils.h | 8 +-
libc/src/__support/math/sincosf.h | 3 +-
libc/src/__support/math/sincosf16_utils.h | 28 +-
libc/src/__support/math/sincosf_float_eval.h | 10 +-
libc/src/__support/math/sincosf_utils.h | 28 +-
libc/src/__support/math/sinf16.h | 4 +-
libc/src/__support/math/sinf_double_eval.h | 4 +-
libc/src/__support/math/sinf_float_eval.h | 2 +-
libc/src/__support/math/sinh_double_eval.h | 10 +-
libc/src/__support/math/sinhbf16.h | 2 +-
.../__support/math/sinhcosh_accurate_128.h | 8 +-
libc/src/__support/math/sinhcoshbf16_utils.h | 4 +-
libc/src/__support/math/sinhf.h | 2 +-
libc/src/__support/math/sinhfcoshf_utils.h | 9 +-
libc/src/__support/math/sinpif.h | 2 +-
libc/src/__support/math/sinpif16.h | 2 +-
libc/src/__support/math/sqrt.h | 4 +-
libc/src/__support/math/sqrtbf16.h | 4 +-
libc/src/__support/math/sqrtf.h | 4 +-
libc/src/__support/math/sqrtf128.h | 2 +-
libc/src/__support/math/sqrtl.h | 2 +-
libc/src/__support/math/tan.h | 17 +-
libc/src/__support/math/tanbf16.h | 4 +-
libc/src/__support/math/tanf16.h | 4 +-
libc/src/__support/math/tanf_double_eval.h | 4 +-
libc/src/__support/math/tanf_float_eval.h | 6 +-
libc/src/__support/math/tanh_accurate_128.h | 6 +-
libc/src/__support/math/tanh_double_eval.h | 10 +-
libc/src/__support/math/tanhf.h | 2 +-
libc/src/__support/math/tanhf16.h | 2 +-
libc/src/__support/math/tanpif.h | 4 +-
libc/src/__support/math/tanpif16.h | 2 +-
libc/src/__support/math/trunc.h | 9 +-
libc/src/__support/math/truncf.h | 9 +-
libc/src/__support/math/truncf16.h | 9 +-
libc/test/shared/CMakeLists.txt | 263 ++++++++++++++++--
.../shared/shared_math_constexpr_test.cpp | 262 ++++++++++++++++-
libc/test/shared/shared_math_test.cpp | 7 +-
219 files changed, 1060 insertions(+), 417 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index c78bab1e45b8a..15d7d0510cc06 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -814,6 +814,7 @@ add_header_library(
HDRS
ceil.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -837,6 +838,7 @@ add_header_library(
HDRS
ceilf.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -858,6 +860,7 @@ add_header_library(
HDRS
ceilf16.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.include.llvm-libc-macros.float16_macros
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.nearest_integer_operations
@@ -2536,6 +2539,7 @@ add_header_library(
HDRS
rint.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -2559,6 +2563,7 @@ add_header_library(
HDRS
rintf.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -2580,6 +2585,7 @@ add_header_library(
HDRS
rintf16.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.include.llvm-libc-macros.float16_macros
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.nearest_integer_operations
@@ -2602,6 +2608,7 @@ add_header_library(
HDRS
round.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.attributes
libc.src.__support.macros.config
@@ -2630,6 +2637,7 @@ add_header_library(
HDRS
roundeven.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -2653,6 +2661,7 @@ add_header_library(
HDRS
roundevenf.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -2674,6 +2683,7 @@ add_header_library(
HDRS
roundevenf16.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.include.llvm-libc-macros.float16_macros
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.nearest_integer_operations
@@ -2696,6 +2706,7 @@ add_header_library(
HDRS
roundf.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.attributes
libc.src.__support.macros.config
@@ -2722,6 +2733,7 @@ add_header_library(
HDRS
roundf16.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.include.llvm-libc-macros.float16_macros
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.nearest_integer_operations
@@ -2941,6 +2953,7 @@ add_header_library(
copysignf16.h
DEPENDS
libc.include.llvm-libc-macros.float16_macros
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.manipulation_functions
libc.src.__support.macros.config
FLAGS
@@ -3937,6 +3950,7 @@ add_header_library(
HDRS
floor.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -3960,6 +3974,7 @@ add_header_library(
HDRS
floorf.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -3981,6 +3996,7 @@ add_header_library(
HDRS
floorf16.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.include.llvm-libc-macros.float16_macros
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.nearest_integer_operations
@@ -6599,6 +6615,7 @@ add_header_library(
HDRS
trunc.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -6622,6 +6639,7 @@ add_header_library(
HDRS
truncf.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.src.__support.FPUtil.nearest_integer_operations
libc.src.__support.macros.config
FLAGS
@@ -6643,6 +6661,7 @@ add_header_library(
HDRS
truncf16.h
DEPENDS
+ libc.src.__support.CPP.type_traits
libc.include.llvm-libc-macros.float16_macros
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.nearest_integer_operations
diff --git a/libc/src/__support/math/acos.h b/libc/src/__support/math/acos.h
index dbcee8f0df6f1..b12561e0789d9 100644
--- a/libc/src/__support/math/acos.h
+++ b/libc/src/__support/math/acos.h
@@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double acos(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double acos(double x) {
using DoubleDouble = fputil::DoubleDouble;
using namespace asin_internal;
using FPBits = fputil::FPBits<double>;
diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h
index e0fb861accb47..16c251dbfd59b 100644
--- a/libc/src/__support/math/acosbf16.h
+++ b/libc/src/__support/math/acosbf16.h
@@ -21,7 +21,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 acosbf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 acosbf16(bfloat16 x) {
// Generated by Sollya using the following command:
// > display = hexadecimal;
// > round(pi/2, SG, RN);
diff --git a/libc/src/__support/math/acosf.h b/libc/src/__support/math/acosf.h
index 2f19ed4d99b29..296d236f755b6 100644
--- a/libc/src/__support/math/acosf.h
+++ b/libc/src/__support/math/acosf.h
@@ -46,7 +46,7 @@ LIBC_INLINE_VAR constexpr fputil::ExceptValues<float, N_EXCEPTS> ACOSF_EXCEPTS =
} // namespace acosf_internal
-LIBC_INLINE float acosf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float acosf(float x) {
constexpr double M_MATH_PI = 0x1.921fb54442d18p+1;
constexpr double M_MATH_PI_2 = 0x1.921fb54442d18p+0;
diff --git a/libc/src/__support/math/acoshbf16.h b/libc/src/__support/math/acoshbf16.h
index aaf9b0c05f786..17c713092651d 100644
--- a/libc/src/__support/math/acoshbf16.h
+++ b/libc/src/__support/math/acoshbf16.h
@@ -28,7 +28,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR bfloat16 acoshbf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 acoshbf16(bfloat16 x) {
using namespace acoshf_internal;
using FPBits = fputil::FPBits<bfloat16>;
diff --git a/libc/src/__support/math/acoshf.h b/libc/src/__support/math/acoshf.h
index 02b2a3d5a513d..7705848189ad2 100644
--- a/libc/src/__support/math/acoshf.h
+++ b/libc/src/__support/math/acoshf.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float acoshf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float acoshf(float x) {
using namespace acoshf_internal;
using FPBits_t = typename fputil::FPBits<float>;
FPBits_t xbits(x);
diff --git a/libc/src/__support/math/acoshf_utils.h b/libc/src/__support/math/acoshf_utils.h
index 3d760f9835249..4a8ff97a4c19e 100644
--- a/libc/src/__support/math/acoshf_utils.h
+++ b/libc/src/__support/math/acoshf_utils.h
@@ -26,7 +26,7 @@ namespace acoshf_internal {
#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) && \
defined(LIBC_MATH_HAS_SMALL_TABLES)
-LIBC_INLINE LIBC_CONSTEXPR double log_eval(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log_eval(double x) {
using FPBits = fputil::FPBits<double>;
FPBits x_bits(x);
uint64_t x_u = x_bits.uintval();
@@ -67,7 +67,7 @@ LIBC_INLINE LIBC_CONSTEXPR double log_eval(double x) {
#else // Accurate evaluation.
-LIBC_INLINE LIBC_CONSTEXPR double log_eval(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log_eval(double x) {
constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
// For x = 2^ex * (1 + mx)
diff --git a/libc/src/__support/math/acospif.h b/libc/src/__support/math/acospif.h
index f193a34f95316..05109716c8bff 100644
--- a/libc/src/__support/math/acospif.h
+++ b/libc/src/__support/math/acospif.h
@@ -21,7 +21,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float acospif(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float acospif(float x) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/asin.h b/libc/src/__support/math/asin.h
index cf4c2736c3e20..c011130aa8a66 100644
--- a/libc/src/__support/math/asin.h
+++ b/libc/src/__support/math/asin.h
@@ -25,7 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double asin(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double asin(double x) {
using namespace asin_internal;
using FPBits = fputil::FPBits<double>;
diff --git a/libc/src/__support/math/asin_utils.h b/libc/src/__support/math/asin_utils.h
index 59d3df2e32b16..69c0f874b5bd6 100644
--- a/libc/src/__support/math/asin_utils.h
+++ b/libc/src/__support/math/asin_utils.h
@@ -30,7 +30,7 @@ namespace asin_internal {
// > dirtyinfnorm(asin(x)/x - P, [0, 0.5]);
// 0x1p-23
// This approximation is intended for 16-bit results, not float results.
-LIBC_INLINE LIBC_CONSTEXPR float asin_eval_float(float xsq) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float asin_eval_float(float xsq) {
float x4 = xsq * xsq;
float c0 = fputil::multiply_add(xsq, 0x1.554c2ap-3f, 0x1.000002p0f);
float c1 = fputil::multiply_add(xsq, 0x1.43b2d6p-5f, 0x1.3541ccp-4f);
@@ -61,7 +61,7 @@ LIBC_INLINE_VAR constexpr double ASINF_COEFFS[12] = {
// Evaluate P(x^2) - 1, where P(x^2) ~ asin(x)/x, for float-precision asin.
// Used as: asin(x) ~ x + x^3 * asin_eval(x^2).
-LIBC_INLINE double asin_eval(double xsq) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double asin_eval(double xsq) {
double x4 = xsq * xsq;
double c0 = fputil::multiply_add(xsq, ASINF_COEFFS[1], ASINF_COEFFS[0]);
double c1 = fputil::multiply_add(xsq, ASINF_COEFFS[3], ASINF_COEFFS[2]);
@@ -110,7 +110,7 @@ LIBC_INLINE_VAR constexpr double ASINPIF_COEFFS[13] = {
// polynomial without c0) using Estrin's scheme.
// Used as: asinpif(x) ~ x * (ASINPIF_COEFFS[0] + v2 * asinpi_eval(v2))
// where v2 = x^2.
-LIBC_INLINE double asinpi_eval(double v2) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double asinpi_eval(double v2) {
double v4 = v2 * v2;
double v8 = v4 * v4;
double v16 = v8 * v8;
@@ -230,8 +230,9 @@ LIBC_INLINE_VAR constexpr double ASIN_COEFFS[9][12] = {
};
// We calculate the lower part of the approximation P(u).
-LIBC_INLINE DoubleDouble asin_eval(const DoubleDouble &u, unsigned &idx,
- double &err) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DoubleDouble asin_eval(const DoubleDouble &u,
+ unsigned &idx,
+ double &err) {
using fputil::multiply_add;
// k = round(u * 32).
double k = fputil::nearest_integer(u.hi * 0x1.0p5);
@@ -620,7 +621,8 @@ LIBC_INLINE_VAR constexpr DFloat128 PI_OVER_TWO_F128 = {
LIBC_INLINE_VAR constexpr DFloat128 PI_F128 = {
Sign::POS, -126, 0xc90fdaa2'2168c234'c4c6628b'80dc1cd1_u128};
-LIBC_INLINE constexpr DFloat128 asin_eval(const DFloat128 &u, unsigned idx) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DFloat128 asin_eval(const DFloat128 &u,
+ unsigned idx) {
return fputil::polyeval(u, ASIN_COEFFS_F128[idx][0], ASIN_COEFFS_F128[idx][1],
ASIN_COEFFS_F128[idx][2], ASIN_COEFFS_F128[idx][3],
ASIN_COEFFS_F128[idx][4], ASIN_COEFFS_F128[idx][5],
@@ -645,8 +647,8 @@ LIBC_INLINE_VAR constexpr DFloat128 HALF_F128 = {
// Compute asin(sqrt(u))/(pi*sqrt(u)) in DoubleDouble precision by wrapping
// asin_eval and multiplying by 1/pi.
-LIBC_INLINE DoubleDouble asinpi_eval(const DoubleDouble &u, unsigned &idx,
- double &err) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DoubleDouble
+asinpi_eval(const DoubleDouble &u, unsigned &idx, double &err) {
DoubleDouble p = asin_eval(u, idx, err);
DoubleDouble result = fputil::quick_mult(p, ONE_OVER_PI_DD);
// Scale error by ~1/pi and add multiplication rounding error.
@@ -655,7 +657,8 @@ LIBC_INLINE DoubleDouble asinpi_eval(const DoubleDouble &u, unsigned &idx,
}
// Compute asin(sqrt(u))/(pi*sqrt(u)) in DFloat128 precision.
-LIBC_INLINE constexpr DFloat128 asinpi_eval(const DFloat128 &u, unsigned idx) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DFloat128 asinpi_eval(const DFloat128 &u,
+ unsigned idx) {
DFloat128 p = asin_eval(u, idx);
return fputil::quick_mul(p, ONE_OVER_PI_F128);
}
diff --git a/libc/src/__support/math/asinbf16.h b/libc/src/__support/math/asinbf16.h
index 9083ca9adcce9..ddfbfaed83e69 100644
--- a/libc/src/__support/math/asinbf16.h
+++ b/libc/src/__support/math/asinbf16.h
@@ -21,7 +21,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR bfloat16 asinbf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 asinbf16(bfloat16 x) {
// Generated by Sollya using the following command:
// > display = hexadecimal;
// > round(pi/2, SG, RN);
diff --git a/libc/src/__support/math/asinf.h b/libc/src/__support/math/asinf.h
index fbcac68dfb55c..e561c35bae7b8 100644
--- a/libc/src/__support/math/asinf.h
+++ b/libc/src/__support/math/asinf.h
@@ -23,7 +23,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float asinf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float asinf(float x) {
using namespace asin_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/asinhf.h b/libc/src/__support/math/asinhf.h
index ac77eb2085853..45dcb74bbb18b 100644
--- a/libc/src/__support/math/asinhf.h
+++ b/libc/src/__support/math/asinhf.h
@@ -21,7 +21,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float asinhf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float asinhf(float x) {
using namespace acoshf_internal;
using FPBits_t = typename fputil::FPBits<float>;
FPBits_t xbits(x);
diff --git a/libc/src/__support/math/asinpi.h b/libc/src/__support/math/asinpi.h
index 9ed3cc419dd5e..3543c576fb098 100644
--- a/libc/src/__support/math/asinpi.h
+++ b/libc/src/__support/math/asinpi.h
@@ -30,7 +30,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double asinpi(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double asinpi(double x) {
using namespace asin_internal;
using FPBits = fputil::FPBits<double>;
diff --git a/libc/src/__support/math/asinpif.h b/libc/src/__support/math/asinpif.h
index 0a823c2bce627..e5c0a0f5c54aa 100644
--- a/libc/src/__support/math/asinpif.h
+++ b/libc/src/__support/math/asinpif.h
@@ -22,7 +22,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float asinpif(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float asinpif(float x) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/asinpif16.h b/libc/src/__support/math/asinpif16.h
index 7aefc35f82d01..342509d34dc67 100644
--- a/libc/src/__support/math/asinpif16.h
+++ b/libc/src/__support/math/asinpif16.h
@@ -27,7 +27,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 asinpif16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 asinpif16(float16 x) {
using FPBits = fputil::FPBits<float16>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h
index 8a11167a6268e..928102314adcd 100644
--- a/libc/src/__support/math/atan.h
+++ b/libc/src/__support/math/atan.h
@@ -52,7 +52,7 @@ namespace math {
// So we can return:
// atan(x) = sign(x) * (pi/2 - epsilon)
-LIBC_INLINE double atan(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double atan(double x) {
using namespace atan_internal;
using FPBits = fputil::FPBits<double>;
diff --git a/libc/src/__support/math/atan2.h b/libc/src/__support/math/atan2.h
index a009af39354a1..6b4de523d8f92 100644
--- a/libc/src/__support/math/atan2.h
+++ b/libc/src/__support/math/atan2.h
@@ -75,7 +75,7 @@ namespace math {
// and relative errors bounded by:
// |(atan(u) - P(u)) / P(u)| < u^10 / 11 < 2^-73.
-LIBC_INLINE double atan2(double y, double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double atan2(double y, double x) {
using namespace atan_internal;
using FPBits = fputil::FPBits<double>;
diff --git a/libc/src/__support/math/atan2f.h b/libc/src/__support/math/atan2f.h
index ff2419e69c6ae..ba7df5f3cfe0d 100644
--- a/libc/src/__support/math/atan2f.h
+++ b/libc/src/__support/math/atan2f.h
@@ -115,9 +115,10 @@ LIBC_INLINE_VAR constexpr fputil::DoubleDouble COEFFS[9] = {
// idx, k_d = round( 2^4 * num_d / den_d )
// final_sign = sign of the final result
// const_term = the constant term in the final expression.
-LIBC_INLINE float atan2f_double_double(double num_d, double den_d, double q_d,
- int idx, double k_d, double final_sign,
- const fputil::DoubleDouble &const_term) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float
+atan2f_double_double(double num_d, double den_d, double q_d, int idx,
+ double k_d, double final_sign,
+ const fputil::DoubleDouble &const_term) {
fputil::DoubleDouble q;
double num_r = 0, den_r = 0;
@@ -239,7 +240,7 @@ LIBC_INLINE float atan2f_double_double(double num_d, double den_d, double q_d,
// 0x1.aec6f...p-100
// which is about rounding errors of double-double (2^-104).
-LIBC_INLINE float atan2f(float y, float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float atan2f(float y, float x) {
using namespace atan2f_internal;
using namespace atan_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/atan2f128.h b/libc/src/__support/math/atan2f128.h
index 5df9071acc51b..7e903455d67d0 100644
--- a/libc/src/__support/math/atan2f128.h
+++ b/libc/src/__support/math/atan2f128.h
@@ -80,7 +80,7 @@ using LIBC_NAMESPACE::fputil::Float128;
// and relative errors bounded by:
// |(atan(u) - P(u)) / P(u)| < 2^-114.
-LIBC_INLINE Float128 atan2f128(Float128 y, Float128 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT Float128 atan2f128(Float128 y, Float128 x) {
using DFloat128 = fputil::DyadicFloat<128>;
constexpr DFloat128 ZERO = {Sign::POS, 0, 0_u128};
diff --git a/libc/src/__support/math/atan2f16.h b/libc/src/__support/math/atan2f16.h
index 0621ab4fd0547..009e1361e9e37 100644
--- a/libc/src/__support/math/atan2f16.h
+++ b/libc/src/__support/math/atan2f16.h
@@ -25,7 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 atan2f16(float16 y, float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 atan2f16(float16 y, float16 x) {
using namespace atan_internal;
using FPBits = fputil::FPBits<float16>;
diff --git a/libc/src/__support/math/atan2f_float.h b/libc/src/__support/math/atan2f_float.h
index 744783e855cef..1ed9a4d69da60 100644
--- a/libc/src/__support/math/atan2f_float.h
+++ b/libc/src/__support/math/atan2f_float.h
@@ -60,7 +60,7 @@ LIBC_INLINE_VAR constexpr FloatFloat ATAN_I[17] = {
// For x = x_hi + x_lo, fully expand the polynomial and drop any terms less than
// ulp(x_hi^3 / 3) gives us:
// P(x) ~ x_hi - x_hi^3/3 + x_lo * (1 - x_hi^2)
-LIBC_INLINE constexpr FloatFloat atan_eval(const FloatFloat &x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT FloatFloat atan_eval(const FloatFloat &x) {
FloatFloat p;
p.hi = x.hi;
float x_hi_sq = x.hi * x.hi;
@@ -124,7 +124,7 @@ LIBC_INLINE constexpr FloatFloat atan_eval(const FloatFloat &x) {
// > dirtyinfnorm(atan(x) - P, [-2^-5, 2^-5]);
// 0x1.995...p-28.
-LIBC_INLINE constexpr float atan2f(float y, float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float atan2f(float y, float x) {
using namespace atan2f_internal;
using FPBits = typename fputil::FPBits<float>;
constexpr float IS_NEG[2] = {1.0f, -1.0f};
diff --git a/libc/src/__support/math/atan_utils.h b/libc/src/__support/math/atan_utils.h
index 60d25e7d7374f..6b31d6839f915 100644
--- a/libc/src/__support/math/atan_utils.h
+++ b/libc/src/__support/math/atan_utils.h
@@ -110,7 +110,8 @@ LIBC_INLINE_VAR constexpr DoubleDouble ATAN_I[65] = {
// + x_lo * (1 - x_hi^2 + x_hi^4)
// Since p.lo is ~ x^3/3, the relative error from rounding is bounded by:
// |(atan(x) - P(x))/atan(x)| < ulp(x^2) <= 2^(-14-52) = 2^-66.
-[[maybe_unused]] LIBC_INLINE DoubleDouble atan_eval(const DoubleDouble &x) {
+[[maybe_unused]] LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DoubleDouble
+atan_eval(const DoubleDouble &x) {
DoubleDouble p;
p.hi = x.hi;
double x_hi_sq = x.hi * x.hi;
@@ -225,7 +226,8 @@ LIBC_INLINE_VAR constexpr DFloat128 ATAN_POLY_F128[] = {
};
// Approximate atan for |x| <= 2^-7.
-[[maybe_unused]] LIBC_INLINE constexpr DFloat128 atan_eval(const DFloat128 &x) {
+[[maybe_unused]] LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DFloat128
+atan_eval(const DFloat128 &x) {
DFloat128 x_sq = fputil::quick_mul(x, x);
DFloat128 x3 = fputil::quick_mul(x, x_sq);
DFloat128 p = fputil::polyeval(x_sq, ATAN_POLY_F128[0], ATAN_POLY_F128[1],
@@ -317,7 +319,7 @@ LIBC_INLINE_VAR constexpr double ATAN_K_OVER_16[17] = {
// For |x| <= 1/32 and 0 <= i <= 16, return Q(x) such that:
// Q(x) ~ (atan(x + i/16) - atan(i/16)) / x.
-LIBC_INLINE double atan_eval(double x, unsigned i) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double atan_eval(double x, unsigned i) {
double x2 = x * x;
double c0 = fputil::multiply_add(x, ATAN_COEFFS[i][2], ATAN_COEFFS[i][1]);
@@ -338,8 +340,8 @@ LIBC_INLINE double atan_eval(double x, unsigned i) {
// So we let q = (n - d * k/16) / (d + n * k/16),
// and approximate with Taylor polynomial:
// atan(q) ~ q - q^3/3 + q^5/5 - q^7/7 + q^9/9
-LIBC_INLINE double atan_eval_no_table(double num, double den,
- double k_over_16) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+atan_eval_no_table(double num, double den, double k_over_16) {
double num_r = fputil::multiply_add(den, -k_over_16, num);
double den_r = fputil::multiply_add(num, k_over_16, den);
double q = num_r / den_r;
diff --git a/libc/src/__support/math/atanbf16.h b/libc/src/__support/math/atanbf16.h
index 796420a6cf4b6..c03caf1c87bd5 100644
--- a/libc/src/__support/math/atanbf16.h
+++ b/libc/src/__support/math/atanbf16.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 atanbf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 atanbf16(bfloat16 x) {
// Generated by Sollya using the following command:
// > display = hexadecimal;
// > round(pi/2, SG, RN);
diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h
index 4f18565032251..ac39db304b64a 100644
--- a/libc/src/__support/math/atanf.h
+++ b/libc/src/__support/math/atanf.h
@@ -30,7 +30,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float atanf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float atanf(float x) {
using namespace atan_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/atanf_float.h b/libc/src/__support/math/atanf_float.h
index 56db2ad2955e6..f1ec4984f18da 100644
--- a/libc/src/__support/math/atanf_float.h
+++ b/libc/src/__support/math/atanf_float.h
@@ -66,7 +66,7 @@ LIBC_INLINE_VAR constexpr float ATANF_REDUCED_ARG[17] = {
// Approximating atan( u / (1 + u * k/16) )
// atan( u / (1 + u * k/16) ) / u ~ 1 - k/16 * u + (k^2/256 - 1/3) * u^2 +
// + (k/16 - (k/16)^3) * u^3 + O(u^4)
-LIBC_INLINE float atanf_eval(float u, float k_over_16) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float atanf_eval(float u, float k_over_16) {
// (k/16)^2
float c2 = k_over_16 * k_over_16;
// -(k/16)^3
@@ -85,7 +85,7 @@ LIBC_INLINE float atanf_eval(float u, float k_over_16) {
// There are several range reduction steps we can take for atan2(y, x) as
// follow:
-LIBC_INLINE float atanf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float atanf(float x) {
using namespace atanf_internal;
using FPBits = typename fputil::FPBits<float>;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/atanhf.h b/libc/src/__support/math/atanhf.h
index 684b74a46d353..3c5b102aad7f8 100644
--- a/libc/src/__support/math/atanhf.h
+++ b/libc/src/__support/math/atanhf.h
@@ -19,7 +19,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float atanhf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float atanhf(float x) {
using namespace acoshf_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/atanhf16.h b/libc/src/__support/math/atanhf16.h
index 0787e126b2691..d185f0b5472be 100644
--- a/libc/src/__support/math/atanhf16.h
+++ b/libc/src/__support/math/atanhf16.h
@@ -109,7 +109,7 @@ LIBC_INLINE_VAR constexpr float LOG_F_FLOAT[128] = {
// x should be positive, normal finite value
// TODO: Simplify range reduction and polynomial degree for float16.
// See issue #137190.
-LIBC_INLINE float log_eval_f(float x) {
+LIBC_INLINE constexpr float log_eval_f(float x) {
// For x = 2^ex * (1 + mx), logf(x) = ex * logf(2) + logf(1 + mx).
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/atanpif16.h b/libc/src/__support/math/atanpif16.h
index e0eaff6678fc5..fac8ca4951be6 100644
--- a/libc/src/__support/math/atanpif16.h
+++ b/libc/src/__support/math/atanpif16.h
@@ -73,7 +73,7 @@ namespace math {
// g(x)'s primary range of accuracy (i.e., if 0.5 < 1/|x| <= 1), the rule
// from Case 2 must be applied recursively to 1/|x|.
-LIBC_INLINE float16 atanpif16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 atanpif16(float16 x) {
using FPBits = fputil::FPBits<float16>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/bf16add.h b/libc/src/__support/math/bf16add.h
index 7d7a24d9bc134..2fe6e33922fa3 100644
--- a/libc/src/__support/math/bf16add.h
+++ b/libc/src/__support/math/bf16add.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16add(double x, double y) {
+LIBC_INLINE constexpr bfloat16 bf16add(double x, double y) {
return fputil::generic::add<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16addf.h b/libc/src/__support/math/bf16addf.h
index 28a836030d127..658686ea120f6 100644
--- a/libc/src/__support/math/bf16addf.h
+++ b/libc/src/__support/math/bf16addf.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16addf(float x, float y) {
+LIBC_INLINE constexpr bfloat16 bf16addf(float x, float y) {
return fputil::generic::add<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16addf128.h b/libc/src/__support/math/bf16addf128.h
index 33a891992e362..47c5736ffc783 100644
--- a/libc/src/__support/math/bf16addf128.h
+++ b/libc/src/__support/math/bf16addf128.h
@@ -20,7 +20,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16addf128(float128 x, float128 y) {
+LIBC_INLINE constexpr bfloat16 bf16addf128(float128 x, float128 y) {
return fputil::generic::add<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16addl.h b/libc/src/__support/math/bf16addl.h
index f9e27cb48ea09..940c74aef2867 100644
--- a/libc/src/__support/math/bf16addl.h
+++ b/libc/src/__support/math/bf16addl.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16addl(long double x, long double y) {
+LIBC_INLINE constexpr bfloat16 bf16addl(long double x, long double y) {
return fputil::generic::add<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16div.h b/libc/src/__support/math/bf16div.h
index 060105b88cd11..e8adc3c379a25 100644
--- a/libc/src/__support/math/bf16div.h
+++ b/libc/src/__support/math/bf16div.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16div(double x, double y) {
+LIBC_INLINE constexpr bfloat16 bf16div(double x, double y) {
return fputil::generic::div<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16divf.h b/libc/src/__support/math/bf16divf.h
index 0e201f8a124ed..ad5cb57aac828 100644
--- a/libc/src/__support/math/bf16divf.h
+++ b/libc/src/__support/math/bf16divf.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16divf(float x, float y) {
+LIBC_INLINE constexpr bfloat16 bf16divf(float x, float y) {
return fputil::generic::div<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16divf128.h b/libc/src/__support/math/bf16divf128.h
index 63d9cc28d1a33..c3312d6c70fa3 100644
--- a/libc/src/__support/math/bf16divf128.h
+++ b/libc/src/__support/math/bf16divf128.h
@@ -21,7 +21,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16divf128(float128 x, float128 y) {
+LIBC_INLINE constexpr bfloat16 bf16divf128(float128 x, float128 y) {
return fputil::generic::div<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16divl.h b/libc/src/__support/math/bf16divl.h
index ec5a9244b98d6..b6283c386be54 100644
--- a/libc/src/__support/math/bf16divl.h
+++ b/libc/src/__support/math/bf16divl.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16divl(long double x, long double y) {
+LIBC_INLINE constexpr bfloat16 bf16divl(long double x, long double y) {
return fputil::generic::div<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16fma.h b/libc/src/__support/math/bf16fma.h
index da88d5753f6bb..83fea3cd90d79 100644
--- a/libc/src/__support/math/bf16fma.h
+++ b/libc/src/__support/math/bf16fma.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16fma(double x, double y, double z) {
+LIBC_INLINE constexpr bfloat16 bf16fma(double x, double y, double z) {
return fputil::fma<bfloat16>(x, y, z);
}
diff --git a/libc/src/__support/math/bf16fmaf.h b/libc/src/__support/math/bf16fmaf.h
index a7072bcb18418..0033f39241e88 100644
--- a/libc/src/__support/math/bf16fmaf.h
+++ b/libc/src/__support/math/bf16fmaf.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16fmaf(float x, float y, float z) {
+LIBC_INLINE constexpr bfloat16 bf16fmaf(float x, float y, float z) {
return fputil::fma<bfloat16>(x, y, z);
}
diff --git a/libc/src/__support/math/bf16fmaf128.h b/libc/src/__support/math/bf16fmaf128.h
index 6376907152173..2b6bbce7a64cc 100644
--- a/libc/src/__support/math/bf16fmaf128.h
+++ b/libc/src/__support/math/bf16fmaf128.h
@@ -20,7 +20,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16fmaf128(float128 x, float128 y, float128 z) {
+LIBC_INLINE constexpr bfloat16 bf16fmaf128(float128 x, float128 y, float128 z) {
return fputil::fma<bfloat16>(x, y, z);
}
diff --git a/libc/src/__support/math/bf16fmal.h b/libc/src/__support/math/bf16fmal.h
index 93a04d0ec8fac..b3196672f23a7 100644
--- a/libc/src/__support/math/bf16fmal.h
+++ b/libc/src/__support/math/bf16fmal.h
@@ -16,7 +16,8 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16fmal(long double x, long double y, long double z) {
+LIBC_INLINE constexpr bfloat16 bf16fmal(long double x, long double y,
+ long double z) {
return fputil::fma<bfloat16>(x, y, z);
}
diff --git a/libc/src/__support/math/bf16mul.h b/libc/src/__support/math/bf16mul.h
index f4f6943e17a84..00aec7bd33731 100644
--- a/libc/src/__support/math/bf16mul.h
+++ b/libc/src/__support/math/bf16mul.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16mul(double x, double y) {
+LIBC_INLINE constexpr bfloat16 bf16mul(double x, double y) {
return fputil::generic::mul<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16mulf.h b/libc/src/__support/math/bf16mulf.h
index 16f2129ef4a5c..50ab51d10debe 100644
--- a/libc/src/__support/math/bf16mulf.h
+++ b/libc/src/__support/math/bf16mulf.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16mulf(float x, float y) {
+LIBC_INLINE constexpr bfloat16 bf16mulf(float x, float y) {
return fputil::generic::mul<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16mulf128.h b/libc/src/__support/math/bf16mulf128.h
index 848a7bca2440e..14d2f9b457025 100644
--- a/libc/src/__support/math/bf16mulf128.h
+++ b/libc/src/__support/math/bf16mulf128.h
@@ -20,7 +20,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16mulf128(float128 x, float128 y) {
+LIBC_INLINE constexpr bfloat16 bf16mulf128(float128 x, float128 y) {
return fputil::generic::mul<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/bf16mull.h b/libc/src/__support/math/bf16mull.h
index 52b8e3be72baf..52b7524eff937 100644
--- a/libc/src/__support/math/bf16mull.h
+++ b/libc/src/__support/math/bf16mull.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 bf16mull(long double x, long double y) {
+LIBC_INLINE constexpr bfloat16 bf16mull(long double x, long double y) {
return fputil::generic::mul<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/cbrt.h b/libc/src/__support/math/cbrt.h
index c6631e6f67e2c..100fe50b013e1 100644
--- a/libc/src/__support/math/cbrt.h
+++ b/libc/src/__support/math/cbrt.h
@@ -35,7 +35,7 @@ using namespace fputil;
// > P = fpminimax(x^(-2/3), 7, [|D...|], [1, 2]);
// > dirtyinfnorm(P/x^(-2/3) - 1, [1, 2]);
// 0x1.28...p-21
-LIBC_INLINE double initial_approximation(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double initial_approximation(double x) {
constexpr double COEFFS[8] = {
0x1.bc52aedead5c6p1, -0x1.b52bfebf110b3p2, 0x1.1d8d71d53d126p3,
-0x1.de2db9e81cf87p2, 0x1.0154ca06153bdp2, -0x1.5973c66ee6da7p0,
@@ -59,14 +59,14 @@ LIBC_INLINE double initial_approximation(double x) {
// Get the error term for Newton iteration:
// h(x) = x^3 * a^2 - 1,
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-LIBC_INLINE double get_error(const DoubleDouble &x_3,
- const DoubleDouble &a_sq) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double get_error(const DoubleDouble &x_3,
+ const DoubleDouble &a_sq) {
return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) +
fputil::multiply_add(x_3.lo, a_sq.hi, x_3.hi * a_sq.lo);
}
#else
-LIBC_INLINE constexpr double get_error(const DoubleDouble &x_3,
- const DoubleDouble &a_sq) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double get_error(const DoubleDouble &x_3,
+ const DoubleDouble &a_sq) {
DoubleDouble x_3_a_sq = fputil::quick_mult(a_sq, x_3);
return (x_3_a_sq.hi - 1.0) + x_3_a_sq.lo;
}
@@ -144,7 +144,7 @@ LIBC_INLINE constexpr double get_error(const DoubleDouble &x_3,
// exceptional handling, similar to what was done in the CORE-MATH project:
// https://gitlab.inria.fr/core-math/core-math/-/blob/master/src/binary64/cbrt/cbrt.c
-LIBC_INLINE double cbrt(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double cbrt(double x) {
using DoubleDouble = fputil::DoubleDouble;
using namespace cbrt_internal;
using FPBits = fputil::FPBits<double>;
diff --git a/libc/src/__support/math/cbrtbf16.h b/libc/src/__support/math/cbrtbf16.h
index 75a08a32edfa1..a0afea08f9775 100644
--- a/libc/src/__support/math/cbrtbf16.h
+++ b/libc/src/__support/math/cbrtbf16.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 cbrtbf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 cbrtbf16(bfloat16 x) {
return fputil::cast<bfloat16>(math::cbrtf(static_cast<float>(x)));
}
diff --git a/libc/src/__support/math/cbrtf.h b/libc/src/__support/math/cbrtf.h
index d73fb001b6362..a5fb18314ae91 100644
--- a/libc/src/__support/math/cbrtf.h
+++ b/libc/src/__support/math/cbrtf.h
@@ -88,7 +88,7 @@ LIBC_INLINE_VAR constexpr double COEFFS[16][7] = {
} // namespace cbrtf_internal
-LIBC_INLINE float cbrtf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float cbrtf(float x) {
using namespace cbrtf_internal;
using FloatBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/ceil.h b/libc/src/__support/math/ceil.h
index e5fc654727025..980d2389a5433 100644
--- a/libc/src/__support/math/ceil.h
+++ b/libc/src/__support/math/ceil.h
@@ -9,16 +9,21 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CEIL_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_CEIL_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR double ceil(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double ceil(double x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_USE_CONSTEXPR)
- return __builtin_ceil(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::ceil(x);
+ } else {
+ return __builtin_ceil(x);
+ }
#else
return fputil::ceil(x);
#endif
diff --git a/libc/src/__support/math/ceilf.h b/libc/src/__support/math/ceilf.h
index 7bd5dc92c09f4..2757f3720e87f 100644
--- a/libc/src/__support/math/ceilf.h
+++ b/libc/src/__support/math/ceilf.h
@@ -9,16 +9,21 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CEILF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_CEILF_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float ceilf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float ceilf(float x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_USE_CONSTEXPR)
- return __builtin_ceilf(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::ceil(x);
+ } else {
+ return __builtin_ceilf(x);
+ }
#else
return fputil::ceil(x);
#endif
diff --git a/libc/src/__support/math/ceilf16.h b/libc/src/__support/math/ceilf16.h
index eb6245c985120..62a3ee04b0734 100644
--- a/libc/src/__support/math/ceilf16.h
+++ b/libc/src/__support/math/ceilf16.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT16
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/FPUtil/cast.h"
#include "src/__support/macros/config.h"
@@ -21,11 +22,15 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float16 ceilf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 ceilf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
!defined(LIBC_USE_CONSTEXPR)
- return fputil::cast<float16>(__builtin_ceilf(x));
+ if (cpp::is_constant_evaluated()) {
+ return fputil::ceil(x);
+ } else {
+ return fputil::cast<float16>(__builtin_ceilf(x));
+ }
#else
return fputil::ceil(x);
#endif
diff --git a/libc/src/__support/math/ceill.h b/libc/src/__support/math/ceill.h
index b6b216a5b3489..3b3db1ddcddc5 100644
--- a/libc/src/__support/math/ceill.h
+++ b/libc/src/__support/math/ceill.h
@@ -16,7 +16,9 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
// TODO(issue#185232): Mark as constexpr once the refactor is done.
-LIBC_INLINE long double ceill(long double x) { return fputil::ceil(x); }
+LIBC_INLINE constexpr long double ceill(long double x) {
+ return fputil::ceil(x);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/copysign.h b/libc/src/__support/math/copysign.h
index 2285e50917649..882a9da8ecdfe 100644
--- a/libc/src/__support/math/copysign.h
+++ b/libc/src/__support/math/copysign.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR double copysign(double x, double y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double copysign(double x, double y) {
#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) && !defined(LIBC_USE_CONSTEXPR)
return __builtin_copysign(x, y);
#else
diff --git a/libc/src/__support/math/copysignf.h b/libc/src/__support/math/copysignf.h
index b873cfde621aa..0ba6faf5a21a2 100644
--- a/libc/src/__support/math/copysignf.h
+++ b/libc/src/__support/math/copysignf.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float copysignf(float x, float y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float copysignf(float x, float y) {
#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) && !defined(LIBC_USE_CONSTEXPR)
return __builtin_copysignf(x, y);
#else
diff --git a/libc/src/__support/math/copysignf16.h b/libc/src/__support/math/copysignf16.h
index abfa7113b1b8e..892d59dbe1981 100644
--- a/libc/src/__support/math/copysignf16.h
+++ b/libc/src/__support/math/copysignf16.h
@@ -13,15 +13,20 @@
#ifdef LIBC_TYPES_HAS_FLOAT16
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float16 copysignf16(float16 x, float16 y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 copysignf16(float16 x, float16 y) {
#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) && !defined(LIBC_USE_CONSTEXPR)
- return __builtin_copysignf16(x, y);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::copysign(x, y);
+ } else {
+ return __builtin_copysignf16(x, y);
+ }
#else
return fputil::copysign(x, y);
#endif
diff --git a/libc/src/__support/math/cos.h b/libc/src/__support/math/cos.h
index df69c8e292b30..efb7a0c918723 100644
--- a/libc/src/__support/math/cos.h
+++ b/libc/src/__support/math/cos.h
@@ -37,7 +37,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_INLINE double
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
cos_accurate(double x, uint16_t x_e, unsigned k,
const range_reduction_double_internal::LargeRangeReduction
&range_reduction_large) {
@@ -77,7 +77,7 @@ cos_accurate(double x, uint16_t x_e, unsigned k,
}
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_INLINE double cos(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double cos(double x) {
using namespace range_reduction_double_internal;
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/cos_integer_eval.h b/libc/src/__support/math/cos_integer_eval.h
index 8268929f3932a..31ae719a79fbd 100644
--- a/libc/src/__support/math/cos_integer_eval.h
+++ b/libc/src/__support/math/cos_integer_eval.h
@@ -23,7 +23,7 @@ namespace math {
namespace integer_only {
-LIBC_INLINE double cos(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double cos(double x) {
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/cosf_double_eval.h b/libc/src/__support/math/cosf_double_eval.h
index aa7132df93786..fddc812d815e8 100644
--- a/libc/src/__support/math/cosf_double_eval.h
+++ b/libc/src/__support/math/cosf_double_eval.h
@@ -29,7 +29,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_INLINE float cosf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float cosf(float x) {
using namespace sincosf_utils_internal;
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
diff --git a/libc/src/__support/math/cosf_float_eval.h b/libc/src/__support/math/cosf_float_eval.h
index 5cf0b1b07d128..1d065a8b4657c 100644
--- a/libc/src/__support/math/cosf_float_eval.h
+++ b/libc/src/__support/math/cosf_float_eval.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace float_eval {
-LIBC_INLINE float cosf(float x) {
+LIBC_INLINE constexpr float cosf(float x) {
return sincosf_float_eval::sincosf_eval</*IS_SIN*/ false>(x);
}
diff --git a/libc/src/__support/math/cosh_double_eval.h b/libc/src/__support/math/cosh_double_eval.h
index abcb90ba71904..1161062198d9e 100644
--- a/libc/src/__support/math/cosh_double_eval.h
+++ b/libc/src/__support/math/cosh_double_eval.h
@@ -38,7 +38,8 @@ LIBC_INLINE_VAR constexpr uint64_t COSH_OVERFLOW_BOUND =
// Handle tiny (|x| <= 2^-27), overflow (|x| >= 0x1.633ce8fb9f87ep+9), inf, and
// nan for cosh(x):
-LIBC_NOINLINE LIBC_INLINE double cosh_exceptional(double x) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+cosh_exceptional(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
@@ -62,7 +63,7 @@ LIBC_NOINLINE LIBC_INLINE double cosh_exceptional(double x) {
return fputil::set_overflow<double>();
}
-LIBC_ALWAYS_INLINE double cosh(double x) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double cosh(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/coshf.h b/libc/src/__support/math/coshf.h
index 843decc7877a6..270f9aea25fbc 100644
--- a/libc/src/__support/math/coshf.h
+++ b/libc/src/__support/math/coshf.h
@@ -19,7 +19,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float coshf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float coshf(float x) {
using namespace sinhfcoshf_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/cospif.h b/libc/src/__support/math/cospif.h
index 49100feba3230..b15863ebc4ad7 100644
--- a/libc/src/__support/math/cospif.h
+++ b/libc/src/__support/math/cospif.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float cospif(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float cospif(float x) {
using namespace sincosf_utils_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/daddl.h b/libc/src/__support/math/daddl.h
index e8bec7b975fb7..1e587446a82a7 100644
--- a/libc/src/__support/math/daddl.h
+++ b/libc/src/__support/math/daddl.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double daddl(long double x, long double y) {
+LIBC_INLINE constexpr double daddl(long double x, long double y) {
return fputil::generic::add<double>(x, y);
}
diff --git a/libc/src/__support/math/dfmaf128.h b/libc/src/__support/math/dfmaf128.h
index a4030d0c07299..df337b1bd79ad 100644
--- a/libc/src/__support/math/dfmaf128.h
+++ b/libc/src/__support/math/dfmaf128.h
@@ -20,7 +20,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double dfmaf128(float128 x, float128 y, float128 z) {
+LIBC_INLINE constexpr double dfmaf128(float128 x, float128 y, float128 z) {
return fputil::fma<double>(x, y, z);
}
diff --git a/libc/src/__support/math/dfmal.h b/libc/src/__support/math/dfmal.h
index 0331d59a2c962..ff5038d5e1eb2 100644
--- a/libc/src/__support/math/dfmal.h
+++ b/libc/src/__support/math/dfmal.h
@@ -16,7 +16,8 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double dfmal(long double x, long double y, long double z) {
+LIBC_INLINE constexpr double dfmal(long double x, long double y,
+ long double z) {
return fputil::fma<double>(x, y, z);
}
diff --git a/libc/src/__support/math/dsubf128.h b/libc/src/__support/math/dsubf128.h
index 4aee93a1114b6..0cbaffac39af1 100644
--- a/libc/src/__support/math/dsubf128.h
+++ b/libc/src/__support/math/dsubf128.h
@@ -19,7 +19,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double dsubf128(float128 x, float128 y) {
+LIBC_INLINE constexpr double dsubf128(float128 x, float128 y) {
return fputil::generic::sub<double>(x, y);
}
diff --git a/libc/src/__support/math/dsubl.h b/libc/src/__support/math/dsubl.h
index f56e253af9aa1..a82a05214f620 100644
--- a/libc/src/__support/math/dsubl.h
+++ b/libc/src/__support/math/dsubl.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double dsubl(long double x, long double y) {
+LIBC_INLINE constexpr double dsubl(long double x, long double y) {
return fputil::generic::sub<double>(x, y);
}
diff --git a/libc/src/__support/math/erfcf16.h b/libc/src/__support/math/erfcf16.h
index 35aaa88bc4236..ca9f70540538c 100644
--- a/libc/src/__support/math/erfcf16.h
+++ b/libc/src/__support/math/erfcf16.h
@@ -25,7 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 erfcf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 erfcf16(float16 x) {
// Polynomials approximating erfc(|x|) on ( k/2, (k+1)/2 ) generated by
// Sollya with: > P = fpminimax(erfc(x), [|0, 1, 2, 3, 4, 5, 6, 7|], [|D...|],
// [k/2, (k+1)/2]);
@@ -58,9 +58,9 @@ LIBC_INLINE float16 erfcf16(float16 x) {
0x1.a7c5af0484892p-12, -0x1.ea82a062010c3p-17},
};
- static constexpr size_t N_ERFCF16_EXCEPTS = 3;
- static constexpr fputil::ExceptValues<float16, N_ERFCF16_EXCEPTS>
- ERFCF16_EXCEPTS = {{
+ constexpr size_t N_ERFCF16_EXCEPTS = 3;
+ constexpr fputil::ExceptValues<float16, N_ERFCF16_EXCEPTS> ERFCF16_EXCEPTS = {
+ {
// (input, RZ output, RU offset, RD offset, RN offset)
// x = 0x0.000216, erfc(x) = 0x1.000
{0x0B17, 0x3BFF, 1, 0, 0},
diff --git a/libc/src/__support/math/erff.h b/libc/src/__support/math/erff.h
index e19997af28cd5..7fa008b59c5b5 100644
--- a/libc/src/__support/math/erff.h
+++ b/libc/src/__support/math/erff.h
@@ -128,7 +128,7 @@ LIBC_INLINE_VAR constexpr double COEFFS[32][8] = {
} // namespace erff_internal
-LIBC_INLINE float erff(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float erff(float x) {
using namespace erff_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/erff16.h b/libc/src/__support/math/erff16.h
index 68b6575957989..0d5fc01f6f69f 100644
--- a/libc/src/__support/math/erff16.h
+++ b/libc/src/__support/math/erff16.h
@@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 erff16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 erff16(float16 x) {
// Polynomials approximating erf(x)/x on ( k/8, (k + 1)/8 ) generated by
// Sollya with: > P = fpminimax(erf(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|],
diff --git a/libc/src/__support/math/exp10_double_eval.h b/libc/src/__support/math/exp10_double_eval.h
index 662c793b08149..dc0b1b0218050 100644
--- a/libc/src/__support/math/exp10_double_eval.h
+++ b/libc/src/__support/math/exp10_double_eval.h
@@ -100,7 +100,7 @@ LIBC_INLINE_VAR constexpr double EXP10_EXACT[22] = {
};
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_ALWAYS_INLINE double exp10(double x) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double exp10(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp10f16_utils.h b/libc/src/__support/math/exp10f16_utils.h
index a0fca6399a78f..c37a9b2043876 100644
--- a/libc/src/__support/math/exp10f16_utils.h
+++ b/libc/src/__support/math/exp10f16_utils.h
@@ -19,7 +19,8 @@
namespace LIBC_NAMESPACE_DECL {
-LIBC_INLINE ExpRangeReduction exp10_range_reduction(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT ExpRangeReduction
+exp10_range_reduction(float16 x) {
// For -8 < x < 5, to compute 10^x, we perform the following range reduction:
// find hi, mid, lo, such that:
// x = (hi + mid) * log2(10) + lo, in which
diff --git a/libc/src/__support/math/exp10f_double_eval.h b/libc/src/__support/math/exp10f_double_eval.h
index 56046c56f3d92..e75461cf07c94 100644
--- a/libc/src/__support/math/exp10f_double_eval.h
+++ b/libc/src/__support/math/exp10f_double_eval.h
@@ -27,7 +27,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_INLINE float exp10f_mid(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp10f_mid(float x) {
// Range reduction: 10^x = 2^(mid + hi) * 10^lo
auto rr = exp_b_range_reduc<Exp10Base>(x);
@@ -115,7 +115,7 @@ LIBC_COLD LIBC_NOINLINE LIBC_INLINE float exp10f_slow(float x) {
return exp10f_mid(x);
}
-LIBC_INLINE float exp10f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp10f(float x) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp10f_float_eval.h b/libc/src/__support/math/exp10f_float_eval.h
index 700397a85bd2b..a9de01ba926ed 100644
--- a/libc/src/__support/math/exp10f_float_eval.h
+++ b/libc/src/__support/math/exp10f_float_eval.h
@@ -29,7 +29,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace float_eval {
-LIBC_INLINE float exp10f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp10f(float x) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp10f_utils.h b/libc/src/__support/math/exp10f_utils.h
index 6134f3e6a8ab1..7cb7a235b8346 100644
--- a/libc/src/__support/math/exp10f_utils.h
+++ b/libc/src/__support/math/exp10f_utils.h
@@ -51,7 +51,7 @@ struct ExpBase {
0x1.ffffffffe5bc8p-2, 0x1.555555555cd67p-3, 0x1.5555c2a9b48b4p-5,
0x1.11112a0e34bdbp-7};
- LIBC_INLINE static double powb_lo(double dx) {
+ LIBC_INLINE static LIBC_CONSTEXPR_DEFAULT double powb_lo(double dx) {
using fputil::multiply_add;
double dx2 = dx * dx;
double c0 = 1.0 + dx;
@@ -89,7 +89,7 @@ struct Exp10Base : public ExpBase {
0x1.0470591dff149p1, 0x1.2bd7c0a9fbc4dp0,
0x1.1429e74a98f43p-1};
- LIBC_INLINE static double powb_lo(double dx) {
+ LIBC_INLINE static LIBC_CONSTEXPR_DEFAULT double powb_lo(double dx) {
using fputil::multiply_add;
double dx2 = dx * dx;
// c0 = 1 + COEFFS[0] * dx
diff --git a/libc/src/__support/math/exp10m1f.h b/libc/src/__support/math/exp10m1f.h
index 4039b708cc09f..74a1d18268758 100644
--- a/libc/src/__support/math/exp10m1f.h
+++ b/libc/src/__support/math/exp10m1f.h
@@ -104,7 +104,7 @@ LIBC_INLINE_VAR constexpr fputil::ExceptValues<float, N_EXCEPTS_HI>
} // namespace exp10m1f_internal
-LIBC_INLINE float exp10m1f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp10m1f(float x) {
using namespace exp10m1f_internal;
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp2_accurate_128.h b/libc/src/__support/math/exp2_accurate_128.h
index 832372591c67b..7eaf24e87ef90 100644
--- a/libc/src/__support/math/exp2_accurate_128.h
+++ b/libc/src/__support/math/exp2_accurate_128.h
@@ -148,7 +148,8 @@ LIBC_INLINE_VAR constexpr ExpBase192 LOG2_10_192 = {
// Evaluate 2^(idx * 2^-4 + u) in [1, 2) as Frac128 (lsb = 2^-127) for
// idx in [0, 15] and u in [0, 2^-4) (lsb = 2^-128).
-LIBC_INLINE Frac128 exp2_eval_frac128(unsigned idx, const Frac128 &u) {
+LIBC_INLINE constexpr Frac128 exp2_eval_frac128(unsigned idx,
+ const Frac128 &u) {
// p ~ (2^u - 1) / u
Frac128 p = EXP2_POLY_128[14]; // lsb = 2^-128
LIBC_LOOP_NOUNROLL
@@ -170,7 +171,8 @@ LIBC_INLINE Frac128 exp2_eval_frac128(unsigned idx, const Frac128 &u) {
// hi = k >> 4
// idx = k & 0x0f
// 2^z = 2^hi * EXP2_MID_FRAC128[idx] * (1 + u * P(u)).
-LIBC_INLINE DFloat128 exp2_df128(const DFloat128 &z, uint64_t lo = 0) {
+LIBC_INLINE constexpr DFloat128 exp2_df128(const DFloat128 &z,
+ uint64_t lo = 0) {
// z.mantissa has lsb = 2^z.exponent, so the 2^-4 bit of |z| is at bit
// (-4 - z.exponent) of z.mantissa, leaving
// shift = 128 - (-4 - z.exponent) = z.exponent + 132 bits
@@ -179,7 +181,7 @@ LIBC_INLINE DFloat128 exp2_df128(const DFloat128 &z, uint64_t lo = 0) {
// [-181, -117], so shift is in [-49, 15].
int shift = z.exponent + 132;
int k = 0;
- UInt<128> frac;
+ UInt<128> frac{};
if (LIBC_LIKELY(shift > 0)) {
// |z| >= 2^-5 (all |z| >= 2^-4, plus 2^-5 <= |z| < 2^-4 with k = 0 when
// bit 127 of z.mantissa is 0): split
@@ -246,7 +248,7 @@ LIBC_INLINE DFloat128 exp2_df128(const DFloat128 &z, uint64_t lo = 0) {
// 192-bit fixed-point range reduction. Here log2_base->log2_b holds:
// round(log2(b) * 2^(191 - base_exp)),
// or log2_base is nullptr when b = 2.
-LIBC_NOINLINE LIBC_INLINE double
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
exp_accurate_128(double x, const ExpBase192 *log2_base = nullptr) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp2_double_eval.h b/libc/src/__support/math/exp2_double_eval.h
index a462e2038e96a..0af9de872bd1f 100644
--- a/libc/src/__support/math/exp2_double_eval.h
+++ b/libc/src/__support/math/exp2_double_eval.h
@@ -34,7 +34,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_ALWAYS_INLINE double exp2(double x) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double exp2(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp2bf16.h b/libc/src/__support/math/exp2bf16.h
index 1dcd05b82f3e1..cc36e5ed479f8 100644
--- a/libc/src/__support/math/exp2bf16.h
+++ b/libc/src/__support/math/exp2bf16.h
@@ -51,7 +51,7 @@ LIBC_INLINE_VAR constexpr float EXP2_MID[16] = {
};
} // namespace exp2bf16_internal
-LIBC_INLINE bfloat16 exp2bf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 exp2bf16(bfloat16 x) {
using FPBits = fputil::FPBits<bfloat16>;
FPBits x_bits(x);
uint16_t x_uint = x_bits.uintval();
diff --git a/libc/src/__support/math/exp2f_double_eval.h b/libc/src/__support/math/exp2f_double_eval.h
index b69dbf65ef18d..ee96b6632f79d 100644
--- a/libc/src/__support/math/exp2f_double_eval.h
+++ b/libc/src/__support/math/exp2f_double_eval.h
@@ -29,7 +29,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_INLINE float exp2f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp2f(float x) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp2f_float_eval.h b/libc/src/__support/math/exp2f_float_eval.h
index fdd8167a09866..96a3c6f64af90 100644
--- a/libc/src/__support/math/exp2f_float_eval.h
+++ b/libc/src/__support/math/exp2f_float_eval.h
@@ -27,7 +27,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace float_eval {
-LIBC_INLINE float exp2f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp2f(float x) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp2f_float_utils.h b/libc/src/__support/math/exp2f_float_utils.h
index 28bad5a92dd76..a34bb78776a94 100644
--- a/libc/src/__support/math/exp2f_float_utils.h
+++ b/libc/src/__support/math/exp2f_float_utils.h
@@ -47,7 +47,7 @@ namespace float_eval {
// The intermediate product (y * 2^(m + 25)) is normal and computed with
// full precision. Then the final multiplication by 2^-25 rounds directly
// to the target subnormal precision in a single rounding step.
-LIBC_INLINE float scale_exp2f(float y, int m) {
+LIBC_INLINE constexpr float scale_exp2f(float y, int m) {
// -126 <= m <= 127: 2^m is a normal float.
if (LIBC_LIKELY(static_cast<uint32_t>(m + 126) <= 253)) {
uint32_t scale_bits = static_cast<uint32_t>(m + 127) << 23;
@@ -89,7 +89,7 @@ LIBC_INLINE float scale_exp2f(float y, int m) {
// p = q0 + u^4 * p2 all round at the exponent of c0 (~2^-1), accumulating
// rounding errors on non-FMA targets. Evaluating c1..c5 first keeps only a
// single rounding at c0's scale.
-LIBC_INLINE float exp2f_eval(float u, int m) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp2f_eval(float u, int m) {
constexpr float COEFFS[6] = {0x1.62e430p-1f, 0x1.ebfbdep-3f,
0x1.c6af9ep-5f, 0x1.3b2c58p-7f,
0x1.5ef4a2p-10f, 0x1.427918p-13f};
diff --git a/libc/src/__support/math/exp2f_integer_eval.h b/libc/src/__support/math/exp2f_integer_eval.h
index 5d60a465fd759..4c113dedac40f 100644
--- a/libc/src/__support/math/exp2f_integer_eval.h
+++ b/libc/src/__support/math/exp2f_integer_eval.h
@@ -27,8 +27,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace static_rounding {
-LIBC_ALWAYS_INLINE uint32_t exp2f_bits(uint32_t x_u,
- [[maybe_unused]] int rounding) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT uint32_t
+exp2f_bits(uint32_t x_u, [[maybe_unused]] int rounding) {
using FPBits = fputil::FPBits<float>;
bool is_neg = (x_u >> 31) != 0;
@@ -112,7 +112,8 @@ LIBC_ALWAYS_INLINE uint32_t exp2f_bits(uint32_t x_u,
// Statically rounded, no-except implementation of exp2f using integer-only
// arithmetic.
-LIBC_ALWAYS_INLINE float exp2f(float x, [[maybe_unused]] int rounding) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT float
+exp2f(float x, [[maybe_unused]] int rounding) {
return cpp::bit_cast<float>(exp2f_bits(cpp::bit_cast<uint32_t>(x), rounding));
}
@@ -120,7 +121,7 @@ LIBC_ALWAYS_INLINE float exp2f(float x, [[maybe_unused]] int rounding) {
namespace integer_eval {
-LIBC_ALWAYS_INLINE float exp2f(float x) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT float exp2f(float x) {
return static_rounding::exp2f(x, FE_TONEAREST);
}
diff --git a/libc/src/__support/math/exp2f_integer_utils.h b/libc/src/__support/math/exp2f_integer_utils.h
index 9f8a7bb094340..665386cd3e05c 100644
--- a/libc/src/__support/math/exp2f_integer_utils.h
+++ b/libc/src/__support/math/exp2f_integer_utils.h
@@ -87,8 +87,8 @@ LIBC_ALWAYS_INLINE uint64_t exp_b_scale(uint32_t x_u, uint32_t x_e,
// Evaluate 2^(x * log2(b)) from u_bits = |x| * log2(b) (with binary point at
// bit 54) and return the IEEE 754 single-precision bit representation.
-LIBC_INLINE uint32_t exp2f_eval_bits(uint64_t u_bits, bool is_neg,
- [[maybe_unused]] int rounding) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT uint32_t
+exp2f_eval_bits(uint64_t u_bits, bool is_neg, [[maybe_unused]] int rounding) {
// Negate in two's complement when x < 0 so that floor(x * log2(b)) is the
// arithmetic right shift by 54, and the fractional part u in [0, 1) is the
// low 54 bits.
diff --git a/libc/src/__support/math/exp2m1f.h b/libc/src/__support/math/exp2m1f.h
index 3b5cfb05918f2..a7569b0a242dd 100644
--- a/libc/src/__support/math/exp2m1f.h
+++ b/libc/src/__support/math/exp2m1f.h
@@ -26,7 +26,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float exp2m1f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp2m1f(float x) {
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
constexpr size_t N_EXCEPTS_LO = 8;
diff --git a/libc/src/__support/math/exp_double_eval.h b/libc/src/__support/math/exp_double_eval.h
index f46bd412e31e7..f73ad2c24af53 100644
--- a/libc/src/__support/math/exp_double_eval.h
+++ b/libc/src/__support/math/exp_double_eval.h
@@ -35,7 +35,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_ALWAYS_INLINE double exp(double x) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double exp(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/exp_double_utils.h b/libc/src/__support/math/exp_double_utils.h
index 00c43235f63ed..d9e48069a736a 100644
--- a/libc/src/__support/math/exp_double_utils.h
+++ b/libc/src/__support/math/exp_double_utils.h
@@ -59,7 +59,7 @@ LIBC_INLINE_VAR constexpr double EXP_P_COEFFS[6] = {
};
// Evaluate P(u) ~ (e^u - 1 - u) / u^2 using Estrin's scheme:
-LIBC_ALWAYS_INLINE double exp_poly(double u, double u2) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double exp_poly(double u, double u2) {
double c0 = fputil::multiply_add(u, EXP_P_COEFFS[1], EXP_P_COEFFS[0]);
double c1 = fputil::multiply_add(u, EXP_P_COEFFS[3], EXP_P_COEFFS[2]);
double u4 = u2 * u2;
@@ -139,8 +139,8 @@ LIBC_INLINE_VAR constexpr double EXP2_MID[128] = {
#endif // LIBC_MATH_HAS_SMALL_TABLES
-LIBC_NOINLINE LIBC_INLINE double exp_denorm(double x, int hi, double r,
- bool is_exp2) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+exp_denorm(double x, int hi, double r, bool is_exp2) {
using FPBits = fputil::FPBits<double>;
if (is_exp2 && LIBC_UNLIKELY(static_cast<double>(hi) == x))
return FPBits(uint64_t(1) << (hi + 1074)).get_val();
diff --git a/libc/src/__support/math/exp_utils.h b/libc/src/__support/math/exp_utils.h
index f5f6805725bd1..39262085f6c57 100644
--- a/libc/src/__support/math/exp_utils.h
+++ b/libc/src/__support/math/exp_utils.h
@@ -108,7 +108,8 @@ LIBC_INLINE_VAR constexpr DoubleDouble LOG2_E_DD = {0x1.777d0ffda0d24p-56,
// exp, exp2, and exp10. Callers have already verified that x is in the exact
// tiny, underflow, or overflow/inf/nan range for the specific function before
// calling this helper.
-LIBC_NOINLINE LIBC_INLINE double set_exceptional(double x) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+set_exceptional(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
@@ -278,7 +279,7 @@ LIBC_INLINE_VAR constexpr double EXP_P_COEFFS[4] = {
};
// Evaluate P(u) ~ (e^u - 1 - u) / u^2 using Estrin's scheme:
-LIBC_ALWAYS_INLINE double exp_poly(double u, double u2) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double exp_poly(double u, double u2) {
double c0 = fputil::multiply_add(u, EXP_P_COEFFS[1], EXP_P_COEFFS[0]);
double c1 = fputil::multiply_add(u, EXP_P_COEFFS[3], EXP_P_COEFFS[2]);
return fputil::multiply_add(u2, c1, c0);
@@ -466,7 +467,7 @@ LIBC_INLINE_VAR constexpr DoubleDouble EXP2_MID[128] = {
// with |lo_u - err_d| << r_hi.
// - err_d: Ziv error bound on r_hi + r_lo.
// - log2_base: pointer to 192-bit log2(b) table (or nullptr when b = 2).
-LIBC_NOINLINE LIBC_INLINE double
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
exp_denorm(double x, int hi, double r_hi, double lo_u, double err_d,
const ExpBase192 *log2_base = nullptr) {
using FPBits = fputil::FPBits<double>;
@@ -510,9 +511,9 @@ exp_denorm(double x, int hi, double r_hi, double lo_u, double err_d,
// - idx: table index k & 0x7f in [0, 127].
// - err_d: Ziv error bound on the double-double approximation r_hi + r_lo.
// - log2_base: pointer to 192-bit log2(b) table (or nullptr when b = 2).
-LIBC_ALWAYS_INLINE double exp2_eval(double x, DoubleDouble u, int hi,
- unsigned idx, double err_d = EXP2_ERR_D,
- const ExpBase192 *log2_base = nullptr) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double
+exp2_eval(double x, DoubleDouble u, int hi, unsigned idx,
+ double err_d = EXP2_ERR_D, const ExpBase192 *log2_base = nullptr) {
DoubleDouble exp2_mid = EXP2_MID[idx];
// Reconstruction of 2^(idx / 2^7) * exp(u) ~ exp2_mid * (1 + u + u^2 * P(u))
diff --git a/libc/src/__support/math/expbf16.h b/libc/src/__support/math/expbf16.h
index 2fdb5c3192583..7b2d9091db5fd 100644
--- a/libc/src/__support/math/expbf16.h
+++ b/libc/src/__support/math/expbf16.h
@@ -57,7 +57,7 @@ LIBC_INLINE_VAR constexpr float EXP_MID[32] = {
} // namespace expbf16_internal
-LIBC_INLINE bfloat16 expbf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 expbf16(bfloat16 x) {
using FPBits = fputil::FPBits<bfloat16>;
FPBits x_bits(x);
diff --git a/libc/src/__support/math/expf16_utils.h b/libc/src/__support/math/expf16_utils.h
index de7add4ecf731..60c3b56c42e65 100644
--- a/libc/src/__support/math/expf16_utils.h
+++ b/libc/src/__support/math/expf16_utils.h
@@ -47,7 +47,8 @@ struct ExpRangeReduction {
float exp_lo;
};
-[[maybe_unused]] LIBC_INLINE ExpRangeReduction exp_range_reduction(float16 x) {
+[[maybe_unused]] LIBC_INLINE LIBC_CONSTEXPR_DEFAULT ExpRangeReduction
+exp_range_reduction(float16 x) {
// For -18 < x < 12, to compute exp(x), we perform the following range
// reduction: find hi, mid, lo, such that:
// x = hi + mid + lo, in which
diff --git a/libc/src/__support/math/expf_double_eval.h b/libc/src/__support/math/expf_double_eval.h
index 2c5c947edbe19..16d4a5c1dedde 100644
--- a/libc/src/__support/math/expf_double_eval.h
+++ b/libc/src/__support/math/expf_double_eval.h
@@ -29,7 +29,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_INLINE float expf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float expf(float x) {
using FPBits = fputil::FPBits<float>;
using FPBounds = math::check::exp_internal::Bounds<float>;
diff --git a/libc/src/__support/math/expf_float_eval.h b/libc/src/__support/math/expf_float_eval.h
index 02cd044495879..756dc7fed736a 100644
--- a/libc/src/__support/math/expf_float_eval.h
+++ b/libc/src/__support/math/expf_float_eval.h
@@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace float_eval {
-LIBC_INLINE float expf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float expf(float x) {
using FPBits = fputil::FPBits<float>;
using FPBounds = math::check::exp_internal::Bounds<float>;
@@ -115,7 +115,7 @@ namespace float_eval_cr_rn {
// Designed for GPU performance. Correctly rounded only in round-to-nearest,
// ties-to-even mode. Requires single-precision FMA. Does not support errno
// or floating-point exception reporting.
-LIBC_INLINE float expf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float expf(float x) {
using FPBits = fputil::FPBits<float>;
using FPBounds = math::check::exp_internal::Bounds<float>;
@@ -174,7 +174,7 @@ LIBC_INLINE float expf(float x) {
float res = expf_internal::scale_result(exp_u, k, produces_subnormal);
// Special cases
- constexpr float INF = FPBits::inf().get_val();
+ LIBC_BIT_CAST_CONSTEXPR_VAR float INF = FPBits::inf().get_val();
res = is_tiny ? 1.0f : res;
res = should_overflow ? x + INF : res;
diff --git a/libc/src/__support/math/expf_float_utils.h b/libc/src/__support/math/expf_float_utils.h
index ac6371225a20a..af494f5931e6f 100644
--- a/libc/src/__support/math/expf_float_utils.h
+++ b/libc/src/__support/math/expf_float_utils.h
@@ -29,8 +29,8 @@ namespace expf_internal {
using fputil::FloatFloat;
-LIBC_ALWAYS_INLINE FloatFloat horner_step(float x, const FloatFloat &poly,
- const FloatFloat &coeff) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT FloatFloat
+horner_step(float x, const FloatFloat &poly, const FloatFloat &coeff) {
float hi = fputil::multiply_add(x, poly.hi, coeff.hi);
float lo = fputil::multiply_add(x, poly.hi, coeff.hi - hi);
lo = fputil::multiply_add(x, poly.lo, lo + coeff.lo);
@@ -51,7 +51,7 @@ LIBC_ALWAYS_INLINE FloatFloat horner_step(float x, const FloatFloat &poly,
// Float-float coefficients are split into:
// > hi = round(coeff(p, i), SG, RN);
// > lo = round(coeff(p, i) - hi, SG, RN);
-LIBC_INLINE FloatFloat exp_eval(const FloatFloat &u) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT FloatFloat exp_eval(const FloatFloat &u) {
// Coefficients of u^2, ..., u^6, in {lo, hi} order.
constexpr FloatFloat COEFFS[] = {
{-0x1.dp-46f, 0x1p-1f},
@@ -103,8 +103,8 @@ LIBC_INLINE FloatFloat exp_eval(const FloatFloat &u) {
}
// Computes 2^k * (exp_u.hi + exp_u.lo).
-LIBC_INLINE float scale_result(const FloatFloat &exp_u, int k,
- bool produces_subnormal) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float
+scale_result(const FloatFloat &exp_u, int k, bool produces_subnormal) {
using FPBits = fputil::FPBits<float>;
// Scaling the rounded sum by 2^k is exact for normal results: only the
diff --git a/libc/src/__support/math/expf_integer_eval.h b/libc/src/__support/math/expf_integer_eval.h
index 89b61870c4e98..aedb33149e8e4 100644
--- a/libc/src/__support/math/expf_integer_eval.h
+++ b/libc/src/__support/math/expf_integer_eval.h
@@ -34,8 +34,8 @@ namespace static_rounding {
// LSB(LOG2_E) = 2^-63
LIBC_INLINE_VAR constexpr Frac64 LOG2_E = Frac64(0xb8aa'3b29'5c17'f0bc);
-LIBC_ALWAYS_INLINE uint32_t expf_bits(uint32_t x_u,
- [[maybe_unused]] int rounding) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT uint32_t
+expf_bits(uint32_t x_u, [[maybe_unused]] int rounding) {
using FPBits = fputil::FPBits<float>;
using FPBounds = math::check::exp_internal::Bounds<float>;
@@ -100,7 +100,8 @@ LIBC_ALWAYS_INLINE uint32_t expf_bits(uint32_t x_u,
// Statically rounded, no-except implementation of expf using integer-only
// arithmetic.
-LIBC_ALWAYS_INLINE float expf(float x, [[maybe_unused]] int rounding) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT float
+expf(float x, [[maybe_unused]] int rounding) {
return cpp::bit_cast<float>(expf_bits(cpp::bit_cast<uint32_t>(x), rounding));
}
@@ -108,7 +109,7 @@ LIBC_ALWAYS_INLINE float expf(float x, [[maybe_unused]] int rounding) {
namespace integer_eval {
-LIBC_ALWAYS_INLINE float expf(float x) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT float expf(float x) {
return static_rounding::expf(x, FE_TONEAREST);
}
diff --git a/libc/src/__support/math/expm1.h b/libc/src/__support/math/expm1.h
index 282d5419b82f5..cff60dcda5af7 100644
--- a/libc/src/__support/math/expm1.h
+++ b/libc/src/__support/math/expm1.h
@@ -63,7 +63,7 @@ LIBC_INLINE_VAR constexpr double MLOG_2_EXP2_M12_LO = 0x1.b0e2633fe0685p-79;
// Return expm1(dx) / x ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24.
// For |dx| < 2^-13 + 2^-30:
// | output - expm1(dx) / dx | < 2^-51.
-LIBC_INLINE double poly_approx_d(double dx) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double poly_approx_d(double dx) {
// dx^2
double dx2 = dx * dx;
// c0 = 1 + dx / 2
@@ -80,7 +80,7 @@ LIBC_INLINE double poly_approx_d(double dx) {
// Return expm1(dx) / dx ~ 1 + dx / 2 + dx^2 / 6 + ... + dx^6 / 5040
// For |dx| < 2^-13 + 2^-30:
// | output - expm1(dx) | < 2^-101
-LIBC_INLINE DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
+LIBC_INLINE constexpr DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
// Taylor polynomial.
constexpr DoubleDouble COEFFS[] = {
{0, 0x1p0}, // 1
@@ -101,7 +101,8 @@ LIBC_INLINE DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
// Return (exp(dx) - 1)/dx ~ 1 + dx / 2 + dx^2 / 6 + ... + dx^6 / 5040
// For |dx| < 2^-13 + 2^-30:
// | output - exp(dx) | < 2^-126.
-[[maybe_unused]] LIBC_INLINE DFloat128 poly_approx_f128(const DFloat128 &dx) {
+[[maybe_unused]] LIBC_INLINE constexpr DFloat128
+poly_approx_f128(const DFloat128 &dx) {
constexpr DFloat128 COEFFS_128[]{
{Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
{Sign::POS, -128, 0x80000000'00000000'00000000'00000000_u128}, // 0.5
@@ -135,8 +136,8 @@ LIBC_INLINE std::ostream &operator<<(std::ostream &OS, const DoubleDouble &r) {
// Compute exp(x) - 1 using 128-bit precision.
// TODO(lntue): investigate triple-double precision implementation for this
// step.
-[[maybe_unused]] LIBC_INLINE DFloat128 expm1_f128(double x, double kd, int idx1,
- int idx2) {
+[[maybe_unused]] LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DFloat128
+expm1_f128(double x, double kd, int idx1, int idx2) {
// Recalculate dx:
double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact
@@ -187,9 +188,9 @@ LIBC_INLINE std::ostream &operator<<(std::ostream &OS, const DoubleDouble &r) {
}
// Compute exp(x) - 1 with double-double precision.
-LIBC_INLINE DoubleDouble exp_double_double(double x, double kd,
- const DoubleDouble &exp_mid,
- const DoubleDouble &hi_part) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DoubleDouble
+exp_double_double(double x, double kd, const DoubleDouble &exp_mid,
+ const DoubleDouble &hi_part) {
// Recalculate dx:
// dx = x - k * 2^-12 * log(2)
double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact
@@ -217,7 +218,7 @@ LIBC_INLINE DoubleDouble exp_double_double(double x, double kd,
// Check for exceptional cases when
// |x| <= 2^-53 or x < log(2^-54) or x >= 0x1.6232bdd7abcd3p+9
-LIBC_INLINE double set_exceptional(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double set_exceptional(double x) {
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);
@@ -265,7 +266,7 @@ LIBC_INLINE double set_exceptional(double x) {
} // namespace expm1_internal
-LIBC_INLINE double expm1(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double expm1(double x) {
using namespace expm1_internal;
using FPBits = typename fputil::FPBits<double>;
diff --git a/libc/src/__support/math/expm1f.h b/libc/src/__support/math/expm1f.h
index 8505d7e72740b..1d34794873e62 100644
--- a/libc/src/__support/math/expm1f.h
+++ b/libc/src/__support/math/expm1f.h
@@ -25,7 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float expm1f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float expm1f(float x) {
using namespace common_constants_internal;
using FPBits = typename fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/expxf16_utils.h b/libc/src/__support/math/expxf16_utils.h
index 33f66a93df631..8c7305afdf7b5 100644
--- a/libc/src/__support/math/expxf16_utils.h
+++ b/libc/src/__support/math/expxf16_utils.h
@@ -29,7 +29,8 @@ namespace math {
namespace expxf16_internal {
-LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT ExpRangeReduction
+exp2_range_reduction(float16 x) {
// For -25 < x < 16, to compute 2^x, we perform the following range reduction:
// find hi, mid, lo, such that:
// x = hi + mid + lo, in which
diff --git a/libc/src/__support/math/f16div.h b/libc/src/__support/math/f16div.h
index f53759330b867..480eb956cc047 100644
--- a/libc/src/__support/math/f16div.h
+++ b/libc/src/__support/math/f16div.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16div(double x, double y) {
+LIBC_INLINE constexpr float16 f16div(double x, double y) {
return fputil::generic::div<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16divf.h b/libc/src/__support/math/f16divf.h
index 488a3fb191b87..06a68460fc49b 100644
--- a/libc/src/__support/math/f16divf.h
+++ b/libc/src/__support/math/f16divf.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16divf(float x, float y) {
+LIBC_INLINE constexpr float16 f16divf(float x, float y) {
return fputil::generic::div<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16divf128.h b/libc/src/__support/math/f16divf128.h
index 011c136c54688..9509f18d201ba 100644
--- a/libc/src/__support/math/f16divf128.h
+++ b/libc/src/__support/math/f16divf128.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16divf128(float128 x, float128 y) {
+LIBC_INLINE constexpr float16 f16divf128(float128 x, float128 y) {
return fputil::generic::div<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16divl.h b/libc/src/__support/math/f16divl.h
index 6d8f63605824b..708d2ea6c3daf 100644
--- a/libc/src/__support/math/f16divl.h
+++ b/libc/src/__support/math/f16divl.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16divl(long double x, long double y) {
+LIBC_INLINE constexpr float16 f16divl(long double x, long double y) {
return fputil::generic::div<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16fma.h b/libc/src/__support/math/f16fma.h
index 2fc8e657b9efe..90c53fd11504b 100644
--- a/libc/src/__support/math/f16fma.h
+++ b/libc/src/__support/math/f16fma.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16fma(double x, double y, double z) {
+LIBC_INLINE constexpr float16 f16fma(double x, double y, double z) {
return fputil::fma<float16>(x, y, z);
}
diff --git a/libc/src/__support/math/f16fmaf.h b/libc/src/__support/math/f16fmaf.h
index 92f94a9721e7d..f109525816b71 100644
--- a/libc/src/__support/math/f16fmaf.h
+++ b/libc/src/__support/math/f16fmaf.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16fmaf(float x, float y, float z) {
+LIBC_INLINE constexpr float16 f16fmaf(float x, float y, float z) {
return fputil::fma<float16>(x, y, z);
}
diff --git a/libc/src/__support/math/f16fmaf128.h b/libc/src/__support/math/f16fmaf128.h
index c6bcd7ff1758d..d11d6e8623259 100644
--- a/libc/src/__support/math/f16fmaf128.h
+++ b/libc/src/__support/math/f16fmaf128.h
@@ -23,7 +23,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16fmaf128(float128 x, float128 y, float128 z) {
+LIBC_INLINE constexpr float16 f16fmaf128(float128 x, float128 y, float128 z) {
return fputil::fma<float16>(x, y, z);
}
diff --git a/libc/src/__support/math/f16fmal.h b/libc/src/__support/math/f16fmal.h
index 72fd6950fab49..600d5789cc639 100644
--- a/libc/src/__support/math/f16fmal.h
+++ b/libc/src/__support/math/f16fmal.h
@@ -20,7 +20,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16fmal(long double x, long double y, long double z) {
+LIBC_INLINE constexpr float16 f16fmal(long double x, long double y,
+ long double z) {
return fputil::fma<float16>(x, y, z);
}
diff --git a/libc/src/__support/math/f16mul.h b/libc/src/__support/math/f16mul.h
index 7ae906b8af279..5c4f3d0ba4c8b 100644
--- a/libc/src/__support/math/f16mul.h
+++ b/libc/src/__support/math/f16mul.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16mul(double x, double y) {
+LIBC_INLINE constexpr float16 f16mul(double x, double y) {
return fputil::generic::mul<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16mulf.h b/libc/src/__support/math/f16mulf.h
index 9a432d3fae4f2..e912764bdabdb 100644
--- a/libc/src/__support/math/f16mulf.h
+++ b/libc/src/__support/math/f16mulf.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16mulf(float x, float y) {
+LIBC_INLINE constexpr float16 f16mulf(float x, float y) {
return fputil::generic::mul<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16mulf128.h b/libc/src/__support/math/f16mulf128.h
index c1d4aa532a250..1b08596ef785a 100644
--- a/libc/src/__support/math/f16mulf128.h
+++ b/libc/src/__support/math/f16mulf128.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16mulf128(float128 x, float128 y) {
+LIBC_INLINE constexpr float16 f16mulf128(float128 x, float128 y) {
return fputil::generic::mul<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16mull.h b/libc/src/__support/math/f16mull.h
index 5fee45ad01124..2c0546bd87c5b 100644
--- a/libc/src/__support/math/f16mull.h
+++ b/libc/src/__support/math/f16mull.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16mull(long double x, long double y) {
+LIBC_INLINE constexpr float16 f16mull(long double x, long double y) {
return fputil::generic::mul<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16sqrtf128.h b/libc/src/__support/math/f16sqrtf128.h
index 40104ec5d791c..a6e1a35482da1 100644
--- a/libc/src/__support/math/f16sqrtf128.h
+++ b/libc/src/__support/math/f16sqrtf128.h
@@ -22,7 +22,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16sqrtf128(float128 x) { return fputil::sqrt<float16>(x); }
+LIBC_INLINE constexpr float16 f16sqrtf128(float128 x) {
+ return fputil::sqrt<float16>(x);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/f16sub.h b/libc/src/__support/math/f16sub.h
index 3280fb08e3103..4ec8bdc9c18e4 100644
--- a/libc/src/__support/math/f16sub.h
+++ b/libc/src/__support/math/f16sub.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16sub(double x, double y) {
+LIBC_INLINE constexpr float16 f16sub(double x, double y) {
return fputil::generic::sub<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16subf.h b/libc/src/__support/math/f16subf.h
index 1e19e90d18a6b..63a401577ee89 100644
--- a/libc/src/__support/math/f16subf.h
+++ b/libc/src/__support/math/f16subf.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16subf(float x, float y) {
+LIBC_INLINE constexpr float16 f16subf(float x, float y) {
return fputil::generic::sub<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16subf128.h b/libc/src/__support/math/f16subf128.h
index ce56475413f03..6bd6867b024f8 100644
--- a/libc/src/__support/math/f16subf128.h
+++ b/libc/src/__support/math/f16subf128.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16subf128(float128 x, float128 y) {
+LIBC_INLINE constexpr float16 f16subf128(float128 x, float128 y) {
return fputil::generic::sub<float16>(x, y);
}
diff --git a/libc/src/__support/math/f16subl.h b/libc/src/__support/math/f16subl.h
index 4515517f60ca2..caeccb054be5c 100644
--- a/libc/src/__support/math/f16subl.h
+++ b/libc/src/__support/math/f16subl.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 f16subl(long double x, long double y) {
+LIBC_INLINE constexpr float16 f16subl(long double x, long double y) {
return fputil::generic::sub<float16>(x, y);
}
diff --git a/libc/src/__support/math/floor.h b/libc/src/__support/math/floor.h
index dbf393a0b84ea..b57c48009d03c 100644
--- a/libc/src/__support/math/floor.h
+++ b/libc/src/__support/math/floor.h
@@ -9,16 +9,21 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FLOOR_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_FLOOR_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR double floor(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double floor(double x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_USE_CONSTEXPR)
- return __builtin_floor(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::floor(x);
+ } else {
+ return __builtin_floor(x);
+ }
#else
return fputil::floor(x);
#endif
diff --git a/libc/src/__support/math/floorf.h b/libc/src/__support/math/floorf.h
index 3012b49b121bd..c7b9cc436dc99 100644
--- a/libc/src/__support/math/floorf.h
+++ b/libc/src/__support/math/floorf.h
@@ -9,16 +9,21 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_FLOORF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_FLOORF_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float floorf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float floorf(float x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_USE_CONSTEXPR)
- return __builtin_floorf(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::floor(x);
+ } else {
+ return __builtin_floorf(x);
+ }
#else
return fputil::floor(x);
#endif
diff --git a/libc/src/__support/math/floorf16.h b/libc/src/__support/math/floorf16.h
index 34748411a31fb..62577991bf282 100644
--- a/libc/src/__support/math/floorf16.h
+++ b/libc/src/__support/math/floorf16.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT16
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/FPUtil/cast.h"
#include "src/__support/macros/config.h"
@@ -21,11 +22,15 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float16 floorf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 floorf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
!defined(LIBC_USE_CONSTEXPR)
- return fputil::cast<float16>(__builtin_floorf(x));
+ if (cpp::is_constant_evaluated()) {
+ return fputil::floor(x);
+ } else {
+ return fputil::cast<float16>(__builtin_floorf(x));
+ }
#else
return fputil::floor(x);
#endif
diff --git a/libc/src/__support/math/fma.h b/libc/src/__support/math/fma.h
index 723ff9fecd9f0..af1c6f0d72cee 100644
--- a/libc/src/__support/math/fma.h
+++ b/libc/src/__support/math/fma.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double fma(double x, double y, double z) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double fma(double x, double y, double z) {
return fputil::fma<double>(x, y, z);
}
diff --git a/libc/src/__support/math/fmabf16.h b/libc/src/__support/math/fmabf16.h
index 41a99df2d952b..d71c68ffb79f4 100644
--- a/libc/src/__support/math/fmabf16.h
+++ b/libc/src/__support/math/fmabf16.h
@@ -17,7 +17,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 fmabf16(bfloat16 x, bfloat16 y, bfloat16 z) {
+LIBC_INLINE constexpr bfloat16 fmabf16(bfloat16 x, bfloat16 y, bfloat16 z) {
return fputil::fma<bfloat16>(x, y, z);
}
diff --git a/libc/src/__support/math/fmaf.h b/libc/src/__support/math/fmaf.h
index 0437bb0ff44d6..b7f8721918c19 100644
--- a/libc/src/__support/math/fmaf.h
+++ b/libc/src/__support/math/fmaf.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float fmaf(float x, float y, float z) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float fmaf(float x, float y, float z) {
return fputil::fma<float>(x, y, z);
}
diff --git a/libc/src/__support/math/fmaf16.h b/libc/src/__support/math/fmaf16.h
index 2e061a97a2bd2..46aabe2694d65 100644
--- a/libc/src/__support/math/fmaf16.h
+++ b/libc/src/__support/math/fmaf16.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 fmaf16(float16 x, float16 y, float16 z) {
+LIBC_INLINE constexpr float16 fmaf16(float16 x, float16 y, float16 z) {
return fputil::fma<float16>(x, y, z);
}
diff --git a/libc/src/__support/math/fmaxf.h b/libc/src/__support/math/fmaxf.h
index 862d9bf1bd062..fe6effb9cf5ee 100644
--- a/libc/src/__support/math/fmaxf.h
+++ b/libc/src/__support/math/fmaxf.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float constexpr fmaxf(float x, float y) {
+LIBC_INLINE constexpr float fmaxf(float x, float y) {
return fputil::fmax(x, y);
}
diff --git a/libc/src/__support/math/fmul.h b/libc/src/__support/math/fmul.h
index ea0368f3b02cb..5b2cf73b67d70 100644
--- a/libc/src/__support/math/fmul.h
+++ b/libc/src/__support/math/fmul.h
@@ -18,7 +18,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float fmul(double x, double y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float fmul(double x, double y) {
// Without FMA instructions, fputil::exact_mult is not
// correctly rounded for all rounding modes, so we fall
diff --git a/libc/src/__support/math/gamma_util.h b/libc/src/__support/math/gamma_util.h
index 78ef9c8a330e3..7e5cbf894efd2 100644
--- a/libc/src/__support/math/gamma_util.h
+++ b/libc/src/__support/math/gamma_util.h
@@ -26,7 +26,7 @@ namespace gamma_internal {
// with max error ~2^{-55}, generated by Sollya with:
// > P = fpminimax(sin(pi*(x+0.5))/(pi*(0.25-x^2)),
// [|0,2,4,6,8,10,12,14|], [|D...|], [-0.5, 0.5]);
-LIBC_INLINE double lg_sinpi(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double lg_sinpi(double x) {
constexpr double COEFFS[8] = {0x000000000000001p+2, -0x1.de9e64df22ea4p+1,
0x1.472be122401f8p+0, -0x1.d4fcd82df91bp-3,
0x1.9f05c97e0aab2p-6, -0x1.f3091c427b611p-10,
@@ -51,7 +51,7 @@ LIBC_INLINE double lg_sinpi(double x) {
// P approximates log(1+z)/z on z in [-1/16, 1/16] with max
// error ~2^{-54}, generated by Sollya with:
// > P = fpminimax(log(1+x)/x, [|0,1,2,3,4,5,6,7|], [|D...|], [-1/16, 1/16]);
-LIBC_INLINE double lg_ln(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double lg_ln(double x) {
using FPBits = fputil::FPBits<double>;
uint64_t u = FPBits(x).uintval();
int e = static_cast<int>(FPBits(x).get_biased_exponent()) - 0x3ff;
diff --git a/libc/src/__support/math/hypot.h b/libc/src/__support/math/hypot.h
index 56516de0c80e8..493604665754f 100644
--- a/libc/src/__support/math/hypot.h
+++ b/libc/src/__support/math/hypot.h
@@ -32,7 +32,7 @@ namespace hypot_internal {
// inverse of the smallest normal number: 2^-1022, so that the correct "hidden"
// bit position is now 1. And by adding and subtracting 1 from the results, we
// can emulate the rounding behavior in the denormal range.
-LIBC_INLINE double hypot_denorm(double a, double b) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double hypot_denorm(double a, double b) {
using fputil::DoubleDouble;
constexpr double SCALE = 0x1.0p1022;
constexpr double SCALE_BACK = 0x1.0p-1022;
@@ -97,7 +97,7 @@ LIBC_INLINE double hypot_denorm(double a, double b) {
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_INLINE double hypot(double x, double y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double hypot(double x, double y) {
using FPBits = fputil::FPBits<double>;
using DoubleDouble = fputil::DoubleDouble;
@@ -111,8 +111,8 @@ LIBC_INLINE double hypot(double x, double y) {
// a = maximum_mag(x, y);
// b = minimum_mag(x, y);
- double a, b;
- uint32_t a_e, b_e;
+ double a{}, b{};
+ uint32_t a_e{}, b_e{};
if (x_e >= y_e) {
a_e = x_e;
diff --git a/libc/src/__support/math/hypotbf16.h b/libc/src/__support/math/hypotbf16.h
index e04013e4e3d68..ef870a9d36073 100644
--- a/libc/src/__support/math/hypotbf16.h
+++ b/libc/src/__support/math/hypotbf16.h
@@ -18,7 +18,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 hypotbf16(bfloat16 x, bfloat16 y) {
+LIBC_INLINE constexpr bfloat16 hypotbf16(bfloat16 x, bfloat16 y) {
return fputil::hypot<bfloat16>(x, y);
}
diff --git a/libc/src/__support/math/hypotf.h b/libc/src/__support/math/hypotf.h
index 5d877db190ace..95a0ba402f362 100644
--- a/libc/src/__support/math/hypotf.h
+++ b/libc/src/__support/math/hypotf.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float hypotf(float x, float y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float hypotf(float x, float y) {
using DoubleBits = fputil::FPBits<double>;
using FPBits = fputil::FPBits<float>;
using fputil::DoubleDouble;
@@ -51,7 +51,7 @@ LIBC_INLINE float hypotf(float x, float y) {
// x^2 and y^2 are exact in double precision.
double x_sq = xd * xd;
- double sum_sq;
+ double sum_sq{};
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
sum_sq = fputil::multiply_add(yd, yd, x_sq);
#else
diff --git a/libc/src/__support/math/hypotf16.h b/libc/src/__support/math/hypotf16.h
index f93500977f0e7..86cd95c117120 100644
--- a/libc/src/__support/math/hypotf16.h
+++ b/libc/src/__support/math/hypotf16.h
@@ -28,7 +28,7 @@ namespace math {
// For targets where conversion from float to float16 has to be
// emulated, fputil::hypot<float16> is faster
-LIBC_INLINE float16 hypotf16(float16 x, float16 y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 hypotf16(float16 x, float16 y) {
using FloatBits = fputil::FPBits<float>;
using FPBits = fputil::FPBits<float16>;
diff --git a/libc/src/__support/math/isnan.h b/libc/src/__support/math/isnan.h
index e02200d2a51fb..842ef0bdeea7a 100644
--- a/libc/src/__support/math/isnan.h
+++ b/libc/src/__support/math/isnan.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
#undef isnan
-LIBC_INLINE LIBC_CONSTEXPR int isnan(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int isnan(double x) {
#if defined(__LIBC_USE_BUILTIN_ISNAN) && !defined(LIBC_USE_CONSTEXPR)
return __builtin_isnan(x);
#else
diff --git a/libc/src/__support/math/isnanf.h b/libc/src/__support/math/isnanf.h
index 6613e67cc35cf..f4522752ba5bc 100644
--- a/libc/src/__support/math/isnanf.h
+++ b/libc/src/__support/math/isnanf.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
#undef isnanf
-LIBC_INLINE LIBC_CONSTEXPR int isnanf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int isnanf(float x) {
#if defined(__LIBC_USE_BUILTIN_ISNAN) && !defined(LIBC_USE_CONSTEXPR)
return __builtin_isnan(x);
#else
diff --git a/libc/src/__support/math/isnanf128.h b/libc/src/__support/math/isnanf128.h
index 422b8c453a6da..2528c06a5585e 100644
--- a/libc/src/__support/math/isnanf128.h
+++ b/libc/src/__support/math/isnanf128.h
@@ -18,7 +18,7 @@ namespace math {
using LIBC_NAMESPACE::fputil::Float128;
-LIBC_INLINE LIBC_CONSTEXPR int isnanf128(Float128 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int isnanf128(Float128 x) {
return fputil::FPBits<Float128>(x).is_nan();
}
diff --git a/libc/src/__support/math/isnanf80.h b/libc/src/__support/math/isnanf80.h
index 00f434c4599ce..01d1586d9de63 100644
--- a/libc/src/__support/math/isnanf80.h
+++ b/libc/src/__support/math/isnanf80.h
@@ -23,7 +23,7 @@ namespace math {
using LIBC_NAMESPACE::fputil::Float80;
-LIBC_INLINE LIBC_CONSTEXPR int isnanf80(Float80 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int isnanf80(Float80 x) {
return fputil::FPBits<Float80>(x).is_nan();
}
diff --git a/libc/src/__support/math/isnanl.h b/libc/src/__support/math/isnanl.h
index 32cf7b39bac69..1b08d3abf10b3 100644
--- a/libc/src/__support/math/isnanl.h
+++ b/libc/src/__support/math/isnanl.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
#undef isnanl
-LIBC_INLINE LIBC_CONSTEXPR int isnanl(long double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int isnanl(long double x) {
#if defined(__LIBC_USE_BUILTIN_ISNAN) && !defined(LIBC_USE_CONSTEXPR)
return __builtin_isnan(x);
#else
diff --git a/libc/src/__support/math/lgammaf.h b/libc/src/__support/math/lgammaf.h
index 9d77291670420..25c086c20d86b 100644
--- a/libc/src/__support/math/lgammaf.h
+++ b/libc/src/__support/math/lgammaf.h
@@ -34,7 +34,7 @@ namespace lgammaf_internal {
// P_M2(d), d = t - 1.5, approximates lgamma(t)/((t-1)(t-2)) on [1, 2].
// Degree 18 centered-monomial coefficients from a 250-bit Chebyshev fit,
// max relative error 2^-51.6
-LIBC_INLINE double lgamma_m2_poly(double d) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double lgamma_m2_poly(double d) {
constexpr double POLY_M2[19] = {
0x1.eeb95b094c191p-2, -0x1.2aed059bd613dp-3, 0x1.01af62a292eb9p-4,
-0x1.007aa83cc458bp-5, 0x1.13342351db068p-6, -0x1.34dbda42244dep-7,
@@ -66,7 +66,7 @@ LIBC_INLINE double lgamma_m2_poly(double d) {
} // namespace lgammaf_internal
-LIBC_INLINE float lgammaf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float lgammaf(float x) {
using namespace gamma_internal;
using namespace lgammaf_internal;
using FPBits = fputil::FPBits<float>;
@@ -105,7 +105,7 @@ LIBC_INLINE float lgammaf(float x) {
double xd = fputil::cast<double>(x);
double abs_xd = xd < 0.0 ? -xd : xd;
- double lgamma_val;
+ double lgamma_val{};
// For very tiny |x| (< 2^-23), use the truncated Laurent series:
// lgamma(x) = -log(x) - gamma*x + O(x^2) for tiny x > 0
diff --git a/libc/src/__support/math/lgammaf16.h b/libc/src/__support/math/lgammaf16.h
index 512a0c03ef22c..dc6b5d4c59f11 100644
--- a/libc/src/__support/math/lgammaf16.h
+++ b/libc/src/__support/math/lgammaf16.h
@@ -34,7 +34,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 lgammaf16(float16 x) {
+LIBC_INLINE constexpr float16 lgammaf16(float16 x) {
using namespace gamma_internal;
using FPBits = fputil::FPBits<float16>;
@@ -73,7 +73,7 @@ LIBC_INLINE float16 lgammaf16(float16 x) {
double xd = fputil::cast<double>(x);
double abs_xd = xd < 0.0 ? -xd : xd;
- double lgamma_val;
+ double lgamma_val{};
if (LIBC_UNLIKELY(x_abs < 0x3948u)) {
// Small path: |x| < 0.66015625
diff --git a/libc/src/__support/math/log.h b/libc/src/__support/math/log.h
index 0ccf347400217..28e58baa3b3c6 100644
--- a/libc/src/__support/math/log.h
+++ b/libc/src/__support/math/log.h
@@ -721,7 +721,8 @@ LIBC_INLINE_VAR constexpr DFloat128 BIG_COEFFS[3]{
// Reuse the output of the fast pass range reduction.
// -2^-8 <= m_x < 2^-7
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_INLINE double log_accurate(int e_x, int index, double m_x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log_accurate(int e_x, int index,
+ double m_x) {
DFloat128 e_x_f128(static_cast<float>(e_x));
DFloat128 sum = fputil::quick_mul(LOG_2, e_x_f128);
sum = fputil::quick_add(sum, LOG_TABLE.step_1[index]);
@@ -743,7 +744,7 @@ LIBC_INLINE double log_accurate(int e_x, int index, double m_x) {
} // namespace log_internal
-LIBC_INLINE LIBC_CONSTEXPR double log(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log(double x) {
using namespace log_internal;
using FPBits_t = typename fputil::FPBits<double>;
diff --git a/libc/src/__support/math/log10.h b/libc/src/__support/math/log10.h
index db6d345f98b71..7e14fcfd37ace 100644
--- a/libc/src/__support/math/log10.h
+++ b/libc/src/__support/math/log10.h
@@ -722,7 +722,8 @@ LIBC_INLINE_VAR constexpr DFloat128 BIG_COEFFS[4]{
// Reuse the output of the fast pass range reduction.
// -2^-8 <= m_x < 2^-7
-LIBC_INLINE double log10_accurate(int e_x, int index, double m_x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log10_accurate(int e_x, int index,
+ double m_x) {
DFloat128 e_x_f128(static_cast<float>(e_x));
DFloat128 sum = fputil::quick_mul(LOG10_2, e_x_f128);
@@ -744,7 +745,7 @@ LIBC_INLINE double log10_accurate(int e_x, int index, double m_x) {
} // namespace log10_internal
-LIBC_INLINE double log10(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log10(double x) {
using namespace log10_internal;
using namespace common_constants_internal;
using FPBits_t = typename fputil::FPBits<double>;
diff --git a/libc/src/__support/math/log10f16.h b/libc/src/__support/math/log10f16.h
index 424cf5f39a087..90d41c8ed094d 100644
--- a/libc/src/__support/math/log10f16.h
+++ b/libc/src/__support/math/log10f16.h
@@ -84,7 +84,7 @@ LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_LOG10F16_EXCEPTS>
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
} // namespace log10f16_internal
-LIBC_INLINE float16 log10f16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 log10f16(float16 x) {
using namespace math::expxf16_internal;
using FPBits = fputil::FPBits<float16>;
FPBits x_bits(x);
diff --git a/libc/src/__support/math/log10f_double_eval.h b/libc/src/__support/math/log10f_double_eval.h
index b2695f33dca7e..b46edfd67d146 100644
--- a/libc/src/__support/math/log10f_double_eval.h
+++ b/libc/src/__support/math/log10f_double_eval.h
@@ -124,7 +124,7 @@ LIBC_INLINE_VAR constexpr double COEFFS[5] = {
} // namespace log10f_internal
-LIBC_INLINE float log10f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float log10f(float x) {
using namespace common_constants_internal;
using namespace log10f_internal;
diff --git a/libc/src/__support/math/log10p1f16.h b/libc/src/__support/math/log10p1f16.h
index 6184a5795741b..b32a4d1ee9740 100644
--- a/libc/src/__support/math/log10p1f16.h
+++ b/libc/src/__support/math/log10p1f16.h
@@ -32,7 +32,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 log10p1f16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 log10p1f16(float16 x) {
using namespace math::expxf16_internal;
using FPBits = fputil::FPBits<float16>;
FPBits x_bits(x);
diff --git a/libc/src/__support/math/log1p.h b/libc/src/__support/math/log1p.h
index 7dd7c66802c83..577f7b71fbcaa 100644
--- a/libc/src/__support/math/log1p.h
+++ b/libc/src/__support/math/log1p.h
@@ -828,8 +828,8 @@ LIBC_INLINE_VAR constexpr DFloat128 BIG_COEFFS[4]{
{Sign::NEG, -128, 0x80000000'00000000'00000000'00000000_u128},
};
-[[maybe_unused]] LIBC_INLINE double log1p_accurate(int e_x, int index,
- fputil::DoubleDouble m_x) {
+[[maybe_unused]] LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+log1p_accurate(int e_x, int index, fputil::DoubleDouble m_x) {
DFloat128 e_x_f128(static_cast<float>(e_x));
DFloat128 sum = fputil::quick_mul(LOG_2, e_x_f128);
sum = fputil::quick_add(sum, LOG_R1[index]);
@@ -884,7 +884,7 @@ LIBC_INLINE_VAR constexpr DFloat128 BIG_COEFFS[4]{
} // namespace log1p_internal
-LIBC_INLINE double log1p(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log1p(double x) {
using namespace log1p_internal;
using FPBits_t = typename fputil::FPBits<double>;
diff --git a/libc/src/__support/math/log1pf_double_eval.h b/libc/src/__support/math/log1pf_double_eval.h
index fdead34cfdf67..feac9489c53d2 100644
--- a/libc/src/__support/math/log1pf_double_eval.h
+++ b/libc/src/__support/math/log1pf_double_eval.h
@@ -40,7 +40,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_INLINE float log1pf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float log1pf(float x) {
using FPBits = typename fputil::FPBits<float>;
FPBits xbits(x);
uint32_t x_u = xbits.uintval();
diff --git a/libc/src/__support/math/log2.h b/libc/src/__support/math/log2.h
index 36e82e8f1e8dc..ad7e645499ede 100644
--- a/libc/src/__support/math/log2.h
+++ b/libc/src/__support/math/log2.h
@@ -841,7 +841,8 @@ LIBC_INLINE_VAR constexpr DFloat128 BIG_COEFFS[4]{
// Reuse the output of the fast pass range reduction.
// -2^-8 <= m_x < 2^-7
-LIBC_INLINE double log2_accurate(int e_x, int index, double m_x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log2_accurate(int e_x, int index,
+ double m_x) {
DFloat128 sum(static_cast<float>(e_x));
sum = fputil::quick_add(sum, LOG2_TABLE.step_1[index]);
@@ -862,7 +863,7 @@ LIBC_INLINE double log2_accurate(int e_x, int index, double m_x) {
} // namespace log2_internal
-LIBC_INLINE double log2(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double log2(double x) {
using namespace log2_internal;
using namespace common_constants_internal;
using FPBits_t = typename fputil::FPBits<double>;
diff --git a/libc/src/__support/math/log2f16.h b/libc/src/__support/math/log2f16.h
index 4a738b5d5b8d4..db39c948ae104 100644
--- a/libc/src/__support/math/log2f16.h
+++ b/libc/src/__support/math/log2f16.h
@@ -72,7 +72,7 @@ LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_LOG2F16_EXCEPTS>
} // namespace log2f16_internal
-LIBC_INLINE float16 log2f16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 log2f16(float16 x) {
using namespace math::expxf16_internal;
using FPBits = fputil::FPBits<float16>;
FPBits x_bits(x);
diff --git a/libc/src/__support/math/log2f_double_eval.h b/libc/src/__support/math/log2f_double_eval.h
index 91c6bd6ae87ed..08dd79bc8c40e 100644
--- a/libc/src/__support/math/log2f_double_eval.h
+++ b/libc/src/__support/math/log2f_double_eval.h
@@ -63,7 +63,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_INLINE float log2f(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float log2f(float x) {
using namespace common_constants_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/log2p1f16.h b/libc/src/__support/math/log2p1f16.h
index c60c94dee2474..d2a9d23999482 100644
--- a/libc/src/__support/math/log2p1f16.h
+++ b/libc/src/__support/math/log2p1f16.h
@@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 log2p1f16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 log2p1f16(float16 x) {
using namespace math::expxf16_internal;
using FPBits = fputil::FPBits<float16>;
FPBits x_bits(x);
diff --git a/libc/src/__support/math/log_bf16.h b/libc/src/__support/math/log_bf16.h
index f809fdc0750e4..be43090994a35 100644
--- a/libc/src/__support/math/log_bf16.h
+++ b/libc/src/__support/math/log_bf16.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 log_bf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 log_bf16(bfloat16 x) {
// Generated by Sollya with the following commands:
// > display = hexadecimal;
diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h
index 8915f60e972a8..91094cad70b37 100644
--- a/libc/src/__support/math/logf16.h
+++ b/libc/src/__support/math/logf16.h
@@ -78,7 +78,7 @@ LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
} // namespace logf16_internal
-LIBC_INLINE float16 logf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 logf16(float16 x) {
using namespace math::expxf16_internal;
using namespace math::logf16_internal;
using FPBits = fputil::FPBits<float16>;
diff --git a/libc/src/__support/math/logf_double_eval.h b/libc/src/__support/math/logf_double_eval.h
index 1116aecab747d..afad021981f3a 100644
--- a/libc/src/__support/math/logf_double_eval.h
+++ b/libc/src/__support/math/logf_double_eval.h
@@ -61,7 +61,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_INLINE float logf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float logf(float x) {
using namespace common_constants_internal;
constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/nextafterl.h b/libc/src/__support/math/nextafterl.h
index cf2c2e6da5fb4..186a47d7d0be5 100644
--- a/libc/src/__support/math/nextafterl.h
+++ b/libc/src/__support/math/nextafterl.h
@@ -16,7 +16,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE long double nextafterl(long double x, long double y) {
+LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR long double nextafterl(long double x,
+ long double y) {
return fputil::nextafter(x, y);
}
diff --git a/libc/src/__support/math/nextdownl.h b/libc/src/__support/math/nextdownl.h
index d0a71fe55fb7d..fadf3aa4c967e 100644
--- a/libc/src/__support/math/nextdownl.h
+++ b/libc/src/__support/math/nextdownl.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE long double nextdownl(long double x) {
+LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR long double nextdownl(long double x) {
return fputil::nextupdown</*IsDown=*/true>(x);
}
diff --git a/libc/src/__support/math/nexttoward.h b/libc/src/__support/math/nexttoward.h
index 7f9ba9df3a816..8ca3fcea31755 100644
--- a/libc/src/__support/math/nexttoward.h
+++ b/libc/src/__support/math/nexttoward.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double nexttoward(double x, long double y) {
+LIBC_INLINE constexpr double nexttoward(double x, long double y) {
// We can reuse the nextafter implementation because the internal nextafter is
// templated on the types of the arguments.
return fputil::nextafter(x, y);
diff --git a/libc/src/__support/math/nexttowardbf16.h b/libc/src/__support/math/nexttowardbf16.h
index 3d0d278f6801f..58cbdc10cb391 100644
--- a/libc/src/__support/math/nexttowardbf16.h
+++ b/libc/src/__support/math/nexttowardbf16.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 nexttowardbf16(bfloat16 x, long double y) {
+LIBC_INLINE constexpr bfloat16 nexttowardbf16(bfloat16 x, long double y) {
// nextafter<T, U> where T != U is nexttoward
return fputil::nextafter(x, y);
}
diff --git a/libc/src/__support/math/nexttowardf.h b/libc/src/__support/math/nexttowardf.h
index 575b50744cd2f..68f259c1a80c4 100644
--- a/libc/src/__support/math/nexttowardf.h
+++ b/libc/src/__support/math/nexttowardf.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float nexttowardf(float x, long double y) {
+LIBC_INLINE constexpr float nexttowardf(float x, long double y) {
// We can reuse the nextafter implementation because the internal nextafter is
// templated on the types of the arguments.
return fputil::nextafter(x, y);
diff --git a/libc/src/__support/math/nexttowardf16.h b/libc/src/__support/math/nexttowardf16.h
index 9e5b3ef049b2d..60dd748680cfa 100644
--- a/libc/src/__support/math/nexttowardf16.h
+++ b/libc/src/__support/math/nexttowardf16.h
@@ -17,10 +17,9 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
-LIBC_INLINE float16 nexttowardf16(float16 x, long double y) {
+LIBC_INLINE constexpr float16 nexttowardf16(float16 x, long double y) {
// We can reuse the nextafter implementation because the internal nextafter is
// templated on the types of the arguments.
return fputil::nextafter(x, y);
diff --git a/libc/src/__support/math/nexttowardl.h b/libc/src/__support/math/nexttowardl.h
index b1a7a66f69886..574d2ea2cbdf7 100644
--- a/libc/src/__support/math/nexttowardl.h
+++ b/libc/src/__support/math/nexttowardl.h
@@ -16,7 +16,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE long double nexttowardl(long double x, long double y) {
+LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR long double nexttowardl(long double x,
+ long double y) {
// We can reuse the nextafter implementation because the internal nextafter is
// templated on the types of the arguments.
return fputil::nextafter(x, y);
diff --git a/libc/src/__support/math/nextupl.h b/libc/src/__support/math/nextupl.h
index 5e2ff41bc8c89..1d11100f7aa24 100644
--- a/libc/src/__support/math/nextupl.h
+++ b/libc/src/__support/math/nextupl.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE long double nextupl(long double x) {
+LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR long double nextupl(long double x) {
return fputil::nextupdown</*IsDown=*/false>(x);
}
diff --git a/libc/src/__support/math/pow.h b/libc/src/__support/math/pow.h
index 143d58d2bb551..5178ea9bde2f4 100644
--- a/libc/src/__support/math/pow.h
+++ b/libc/src/__support/math/pow.h
@@ -140,7 +140,7 @@ namespace math {
// extreme exponent boundaries (|e_h| ~ 2^10 and |dx| ~ 2^-7) the error can
// drift up to ~7.8 ULPs (bounded theoretically by ~16 ULPs).
-LIBC_INLINE double pow(double x, double y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double pow(double x, double y) {
using namespace common_constants_internal;
using namespace pow_internal;
using FPBits = fputil::FPBits<double>;
diff --git a/libc/src/__support/math/pow_fast.h b/libc/src/__support/math/pow_fast.h
index 2ef040c76f75b..b8c2e026cf881 100644
--- a/libc/src/__support/math/pow_fast.h
+++ b/libc/src/__support/math/pow_fast.h
@@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double pow(double x, double y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double pow(double x, double y) {
using namespace common_constants_internal;
using namespace pow_internal;
using FPBits = fputil::FPBits<double>;
diff --git a/libc/src/__support/math/powf_double_eval.h b/libc/src/__support/math/powf_double_eval.h
index 7c8b8c57cb965..2104c98fde164 100644
--- a/libc/src/__support/math/powf_double_eval.h
+++ b/libc/src/__support/math/powf_double_eval.h
@@ -38,7 +38,8 @@ namespace powf_internal = LIBC_NAMESPACE::math::powf_internal;
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
// Accurate evaluation in DoubleDouble precision when Ziv's test fails.
-LIBC_INLINE double powf_accurate(float x, float y, double e_x, uint64_t sign) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+powf_accurate(float x, float y, double e_x, uint64_t sign) {
using FloatBits = fputil::FPBits<float>;
using DoubleBits = fputil::FPBits<double>;
using fputil::DoubleDouble;
@@ -200,7 +201,7 @@ LIBC_INLINE double powf_accurate(float x, float y, double e_x, uint64_t sign) {
// range [-1022, 1023] of double precision.
// We compute exact_d = exact_m * 2^exact_exp in double precision and cast to
// float for the final rounded result.
-LIBC_INLINE cpp::optional<float>
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT cpp::optional<float>
check_exact_boundary(float x, float y, uint64_t sign,
Sign out_sign = Sign::POS) {
uint32_t exact_m = 0;
@@ -277,7 +278,7 @@ check_exact_boundary(float x, float y, uint64_t sign,
// y * (e_x + hi) is exact in double precision.
// 3. Exponent reduction with 64-entry EXP2_MID1 table and degree-8 polynomial
// in DoubleDouble.
-LIBC_INLINE float powf(float x, float y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float powf(float x, float y) {
using FloatBits = fputil::FPBits<float>;
using DoubleBits = fputil::FPBits<double>;
using fputil::DoubleDouble;
diff --git a/libc/src/__support/math/powf_float_eval.h b/libc/src/__support/math/powf_float_eval.h
index d19effd17d0f6..c97c39e014180 100644
--- a/libc/src/__support/math/powf_float_eval.h
+++ b/libc/src/__support/math/powf_float_eval.h
@@ -100,7 +100,7 @@ namespace powf_internal = LIBC_NAMESPACE::math::powf_internal;
// which provides a compact, code-size-friendly implementation without large
// exponent lookup tables.
-LIBC_INLINE float powf(float x, float y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float powf(float x, float y) {
using FPBits = fputil::FPBits<float>;
using namespace powf_internal;
diff --git a/libc/src/__support/math/powf_utils.h b/libc/src/__support/math/powf_utils.h
index bbbfbb63aea11..98059e4371e67 100644
--- a/libc/src/__support/math/powf_utils.h
+++ b/libc/src/__support/math/powf_utils.h
@@ -327,7 +327,8 @@ constexpr uint32_t Y_LOWER_BOUND = 0x2f5a'740d;
// Fast checks for special inputs:
// x = 0, +-1, 2^k, 10, +- Inf
// y = 0, +-1, 2, 0.5
-LIBC_ALWAYS_INLINE cpp::optional<float> check_special_inputs(float x, float y) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT cpp::optional<float>
+check_special_inputs(float x, float y) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x), ybits(y);
@@ -436,7 +437,7 @@ LIBC_ALWAYS_INLINE cpp::optional<float> check_special_inputs(float x, float y) {
// determined, or nullopt if regular evaluation should proceed (in which case x,
// y, ex, sign may be updated).
template <typename SignType = uint64_t>
-LIBC_ALWAYS_INLINE cpp::optional<float>
+LIBC_ALWAYS_INLINE constexpr cpp::optional<float>
check_exceptional_cases(float &x, float &y, int &ex, SignType &sign) {
using FloatBits = fputil::FPBits<float>;
FloatBits xbits(x), ybits(y);
@@ -579,8 +580,9 @@ check_exceptional_cases(float &x, float &y, int &ex, SignType &sign) {
// - The resulting significand (m^(2^f))^n <= 2^25.
//
// Returns true and sets exact_m and exact_exp if x^y is an exact boundary.
-LIBC_INLINE bool is_exact_rounding_boundary(float x, float y, uint32_t &exact_m,
- int &exact_exp) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bool
+is_exact_rounding_boundary(float x, float y, uint32_t &exact_m,
+ int &exact_exp) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/range_reduction.h b/libc/src/__support/math/range_reduction.h
index 93ec9846e5f1b..f107d482b8d5c 100644
--- a/libc/src/__support/math/range_reduction.h
+++ b/libc/src/__support/math/range_reduction.h
@@ -44,7 +44,8 @@ LIBC_INLINE_VAR constexpr int THIRTYTWO_OVER_PI_28_LSB_EXP[N_ENTRIES] = {
// Return k and y, where
// k = round(x * 16 / pi) and y = (x * 16 / pi) - k.
-LIBC_INLINE int64_t small_range_reduction(double x, double &y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int64_t small_range_reduction(double x,
+ double &y) {
double prod = x * THIRTYTWO_OVER_PI_28[0];
double kd = fputil::nearest_integer(prod);
y = prod - kd;
@@ -59,7 +60,9 @@ LIBC_INLINE int64_t small_range_reduction(double x, double &y) {
// contributing to the lowest 6 binary digits (k & 63). If the least
// significant bit of x * the least significant bit of THIRTYTWO_OVER_PI_28[i]
// >= 64, we can completely ignore THIRTYTWO_OVER_PI_28[i].
-LIBC_INLINE int64_t large_range_reduction(double x, int x_exp, double &y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int64_t large_range_reduction(double x,
+ int x_exp,
+ double &y) {
int idx = 0;
y = 0;
int x_lsb_exp_m4 = x_exp - fputil::FPBits<float>::FRACTION_LEN;
diff --git a/libc/src/__support/math/range_reduction_double_common.h b/libc/src/__support/math/range_reduction_double_common.h
index f26a8d5986d2e..6c0dd6fb11e6b 100644
--- a/libc/src/__support/math/range_reduction_double_common.h
+++ b/libc/src/__support/math/range_reduction_double_common.h
@@ -44,7 +44,8 @@ using DFloat128 = LIBC_NAMESPACE::fputil::DyadicFloat<128>;
// Error bound:
// |(x - k * pi/128) - (u_hi + u_lo)| <= max(ulp(ulp(u_hi)), 2^-119)
// <= 2^-111.
-LIBC_INLINE unsigned range_reduction_small(double x, DoubleDouble &u) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT unsigned
+range_reduction_small(double x, DoubleDouble &u) {
// Values of -pi/128 used for inputs with absolute value <= 2^16.
// The first 3 parts are generated with (53 - 21 = 32)-bit precision, so that
// the product k * MPI_OVER_128[i] is exact.
@@ -253,10 +254,10 @@ struct LargeRangeReduction {
// To be implemented in range_reduction_double_fma.h and
// range_reduction_double_nofma.h.
- unsigned fast(double x, DoubleDouble &u);
+ LIBC_CONSTEXPR_DEFAULT unsigned fast(double x, DoubleDouble &u);
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
- LIBC_INLINE DFloat128 accurate() const {
+ LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DFloat128 accurate() const {
constexpr DFloat128 PI_OVER_128_F128 = {
Sign::POS, -133, 0xc90f'daa2'2168'c234'c4c6'628b'80dc'1cd1_u128};
@@ -285,7 +286,8 @@ struct LargeRangeReduction {
};
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_INLINE DFloat128 range_reduction_small_f128(double x) {
+LIBC_INLINE
+LIBC_CONSTEXPR_DEFAULT DFloat128 range_reduction_small_f128(double x) {
constexpr DFloat128 PI_OVER_128_F128 = {
Sign::POS, -133, 0xc90f'daa2'2168'c234'c4c6'628b'80dc'1cd1_u128};
constexpr double ONE_TWENTY_EIGHT_OVER_PI_D = 0x1.45f306dc9c883p5;
diff --git a/libc/src/__support/math/range_reduction_double_fma.h b/libc/src/__support/math/range_reduction_double_fma.h
index b03d7f08713a3..4bae6d3d37ae2 100644
--- a/libc/src/__support/math/range_reduction_double_fma.h
+++ b/libc/src/__support/math/range_reduction_double_fma.h
@@ -24,7 +24,8 @@ namespace range_reduction_double_internal {
using LIBC_NAMESPACE::fputil::DoubleDouble;
-LIBC_INLINE unsigned LargeRangeReduction::fast(double x, DoubleDouble &u) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT unsigned
+LargeRangeReduction::fast(double x, DoubleDouble &u) {
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/range_reduction_double_nofma.h b/libc/src/__support/math/range_reduction_double_nofma.h
index d3ad74b6ffb7d..24e1c62e5d9f5 100644
--- a/libc/src/__support/math/range_reduction_double_nofma.h
+++ b/libc/src/__support/math/range_reduction_double_nofma.h
@@ -26,7 +26,8 @@ namespace range_reduction_double_internal {
using fputil::DoubleDouble;
-LIBC_INLINE unsigned LargeRangeReduction::fast(double x, DoubleDouble &u) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT unsigned
+LargeRangeReduction::fast(double x, DoubleDouble &u) {
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/range_reduction_fma.h b/libc/src/__support/math/range_reduction_fma.h
index fe6d94946af7e..80e3fd4b9d849 100644
--- a/libc/src/__support/math/range_reduction_fma.h
+++ b/libc/src/__support/math/range_reduction_fma.h
@@ -33,7 +33,8 @@ LIBC_INLINE_VAR constexpr double THIRTYTWO_OVER_PI[5] = {
// Return k and y, where
// k = round(x * 32 / pi) and y = (x * 32 / pi) - k.
-LIBC_INLINE int64_t small_range_reduction(double x, double &y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int64_t small_range_reduction(double x,
+ double &y) {
double kd = fputil::nearest_integer(x * THIRTYTWO_OVER_PI[0]);
y = fputil::fma<double>(x, THIRTYTWO_OVER_PI[0], -kd);
y = fputil::fma<double>(x, THIRTYTWO_OVER_PI[1], y);
@@ -43,7 +44,9 @@ LIBC_INLINE int64_t small_range_reduction(double x, double &y) {
// Return k and y, where
// k = round(x * 32 / pi) and y = (x * 32 / pi) - k.
// This is used for sinf, cosf, sincosf.
-LIBC_INLINE int64_t large_range_reduction(double x, int x_exp, double &y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int64_t large_range_reduction(double x,
+ int x_exp,
+ double &y) {
// 2^45 <= |x| < 2^99
if (x_exp < 99) {
// - When x < 2^99, the full exact product of x * THIRTYTWO_OVER_PI[0]
diff --git a/libc/src/__support/math/rint.h b/libc/src/__support/math/rint.h
index 278428e350a1e..66cbe7cfcbf63 100644
--- a/libc/src/__support/math/rint.h
+++ b/libc/src/__support/math/rint.h
@@ -9,16 +9,21 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_RINT_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_RINT_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR double rint(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double rint(double x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_USE_CONSTEXPR)
- return __builtin_rint(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round_using_current_rounding_mode(x);
+ } else {
+ return __builtin_rint(x);
+ }
#else
return fputil::round_using_current_rounding_mode(x);
#endif
diff --git a/libc/src/__support/math/rintf.h b/libc/src/__support/math/rintf.h
index b3256a73f9708..091933830d9ad 100644
--- a/libc/src/__support/math/rintf.h
+++ b/libc/src/__support/math/rintf.h
@@ -9,16 +9,21 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_RINTF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_RINTF_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float rintf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float rintf(float x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_USE_CONSTEXPR)
- return __builtin_rintf(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round_using_current_rounding_mode(x);
+ } else {
+ return __builtin_rintf(x);
+ }
#else
return fputil::round_using_current_rounding_mode(x);
#endif
diff --git a/libc/src/__support/math/rintf16.h b/libc/src/__support/math/rintf16.h
index 036846d0dee5f..93dc2febde553 100644
--- a/libc/src/__support/math/rintf16.h
+++ b/libc/src/__support/math/rintf16.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT16
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/FPUtil/cast.h"
#include "src/__support/macros/config.h"
@@ -21,12 +22,15 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float16 rintf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 rintf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
!defined(LIBC_USE_CONSTEXPR)
-
- return fputil::cast<float16>(__builtin_rintf(x));
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round_using_current_rounding_mode(x);
+ } else {
+ return fputil::cast<float16>(__builtin_rintf(x));
+ }
#else
return fputil::round_using_current_rounding_mode(x);
#endif
diff --git a/libc/src/__support/math/round.h b/libc/src/__support/math/round.h
index 1f0d018957988..8e242360fdd9a 100644
--- a/libc/src/__support/math/round.h
+++ b/libc/src/__support/math/round.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ROUND_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_ROUND_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
@@ -46,7 +47,7 @@ namespace math {
// So we will only use `__builtin_round` with clang on x86-64 if we assume
// default rounding mode (FE_TONEAREST) only.
-LIBC_INLINE LIBC_CONSTEXPR double round(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double round(double x) {
#if __has_builtin(__builtin_round) && !defined(LIBC_USE_CONSTEXPR) && \
(defined(__LIBC_USE_BUILTIN_ROUND) || \
(defined(LIBC_COMPILER_IS_CLANG) && \
@@ -54,7 +55,11 @@ LIBC_INLINE LIBC_CONSTEXPR double round(double x) {
(!defined(__ARM_ARCH) || (__ARM_ARCH >= 8)) && \
(!defined(LIBC_TARGET_ARCH_IS_X86) || \
defined(LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY))))
- return __builtin_round(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round(x);
+ } else {
+ return __builtin_round(x);
+ }
#else
return fputil::round(x);
#endif
diff --git a/libc/src/__support/math/roundeven.h b/libc/src/__support/math/roundeven.h
index 861feb70ba9e7..0a36d1216e2c8 100644
--- a/libc/src/__support/math/roundeven.h
+++ b/libc/src/__support/math/roundeven.h
@@ -9,15 +9,20 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ROUNDEVEN_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_ROUNDEVEN_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR double roundeven(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double roundeven(double x) {
#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN) && !defined(LIBC_USE_CONSTEXPR)
- return __builtin_roundeven(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
+ } else {
+ return __builtin_roundeven(x);
+ }
#else
return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
#endif
diff --git a/libc/src/__support/math/roundevenf.h b/libc/src/__support/math/roundevenf.h
index c668374ea56ca..ee302eb706a51 100644
--- a/libc/src/__support/math/roundevenf.h
+++ b/libc/src/__support/math/roundevenf.h
@@ -9,15 +9,20 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ROUNDEVENF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_ROUNDEVENF_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float roundevenf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float roundevenf(float x) {
#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN) && !defined(LIBC_USE_CONSTEXPR)
- return __builtin_roundevenf(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
+ } else {
+ return __builtin_roundevenf(x);
+ }
#else
return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
#endif
diff --git a/libc/src/__support/math/roundevenf16.h b/libc/src/__support/math/roundevenf16.h
index 18bc7a3629284..35172b9037fea 100644
--- a/libc/src/__support/math/roundevenf16.h
+++ b/libc/src/__support/math/roundevenf16.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT16
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/FPUtil/cast.h"
#include "src/__support/macros/config.h"
@@ -21,11 +22,15 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float16 roundevenf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 roundevenf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN) && \
defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
!defined(LIBC_USE_CONSTEXPR)
- return fputil::cast<float16>(__builtin_roundevenf(x));
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
+ } else {
+ return fputil::cast<float16>(__builtin_roundevenf(x));
+ }
#else
return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
#endif
diff --git a/libc/src/__support/math/roundf.h b/libc/src/__support/math/roundf.h
index e1cb4fc9c2e9e..9e629957de6e0 100644
--- a/libc/src/__support/math/roundf.h
+++ b/libc/src/__support/math/roundf.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_ROUNDF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_ROUNDF_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
@@ -46,7 +47,7 @@ namespace math {
// So we will only use `__builtin_round` with clang on x86-64 if we assume
// default rounding mode (FE_TONEAREST) only.
-LIBC_INLINE LIBC_CONSTEXPR float roundf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float roundf(float x) {
#if __has_builtin(__builtin_roundf) && !defined(LIBC_USE_CONSTEXPR) && \
(defined(__LIBC_USE_BUILTIN_ROUND) || \
(defined(LIBC_COMPILER_IS_CLANG) && \
@@ -54,7 +55,11 @@ LIBC_INLINE LIBC_CONSTEXPR float roundf(float x) {
(!defined(__ARM_ARCH) || (__ARM_ARCH >= 8)) && \
(!defined(LIBC_TARGET_ARCH_IS_X86) || \
defined(LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY))))
- return __builtin_roundf(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round(x);
+ } else {
+ return __builtin_roundf(x);
+ }
#else
return fputil::round(x);
#endif
diff --git a/libc/src/__support/math/roundf16.h b/libc/src/__support/math/roundf16.h
index 1eae339118fd0..de22d4dc0d24b 100644
--- a/libc/src/__support/math/roundf16.h
+++ b/libc/src/__support/math/roundf16.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT16
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/FPUtil/cast.h"
#include "src/__support/macros/config.h"
@@ -21,11 +22,15 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float16 roundf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 roundf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_ROUND) && \
defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
!defined(LIBC_USE_CONSTEXPR)
- return fputil::cast<float16>(__builtin_roundf(x));
+ if (cpp::is_constant_evaluated()) {
+ return fputil::round(x);
+ } else {
+ return fputil::cast<float16>(__builtin_roundf(x));
+ }
#else
return fputil::round(x);
#endif
diff --git a/libc/src/__support/math/rsqrtf.h b/libc/src/__support/math/rsqrtf.h
index 78606be68cce1..4176a499d2f53 100644
--- a/libc/src/__support/math/rsqrtf.h
+++ b/libc/src/__support/math/rsqrtf.h
@@ -20,7 +20,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float rsqrtf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float rsqrtf(float x) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/sin.h b/libc/src/__support/math/sin.h
index 4ae04ae64a423..4ab7f1757856c 100644
--- a/libc/src/__support/math/sin.h
+++ b/libc/src/__support/math/sin.h
@@ -36,7 +36,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_INLINE double
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
sin_accurate(double x, uint16_t x_e, unsigned k,
const range_reduction_double_internal::LargeRangeReduction
&range_reduction_large) {
@@ -75,7 +75,7 @@ sin_accurate(double x, uint16_t x_e, unsigned k,
}
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-LIBC_INLINE double sin(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sin(double x) {
using namespace math::range_reduction_double_internal;
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/sin_integer_eval.h b/libc/src/__support/math/sin_integer_eval.h
index 5aa1f27e9d111..d538e6c75d147 100644
--- a/libc/src/__support/math/sin_integer_eval.h
+++ b/libc/src/__support/math/sin_integer_eval.h
@@ -23,7 +23,7 @@ namespace math {
namespace integer_only {
-LIBC_INLINE double sin(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sin(double x) {
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/sincos.h b/libc/src/__support/math/sincos.h
index 4d8cf96dc4511..8cb2b5e08b728 100644
--- a/libc/src/__support/math/sincos.h
+++ b/libc/src/__support/math/sincos.h
@@ -34,7 +34,8 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE void sincos(double x, double *sin_x, double *cos_x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void sincos(double x, double *sin_x,
+ double *cos_x) {
using DoubleDouble = fputil::DoubleDouble;
using namespace math::range_reduction_double_internal;
using FPBits = typename fputil::FPBits<double>;
diff --git a/libc/src/__support/math/sincos_eval.h b/libc/src/__support/math/sincos_eval.h
index 4a3dd972861cf..1cdaf67cd87c5 100644
--- a/libc/src/__support/math/sincos_eval.h
+++ b/libc/src/__support/math/sincos_eval.h
@@ -25,8 +25,8 @@ namespace sincos_eval_internal {
using fputil::DoubleDouble;
using DFloat128 = fputil::DyadicFloat<128>;
-LIBC_INLINE double sincos_eval(const DoubleDouble &u, DoubleDouble &sin_u,
- DoubleDouble &cos_u) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+sincos_eval(const DoubleDouble &u, DoubleDouble &sin_u, DoubleDouble &cos_u) {
// Evaluate sin(y) = sin(x - k * (pi/128))
// We use the degree-7 Taylor approximation:
// sin(y) ~ y - y^3/3! + y^5/5! - y^7/7!
@@ -98,8 +98,8 @@ LIBC_INLINE double sincos_eval(const DoubleDouble &u, DoubleDouble &sin_u,
0x1.0p-51, 0x1.0p-105);
}
-LIBC_INLINE void sincos_eval(const DFloat128 &u, DFloat128 &sin_u,
- DFloat128 &cos_u) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
+sincos_eval(const DFloat128 &u, DFloat128 &sin_u, DFloat128 &cos_u) {
DFloat128 u_sq = fputil::quick_mul(u, u);
// sin(u) ~ x - x^3/3! + x^5/5! - x^7/7! + x^9/9! - x^11/11! + x^13/13!
diff --git a/libc/src/__support/math/sincos_integer_utils.h b/libc/src/__support/math/sincos_integer_utils.h
index 45bbba12b0ac4..9cd19d598be19 100644
--- a/libc/src/__support/math/sincos_integer_utils.h
+++ b/libc/src/__support/math/sincos_integer_utils.h
@@ -86,8 +86,8 @@ LIBC_INLINE_VAR constexpr Frac128 PI_OVER_2_M1({0x898c'c517'01b8'39a2,
// x_frac: |x - k * pi/2|
// Return:
// x_frac_is_neg.
-LIBC_INLINE bool trig_range_reduction(uint64_t x_u, unsigned x_e, unsigned &k,
- Frac128 &x_frac) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bool
+trig_range_reduction(uint64_t x_u, unsigned x_e, unsigned &k, Frac128 &x_frac) {
using FPBits = typename fputil::FPBits<double>;
bool x_frac_is_neg = false;
// We do multiplication x * (2/pi)
@@ -221,8 +221,8 @@ LIBC_INLINE_VAR constexpr Frac128 COS_COEFF[] = {
};
// Compute sin(x) with relative errors ~ 2^-54.
-LIBC_INLINE double sin_eval(const Frac128 &x_frac, unsigned k, bool is_neg,
- bool x_frac_is_neg) {
+LIBC_INLINE constexpr double sin_eval(const Frac128 &x_frac, unsigned k,
+ bool is_neg, bool x_frac_is_neg) {
// cos when k = 1, 3
bool is_cos = ((k & 1) == 1);
// flip sign when k = 2, 3
diff --git a/libc/src/__support/math/sincosf.h b/libc/src/__support/math/sincosf.h
index b5f700011bfd4..647b28a81b85f 100644
--- a/libc/src/__support/math/sincosf.h
+++ b/libc/src/__support/math/sincosf.h
@@ -58,7 +58,8 @@ LIBC_INLINE_VAR constexpr uint32_t EXCEPT_OUTPUTS_COS[N_EXCEPTS][4] = {
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
} // namespace sincosf_internal
-LIBC_INLINE void sincosf(float x, float *sinp, float *cosp) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void sincosf(float x, float *sinp,
+ float *cosp) {
using namespace sincosf_internal;
using namespace sincosf_utils_internal;
using FPBits = typename fputil::FPBits<float>;
diff --git a/libc/src/__support/math/sincosf16_utils.h b/libc/src/__support/math/sincosf16_utils.h
index cd91eaf711df2..d71670687f189 100644
--- a/libc/src/__support/math/sincosf16_utils.h
+++ b/libc/src/__support/math/sincosf16_utils.h
@@ -42,7 +42,8 @@ LIBC_INLINE_VAR constexpr float SIN_K_PI_OVER_32[64] = {
-0x1.6a09e6p-1, -0x1.44cf32p-1, -0x1.1c73b4p-1, -0x1.e2b5d4p-2,
-0x1.87de2ap-2, -0x1.294062p-2, -0x1.8f8b84p-3, -0x1.917a6cp-4};
-LIBC_INLINE int32_t range_reduction_sincospif16(float x, float &y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int32_t
+range_reduction_sincospif16(float x, float &y) {
float kf = fputil::nearest_integer(x * 32);
y = fputil::multiply_add(x, 32.0f, -kf);
@@ -58,7 +59,8 @@ LIBC_INLINE int32_t range_reduction_sincospif16(float x, float &y) {
// y = x * 32/pi - round(x * 32/pi)
// are beyond the least-significant bit of single-precision used during
// further intermediate computation.
-LIBC_INLINE int32_t range_reduction_sincosf16(float x, float &y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int32_t range_reduction_sincosf16(float x,
+ float &y) {
// Generated by Sollya with:
// > D(32/pi);
constexpr double THIRTYTWO_OVER_PI = 0x1.45f306dc9c883p3;
@@ -70,9 +72,9 @@ LIBC_INLINE int32_t range_reduction_sincosf16(float x, float &y) {
return static_cast<int32_t>(kd);
}
-LIBC_INLINE void sincosf16_poly_eval(int32_t k, float y, float &sin_k,
- float &cos_k, float &sin_y,
- float &cosm1_y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
+sincosf16_poly_eval(int32_t k, float y, float &sin_k, float &cos_k,
+ float &sin_y, float &cosm1_y) {
sin_k = SIN_K_PI_OVER_32[k & 63];
cos_k = SIN_K_PI_OVER_32[(k + 16) & 63];
@@ -95,17 +97,21 @@ LIBC_INLINE void sincosf16_poly_eval(int32_t k, float y, float &sin_k,
0x1.a6f7a2p-29f);
}
-LIBC_INLINE void sincosf16_eval(float xf, float &sin_k, float &cos_k,
- float &sin_y, float &cosm1_y) {
- float y;
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void sincosf16_eval(float xf, float &sin_k,
+ float &cos_k,
+ float &sin_y,
+ float &cosm1_y) {
+ float y{};
int32_t k = range_reduction_sincosf16(xf, y);
sincosf16_poly_eval(k, y, sin_k, cos_k, sin_y, cosm1_y);
}
-LIBC_INLINE void sincospif16_eval(float xf, float &sin_k, float &cos_k,
- float &sin_y, float &cosm1_y) {
- float y;
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void sincospif16_eval(float xf, float &sin_k,
+ float &cos_k,
+ float &sin_y,
+ float &cosm1_y) {
+ float y{};
int32_t k = range_reduction_sincospif16(xf, y);
sincosf16_poly_eval(k, y, sin_k, cos_k, sin_y, cosm1_y);
diff --git a/libc/src/__support/math/sincosf_float_eval.h b/libc/src/__support/math/sincosf_float_eval.h
index d834f3d7cf23f..a8660255e09f5 100644
--- a/libc/src/__support/math/sincosf_float_eval.h
+++ b/libc/src/__support/math/sincosf_float_eval.h
@@ -81,7 +81,8 @@ LIBC_INLINE_VAR constexpr float SIN_K_PI_OVER_8[16] = {
// p_k <= 7.
// We set the bound for p_k to be 6 so that we can have some more wiggle room
// for computations.
-LIBC_INLINE unsigned sincosf_range_reduction_small(float x, float &u) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT unsigned
+sincosf_range_reduction_small(float x, float &u) {
// > display=hexadecimal;
// > a = round(pi/8, 18, RN);
// > b = round(pi/8 - a, 18, RN);
@@ -99,7 +100,8 @@ LIBC_INLINE unsigned sincosf_range_reduction_small(float x, float &u) {
}
// TODO: Add non-FMA version of large range reduction.
-LIBC_INLINE unsigned sincosf_range_reduction_large(float x, float &u) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT unsigned
+sincosf_range_reduction_large(float x, float &u) {
using FPBits = typename fputil::FPBits<float>;
using fputil::FloatFloat;
FPBits xbits(x);
@@ -145,12 +147,12 @@ LIBC_INLINE unsigned sincosf_range_reduction_large(float x, float &u) {
return static_cast<unsigned>(static_cast<int>(k));
}
-template <bool IS_SIN> LIBC_INLINE float sincosf_eval(float x) {
+template <bool IS_SIN> LIBC_INLINE constexpr float sincosf_eval(float x) {
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
uint32_t x_abs = cpp::bit_cast<uint32_t>(x) & 0x7fff'ffffU;
- float y;
+ float y = 0.0f;
unsigned k = 0;
if (x_abs < 0x4880'0000U) {
k = sincosf_range_reduction_small(x, y);
diff --git a/libc/src/__support/math/sincosf_utils.h b/libc/src/__support/math/sincosf_utils.h
index 8fbc8384f9e32..a327576dd7f1b 100644
--- a/libc/src/__support/math/sincosf_utils.h
+++ b/libc/src/__support/math/sincosf_utils.h
@@ -46,7 +46,7 @@ using math::trigonometric_func_utils_internal::small_range_reduction;
// Table is generated with Sollya as follow:
// > display = hexadecimal;
// > for k from 0 to 63 do { D(sin(k * pi/32)); };
-LIBC_INLINE_VAR const double SIN_K_PI_OVER_32[64] = {
+LIBC_INLINE_VAR constexpr double SIN_K_PI_OVER_32[64] = {
0x0.0000000000000p+0, 0x1.917a6bc29b42cp-4, 0x1.8f8b83c69a60bp-3,
0x1.294062ed59f06p-2, 0x1.87de2a6aea963p-2, 0x1.e2b5d3806f63bp-2,
0x1.1c73b39ae68c8p-1, 0x1.44cf325091dd6p-1, 0x1.6a09e667f3bcdp-1,
@@ -71,9 +71,9 @@ LIBC_INLINE_VAR const double SIN_K_PI_OVER_32[64] = {
-0x1.917a6bc29b42cp-4,
};
-LIBC_INLINE void sincosf_poly_eval(int64_t k, double y, double &sin_k,
- double &cos_k, double &sin_y,
- double &cosm1_y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
+sincosf_poly_eval(int64_t k, double y, double &sin_k, double &cos_k,
+ double &sin_y, double &cosm1_y) {
// After range reduction, k = round(x * 32 / pi) and y = (x * 32 / pi) - k.
// So k is an integer and -0.5 <= y <= 0.5.
// Then sin(x) = sin((k + y)*pi/32)
@@ -99,10 +99,11 @@ LIBC_INLINE void sincosf_poly_eval(int64_t k, double y, double &sin_k,
0x1.03c1f070c2e27p-18, -0x1.55cc84bd942p-30);
}
-LIBC_INLINE void sincosf_eval(double xd, uint32_t x_abs, double &sin_k,
- double &cos_k, double &sin_y, double &cosm1_y) {
- int64_t k;
- double y;
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void
+sincosf_eval(double xd, uint32_t x_abs, double &sin_k, double &cos_k,
+ double &sin_y, double &cosm1_y) {
+ int64_t k{};
+ double y{};
if (LIBC_LIKELY(x_abs < FAST_PASS_BOUND)) {
k = small_range_reduction(xd, y);
@@ -117,16 +118,19 @@ LIBC_INLINE void sincosf_eval(double xd, uint32_t x_abs, double &sin_k,
// Return k and y, where
// k = round(x * 32) and y = (x * 32) - k.
// => pi * x = (k + y) * pi / 32
-LIBC_INLINE int64_t range_reduction_sincospi(double x, double &y) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT int64_t range_reduction_sincospi(double x,
+ double &y) {
double kd = fputil::nearest_integer(x * 32);
y = fputil::multiply_add(x, 32.0, -kd);
return static_cast<int64_t>(kd);
}
-LIBC_INLINE void sincospif_eval(double xd, double &sin_k, double &cos_k,
- double &sin_y, double &cosm1_y) {
- double y;
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT void sincospif_eval(double xd, double &sin_k,
+ double &cos_k,
+ double &sin_y,
+ double &cosm1_y) {
+ double y{};
int64_t k = range_reduction_sincospi(xd, y);
sincosf_poly_eval(k, y, sin_k, cos_k, sin_y, cosm1_y);
}
diff --git a/libc/src/__support/math/sinf16.h b/libc/src/__support/math/sinf16.h
index 65ac5639acbc7..12f340fb4bbcb 100644
--- a/libc/src/__support/math/sinf16.h
+++ b/libc/src/__support/math/sinf16.h
@@ -44,7 +44,7 @@ LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_EXCEPTS>
} // namespace sinf16_internal
-LIBC_INLINE float16 sinf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 sinf16(float16 x) {
using namespace sinf16_internal;
using namespace sincosf16_internal;
using FPBits = fputil::FPBits<float16>;
@@ -122,7 +122,7 @@ LIBC_INLINE float16 sinf16(float16 x) {
return x + FPBits::quiet_nan().get_val();
}
- float sin_k, cos_k, sin_y, cosm1_y;
+ float sin_k{}, cos_k{}, sin_y{}, cosm1_y{};
sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
diff --git a/libc/src/__support/math/sinf_double_eval.h b/libc/src/__support/math/sinf_double_eval.h
index 4d49810922195..fb097a4892118 100644
--- a/libc/src/__support/math/sinf_double_eval.h
+++ b/libc/src/__support/math/sinf_double_eval.h
@@ -37,7 +37,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace double_eval {
-LIBC_INLINE float sinf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sinf(float x) {
using namespace sincosf_utils_internal;
using FPBits = typename fputil::FPBits<float>;
FPBits xbits(x);
@@ -166,7 +166,7 @@ LIBC_INLINE float sinf(float x) {
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
// = sin_y * cos_k + (1 + cosm1_y) * sin_k
// = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
- double sin_k, cos_k, sin_y, cosm1_y;
+ double sin_k{}, cos_k{}, sin_y{}, cosm1_y{};
sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
diff --git a/libc/src/__support/math/sinf_float_eval.h b/libc/src/__support/math/sinf_float_eval.h
index cfb137044389c..fe28798a26611 100644
--- a/libc/src/__support/math/sinf_float_eval.h
+++ b/libc/src/__support/math/sinf_float_eval.h
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
namespace float_eval {
-LIBC_INLINE float sinf(float x) {
+LIBC_INLINE constexpr float sinf(float x) {
return sincosf_float_eval::sincosf_eval</*IS_SIN*/ true>(x);
}
diff --git a/libc/src/__support/math/sinh_double_eval.h b/libc/src/__support/math/sinh_double_eval.h
index 7d2c2c04fa8f3..8044f65dd4284 100644
--- a/libc/src/__support/math/sinh_double_eval.h
+++ b/libc/src/__support/math/sinh_double_eval.h
@@ -62,7 +62,7 @@ LIBC_INLINE_VAR constexpr double SINH_SMALL_P_COEFFS[6] = {
// Evaluate P_rem(x^2) ~ (sinh(x) - x) / x^3 - 0x1.554p-3 using Estrin's
// scheme:
-LIBC_ALWAYS_INLINE double sinh_small_poly(double x2) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double sinh_small_poly(double x2) {
double x4 = x2 * x2;
double x8 = x4 * x4;
double c0 =
@@ -79,7 +79,8 @@ LIBC_ALWAYS_INLINE double sinh_small_poly(double x2) {
// Evaluate sinh(x) for |x| < SMALL_BOUND (2^-26 + ulp(2^-26) when
// LIBC_MATH_HAS_SKIP_ACCURATE_PASS is defined, or 2^-2 otherwise):
-LIBC_INLINE double sinh_small_eval(double x, uint64_t x_abs) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sinh_small_eval(double x,
+ uint64_t x_abs) {
// Tiny and subnormal range: |x| <= 2^-26.
// For 0 < |x| <= 2^-26:
// 0 < (sinh(|x|) - |x|) / |x| ~ x^2 / 6 < 2^-54 < 2^-1 * ulp(x),
@@ -169,7 +170,8 @@ LIBC_INLINE double sinh_small_eval(double x, uint64_t x_abs) {
}
// Handle overflow, inf, and nan for sinh(x) when |x| >= 0x1.633ce8fb9f87ep+9:
-LIBC_NOINLINE LIBC_INLINE double sinh_exceptional(double x) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+sinh_exceptional(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
@@ -182,7 +184,7 @@ LIBC_NOINLINE LIBC_INLINE double sinh_exceptional(double x) {
return fputil::set_overflow<double>(xbits.sign());
}
-LIBC_ALWAYS_INLINE double sinh(double x) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double sinh(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/sinhbf16.h b/libc/src/__support/math/sinhbf16.h
index dc40b12650825..8de36abc005dc 100644
--- a/libc/src/__support/math/sinhbf16.h
+++ b/libc/src/__support/math/sinhbf16.h
@@ -31,7 +31,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 sinhbf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 sinhbf16(bfloat16 x) {
using FPBits = fputil::FPBits<bfloat16>;
FPBits x_bits(x);
uint16_t x_u = x_bits.uintval();
diff --git a/libc/src/__support/math/sinhcosh_accurate_128.h b/libc/src/__support/math/sinhcosh_accurate_128.h
index 58fdbf1250de6..4dd3a42942254 100644
--- a/libc/src/__support/math/sinhcosh_accurate_128.h
+++ b/libc/src/__support/math/sinhcosh_accurate_128.h
@@ -63,7 +63,8 @@ LIBC_INLINE_VAR constexpr Frac128 SINH_SMALL_POLY_128[10] = {
};
// 128-bit accurate path for sinh(x) when 2^-26 < |x| < 2^-2:
-LIBC_NOINLINE LIBC_INLINE double sinh_small_accurate_128(double x) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+sinh_small_accurate_128(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
int exp_x = xbits.get_exponent(); // in [-26, -3]
@@ -188,7 +189,8 @@ LIBC_INLINE_VAR constexpr Frac64 EXP2_COSH_W_LO64[3] = {
// in 128-bit precision using direct base-2 192-bit fixed-point range reduction
// for 2^-2 <= |x| < 0x1.633ce8fb9f87ep+9 (sinh) or
// 2^-27 < |x| < 0x1.633ce8fb9f87ep+9 (cosh).
-LIBC_NOINLINE LIBC_INLINE double exp_pm_accurate_128(double x, bool is_sinh) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+exp_pm_accurate_128(double x, bool is_sinh) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
int exp_x = xbits.get_exponent(); // in [-2, 9] for sinh, [-27, 9] for cosh
@@ -208,7 +210,7 @@ LIBC_NOINLINE LIBC_INLINE double exp_pm_accurate_128(double x, bool is_sinh) {
// w = v^2 in [0, 1) (lsb = 2^-128).
int k_p = 0;
bool u_is_neg = false;
- Frac128 v;
+ Frac128 v{};
if (LIBC_LIKELY(exp_x >= -6)) {
// Since p.val[3] has lsb = 2^(exp_x - 62), the 2^-5 bit of |z| is at bit
// shr = 57 - exp_x (in [48, 63]) of p.val[3]:
diff --git a/libc/src/__support/math/sinhcoshbf16_utils.h b/libc/src/__support/math/sinhcoshbf16_utils.h
index 8e9a687a0579b..36a8fc06a3f36 100644
--- a/libc/src/__support/math/sinhcoshbf16_utils.h
+++ b/libc/src/__support/math/sinhcoshbf16_utils.h
@@ -87,7 +87,7 @@ LIBC_INLINE_VAR constexpr cpp::array<uint32_t, 32> EXP2_MID_5_BITS = {
// The main point of these formulas is that the expensive part of calculating
// the polynomials approximating lower parts of e^x and e^(-x) is shared and
// only done once.
-template <bool IsSinh> LIBC_INLINE float eval_sinh_or_cosh(float xf) {
+template <bool IsSinh> LIBC_INLINE constexpr float eval_sinh_or_cosh(float xf) {
float kf = fputil::nearest_integer(xf * (LOG2F_E * 0x1.0p+5f));
int x_hi_mid_p = static_cast<int>(kf);
int x_hi_mid_m = -x_hi_mid_p;
@@ -141,7 +141,7 @@ template <bool IsSinh> LIBC_INLINE float eval_sinh_or_cosh(float xf) {
}
// Calculate e^x / 2
-LIBC_INLINE float exp_half(float xf) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float exp_half(float xf) {
float kf = fputil::nearest_integer(xf * (LOG2F_E * 0x1.0p+5f));
int x_hi_mid_p = static_cast<int>(kf);
diff --git a/libc/src/__support/math/sinhf.h b/libc/src/__support/math/sinhf.h
index 28dfd074966ca..c3a70806616e2 100644
--- a/libc/src/__support/math/sinhf.h
+++ b/libc/src/__support/math/sinhf.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float sinhf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sinhf(float x) {
using FPBits = typename fputil::FPBits<float>;
FPBits xbits(x);
uint32_t x_abs = xbits.abs().uintval();
diff --git a/libc/src/__support/math/sinhfcoshf_utils.h b/libc/src/__support/math/sinhfcoshf_utils.h
index 5f19b81246980..3d9ec127f9986 100644
--- a/libc/src/__support/math/sinhfcoshf_utils.h
+++ b/libc/src/__support/math/sinhfcoshf_utils.h
@@ -55,14 +55,15 @@ namespace sinhfcoshf_internal {
// The main point of these formulas is that the expensive part of calculating
// the polynomials approximating lower parts of e^(x) and e^(-x) are shared
// and only done once.
-template <bool is_sinh> LIBC_INLINE double exp_pm_eval(float x) {
+template <bool is_sinh>
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double exp_pm_eval(float x) {
double xd = static_cast<double>(x);
// kd = round(x * log2(e) * 2^5)
// k_p = round(x * log2(e) * 2^5)
// k_m = round(-x * log2(e) * 2^5)
- double kd;
- int k_p, k_m;
+ double kd{};
+ int k_p{}, k_m{};
#ifdef LIBC_TARGET_CPU_HAS_NEAREST_INT
kd = fputil::nearest_integer(ExpBase::LOG2_B * xd);
@@ -109,7 +110,7 @@ template <bool is_sinh> LIBC_INLINE double exp_pm_eval(float x) {
double p_odd = fputil::polyeval(dx2, 0.5, ExpBase::COEFFS[1] * 0.5,
ExpBase::COEFFS[3] * 0.5);
- double r;
+ double r{};
if constexpr (is_sinh)
r = fputil::multiply_add(dx * mh_sum, p_odd, p_even * mh_diff);
else
diff --git a/libc/src/__support/math/sinpif.h b/libc/src/__support/math/sinpif.h
index 8358b6012937a..af8bcf69fb2cc 100644
--- a/libc/src/__support/math/sinpif.h
+++ b/libc/src/__support/math/sinpif.h
@@ -21,7 +21,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float sinpif(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sinpif(float x) {
using namespace sincosf_utils_internal;
using FPBits = typename fputil::FPBits<float>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/sinpif16.h b/libc/src/__support/math/sinpif16.h
index 2d707837c776d..f3a6e7fafa46e 100644
--- a/libc/src/__support/math/sinpif16.h
+++ b/libc/src/__support/math/sinpif16.h
@@ -26,7 +26,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 sinpif16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 sinpif16(float16 x) {
using namespace sincosf16_internal;
using FPBits = typename fputil::FPBits<float16>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/sqrt.h b/libc/src/__support/math/sqrt.h
index baef03fca9587..3630301008c6f 100644
--- a/libc/src/__support/math/sqrt.h
+++ b/libc/src/__support/math/sqrt.h
@@ -15,7 +15,9 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double sqrt(double x) { return fputil::sqrt<double>(x); }
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double sqrt(double x) {
+ return fputil::sqrt<double>(x);
+}
} // namespace math
diff --git a/libc/src/__support/math/sqrtbf16.h b/libc/src/__support/math/sqrtbf16.h
index 365618fde892b..2b4c58c32e325 100644
--- a/libc/src/__support/math/sqrtbf16.h
+++ b/libc/src/__support/math/sqrtbf16.h
@@ -17,7 +17,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 sqrtbf16(bfloat16 x) { return fputil::sqrt<bfloat16>(x); }
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 sqrtbf16(bfloat16 x) {
+ return fputil::sqrt<bfloat16>(x);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/sqrtf.h b/libc/src/__support/math/sqrtf.h
index aaa25d91ee820..4f14326d61d94 100644
--- a/libc/src/__support/math/sqrtf.h
+++ b/libc/src/__support/math/sqrtf.h
@@ -15,7 +15,9 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float sqrtf(float x) { return fputil::sqrt<float>(x); }
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float sqrtf(float x) {
+ return fputil::sqrt<float>(x);
+}
} // namespace math
diff --git a/libc/src/__support/math/sqrtf128.h b/libc/src/__support/math/sqrtf128.h
index a1fe100ff4b22..8ecba72ba0741 100644
--- a/libc/src/__support/math/sqrtf128.h
+++ b/libc/src/__support/math/sqrtf128.h
@@ -279,7 +279,7 @@ LIBC_INLINE constexpr uint64_t rsqrt_approx(uint64_t m) {
} // namespace sqrtf128_internal
-LIBC_INLINE Float128 sqrtf128(Float128 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT Float128 sqrtf128(Float128 x) {
using namespace sqrtf128_internal;
using FPBits = fputil::FPBits<Float128>;
// Get rounding mode.
diff --git a/libc/src/__support/math/sqrtl.h b/libc/src/__support/math/sqrtl.h
index 4d43067ccf904..2345aca892a71 100644
--- a/libc/src/__support/math/sqrtl.h
+++ b/libc/src/__support/math/sqrtl.h
@@ -17,7 +17,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR long double sqrtl(long double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT long double sqrtl(long double x) {
return fputil::sqrt<long double>(x);
}
diff --git a/libc/src/__support/math/tan.h b/libc/src/__support/math/tan.h
index 87f46acbf7017..ec22a600b9ca4 100644
--- a/libc/src/__support/math/tan.h
+++ b/libc/src/__support/math/tan.h
@@ -38,7 +38,8 @@ namespace tan_internal {
using DoubleDouble = fputil::DoubleDouble;
using DFloat128 = typename fputil::DyadicFloat<128>;
-LIBC_INLINE double tan_eval(const DoubleDouble &u, DoubleDouble &result) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double tan_eval(const DoubleDouble &u,
+ DoubleDouble &result) {
// Evaluate tan(y) = tan(x - k * (pi/128))
// We use the degree-9 Taylor approximation:
// tan(y) ~ P(y) = y + y^3/3 + 2*y^5/15 + 17*y^7/315 + 62*y^9/2835
@@ -76,7 +77,8 @@ LIBC_INLINE double tan_eval(const DoubleDouble &u, DoubleDouble &result) {
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
// Accurate evaluation of tan for small u.
-[[maybe_unused]] LIBC_INLINE DFloat128 tan_eval(const DFloat128 &u) {
+[[maybe_unused]] LIBC_INLINE LIBC_CONSTEXPR_DEFAULT DFloat128
+tan_eval(const DFloat128 &u) {
DFloat128 u_sq = fputil::quick_mul(u, u);
// tan(x) ~ x + x^3/3 + x^5 * 2/15 + x^7 * 17/315 + x^9 * 62/2835 +
@@ -108,9 +110,8 @@ LIBC_INLINE double tan_eval(const DoubleDouble &u, DoubleDouble &result) {
// Calculation a / b = a * (1/b) for DFloat128.
// Using the initial approximation of q ~ (1/b), then apply 2 Newton-Raphson
// iterations, before multiplying by a.
-[[maybe_unused]] LIBC_INLINE DFloat128 newton_raphson_div(const DFloat128 &a,
- DFloat128 b,
- double q) {
+[[maybe_unused]] LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR DFloat128
+newton_raphson_div(const DFloat128 &a, DFloat128 b, double q) {
DFloat128 q0(q);
LIBC_BIT_CAST_CONSTEXPR_VAR DFloat128 TWO(2.0);
b.sign = (b.sign == Sign::POS) ? Sign::NEG : Sign::POS;
@@ -124,7 +125,7 @@ LIBC_INLINE double tan_eval(const DoubleDouble &u, DoubleDouble &result) {
} // namespace tan_internal
-LIBC_INLINE double tan(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double tan(double x) {
using namespace tan_internal;
using namespace math::range_reduction_double_internal;
using FPBits = typename fputil::FPBits<double>;
@@ -132,8 +133,8 @@ LIBC_INLINE double tan(double x) {
uint16_t x_e = xbits.get_biased_exponent();
- DoubleDouble y;
- unsigned k;
+ DoubleDouble y{};
+ unsigned k{};
LargeRangeReduction range_reduction_large{};
// |x| < 2^16
diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h
index 3dae149a4a643..c0c1c89991748 100644
--- a/libc/src/__support/math/tanbf16.h
+++ b/libc/src/__support/math/tanbf16.h
@@ -23,7 +23,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT bfloat16 tanbf16(bfloat16 x) {
using FPBits = fputil::FPBits<bfloat16>;
FPBits xbits(x);
@@ -60,7 +60,7 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) {
double xd = static_cast<double>(xf);
uint32_t x_abs_f = fputil::FPBits<float>(xf).uintval() & 0x7fffffff;
- double sin_k, cos_k, sin_y, cosm1_y;
+ double sin_k{}, cos_k{}, sin_y{}, cosm1_y{};
// TODO: Use bfloat16 version for inv_trigf_utils_internals after they are
// available
diff --git a/libc/src/__support/math/tanf16.h b/libc/src/__support/math/tanf16.h
index 761990f73a90b..814957cab314b 100644
--- a/libc/src/__support/math/tanf16.h
+++ b/libc/src/__support/math/tanf16.h
@@ -27,7 +27,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 tanf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 tanf16(float16 x) {
using namespace sincosf16_internal;
using FPBits = fputil::FPBits<float16>;
FPBits xbits(x);
@@ -120,7 +120,7 @@ LIBC_INLINE float16 tanf16(float16 x) {
// Once k and y are computed, we then deduce the answer by the formula:
// tan(x) = sin(x) / cos(x)
// = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
- float sin_k, cos_k, sin_y, cosm1_y;
+ float sin_k{}, cos_k{}, sin_y{}, cosm1_y{};
sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
// Note that, cosm1_y = cos_y - 1:
diff --git a/libc/src/__support/math/tanf_double_eval.h b/libc/src/__support/math/tanf_double_eval.h
index e5ef67ed0d157..da1968bde9e57 100644
--- a/libc/src/__support/math/tanf_double_eval.h
+++ b/libc/src/__support/math/tanf_double_eval.h
@@ -55,7 +55,7 @@ LIBC_INLINE_VAR constexpr fputil::ExceptValues<float, N_EXCEPTS> TANF_EXCEPTS{{
} // namespace tanf_internal
-LIBC_INLINE float tanf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float tanf(float x) {
using namespace sincosf_utils_internal;
using namespace tanf_internal;
using FPBits = typename fputil::FPBits<float>;
@@ -153,7 +153,7 @@ LIBC_INLINE float tanf(float x) {
// and sincosf.
double xd = static_cast<double>(x);
- double sin_k, cos_k, sin_y, cosm1_y;
+ double sin_k{}, cos_k{}, sin_y{}, cosm1_y{};
sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
// tan(x) = sin(x) / cos(x)
diff --git a/libc/src/__support/math/tanf_float_eval.h b/libc/src/__support/math/tanf_float_eval.h
index 27f49c8e35bb6..a236af3ae1972 100644
--- a/libc/src/__support/math/tanf_float_eval.h
+++ b/libc/src/__support/math/tanf_float_eval.h
@@ -28,7 +28,7 @@ namespace float_eval {
// Float-only tanf, requiring FMA. The maximum error, checked exhaustively in
// round-to-nearest mode against MPFR, is 3.37 ULP.
-LIBC_INLINE float tanf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float tanf(float x) {
using namespace sincosf_float_eval;
using FPBits = fputil::FPBits<float>;
FPBits xbits(x);
@@ -46,8 +46,8 @@ LIBC_INLINE float tanf(float x) {
return fputil::multiply_add(x, 0x1.0p-25f, x);
}
- float y;
- unsigned k;
+ float y{};
+ unsigned k{};
if (x_abs < 0x4880'0000U) {
k = sincosf_range_reduction_small(x, y);
} else {
diff --git a/libc/src/__support/math/tanh_accurate_128.h b/libc/src/__support/math/tanh_accurate_128.h
index de493c133cc22..194992e2b9d3e 100644
--- a/libc/src/__support/math/tanh_accurate_128.h
+++ b/libc/src/__support/math/tanh_accurate_128.h
@@ -75,7 +75,8 @@ LIBC_INLINE_VAR constexpr Frac64 TANH_SMALL_W_LO64[4] = {
};
// 128-bit accurate path for tanh(x) when 2^-27 < |x| < 2^-3:
-LIBC_NOINLINE LIBC_INLINE double tanh_small_accurate_128(double x) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+tanh_small_accurate_128(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
int exp_x = xbits.get_exponent(); // in [-27, -4]
@@ -142,7 +143,8 @@ LIBC_NOINLINE LIBC_INLINE double tanh_small_accurate_128(double x) {
// Dividing numerator and denominator by 2^hi_p gives:
// tanh(|x|) = (m_final - 2^-hi_p) / (m_final + 2^-hi_p)
// where 2^-hi_p is a single bit in Frac128.
-LIBC_NOINLINE LIBC_INLINE double tanh_accurate_128(double x) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+tanh_accurate_128(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
int exp_x = xbits.get_exponent(); // in [-3, 4]
diff --git a/libc/src/__support/math/tanh_double_eval.h b/libc/src/__support/math/tanh_double_eval.h
index 3c3b015030e56..d9445bf629e7f 100644
--- a/libc/src/__support/math/tanh_double_eval.h
+++ b/libc/src/__support/math/tanh_double_eval.h
@@ -232,7 +232,7 @@ LIBC_INLINE_VAR constexpr double TANH_SMALL_P_COEFFS[6] = {
// Evaluate P_rem(x^2) ~ (tanh(x) - x) / x^3 - (-0x1.554p-2) using Estrin's
// scheme:
-LIBC_ALWAYS_INLINE double tanh_small_poly(double x2) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double tanh_small_poly(double x2) {
double x4 = x2 * x2;
double x8 = x4 * x4;
double c0 =
@@ -409,7 +409,8 @@ LIBC_MATH_SMALL_TABLES_INLINE double tanh_eval(double x) {
// Evaluate tanh(x) for |x| < SMALL_BOUND (2^-27 + ulp(2^-27) when
// LIBC_MATH_HAS_SKIP_ACCURATE_PASS is defined, or 2^-3 otherwise):
-LIBC_INLINE double tanh_small_eval(double x, uint64_t x_abs) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double tanh_small_eval(double x,
+ uint64_t x_abs) {
using FPBits = fputil::FPBits<double>;
// Tiny and subnormal range: |x| <= 2^-27.
@@ -506,7 +507,8 @@ LIBC_INLINE double tanh_small_eval(double x, uint64_t x_abs) {
// Handle saturation, inf, and nan for tanh(x) when
// |x| >= 0x1.30fc1931f09cap+4:
-LIBC_NOINLINE LIBC_INLINE double tanh_exceptional(double x) {
+LIBC_NOINLINE LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double
+tanh_exceptional(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
@@ -520,7 +522,7 @@ LIBC_NOINLINE LIBC_INLINE double tanh_exceptional(double x) {
: fputil::round_result_slightly_down(1.0);
}
-LIBC_ALWAYS_INLINE double tanh(double x) {
+LIBC_ALWAYS_INLINE LIBC_CONSTEXPR_DEFAULT double tanh(double x) {
using FPBits = fputil::FPBits<double>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/tanhf.h b/libc/src/__support/math/tanhf.h
index 28d1c60a6912d..0f96459285063 100644
--- a/libc/src/__support/math/tanhf.h
+++ b/libc/src/__support/math/tanhf.h
@@ -23,7 +23,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float tanhf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float tanhf(float x) {
// 2^6 * log2(e)
constexpr double LOG2_E_EXP2_6 = ExpBase::LOG2_B * 2.0;
diff --git a/libc/src/__support/math/tanhf16.h b/libc/src/__support/math/tanhf16.h
index 768ec6845fe94..989fde67d43fa 100644
--- a/libc/src/__support/math/tanhf16.h
+++ b/libc/src/__support/math/tanhf16.h
@@ -32,7 +32,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 tanhf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 tanhf16(float16 x) {
using namespace math::expxf16_internal;
using FPBits = fputil::FPBits<float16>;
FPBits x_bits(x);
diff --git a/libc/src/__support/math/tanpif.h b/libc/src/__support/math/tanpif.h
index 722189e7c57c3..db79b78f5e3b9 100644
--- a/libc/src/__support/math/tanpif.h
+++ b/libc/src/__support/math/tanpif.h
@@ -24,7 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float tanpif(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float tanpif(float x) {
using namespace sincosf_utils_internal;
using FPBits = typename fputil::FPBits<float>;
@@ -105,7 +105,7 @@ LIBC_INLINE float tanpif(float x) {
// Once k and y are computed, we then deduce the answer by the formula:
// tan(x) = sin(x) / cos(x)
// = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
- double sin_k, cos_k, sin_y, cosm1_y;
+ double sin_k{}, cos_k{}, sin_y{}, cosm1_y{};
sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) {
diff --git a/libc/src/__support/math/tanpif16.h b/libc/src/__support/math/tanpif16.h
index db317ee9d3fbd..88922775ff458 100644
--- a/libc/src/__support/math/tanpif16.h
+++ b/libc/src/__support/math/tanpif16.h
@@ -27,7 +27,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 tanpif16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 tanpif16(float16 x) {
using namespace sincosf16_internal;
using FPBits = typename fputil::FPBits<float16>;
FPBits xbits(x);
diff --git a/libc/src/__support/math/trunc.h b/libc/src/__support/math/trunc.h
index 803481753e379..90b0514c89f6c 100644
--- a/libc/src/__support/math/trunc.h
+++ b/libc/src/__support/math/trunc.h
@@ -9,16 +9,21 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TRUNC_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_TRUNC_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR double trunc(double x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double trunc(double x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_USE_CONSTEXPR)
- return __builtin_trunc(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::trunc(x);
+ } else {
+ return __builtin_trunc(x);
+ }
#else
return fputil::trunc(x);
#endif
diff --git a/libc/src/__support/math/truncf.h b/libc/src/__support/math/truncf.h
index 6dac9d3200172..f5ca0871351b9 100644
--- a/libc/src/__support/math/truncf.h
+++ b/libc/src/__support/math/truncf.h
@@ -9,16 +9,21 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TRUNCF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_TRUNCF_H
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float truncf(float x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float truncf(float x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
!defined(LIBC_USE_CONSTEXPR)
- return __builtin_truncf(x);
+ if (cpp::is_constant_evaluated()) {
+ return fputil::trunc(x);
+ } else {
+ return __builtin_truncf(x);
+ }
#else
return fputil::trunc(x);
#endif
diff --git a/libc/src/__support/math/truncf16.h b/libc/src/__support/math/truncf16.h
index d32ae22d87b5b..e7c97920d69f9 100644
--- a/libc/src/__support/math/truncf16.h
+++ b/libc/src/__support/math/truncf16.h
@@ -13,6 +13,7 @@
#ifdef LIBC_TYPES_HAS_FLOAT16
+#include "src/__support/CPP/type_traits.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/FPUtil/cast.h"
#include "src/__support/macros/config.h"
@@ -21,11 +22,15 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE LIBC_CONSTEXPR float16 truncf16(float16 x) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 truncf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
!defined(LIBC_USE_CONSTEXPR)
- return fputil::cast<float16>(__builtin_truncf(x));
+ if (cpp::is_constant_evaluated()) {
+ return fputil::trunc(x);
+ } else {
+ return fputil::cast<float16>(__builtin_truncf(x));
+ }
#else
return fputil::trunc(x);
#endif
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 7d368e7752361..dc3f7bbedaf26 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -576,16 +576,66 @@ else()
DEPENDS
libc.src.__support.FPUtil.float128
libc.src.__support.FPUtil.float80
+ libc.src.__support.math.acos
+ libc.src.__support.math.acosbf16
+ libc.src.__support.math.acosf
+ libc.src.__support.math.acosf16
libc.src.__support.math.acoshbf16
+ libc.src.__support.math.acoshf
+ libc.src.__support.math.acoshf16
+ libc.src.__support.math.acospif
+ libc.src.__support.math.acospif16
+ libc.src.__support.math.asin
libc.src.__support.math.asinbf16
+ libc.src.__support.math.asinf
+ libc.src.__support.math.asinf16
+ libc.src.__support.math.asinhf
+ libc.src.__support.math.asinhf16
+ libc.src.__support.math.asinpi
+ libc.src.__support.math.asinpif
+ libc.src.__support.math.asinpif16
+ libc.src.__support.math.atan
+ libc.src.__support.math.atan2
+ libc.src.__support.math.atan2f
+ libc.src.__support.math.atan2f128
+ libc.src.__support.math.atan2f16
+ libc.src.__support.math.atanbf16
+ libc.src.__support.math.atanf
+ libc.src.__support.math.atanf16
+ libc.src.__support.math.atanhf
+ libc.src.__support.math.atanhf16
+ libc.src.__support.math.atanpif16
+ libc.src.__support.math.bf16add
+ libc.src.__support.math.bf16addf
+ libc.src.__support.math.bf16addf128
+ libc.src.__support.math.bf16addl
+ libc.src.__support.math.bf16div
+ libc.src.__support.math.bf16divf
+ libc.src.__support.math.bf16divf128
+ libc.src.__support.math.bf16divl
+ libc.src.__support.math.bf16fma
+ libc.src.__support.math.bf16fmaf
+ libc.src.__support.math.bf16fmaf128
+ libc.src.__support.math.bf16fmal
+ libc.src.__support.math.bf16mul
+ libc.src.__support.math.bf16mulf
+ libc.src.__support.math.bf16mulf128
+ libc.src.__support.math.bf16mull
+ libc.src.__support.math.bf16sub
+ libc.src.__support.math.bf16subf
+ libc.src.__support.math.bf16subf128
libc.src.__support.math.bf16subl
libc.src.__support.math.canonicalize
libc.src.__support.math.canonicalizebf16
libc.src.__support.math.canonicalizef
libc.src.__support.math.canonicalizef128
- libc.src.__support.math.canonicalizef80
libc.src.__support.math.canonicalizef16
+ libc.src.__support.math.canonicalizef80
libc.src.__support.math.canonicalizel
+ libc.src.__support.math.cbrt
+ libc.src.__support.math.cbrtbf16
+ libc.src.__support.math.cbrtf
+ libc.src.__support.math.cbrtf16
libc.src.__support.math.ceil
libc.src.__support.math.ceilbf16
libc.src.__support.math.ceilf
@@ -596,19 +646,81 @@ else()
libc.src.__support.math.copysignbf16
libc.src.__support.math.copysignf
libc.src.__support.math.copysignf128
- libc.src.__support.math.copysignf80
libc.src.__support.math.copysignf16
+ libc.src.__support.math.copysignf80
libc.src.__support.math.copysignl
+ libc.src.__support.math.cos
+ libc.src.__support.math.cosf
+ libc.src.__support.math.cosf16
+ libc.src.__support.math.cosh
+ libc.src.__support.math.coshf
+ libc.src.__support.math.coshf16
+ libc.src.__support.math.cospif
+ libc.src.__support.math.cospif16
libc.src.__support.math.daddf128
+ libc.src.__support.math.daddl
libc.src.__support.math.ddivf128
libc.src.__support.math.ddivl
+ libc.src.__support.math.dfmaf128
+ libc.src.__support.math.dfmal
libc.src.__support.math.dmulf128
libc.src.__support.math.dmull
libc.src.__support.math.dsqrtf128
+ libc.src.__support.math.dsqrtl
+ libc.src.__support.math.dsubf128
+ libc.src.__support.math.dsubl
+ libc.src.__support.math.erfcf16
+ libc.src.__support.math.erff
+ libc.src.__support.math.erff16
+ libc.src.__support.math.exp
+ libc.src.__support.math.exp10
+ libc.src.__support.math.exp10f
+ libc.src.__support.math.exp10f16
+ libc.src.__support.math.exp10m1f
+ libc.src.__support.math.exp10m1f16
+ libc.src.__support.math.exp2
+ libc.src.__support.math.exp2bf16
+ libc.src.__support.math.exp2f
+ libc.src.__support.math.exp2f16
+ libc.src.__support.math.exp2m1f
+ libc.src.__support.math.exp2m1f16
+ libc.src.__support.math.expbf16
+ libc.src.__support.math.expf
+ libc.src.__support.math.expf16
+ libc.src.__support.math.expm1
+ libc.src.__support.math.expm1f
+ libc.src.__support.math.expm1f16
libc.src.__support.math.f16add
libc.src.__support.math.f16addf
libc.src.__support.math.f16addf128
libc.src.__support.math.f16addl
+ libc.src.__support.math.f16div
+ libc.src.__support.math.f16divf
+ libc.src.__support.math.f16divf128
+ libc.src.__support.math.f16divl
+ libc.src.__support.math.f16fma
+ libc.src.__support.math.f16fmaf
+ libc.src.__support.math.f16fmaf128
+ libc.src.__support.math.f16fmal
+ libc.src.__support.math.f16mul
+ libc.src.__support.math.f16mulf
+ libc.src.__support.math.f16mulf128
+ libc.src.__support.math.f16mull
+ libc.src.__support.math.f16sqrt
+ libc.src.__support.math.f16sqrtf
+ libc.src.__support.math.f16sqrtf128
+ libc.src.__support.math.f16sqrtl
+ libc.src.__support.math.f16sub
+ libc.src.__support.math.f16subf
+ libc.src.__support.math.f16subf128
+ libc.src.__support.math.f16subl
+ libc.src.__support.math.fabs
+ libc.src.__support.math.fabsbf16
+ libc.src.__support.math.fabsf
+ libc.src.__support.math.fabsf128
+ libc.src.__support.math.fabsf16
+ libc.src.__support.math.fabsf80
+ libc.src.__support.math.fabsl
libc.src.__support.math.fadd
libc.src.__support.math.faddf128
libc.src.__support.math.faddl
@@ -623,21 +735,24 @@ else()
libc.src.__support.math.fdivf128
libc.src.__support.math.fdivl
libc.src.__support.math.ffma
- libc.src.__support.math.ffmal
libc.src.__support.math.ffmaf128
+ libc.src.__support.math.ffmal
libc.src.__support.math.floor
libc.src.__support.math.floorbf16
libc.src.__support.math.floorf
libc.src.__support.math.floorf128
libc.src.__support.math.floorf16
libc.src.__support.math.floorl
- libc.src.__support.math.fabs
- libc.src.__support.math.fabsbf16
- libc.src.__support.math.fabsf
- libc.src.__support.math.fabsf128
- libc.src.__support.math.fabsf80
- libc.src.__support.math.fabsf16
- libc.src.__support.math.fabsl
+ libc.src.__support.math.fma
+ libc.src.__support.math.fmabf16
+ libc.src.__support.math.fmaf
+ libc.src.__support.math.fmaf16
+ libc.src.__support.math.fmax
+ libc.src.__support.math.fmaxbf16
+ libc.src.__support.math.fmaxf
+ libc.src.__support.math.fmaxf128
+ libc.src.__support.math.fmaxf16
+ libc.src.__support.math.fmaxf80
libc.src.__support.math.fmaximum
libc.src.__support.math.fmaximum_mag
libc.src.__support.math.fmaximum_mag_num
@@ -666,6 +781,7 @@ else()
libc.src.__support.math.fmaximumf16
libc.src.__support.math.fmaximumf80
libc.src.__support.math.fmaximuml
+ libc.src.__support.math.fmaxl
libc.src.__support.math.fmin
libc.src.__support.math.fminbf16
libc.src.__support.math.fminf
@@ -712,6 +828,9 @@ else()
libc.src.__support.math.fmull
libc.src.__support.math.frexp
libc.src.__support.math.frexpbf16
+ libc.src.__support.math.frexpf
+ libc.src.__support.math.frexpf128
+ libc.src.__support.math.frexpf16
libc.src.__support.math.frexpl
libc.src.__support.math.fromfp
libc.src.__support.math.fromfpbf16
@@ -725,6 +844,9 @@ else()
libc.src.__support.math.fromfpxf128
libc.src.__support.math.fromfpxf16
libc.src.__support.math.fromfpxl
+ libc.src.__support.math.fsqrt
+ libc.src.__support.math.fsqrtf128
+ libc.src.__support.math.fsqrtl
libc.src.__support.math.fsub
libc.src.__support.math.fsubf128
libc.src.__support.math.fsubl
@@ -734,30 +856,50 @@ else()
libc.src.__support.math.getpayloadf128
libc.src.__support.math.getpayloadf16
libc.src.__support.math.getpayloadl
+ libc.src.__support.math.hypot
+ libc.src.__support.math.hypotbf16
+ libc.src.__support.math.hypotf
+ libc.src.__support.math.hypotf16
+ libc.src.__support.math.ilogb
+ libc.src.__support.math.ilogbbf16
+ libc.src.__support.math.ilogbf
+ libc.src.__support.math.ilogbf128
+ libc.src.__support.math.ilogbf16
+ libc.src.__support.math.ilogbl
libc.src.__support.math.iscanonical
libc.src.__support.math.iscanonicalbf16
libc.src.__support.math.iscanonicalf
libc.src.__support.math.iscanonicalf128
- libc.src.__support.math.iscanonicalf80
libc.src.__support.math.iscanonicalf16
+ libc.src.__support.math.iscanonicalf80
libc.src.__support.math.iscanonicall
libc.src.__support.math.isnan
libc.src.__support.math.isnanf
libc.src.__support.math.isnanf128
- libc.src.__support.math.isnanf80
libc.src.__support.math.isnanf16
+ libc.src.__support.math.isnanf80
libc.src.__support.math.isnanl
libc.src.__support.math.issignaling
libc.src.__support.math.issignalingbf16
libc.src.__support.math.issignalingf
libc.src.__support.math.issignalingf128
- libc.src.__support.math.issignalingf80
libc.src.__support.math.issignalingf16
+ libc.src.__support.math.issignalingf80
libc.src.__support.math.issignalingl
libc.src.__support.math.ldexp
libc.src.__support.math.ldexpbf16
+ libc.src.__support.math.ldexpf
+ libc.src.__support.math.ldexpf128
+ libc.src.__support.math.ldexpf16
libc.src.__support.math.ldexpl
+ libc.src.__support.math.lgammaf
+ libc.src.__support.math.lgammaf16
+ libc.src.__support.math.llogb
libc.src.__support.math.llogbbf16
+ libc.src.__support.math.llogbf
+ libc.src.__support.math.llogbf128
+ libc.src.__support.math.llogbf16
+ libc.src.__support.math.llogbl
libc.src.__support.math.llrint
libc.src.__support.math.llrintbf16
libc.src.__support.math.llrintf
@@ -771,8 +913,25 @@ else()
libc.src.__support.math.llroundf16
libc.src.__support.math.llroundl
libc.src.__support.math.log
+ libc.src.__support.math.log10
+ libc.src.__support.math.log10f
+ libc.src.__support.math.log10f16
+ libc.src.__support.math.log10p1f16
+ libc.src.__support.math.log1p
+ libc.src.__support.math.log1pf
+ libc.src.__support.math.log2
+ libc.src.__support.math.log2f
+ libc.src.__support.math.log2f16
+ libc.src.__support.math.log2p1f16
+ libc.src.__support.math.log_bf16
+ libc.src.__support.math.logb
libc.src.__support.math.logbbf16
- libc.src.__support.math.ilogbbf16
+ libc.src.__support.math.logbf
+ libc.src.__support.math.logbf128
+ libc.src.__support.math.logbf16
+ libc.src.__support.math.logbl
+ libc.src.__support.math.logf
+ libc.src.__support.math.logf16
libc.src.__support.math.lrint
libc.src.__support.math.lrintbf16
libc.src.__support.math.lrintf
@@ -806,9 +965,28 @@ else()
libc.src.__support.math.nextafter
libc.src.__support.math.nextafterbf16
libc.src.__support.math.nextafterf
+ libc.src.__support.math.nextafterf128
libc.src.__support.math.nextafterf16
libc.src.__support.math.nextafterl
- libc.src.__support.math.nextafterf128
+ libc.src.__support.math.nextdown
+ libc.src.__support.math.nextdownbf16
+ libc.src.__support.math.nextdownf
+ libc.src.__support.math.nextdownf128
+ libc.src.__support.math.nextdownf16
+ libc.src.__support.math.nextdownl
+ libc.src.__support.math.nexttoward
+ libc.src.__support.math.nexttowardbf16
+ libc.src.__support.math.nexttowardf
+ libc.src.__support.math.nexttowardf16
+ libc.src.__support.math.nexttowardl
+ libc.src.__support.math.nextup
+ libc.src.__support.math.nextupbf16
+ libc.src.__support.math.nextupf
+ libc.src.__support.math.nextupf128
+ libc.src.__support.math.nextupf16
+ libc.src.__support.math.nextupl
+ libc.src.__support.math.pow
+ libc.src.__support.math.powf
libc.src.__support.math.remainder
libc.src.__support.math.remainderbf16
libc.src.__support.math.remainderf
@@ -839,6 +1017,8 @@ else()
libc.src.__support.math.roundf128
libc.src.__support.math.roundf16
libc.src.__support.math.roundl
+ libc.src.__support.math.rsqrtf
+ libc.src.__support.math.rsqrtf16
libc.src.__support.math.scalbln
libc.src.__support.math.scalblnbf16
libc.src.__support.math.scalblnf
@@ -863,7 +1043,46 @@ else()
libc.src.__support.math.setpayloadsigf128
libc.src.__support.math.setpayloadsigf16
libc.src.__support.math.setpayloadsigl
+ libc.src.__support.math.sin
+ libc.src.__support.math.sincos
+ libc.src.__support.math.sincosf
+ libc.src.__support.math.sinf
+ libc.src.__support.math.sinf16
+ libc.src.__support.math.sinh
+ libc.src.__support.math.sinhbf16
+ libc.src.__support.math.sinhf
+ libc.src.__support.math.sinhf16
+ libc.src.__support.math.sinpif
+ libc.src.__support.math.sinpif16
+ libc.src.__support.math.sqrt
+ libc.src.__support.math.sqrtbf16
+ libc.src.__support.math.sqrtf
+ libc.src.__support.math.sqrtf128
+ libc.src.__support.math.sqrtf16
libc.src.__support.math.sqrtl
+ libc.src.__support.math.tan
+ libc.src.__support.math.tanbf16
+ libc.src.__support.math.tanf
+ libc.src.__support.math.tanf16
+ libc.src.__support.math.tanh
+ libc.src.__support.math.tanhf
+ libc.src.__support.math.tanhf16
+ libc.src.__support.math.tanpif
+ libc.src.__support.math.tanpif16
+ libc.src.__support.math.totalorder
+ libc.src.__support.math.totalorderbf16
+ libc.src.__support.math.totalorderf
+ libc.src.__support.math.totalorderf128
+ libc.src.__support.math.totalorderf16
+ libc.src.__support.math.totalorderf80
+ libc.src.__support.math.totalorderl
+ libc.src.__support.math.totalordermag
+ libc.src.__support.math.totalordermagbf16
+ libc.src.__support.math.totalordermagf
+ libc.src.__support.math.totalordermagf128
+ libc.src.__support.math.totalordermagf16
+ libc.src.__support.math.totalordermagf80
+ libc.src.__support.math.totalordermagl
libc.src.__support.math.trunc
libc.src.__support.math.truncbf16
libc.src.__support.math.truncf
@@ -882,20 +1101,6 @@ else()
libc.src.__support.math.ufromfpxf128
libc.src.__support.math.ufromfpxf16
libc.src.__support.math.ufromfpxl
- libc.src.__support.math.totalorder
- libc.src.__support.math.totalorderbf16
- libc.src.__support.math.totalorderf
- libc.src.__support.math.totalorderf128
- libc.src.__support.math.totalorderf80
- libc.src.__support.math.totalorderf16
- libc.src.__support.math.totalorderl
- libc.src.__support.math.totalordermag
- libc.src.__support.math.totalordermagbf16
- libc.src.__support.math.totalordermagf
- libc.src.__support.math.totalordermagf128
- libc.src.__support.math.totalordermagf80
- libc.src.__support.math.totalordermagf16
- libc.src.__support.math.totalordermagl
)
endif() # LIBC_TEST_SKIP_SHARED_MATH_TESTS
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 57776b640b1c3..233f3ea780e89 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -81,6 +81,7 @@ static_assert(0.0 == [] {
static_assert(0.0 == LIBC_NAMESPACE::shared::ldexp(0.0, 0.0));
static_assert(0.0 == LIBC_NAMESPACE::shared::scalbln(0.0, 0.0));
static_assert(0.0 == LIBC_NAMESPACE::shared::scalbn(0.0, 0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::hypot(0.0, 0.0));
static_assert(0 == [] {
double setpayload_x = 0.0;
return LIBC_NAMESPACE::shared::setpayload(&setpayload_x, 0.0);
@@ -116,6 +117,48 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::trunc(0.0));
// TODO: isnan clashes with a macro defined in <math.h>
// static_assert(0 == LIBC_NAMESPACE::shared::isnan(0.0));
+static_assert([] {
+ double sin{}, cos{};
+ LIBC_NAMESPACE::shared::sincos(0.0, &sin, &cos);
+ return 1.0 == cos && 0.0 == sin;
+}());
+static_assert(0.0 == LIBC_NAMESPACE::shared::acos(1.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::asin(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::asinpi(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::atan(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::atan2(0.0, 0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::cbrt(0.0));
+static_assert(1.0 == LIBC_NAMESPACE::shared::cos(0.0));
+static_assert(1.0 == LIBC_NAMESPACE::shared::exp(0.0));
+static_assert(1.0 == LIBC_NAMESPACE::shared::exp2(0.0));
+static_assert(1.0 == LIBC_NAMESPACE::shared::exp10(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::expm1(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::fma(0.0, 0.0, 0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::fsqrt(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::log10(1.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::log1p(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::log2(1.0));
+// pow/powf: constexpr deferred; upstream pow_utils uses a volatile for the
+// inexact-exception path, which cannot be constant-evaluated.
+static_assert(0.0 == LIBC_NAMESPACE::shared::sin(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::sqrt(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::tan(0.0));
+static_assert(0 == LIBC_NAMESPACE::shared::ilogb(1.0));
+static_assert(0L == LIBC_NAMESPACE::shared::llogb(1.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::logb(1.0));
+static_assert(bfloat16(5.0) == LIBC_NAMESPACE::shared::bf16add(2.0, 3.0));
+static_assert(bfloat16(2.0) == LIBC_NAMESPACE::shared::bf16div(4.0, 2.0));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::bf16mul(0.0, 0.0));
+static_assert(bfloat16(10.0) == LIBC_NAMESPACE::shared::bf16fma(2.0, 3.0, 4.0));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<double>::min_subnormal(
+ LIBC_NAMESPACE::Sign::NEG)
+ .get_val() == LIBC_NAMESPACE::shared::nextdown(0.0));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<double>::min_subnormal(
+ LIBC_NAMESPACE::Sign::POS)
+ .get_val() == LIBC_NAMESPACE::shared::nextup(0.0));
+static_assert(1.0 == LIBC_NAMESPACE::shared::cosh(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::sinh(0.0));
+static_assert(0.0 == LIBC_NAMESPACE::shared::tanh(0.0));
//===----------------------------------------------------------------------===//
// Float Tests
//===----------------------------------------------------------------------===//
@@ -201,6 +244,68 @@ static_assert(0.0f == LIBC_NAMESPACE::shared::roundevenf(0.0f));
static_assert(0.0f == LIBC_NAMESPACE::shared::truncf(0.0f));
static_assert(0 == LIBC_NAMESPACE::shared::isnanf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::acosf(1.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::acoshf(1.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::acospif(1.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::asinf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::asinhf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::asinpif(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::atan2f(0.0f, 0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::atanf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::atanhf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::cbrtf(0.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::cosf(0.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::coshf(0.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::cospif(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::exp10m1f(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::erff(0.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::exp10f(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::exp2m1f(0.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::expf(0.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::exp2f(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::expm1f(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fmaf(0.0f, 0.0f, 0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::hypotf(0.0f, 0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::logf(1.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::sinhf(0.0f));
+static_assert(0.75f == [] {
+ int exponent{};
+ return LIBC_NAMESPACE::shared::frexpf(24.0f, &exponent);
+}());
+static_assert(0 == LIBC_NAMESPACE::shared::ilogbf(1.0f));
+static_assert(float(8 << 5) == LIBC_NAMESPACE::shared::ldexpf(8.0f, 5));
+static_assert(float(-1 * (8 << 5)) == LIBC_NAMESPACE::shared::ldexpf(-8.0f, 5));
+static_assert(0L == LIBC_NAMESPACE::shared::llogbf(1.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::log1pf(0.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::log10f(10.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::log2f(2.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::logbf(1.0f));
+static_assert(1.0f == LIBC_NAMESPACE::shared::rsqrtf(1.0f));
+static_assert([] {
+ float s{}, c{};
+ LIBC_NAMESPACE::shared::sincosf(0.0f, &s, &c);
+ return s == 0.0f && c == 1.0f;
+}());
+static_assert(0.0f == LIBC_NAMESPACE::shared::sinpif(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::sinf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::sqrtf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::tanf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::tanhf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::tanpif(0.0f));
+static_assert(bfloat16(5.0f) == LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
+static_assert(bfloat16(2.0f) == LIBC_NAMESPACE::shared::bf16divf(4.0f, 2.0f));
+static_assert(bfloat16(0.0f) == LIBC_NAMESPACE::shared::bf16mulf(0.0f, 0.0f));
+static_assert(bfloat16(10.0f) ==
+ LIBC_NAMESPACE::shared::bf16fmaf(2.0f, 3.0f, 4.0f));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<float>::min_subnormal(
+ LIBC_NAMESPACE::Sign::NEG)
+ .get_val() == LIBC_NAMESPACE::shared::nextdownf(0.0f));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<float>::min_subnormal(
+ LIBC_NAMESPACE::Sign::POS)
+ .get_val() == LIBC_NAMESPACE::shared::nextupf(0.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::lgammaf(1.0f));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fromfpf(0.0f, 0, 32));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fromfpxf(0.0f, 0, 32));
//===----------------------------------------------------------------------===//
// Float16 Tests
//===----------------------------------------------------------------------===//
@@ -302,6 +407,82 @@ static_assert(1 == [] {
static_assert(0.0f16 == LIBC_NAMESPACE::shared::roundf16(0.0f16));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::roundevenf16(0.0f16));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::truncf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::acoshf16(1.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::acospif16(1.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::rsqrtf16(1.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::sqrtf16(1.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::asinf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::asinhf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::asinpif16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::atan2f16(0.0f16, 0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::atanf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::atanhf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::atanpif16(0.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::cosf16(0.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::coshf16(0.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::cospif16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::erff16(0.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::erfcf16(0.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::exp10f16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::exp10m1f16(0.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::exp2f16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::exp2m1f16(0.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::expf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::expm1f16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::fmaf16(0.0f16, 0.0f16, 0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::hypotf16(0.0f16, 0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::logf16(1.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::sinhf16(0.0f16));
+static_assert(float16(8 << 5) == LIBC_NAMESPACE::shared::ldexpf16(8.0f16, 5));
+static_assert(float16(-1 * (8 << 5)) ==
+ LIBC_NAMESPACE::shared::ldexpf16(-8.0f16, 5));
+static_assert(0.75f16 == [] {
+ int exponent{};
+ return LIBC_NAMESPACE::shared::frexpf16(24.0f16, &exponent);
+}());
+static_assert(0 == LIBC_NAMESPACE::shared::ilogbf16(1.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::log10f16(10.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::log10p1f16(9.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::log2f16(2.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::log2p1f16(1.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::logbf16(1.0f16));
+static_assert(0L == LIBC_NAMESPACE::shared::llogbf16(1.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::acosf16(1.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::sinf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::tanf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::sinpif16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::tanhf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::tanpif16(0.0f16));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<float16>::min_subnormal(
+ LIBC_NAMESPACE::Sign::NEG)
+ .get_val() == LIBC_NAMESPACE::shared::nextdownf16(0.0f16));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<float16>::min_subnormal(
+ LIBC_NAMESPACE::Sign::POS)
+ .get_val() == LIBC_NAMESPACE::shared::nextupf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::cbrtf16(0.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::lgammaf16(1.0f16));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16div(0.0, 1.0));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16mul(0.0, 0.0));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16sub(0.0, 0.0));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16sqrt(0.0));
+static_assert(10.0f16 == LIBC_NAMESPACE::shared::f16fma(2.0, 3.0, 4.0));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16divf(0.0f, 1.0f));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16mulf(0.0f, 0.0f));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16subf(0.0f, 0.0f));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16sqrtf(0.0f));
+static_assert(10.0f16 == LIBC_NAMESPACE::shared::f16fmaf(2.0f, 3.0f, 4.0f));
+#ifdef LIBC_TYPES_HAS_NATIVE_FLOAT128
+static_assert(10.0f16 == LIBC_NAMESPACE::shared::f16fmaf128(float128(2.0),
+ float128(3.0),
+ float128(4.0)));
+static_assert(0.0f16 ==
+ LIBC_NAMESPACE::shared::f16divf128(float128(0.0), float128(1.0)));
+static_assert(0.0f16 ==
+ LIBC_NAMESPACE::shared::f16mulf128(float128(0.0), float128(0.0)));
+static_assert(0.0f16 ==
+ LIBC_NAMESPACE::shared::f16subf128(float128(0.0), float128(0.0)));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16sqrtf128(float128(0.0)));
+#endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
#endif // LIBC_TYPES_HAS_FLOAT16
//===----------------------------------------------------------------------===//
@@ -401,6 +582,30 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::roundevenl(0.0L));
static_assert(0.0L == LIBC_NAMESPACE::shared::truncl(0.0L));
static_assert(0 == LIBC_NAMESPACE::shared::isnanl(0.0L));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::nexttowardf16(0.0f16, 0.0L));
+static_assert(0.0f == LIBC_NAMESPACE::shared::nexttowardf(0.0f, 0.0L));
+static_assert(0.0 == LIBC_NAMESPACE::shared::nexttoward(0.0, 0.0L));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::nexttowardbf16(bfloat16(0.0), 0.0L));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::bf16addl(0.0L, 0.0L));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::bf16divl(0.0L, 1.0L));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::bf16fmal(0.0L, 0.0L, 0.0L));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::bf16mull(0.0L, 0.0L));
+static_assert(0.0 == LIBC_NAMESPACE::shared::daddl(0.0L, 0.0L));
+static_assert(0.0 == LIBC_NAMESPACE::shared::dfmal(0.0L, 0.0L, 0.0L));
+static_assert(0.0 == LIBC_NAMESPACE::shared::dsqrtl(0.0L));
+static_assert(0.0 == LIBC_NAMESPACE::shared::dsubl(0.0L, 0.0L));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16divl(0.0L, 1.0L));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16fmal(0.0L, 0.0L, 0.0L));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16mull(0.0L, 0.0L));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16sqrtl(0.0L));
+static_assert(0.0f16 == LIBC_NAMESPACE::shared::f16subl(0.0L, 0.0L));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fsqrtl(0.0L));
+static_assert(0 == LIBC_NAMESPACE::shared::ilogbl(1.0L));
+static_assert(0L == LIBC_NAMESPACE::shared::llogbl(1.0L));
+static_assert(0.0L == LIBC_NAMESPACE::shared::logbl(1.0L));
+static_assert(0.0L == LIBC_NAMESPACE::shared::nextdownl(0.0L));
+static_assert(0.0L == LIBC_NAMESPACE::shared::nexttowardl(0.0L, 0.0L));
+static_assert(0.0L == LIBC_NAMESPACE::shared::nextupl(0.0L));
#endif
//===----------------------------------------------------------------------===//
@@ -567,6 +772,9 @@ static_assert(1 == [] {
Float128(0.0));
}());
+static_assert(Float128(0.0) ==
+ LIBC_NAMESPACE::shared::atan2f128(Float128(0.0), Float128(0.0)));
+static_assert(Float128(0.0) == LIBC_NAMESPACE::shared::sqrtf128(Float128(0.0)));
//===----------------------------------------------------------------------===//
// Native Float128 Tests
//===----------------------------------------------------------------------===//
@@ -610,6 +818,34 @@ static_assert(float128(0.0) ==
LIBC_NAMESPACE::shared::nextafterf128(float128(0.0),
float128(0.0)));
+static_assert(1.0f == LIBC_NAMESPACE::shared::fsqrtf128(float128(1.0f)));
+static_assert(0.0 == LIBC_NAMESPACE::shared::dfmaf128(float128(0.0),
+ float128(0.0),
+ float128(0.0)));
+static_assert(bfloat16(5.0) ==
+ LIBC_NAMESPACE::shared::bf16addf128(float128(2.0),
+ float128(3.0)));
+static_assert(bfloat16(0.0) ==
+ LIBC_NAMESPACE::shared::bf16fmaf128(float128(0.0), float128(0.0),
+ float128(0.0)));
+static_assert(bfloat16(0.0) ==
+ LIBC_NAMESPACE::shared::bf16mulf128(float128(0.0),
+ float128(0.0)));
+static_assert(bfloat16(2.0) ==
+ LIBC_NAMESPACE::shared::bf16divf128(float128(4.0),
+ float128(2.0)));
+static_assert(float128(0.0) ==
+ LIBC_NAMESPACE::shared::daddf128(Float128(0.0), Float128(0.0)));
+static_assert(0.0 ==
+ LIBC_NAMESPACE::shared::dsubf128(float128(0.0), float128(0.0)));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<float128>::min_subnormal(
+ LIBC_NAMESPACE::Sign::NEG)
+ .get_val() ==
+ LIBC_NAMESPACE::shared::nextdownf128(float128(0.0)));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<float128>::min_subnormal(
+ LIBC_NAMESPACE::Sign::POS)
+ .get_val() ==
+ LIBC_NAMESPACE::shared::nextupf128(float128(0.0)));
#endif // LIBC_TYPES_HAS_NATIVE_FLOAT128
//===----------------------------------------------------------------------===//
@@ -746,7 +982,29 @@ static_assert(bfloat16(0.0) ==
LIBC_NAMESPACE::shared::roundevenbf16(bfloat16(0.0)));
static_assert(bfloat16(0.0) ==
LIBC_NAMESPACE::shared::truncbf16(bfloat16(0.0)));
-
-#endif // LIBC_USE_CONTEXPR
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::atanbf16(bfloat16(0.0)));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::cbrtbf16(bfloat16(0.0)));
+static_assert(bfloat16(10.0) == LIBC_NAMESPACE::shared::fmabf16(bfloat16(2.0),
+ bfloat16(3.0),
+ bfloat16(4.0)));
+static_assert(bfloat16(5.0) ==
+ LIBC_NAMESPACE::shared::hypotbf16(bfloat16(4.0), bfloat16(3.0)));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::log_bf16(bfloat16(1.0)));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<bfloat16>::min_subnormal(
+ LIBC_NAMESPACE::Sign::NEG)
+ .get_val() ==
+ LIBC_NAMESPACE::shared::nextdownbf16(bfloat16(0.0)));
+static_assert(LIBC_NAMESPACE::fputil::FPBits<bfloat16>::min_subnormal(
+ LIBC_NAMESPACE::Sign::POS)
+ .get_val() ==
+ LIBC_NAMESPACE::shared::nextupbf16(bfloat16(0.0)));
+static_assert(bfloat16(1.0) == LIBC_NAMESPACE::shared::sqrtbf16(bfloat16(1.0)));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::acosbf16(bfloat16(1.0)));
+static_assert(bfloat16(1.0) == LIBC_NAMESPACE::shared::exp2bf16(bfloat16(0.0)));
+static_assert(bfloat16(1.0) == LIBC_NAMESPACE::shared::expbf16(bfloat16(0.0)));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::sinhbf16(bfloat16(0.0)));
+static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::tanbf16(bfloat16(0.0)));
+
+#endif // LIBC_USE_CONSTEXPR
TEST(LlvmLibcSharedMathTest, ConstantEvaluation) {}
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 37244fe000f55..66f4b8e38ba6b 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -56,15 +56,12 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::hypotf16(0.0f16, 0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::logf16(1.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::sinhf16(0.0f16));
-
ASSERT_FP_EQ(float16(8 << 5), LIBC_NAMESPACE::shared::ldexpf16(8.0f16, 5));
ASSERT_FP_EQ(float16(-1 * (8 << 5)),
LIBC_NAMESPACE::shared::ldexpf16(-8.0f16, 5));
-
EXPECT_FP_EQ_ALL_ROUNDING(
0.75f16, LIBC_NAMESPACE::shared::frexpf16(24.0f16, &exponent));
EXPECT_EQ(exponent, 5);
-
EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbf16(1.0f16));
EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::shared::log10f16(10.0f16));
EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::shared::log10p1f16(9.0f16));
@@ -73,7 +70,6 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::logbf16(1.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::lgammaf16(1.0f16));
EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogbf16(1.0f16));
-
EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::sinf16(0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::tanf16(0.0f16));
@@ -925,7 +921,8 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
bfloat16 totalordermagbf16_y = bfloat16(0.0);
EXPECT_EQ(1, LIBC_NAMESPACE::shared::totalordermagbf16(&totalordermagbf16_x,
&totalordermagbf16_y));
- LIBC_NAMESPACE::shared::fmodbf16(bfloat16(1.0), bfloat16(1.0));
+ EXPECT_FP_EQ(bfloat16(0.0),
+ LIBC_NAMESPACE::shared::fmodbf16(bfloat16(1.0), bfloat16(1.0)));
bfloat16 modfbf16_iptr = bfloat16(0.0);
EXPECT_FP_EQ(bfloat16(0.0),
LIBC_NAMESPACE::shared::modfbf16(bfloat16(0.0), &modfbf16_iptr));
>From 67bd9a4c537da916171a8e3e8b752e41c89e459a Mon Sep 17 00:00:00 2001
From: linisha <linisha232 at gmail.com>
Date: Sat, 14 Nov 2026 00:09:24 +0530
Subject: [PATCH 3/3] [libc][math] Drop redundant LIBC_USE_CONSTEXPR guard from
rounding funcs
is_constant_evaluated() already routes constant evaluation to the generic implementation, so the guard was redundant.
---
libc/src/__support/math/ceil.h | 3 +--
libc/src/__support/math/ceilf.h | 3 +--
libc/src/__support/math/ceilf16.h | 3 +--
libc/src/__support/math/floor.h | 3 +--
libc/src/__support/math/floorf.h | 3 +--
libc/src/__support/math/floorf16.h | 3 +--
libc/src/__support/math/rint.h | 3 +--
libc/src/__support/math/rintf.h | 3 +--
libc/src/__support/math/rintf16.h | 3 +--
libc/src/__support/math/round.h | 2 +-
libc/src/__support/math/roundeven.h | 2 +-
libc/src/__support/math/roundevenf.h | 2 +-
libc/src/__support/math/roundevenf16.h | 3 +--
libc/src/__support/math/roundf16.h | 3 +--
libc/src/__support/math/trunc.h | 3 +--
libc/src/__support/math/truncf.h | 3 +--
libc/src/__support/math/truncf16.h | 3 +--
17 files changed, 17 insertions(+), 31 deletions(-)
diff --git a/libc/src/__support/math/ceil.h b/libc/src/__support/math/ceil.h
index 980d2389a5433..870a6be418d88 100644
--- a/libc/src/__support/math/ceil.h
+++ b/libc/src/__support/math/ceil.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double ceil(double x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
if (cpp::is_constant_evaluated()) {
return fputil::ceil(x);
} else {
diff --git a/libc/src/__support/math/ceilf.h b/libc/src/__support/math/ceilf.h
index 2757f3720e87f..5c05d5c7250cc 100644
--- a/libc/src/__support/math/ceilf.h
+++ b/libc/src/__support/math/ceilf.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float ceilf(float x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
if (cpp::is_constant_evaluated()) {
return fputil::ceil(x);
} else {
diff --git a/libc/src/__support/math/ceilf16.h b/libc/src/__support/math/ceilf16.h
index 62a3ee04b0734..f46f109c110a3 100644
--- a/libc/src/__support/math/ceilf16.h
+++ b/libc/src/__support/math/ceilf16.h
@@ -24,8 +24,7 @@ namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 ceilf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
- !defined(LIBC_USE_CONSTEXPR)
+ defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS)
if (cpp::is_constant_evaluated()) {
return fputil::ceil(x);
} else {
diff --git a/libc/src/__support/math/floor.h b/libc/src/__support/math/floor.h
index b57c48009d03c..bb7ffe825cf89 100644
--- a/libc/src/__support/math/floor.h
+++ b/libc/src/__support/math/floor.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double floor(double x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
if (cpp::is_constant_evaluated()) {
return fputil::floor(x);
} else {
diff --git a/libc/src/__support/math/floorf.h b/libc/src/__support/math/floorf.h
index c7b9cc436dc99..9bb5532a14fdd 100644
--- a/libc/src/__support/math/floorf.h
+++ b/libc/src/__support/math/floorf.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float floorf(float x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
if (cpp::is_constant_evaluated()) {
return fputil::floor(x);
} else {
diff --git a/libc/src/__support/math/floorf16.h b/libc/src/__support/math/floorf16.h
index 62577991bf282..8623db664bb1e 100644
--- a/libc/src/__support/math/floorf16.h
+++ b/libc/src/__support/math/floorf16.h
@@ -24,8 +24,7 @@ namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 floorf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
- !defined(LIBC_USE_CONSTEXPR)
+ defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS)
if (cpp::is_constant_evaluated()) {
return fputil::floor(x);
} else {
diff --git a/libc/src/__support/math/rint.h b/libc/src/__support/math/rint.h
index 66cbe7cfcbf63..d678e5e230eb1 100644
--- a/libc/src/__support/math/rint.h
+++ b/libc/src/__support/math/rint.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double rint(double x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
if (cpp::is_constant_evaluated()) {
return fputil::round_using_current_rounding_mode(x);
} else {
diff --git a/libc/src/__support/math/rintf.h b/libc/src/__support/math/rintf.h
index 091933830d9ad..11ab34511cbcb 100644
--- a/libc/src/__support/math/rintf.h
+++ b/libc/src/__support/math/rintf.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float rintf(float x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
if (cpp::is_constant_evaluated()) {
return fputil::round_using_current_rounding_mode(x);
} else {
diff --git a/libc/src/__support/math/rintf16.h b/libc/src/__support/math/rintf16.h
index 93dc2febde553..71fa9635e6101 100644
--- a/libc/src/__support/math/rintf16.h
+++ b/libc/src/__support/math/rintf16.h
@@ -24,8 +24,7 @@ namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 rintf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
- !defined(LIBC_USE_CONSTEXPR)
+ defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS)
if (cpp::is_constant_evaluated()) {
return fputil::round_using_current_rounding_mode(x);
} else {
diff --git a/libc/src/__support/math/round.h b/libc/src/__support/math/round.h
index 8e242360fdd9a..e4f8164fb5dc2 100644
--- a/libc/src/__support/math/round.h
+++ b/libc/src/__support/math/round.h
@@ -48,7 +48,7 @@ namespace math {
// default rounding mode (FE_TONEAREST) only.
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double round(double x) {
-#if __has_builtin(__builtin_round) && !defined(LIBC_USE_CONSTEXPR) && \
+#if __has_builtin(__builtin_round) && \
(defined(__LIBC_USE_BUILTIN_ROUND) || \
(defined(LIBC_COMPILER_IS_CLANG) && \
defined(LIBC_TARGET_CPU_HAS_FPU_DOUBLE) && \
diff --git a/libc/src/__support/math/roundeven.h b/libc/src/__support/math/roundeven.h
index 0a36d1216e2c8..f5c76aba88819 100644
--- a/libc/src/__support/math/roundeven.h
+++ b/libc/src/__support/math/roundeven.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double roundeven(double x) {
-#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN) && !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN)
if (cpp::is_constant_evaluated()) {
return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
} else {
diff --git a/libc/src/__support/math/roundevenf.h b/libc/src/__support/math/roundevenf.h
index ee302eb706a51..fdf5a386cf8e6 100644
--- a/libc/src/__support/math/roundevenf.h
+++ b/libc/src/__support/math/roundevenf.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float roundevenf(float x) {
-#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN) && !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN)
if (cpp::is_constant_evaluated()) {
return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
} else {
diff --git a/libc/src/__support/math/roundevenf16.h b/libc/src/__support/math/roundevenf16.h
index 35172b9037fea..7a9a06d4d0a17 100644
--- a/libc/src/__support/math/roundevenf16.h
+++ b/libc/src/__support/math/roundevenf16.h
@@ -24,8 +24,7 @@ namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 roundevenf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN) && \
- defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
- !defined(LIBC_USE_CONSTEXPR)
+ defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS)
if (cpp::is_constant_evaluated()) {
return fputil::round_using_specific_rounding_mode(x, FP_INT_TONEAREST);
} else {
diff --git a/libc/src/__support/math/roundf16.h b/libc/src/__support/math/roundf16.h
index de22d4dc0d24b..a3e0e3efb22e8 100644
--- a/libc/src/__support/math/roundf16.h
+++ b/libc/src/__support/math/roundf16.h
@@ -24,8 +24,7 @@ namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 roundf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_ROUND) && \
- defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
- !defined(LIBC_USE_CONSTEXPR)
+ defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS)
if (cpp::is_constant_evaluated()) {
return fputil::round(x);
} else {
diff --git a/libc/src/__support/math/trunc.h b/libc/src/__support/math/trunc.h
index 90b0514c89f6c..05a69eee91bfa 100644
--- a/libc/src/__support/math/trunc.h
+++ b/libc/src/__support/math/trunc.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double trunc(double x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
if (cpp::is_constant_evaluated()) {
return fputil::trunc(x);
} else {
diff --git a/libc/src/__support/math/truncf.h b/libc/src/__support/math/truncf.h
index f5ca0871351b9..d03907124848d 100644
--- a/libc/src/__support/math/truncf.h
+++ b/libc/src/__support/math/truncf.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float truncf(float x) {
-#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- !defined(LIBC_USE_CONSTEXPR)
+#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
if (cpp::is_constant_evaluated()) {
return fputil::trunc(x);
} else {
diff --git a/libc/src/__support/math/truncf16.h b/libc/src/__support/math/truncf16.h
index e7c97920d69f9..a46853cf4a217 100644
--- a/libc/src/__support/math/truncf16.h
+++ b/libc/src/__support/math/truncf16.h
@@ -24,8 +24,7 @@ namespace math {
LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float16 truncf16(float16 x) {
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
- defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) && \
- !defined(LIBC_USE_CONSTEXPR)
+ defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS)
if (cpp::is_constant_evaluated()) {
return fputil::trunc(x);
} else {
More information about the libc-commits
mailing list