[libc] [llvm] [libc][math] Refactor canonicalize family to header-only (PR #181467)

via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 14 00:37:26 PST 2026


https://github.com/AnonMiraj created https://github.com/llvm/llvm-project/pull/181467

closes: #181466

>From 0e1e402ba0842c75c60271655404f2f496ae8d1f Mon Sep 17 00:00:00 2001
From: Ezzeldin Ibrahim <ezzibrahimx at gmail.com>
Date: Sat, 14 Feb 2026 10:19:43 +0200
Subject: [PATCH] [libc][math] Refactor canonicalize family to header-only

---
 libc/shared/math.h                            |  6 ++
 libc/shared/math/canonicalize.h               | 23 +++++
 libc/shared/math/canonicalizebf16.h           | 23 +++++
 libc/shared/math/canonicalizef.h              | 23 +++++
 libc/shared/math/canonicalizef128.h           | 29 ++++++
 libc/shared/math/canonicalizef16.h            | 29 ++++++
 libc/shared/math/canonicalizel.h              | 23 +++++
 libc/src/__support/math/CMakeLists.txt        | 54 +++++++++++
 libc/src/__support/math/canonicalize.h        | 27 ++++++
 libc/src/__support/math/canonicalizebf16.h    | 28 ++++++
 libc/src/__support/math/canonicalizef.h       | 27 ++++++
 libc/src/__support/math/canonicalizef128.h    | 33 +++++++
 libc/src/__support/math/canonicalizef16.h     | 33 +++++++
 libc/src/__support/math/canonicalizel.h       | 27 ++++++
 libc/src/math/generic/CMakeLists.txt          | 18 ++--
 libc/src/math/generic/canonicalize.cpp        |  6 +-
 libc/src/math/generic/canonicalizebf16.cpp    |  7 +-
 libc/src/math/generic/canonicalizef.cpp       |  6 +-
 libc/src/math/generic/canonicalizef128.cpp    |  6 +-
 libc/src/math/generic/canonicalizef16.cpp     |  6 +-
 libc/src/math/generic/canonicalizel.cpp       |  6 +-
 libc/test/shared/CMakeLists.txt               |  6 ++
 libc/test/shared/shared_math_test.cpp         | 39 ++++++++
 .../llvm-project-overlay/libc/BUILD.bazel     | 89 +++++++++++++++++--
 24 files changed, 532 insertions(+), 42 deletions(-)
 create mode 100644 libc/shared/math/canonicalize.h
 create mode 100644 libc/shared/math/canonicalizebf16.h
 create mode 100644 libc/shared/math/canonicalizef.h
 create mode 100644 libc/shared/math/canonicalizef128.h
 create mode 100644 libc/shared/math/canonicalizef16.h
 create mode 100644 libc/shared/math/canonicalizel.h
 create mode 100644 libc/src/__support/math/canonicalize.h
 create mode 100644 libc/src/__support/math/canonicalizebf16.h
 create mode 100644 libc/src/__support/math/canonicalizef.h
 create mode 100644 libc/src/__support/math/canonicalizef128.h
 create mode 100644 libc/src/__support/math/canonicalizef16.h
 create mode 100644 libc/src/__support/math/canonicalizel.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 06cf83d0f3334..fbde5c055fc80 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -31,6 +31,12 @@
 #include "math/atanhf.h"
 #include "math/atanhf16.h"
 #include "math/bf16addf128.h"
+#include "math/canonicalize.h"
+#include "math/canonicalizebf16.h"
+#include "math/canonicalizef.h"
+#include "math/canonicalizef128.h"
+#include "math/canonicalizef16.h"
+#include "math/canonicalizel.h"
 #include "math/cbrt.h"
 #include "math/cbrtf.h"
 #include "math/cos.h"
diff --git a/libc/shared/math/canonicalize.h b/libc/shared/math/canonicalize.h
new file mode 100644
index 0000000000000..5e21641de2f40
--- /dev/null
+++ b/libc/shared/math/canonicalize.h
@@ -0,0 +1,23 @@
+//===-- Shared canonicalize function ----------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_CANONICALIZE_H
+#define LLVM_LIBC_SHARED_MATH_CANONICALIZE_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/canonicalize.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::canonicalize;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_CANONICALIZE_H
diff --git a/libc/shared/math/canonicalizebf16.h b/libc/shared/math/canonicalizebf16.h
new file mode 100644
index 0000000000000..1b92e00076362
--- /dev/null
+++ b/libc/shared/math/canonicalizebf16.h
@@ -0,0 +1,23 @@
+//===-- Shared canonicalizebf16 function ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_CANONICALIZEBF16_H
+#define LLVM_LIBC_SHARED_MATH_CANONICALIZEBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/canonicalizebf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::canonicalizebf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_CANONICALIZEBF16_H
diff --git a/libc/shared/math/canonicalizef.h b/libc/shared/math/canonicalizef.h
new file mode 100644
index 0000000000000..8097cdf0987fd
--- /dev/null
+++ b/libc/shared/math/canonicalizef.h
@@ -0,0 +1,23 @@
+//===-- Shared canonicalizef function ---------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_CANONICALIZEF_H
+#define LLVM_LIBC_SHARED_MATH_CANONICALIZEF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/canonicalizef.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::canonicalizef;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_CANONICALIZEF_H
diff --git a/libc/shared/math/canonicalizef128.h b/libc/shared/math/canonicalizef128.h
new file mode 100644
index 0000000000000..dfc5f89afee50
--- /dev/null
+++ b/libc/shared/math/canonicalizef128.h
@@ -0,0 +1,29 @@
+//===-- Shared canonicalizef128 function ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_CANONICALIZEF128_H
+#define LLVM_LIBC_SHARED_MATH_CANONICALIZEF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/canonicalizef128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::canonicalizef128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_CANONICALIZEF128_H
diff --git a/libc/shared/math/canonicalizef16.h b/libc/shared/math/canonicalizef16.h
new file mode 100644
index 0000000000000..1925d052ac0fd
--- /dev/null
+++ b/libc/shared/math/canonicalizef16.h
@@ -0,0 +1,29 @@
+//===-- Shared canonicalizef16 function -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_CANONICALIZEF16_H
+#define LLVM_LIBC_SHARED_MATH_CANONICALIZEF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/math/canonicalizef16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::canonicalizef16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_CANONICALIZEF16_H
diff --git a/libc/shared/math/canonicalizel.h b/libc/shared/math/canonicalizel.h
new file mode 100644
index 0000000000000..66e2ed9053dc2
--- /dev/null
+++ b/libc/shared/math/canonicalizel.h
@@ -0,0 +1,23 @@
+//===-- Shared canonicalizel function ---------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_CANONICALIZEL_H
+#define LLVM_LIBC_SHARED_MATH_CANONICALIZEL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/canonicalizel.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::canonicalizel;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_CANONICALIZEL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index fe20faee176fb..4494902d86ee3 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -341,6 +341,60 @@ add_header_library(
     libc.src.__support.FPUtil.generic.add_sub
     libc.src.__support.macros.config
 )
+add_header_library(
+  canonicalize
+  HDRS
+    canonicalize.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+)
+
+add_header_library(
+  canonicalizebf16
+  HDRS
+    canonicalizebf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  canonicalizef
+  HDRS
+    canonicalizef.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+)
+
+add_header_library(
+  canonicalizef128
+  HDRS
+    canonicalizef128.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  canonicalizef16
+  HDRS
+    canonicalizef16.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.properties.types
+)
+
+add_header_library(
+  canonicalizel
+  HDRS
+    canonicalizel.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+)
+
 
 add_header_library(
   cbrt
diff --git a/libc/src/__support/math/canonicalize.h b/libc/src/__support/math/canonicalize.h
new file mode 100644
index 0000000000000..eb208b2de744e
--- /dev/null
+++ b/libc/src/__support/math/canonicalize.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for canonicalize ------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZE_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZE_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE int canonicalize(double *cx, const double *x) {
+  return fputil::canonicalize(*cx, *x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZE_H
diff --git a/libc/src/__support/math/canonicalizebf16.h b/libc/src/__support/math/canonicalizebf16.h
new file mode 100644
index 0000000000000..9a2fe3b6561cf
--- /dev/null
+++ b/libc/src/__support/math/canonicalizebf16.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for canonicalizebf16 --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEBF16_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE int canonicalizebf16(bfloat16 *cx, const bfloat16 *x) {
+  return fputil::canonicalize(*cx, *x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEBF16_H
diff --git a/libc/src/__support/math/canonicalizef.h b/libc/src/__support/math/canonicalizef.h
new file mode 100644
index 0000000000000..39e20b9253e53
--- /dev/null
+++ b/libc/src/__support/math/canonicalizef.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for canonicalizef -----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE int canonicalizef(float *cx, const float *x) {
+  return fputil::canonicalize(*cx, *x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF_H
diff --git a/libc/src/__support/math/canonicalizef128.h b/libc/src/__support/math/canonicalizef128.h
new file mode 100644
index 0000000000000..de357c75c655f
--- /dev/null
+++ b/libc/src/__support/math/canonicalizef128.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for canonicalizef128 --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE int canonicalizef128(float128 *cx, const float128 *x) {
+  return fputil::canonicalize(*cx, *x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF128_H
diff --git a/libc/src/__support/math/canonicalizef16.h b/libc/src/__support/math/canonicalizef16.h
new file mode 100644
index 0000000000000..6feea9aa271d1
--- /dev/null
+++ b/libc/src/__support/math/canonicalizef16.h
@@ -0,0 +1,33 @@
+//===-- Implementation header for canonicalizef16 ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE int canonicalizef16(float16 *cx, const float16 *x) {
+  return fputil::canonicalize(*cx, *x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEF16_H
diff --git a/libc/src/__support/math/canonicalizel.h b/libc/src/__support/math/canonicalizel.h
new file mode 100644
index 0000000000000..63b4af24718ad
--- /dev/null
+++ b/libc/src/__support/math/canonicalizel.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for canonicalizel -----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEL_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE int canonicalizel(long double *cx, const long double *x) {
+  return fputil::canonicalize(*cx, *x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_CANONICALIZEL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index b0a2e5a2a381f..6cec4c4518234 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -7,7 +7,7 @@ add_entrypoint_object(
   HDRS
     ../canonicalize.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.canonicalize
 )
 
 add_entrypoint_object(
@@ -17,7 +17,7 @@ add_entrypoint_object(
   HDRS
     ../canonicalizef.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.canonicalizef
 )
 
 add_entrypoint_object(
@@ -27,8 +27,7 @@ add_entrypoint_object(
   HDRS
     ../canonicalizef16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.canonicalizef16
 )
 
 add_entrypoint_object(
@@ -38,8 +37,7 @@ add_entrypoint_object(
   HDRS
     ../canonicalizef128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.canonicalizef128
 )
 
 add_entrypoint_object(
@@ -49,7 +47,7 @@ add_entrypoint_object(
   HDRS
     ../canonicalizel.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.canonicalizel
 )
 
 add_entrypoint_object(
@@ -59,11 +57,7 @@ add_entrypoint_object(
   HDRS
     ../canonicalizebf16.h
   DEPENDS
-    libc.src.__support.common
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.basic_operations
-    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.math.canonicalizebf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/canonicalize.cpp b/libc/src/math/generic/canonicalize.cpp
index d93501d597376..0d2aaf7cc799d 100644
--- a/libc/src/math/generic/canonicalize.cpp
+++ b/libc/src/math/generic/canonicalize.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/canonicalize.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/canonicalize.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, canonicalize, (double *cx, const double *x)) {
-  return fputil::canonicalize(*cx, *x);
+  return math::canonicalize(cx, x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/canonicalizebf16.cpp b/libc/src/math/generic/canonicalizebf16.cpp
index 9cc379060c3de..bad7cef480c38 100644
--- a/libc/src/math/generic/canonicalizebf16.cpp
+++ b/libc/src/math/generic/canonicalizebf16.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/canonicalizebf16.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/canonicalizebf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, canonicalizebf16, (bfloat16 * cx, const bfloat16 *x)) {
-  return fputil::canonicalize(*cx, *x);
+  return math::canonicalizebf16(cx, x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/canonicalizef.cpp b/libc/src/math/generic/canonicalizef.cpp
index 437cf8768458a..77ffba9789c22 100644
--- a/libc/src/math/generic/canonicalizef.cpp
+++ b/libc/src/math/generic/canonicalizef.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/canonicalizef.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/canonicalizef.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, canonicalizef, (float *cx, const float *x)) {
-  return fputil::canonicalize(*cx, *x);
+  return math::canonicalizef(cx, x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/canonicalizef128.cpp b/libc/src/math/generic/canonicalizef128.cpp
index 477d9c740a746..48bce08a555cf 100644
--- a/libc/src/math/generic/canonicalizef128.cpp
+++ b/libc/src/math/generic/canonicalizef128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/canonicalizef128.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/canonicalizef128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, canonicalizef128, (float128 * cx, const float128 *x)) {
-  return fputil::canonicalize(*cx, *x);
+  return math::canonicalizef128(cx, x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/canonicalizef16.cpp b/libc/src/math/generic/canonicalizef16.cpp
index ff32c918793ff..f02e57b0a1070 100644
--- a/libc/src/math/generic/canonicalizef16.cpp
+++ b/libc/src/math/generic/canonicalizef16.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/canonicalizef16.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/canonicalizef16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, canonicalizef16, (float16 * cx, const float16 *x)) {
-  return fputil::canonicalize(*cx, *x);
+  return math::canonicalizef16(cx, x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/canonicalizel.cpp b/libc/src/math/generic/canonicalizel.cpp
index fa2b846216d7b..75b0ba8931bd6 100644
--- a/libc/src/math/generic/canonicalizel.cpp
+++ b/libc/src/math/generic/canonicalizel.cpp
@@ -7,15 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/canonicalizel.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/canonicalizel.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, canonicalizel,
                    (long double *cx, const long double *x)) {
-  return fputil::canonicalize(*cx, *x);
+  return math::canonicalizel(cx, x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 6d04405e75163..0c0a329051bb2 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -27,6 +27,12 @@ add_fp_unittest(
     libc.src.__support.math.atanhf
     libc.src.__support.math.atanhf16
     libc.src.__support.math.bf16addf128
+    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.cbrt
     libc.src.__support.math.cbrtf
     libc.src.__support.math.cos
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 4ed86b7b862b4..4fcbac7b80679 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -73,6 +73,12 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_FP_EQ(0x1.921fb6p+0f16, LIBC_NAMESPACE::shared::acosf16(0.0f16));
   EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::f16sqrtl(1.0L));
   EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::sinf16(0.0f16));
+
+  float16 canonicalizef16_cx = 0.0f16;
+  float16 canonicalizef16_x = 0.0f16;
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef16(&canonicalizef16_cx,
+                                                       &canonicalizef16_x));
+  EXPECT_FP_EQ(0x0p+0f16, canonicalizef16_cx);
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT16
@@ -128,6 +134,12 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sqrtf(0.0f));
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanf(0.0f));
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanhf(0.0f));
+
+  float canonicalizef_cx = 0.0f;
+  float canonicalizef_x = 0.0f;
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef(&canonicalizef_cx,
+                                                     &canonicalizef_x));
+  EXPECT_FP_EQ(0x0p+0f, canonicalizef_cx);
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
@@ -154,6 +166,12 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::tan(0.0));
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogb(1.0));
   EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogb(1.0));
+
+  double canonicalize_cx = 0.0;
+  double canonicalize_x = 0.0;
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalize(&canonicalize_cx,
+                                                    &canonicalize_x));
+  EXPECT_FP_EQ(0.0, canonicalize_cx);
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {
@@ -163,6 +181,12 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogbl(0x1.p+0L));
   EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogbl(1.0L));
   EXPECT_FP_EQ(10.0f, LIBC_NAMESPACE::shared::ffmal(2.0L, 3.0, 4.0L));
+
+  long double canonicalizel_cx = 0.0L;
+  long double canonicalizel_x = 0.0L;
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizel(&canonicalizel_cx,
+                                                     &canonicalizel_x));
+  EXPECT_FP_EQ(0x0p+0L, canonicalizel_cx);
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
@@ -192,6 +216,21 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
 
   EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf128(
                                   float128(2.0), float128(3.0)));
+
+  float128 canonicalizef128_cx = float128(0.0);
+  float128 canonicalizef128_x = float128(0.0);
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef128(&canonicalizef128_cx,
+                                                        &canonicalizef128_x));
+  EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
+
+TEST(LlvmLibcSharedMathTest, AllBFloat16) {
+
+  bfloat16 canonicalizebf16_cx = bfloat16(0.0);
+  bfloat16 canonicalizebf16_x = bfloat16(0.0);
+  EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizebf16(&canonicalizebf16_cx,
+                                                        &canonicalizebf16_x));
+  EXPECT_FP_EQ(bfloat16(0.0), canonicalizebf16_cx);
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 7f537270ddb3c..1f0d4df81275b 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2645,6 +2645,60 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_canonicalize",
+    hdrs = ["src/__support/math/canonicalize.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_canonicalizebf16",
+    hdrs = ["src/__support/math/canonicalizebf16.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+        ":__support_macros_properties_types",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_canonicalizef",
+    hdrs = ["src/__support/math/canonicalizef.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_canonicalizef128",
+    hdrs = ["src/__support/math/canonicalizef128.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_properties_types",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_canonicalizef16",
+    hdrs = ["src/__support/math/canonicalizef16.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_properties_types",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_canonicalizel",
+    hdrs = ["src/__support/math/canonicalizel.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_cbrt",
     hdrs = ["src/__support/math/cbrt.h"],
@@ -4309,15 +4363,40 @@ libc_math_function(
     additional_deps = [":__support_math_bf16addf128"],
 )
 
-libc_math_function(name = "canonicalize")
+libc_math_function(
+    name = "canonicalize",
+    additional_deps = [
+        ":__support_math_canonicalize",
+    ],
+)
 
-libc_math_function(name = "canonicalizef")
+libc_math_function(
+    name = "canonicalizef",
+    additional_deps = [
+        ":__support_math_canonicalizef",
+    ],
+)
 
-libc_math_function(name = "canonicalizel")
+libc_math_function(
+    name = "canonicalizel",
+    additional_deps = [
+        ":__support_math_canonicalizel",
+    ],
+)
 
-libc_math_function(name = "canonicalizef128")
+libc_math_function(
+    name = "canonicalizef128",
+    additional_deps = [
+        ":__support_math_canonicalizef128",
+    ],
+)
 
-libc_math_function(name = "canonicalizef16")
+libc_math_function(
+    name = "canonicalizef16",
+    additional_deps = [
+        ":__support_math_canonicalizef16",
+    ],
+)
 
 libc_math_function(name = "iscanonical")
 



More information about the llvm-commits mailing list