[libc-commits] [libc] [libc][math] Qualify canonicalize functions to constexpr (PR #195630)

via libc-commits libc-commits at lists.llvm.org
Mon May 4 03:42:22 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Kiriti Ponduri  (udaykiriti)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/195630.diff


8 Files Affected:

- (modified) libc/src/__support/math/canonicalize.h (+1-1) 
- (modified) libc/src/__support/math/canonicalizebf16.h (+1-1) 
- (modified) libc/src/__support/math/canonicalizef.h (+1-1) 
- (modified) libc/src/__support/math/canonicalizef128.h (+1-1) 
- (modified) libc/src/__support/math/canonicalizef16.h (+1-1) 
- (modified) libc/src/__support/math/canonicalizel.h (+1-1) 
- (modified) libc/test/shared/CMakeLists.txt (+6) 
- (modified) libc/test/shared/shared_math_constexpr_test.cpp (+30) 


``````````diff
diff --git a/libc/src/__support/math/canonicalize.h b/libc/src/__support/math/canonicalize.h
index e7c60d4026bce..b72161c47ec37 100644
--- a/libc/src/__support/math/canonicalize.h
+++ b/libc/src/__support/math/canonicalize.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE int canonicalize(double *cx, const double *x) {
+LIBC_INLINE constexpr int canonicalize(double *cx, const double *x) {
   return fputil::canonicalize(*cx, *x);
 }
 
diff --git a/libc/src/__support/math/canonicalizebf16.h b/libc/src/__support/math/canonicalizebf16.h
index cc6abd40973e7..013d2cc4a7d3b 100644
--- a/libc/src/__support/math/canonicalizebf16.h
+++ b/libc/src/__support/math/canonicalizebf16.h
@@ -17,7 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE int canonicalizebf16(bfloat16 *cx, const bfloat16 *x) {
+LIBC_INLINE constexpr int canonicalizebf16(bfloat16 *cx, const bfloat16 *x) {
   return fputil::canonicalize(*cx, *x);
 }
 
diff --git a/libc/src/__support/math/canonicalizef.h b/libc/src/__support/math/canonicalizef.h
index da166064219d9..a6c28e5384fdf 100644
--- a/libc/src/__support/math/canonicalizef.h
+++ b/libc/src/__support/math/canonicalizef.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE int canonicalizef(float *cx, const float *x) {
+LIBC_INLINE constexpr int canonicalizef(float *cx, const float *x) {
   return fputil::canonicalize(*cx, *x);
 }
 
diff --git a/libc/src/__support/math/canonicalizef128.h b/libc/src/__support/math/canonicalizef128.h
index 8b4905353ddfa..69fd62b158900 100644
--- a/libc/src/__support/math/canonicalizef128.h
+++ b/libc/src/__support/math/canonicalizef128.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE int canonicalizef128(float128 *cx, const float128 *x) {
+LIBC_INLINE constexpr int canonicalizef128(float128 *cx, const float128 *x) {
   return fputil::canonicalize(*cx, *x);
 }
 
diff --git a/libc/src/__support/math/canonicalizef16.h b/libc/src/__support/math/canonicalizef16.h
index b7f44caa8ce3b..d2b695cb72537 100644
--- a/libc/src/__support/math/canonicalizef16.h
+++ b/libc/src/__support/math/canonicalizef16.h
@@ -20,7 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE int canonicalizef16(float16 *cx, const float16 *x) {
+LIBC_INLINE constexpr int canonicalizef16(float16 *cx, const float16 *x) {
   return fputil::canonicalize(*cx, *x);
 }
 
diff --git a/libc/src/__support/math/canonicalizel.h b/libc/src/__support/math/canonicalizel.h
index fb69352fa1fb2..96123713fd303 100644
--- a/libc/src/__support/math/canonicalizel.h
+++ b/libc/src/__support/math/canonicalizel.h
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 namespace math {
 
-LIBC_INLINE int canonicalizel(long double *cx, const long double *x) {
+LIBC_INLINE constexpr int canonicalizel(long double *cx, const long double *x) {
   return fputil::canonicalize(*cx, *x);
 }
 
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 81e7a24a977e8..6251cbde7901e 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -509,6 +509,12 @@ add_fp_unittest(
   DEPENDS
     libc.src.__support.math.asinbf16
     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.canonicalizef16
+    libc.src.__support.math.canonicalizel
     libc.src.__support.math.ceil
     libc.src.__support.math.ceilbf16
     libc.src.__support.math.ceilf
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 21bcdc422c8d8..53bf664bf8827 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -15,6 +15,11 @@
 //                       Double Tests
 //===----------------------------------------------------------------------===//
 
+static_assert(0 == [] {
+  double cx = 0.0;
+  double x = 0.0;
+  return LIBC_NAMESPACE::shared::canonicalize(&cx, &x);
+}());
 static_assert(0.0 == LIBC_NAMESPACE::shared::ceil(0.0));
 static_assert(0.0 == LIBC_NAMESPACE::shared::copysign(0.0, 0.0));
 static_assert(1.0 == LIBC_NAMESPACE::shared::fabs(-1.0));
@@ -93,6 +98,11 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::trunc(0.0));
 //                       Float Tests
 //===----------------------------------------------------------------------===//
 
+static_assert(0 == [] {
+  float cx = 0.0f;
+  float x = 0.0f;
+  return LIBC_NAMESPACE::shared::canonicalizef(&cx, &x);
+}());
 static_assert(0.0f == LIBC_NAMESPACE::shared::ceilf(0.0f));
 static_assert(0.0f == LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f));
 static_assert(1.0f == LIBC_NAMESPACE::shared::fabsf(-1.0f));
@@ -169,6 +179,11 @@ static_assert(0.0f == LIBC_NAMESPACE::shared::truncf(0.0f));
 
 #ifdef LIBC_TYPES_HAS_FLOAT16
 
+static_assert(0 == [] {
+  float16 cx = 0.0f16;
+  float16 x = 0.0f16;
+  return LIBC_NAMESPACE::shared::canonicalizef16(&cx, &x);
+}());
 static_assert(0.0f16 == LIBC_NAMESPACE::shared::ceilf16(0.0f16));
 static_assert(0.0f16 == LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16));
 static_assert(3.0f16 == LIBC_NAMESPACE::shared::f16add(1.0, 2.0));
@@ -263,6 +278,11 @@ static_assert(0.0f16 == LIBC_NAMESPACE::shared::truncf16(0.0f16));
 // TODO(issue#185232): Mark as constexpr once the refactor is done.
 #if 0 // Temporarily disable long double tests
 
+static_assert(0 == [] {
+  long double cx = 0.0L;
+  long double x = 0.0L;
+  return LIBC_NAMESPACE::shared::canonicalizel(&cx, &x);
+}());
 static_assert(0.0L == LIBC_NAMESPACE::shared::ceill(0.0L));
 static_assert(0.0L == LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L));
 static_assert(0.0 == LIBC_NAMESPACE::shared::ddivl(0.0L, 1.0L));
@@ -350,6 +370,11 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::truncl(0.0L));
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
 
+static_assert(0 == [] {
+  float128 cx = float128(0.0);
+  float128 x = float128(0.0);
+  return LIBC_NAMESPACE::shared::canonicalizef128(&cx, &x);
+}());
 static_assert(float128(0.0) == LIBC_NAMESPACE::shared::ceilf128(float128(0.0)));
 static_assert(float128(1.0) ==
               LIBC_NAMESPACE::shared::fabsf128(float128(-1.0)));
@@ -477,6 +502,11 @@ static_assert(float128(0.0) ==
 //                       BFloat16 Tests
 //===----------------------------------------------------------------------===//
 
+static_assert(0 == [] {
+  bfloat16 cx = bfloat16(0.0);
+  bfloat16 x = bfloat16(0.0);
+  return LIBC_NAMESPACE::shared::canonicalizebf16(&cx, &x);
+}());
 static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::asinbf16(bfloat16(0.0)));
 static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0)));
 static_assert(bfloat16(1.0) ==

``````````

</details>


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


More information about the libc-commits mailing list