[libc-commits] [libc] [llvm] [libc][math] Refactor fdim family to header-only (PR #182190)

Mohamed Emad via libc-commits libc-commits at lists.llvm.org
Wed Feb 18 16:07:47 PST 2026


https://github.com/hulxv created https://github.com/llvm/llvm-project/pull/182190

Refactors the fdim math family to be header-only.

Closes https://github.com/llvm/llvm-project/issues/182188

Target Functions:
  - fdim
  - fdimbf16
  - fdimf
  - fdimf128
  - fdimf16
  - fdiml

>From 00428911e36d89fc3b12bbfe5f604befa0132f9f Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 19 Feb 2026 02:07:06 +0200
Subject: [PATCH] [libc][math] Refactor fdim family to header-only

Refactored functions:
  - fdim
  - fdimbf16
  - fdimf
  - fdimf128
  - fdimf16
  - fdiml
---
 libc/shared/math.h                            |  6 ++
 libc/shared/math/fdim.h                       | 22 +++++
 libc/shared/math/fdimbf16.h                   | 22 +++++
 libc/shared/math/fdimf.h                      | 22 +++++
 libc/shared/math/fdimf128.h                   | 28 ++++++
 libc/shared/math/fdimf16.h                    | 28 ++++++
 libc/shared/math/fdiml.h                      | 22 +++++
 libc/src/__support/math/CMakeLists.txt        | 51 ++++++++++
 libc/src/__support/math/fdim.h                | 24 +++++
 libc/src/__support/math/fdimbf16.h            | 27 +++++
 libc/src/__support/math/fdimf.h               | 24 +++++
 libc/src/__support/math/fdimf128.h            | 32 ++++++
 libc/src/__support/math/fdimf16.h             | 30 ++++++
 libc/src/__support/math/fdiml.h               | 26 +++++
 libc/src/math/generic/CMakeLists.txt          | 18 ++--
 libc/src/math/generic/fdim.cpp                |  6 +-
 libc/src/math/generic/fdimbf16.cpp            |  7 +-
 libc/src/math/generic/fdimf.cpp               |  6 +-
 libc/src/math/generic/fdimf128.cpp            |  6 +-
 libc/src/math/generic/fdimf16.cpp             |  6 +-
 libc/src/math/generic/fdiml.cpp               |  6 +-
 libc/test/shared/CMakeLists.txt               |  6 ++
 libc/test/shared/shared_math_test.cpp         | 14 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 99 ++++++++++++++++++-
 24 files changed, 496 insertions(+), 42 deletions(-)
 create mode 100644 libc/shared/math/fdim.h
 create mode 100644 libc/shared/math/fdimbf16.h
 create mode 100644 libc/shared/math/fdimf.h
 create mode 100644 libc/shared/math/fdimf128.h
 create mode 100644 libc/shared/math/fdimf16.h
 create mode 100644 libc/shared/math/fdiml.h
 create mode 100644 libc/src/__support/math/fdim.h
 create mode 100644 libc/src/__support/math/fdimbf16.h
 create mode 100644 libc/src/__support/math/fdimf.h
 create mode 100644 libc/src/__support/math/fdimf128.h
 create mode 100644 libc/src/__support/math/fdimf16.h
 create mode 100644 libc/src/__support/math/fdiml.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8a5aca82c6ec3..b13607b6aa7f1 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -83,6 +83,12 @@
 #include "math/f16sqrt.h"
 #include "math/f16sqrtf.h"
 #include "math/f16sqrtl.h"
+#include "math/fdim.h"
+#include "math/fdimbf16.h"
+#include "math/fdimf.h"
+#include "math/fdimf128.h"
+#include "math/fdimf16.h"
+#include "math/fdiml.h"
 #include "math/ffma.h"
 #include "math/ffmal.h"
 #include "math/frexpf.h"
diff --git a/libc/shared/math/fdim.h b/libc/shared/math/fdim.h
new file mode 100644
index 0000000000000..394a1339915e1
--- /dev/null
+++ b/libc/shared/math/fdim.h
@@ -0,0 +1,22 @@
+//===-- Shared fdim 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_FDIM_H
+#define LLVM_LIBC_SHARED_MATH_FDIM_H
+
+#include "src/__support/math/fdim.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdim;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIM_H
diff --git a/libc/shared/math/fdimbf16.h b/libc/shared/math/fdimbf16.h
new file mode 100644
index 0000000000000..a642d770abf2a
--- /dev/null
+++ b/libc/shared/math/fdimbf16.h
@@ -0,0 +1,22 @@
+//===-- Shared fdimbf16 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_FDIMBF16_H
+#define LLVM_LIBC_SHARED_MATH_FDIMBF16_H
+
+#include "src/__support/math/fdimbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdimbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIMBF16_H
diff --git a/libc/shared/math/fdimf.h b/libc/shared/math/fdimf.h
new file mode 100644
index 0000000000000..ba29a0bb0b5dd
--- /dev/null
+++ b/libc/shared/math/fdimf.h
@@ -0,0 +1,22 @@
+//===-- Shared fdimf 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_FDIMF_H
+#define LLVM_LIBC_SHARED_MATH_FDIMF_H
+
+#include "src/__support/math/fdimf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdimf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIMF_H
diff --git a/libc/shared/math/fdimf128.h b/libc/shared/math/fdimf128.h
new file mode 100644
index 0000000000000..4795c0f418517
--- /dev/null
+++ b/libc/shared/math/fdimf128.h
@@ -0,0 +1,28 @@
+//===-- Shared fdimf128 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_FDIMF128_H
+#define LLVM_LIBC_SHARED_MATH_FDIMF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/fdimf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdimf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIMF128_H
diff --git a/libc/shared/math/fdimf16.h b/libc/shared/math/fdimf16.h
new file mode 100644
index 0000000000000..5325022b7e965
--- /dev/null
+++ b/libc/shared/math/fdimf16.h
@@ -0,0 +1,28 @@
+//===-- Shared fdimf16 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_FDIMF16_H
+#define LLVM_LIBC_SHARED_MATH_FDIMF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/fdimf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdimf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIMF16_H
diff --git a/libc/shared/math/fdiml.h b/libc/shared/math/fdiml.h
new file mode 100644
index 0000000000000..ed84cad129274
--- /dev/null
+++ b/libc/shared/math/fdiml.h
@@ -0,0 +1,22 @@
+//===-- Shared fdiml 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_FDIML_H
+#define LLVM_LIBC_SHARED_MATH_FDIML_H
+
+#include "src/__support/math/fdiml.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fdiml;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FDIML_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e21fe8ef0ab93..93b8457744852 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -751,6 +751,57 @@ add_header_library(
     libc.src.__support.FPUtil.generic.add_sub
     libc.src.__support.macros.config
 )
+add_header_library(
+  fdim
+  HDRS
+    fdim.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fdimbf16
+  HDRS
+    fdimbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fdimf
+  HDRS
+    fdimf.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fdimf128
+  HDRS
+    fdimf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fdimf16
+  HDRS
+    fdimf16.h
+  DEPENDS
+    libc.include.llvm-libc-macros.float16_macros
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fdiml
+  HDRS
+    fdiml.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
 
 add_header_library(
   ffmal
diff --git a/libc/src/__support/math/fdim.h b/libc/src/__support/math/fdim.h
new file mode 100644
index 0000000000000..b85d6ed4d20c1
--- /dev/null
+++ b/libc/src/__support/math/fdim.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for fdim --------------------------*- 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_FDIM_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIM_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double fdim(double x, double y) { return fputil::fdim(x, y); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIM_H
diff --git a/libc/src/__support/math/fdimbf16.h b/libc/src/__support/math/fdimbf16.h
new file mode 100644
index 0000000000000..c67da2fa43b4d
--- /dev/null
+++ b/libc/src/__support/math/fdimbf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for fdimbf16 ----------------------*- 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_FDIMBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIMBF16_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE bfloat16 fdimbf16(bfloat16 x, bfloat16 y) {
+  return fputil::fdim(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIMBF16_H
diff --git a/libc/src/__support/math/fdimf.h b/libc/src/__support/math/fdimf.h
new file mode 100644
index 0000000000000..eb9538b4e3707
--- /dev/null
+++ b/libc/src/__support/math/fdimf.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for fdimf -------------------------*- 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_FDIMF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIMF_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float fdimf(float x, float y) { return fputil::fdim(x, y); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIMF_H
diff --git a/libc/src/__support/math/fdimf128.h b/libc/src/__support/math/fdimf128.h
new file mode 100644
index 0000000000000..342b72ffb0745
--- /dev/null
+++ b/libc/src/__support/math/fdimf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for fdimf128 ----------------------*- 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_FDIMF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIMF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float128 fdimf128(float128 x, float128 y) {
+  return fputil::fdim(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIMF128_H
diff --git a/libc/src/__support/math/fdimf16.h b/libc/src/__support/math/fdimf16.h
new file mode 100644
index 0000000000000..7c2b953550f42
--- /dev/null
+++ b/libc/src/__support/math/fdimf16.h
@@ -0,0 +1,30 @@
+//===-- Implementation header for fdimf16 -----------------------*- 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_FDIMF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIMF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float16 fdimf16(float16 x, float16 y) { return fputil::fdim(x, y); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIMF16_H
diff --git a/libc/src/__support/math/fdiml.h b/libc/src/__support/math/fdiml.h
new file mode 100644
index 0000000000000..feb5d35f1a26d
--- /dev/null
+++ b/libc/src/__support/math/fdiml.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for fdiml -------------------------*- 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_FDIML_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FDIML_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE long double fdiml(long double x, long double y) {
+  return fputil::fdim(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FDIML_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 57a29665318a3..a8dd0675d16a2 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3099,7 +3099,7 @@ add_entrypoint_object(
   HDRS
     ../fdim.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fdim
 )
 
 add_entrypoint_object(
@@ -3109,7 +3109,7 @@ add_entrypoint_object(
   HDRS
     ../fdimf.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fdimf
 )
 
 add_entrypoint_object(
@@ -3119,7 +3119,7 @@ add_entrypoint_object(
   HDRS
     ../fdiml.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fdiml
 )
 
 add_entrypoint_object(
@@ -3129,8 +3129,7 @@ add_entrypoint_object(
   HDRS
     ../fdimf16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fdimf16
 )
 
 add_entrypoint_object(
@@ -3140,8 +3139,7 @@ add_entrypoint_object(
   HDRS
     ../fdimf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fdimf128
 )
 
 add_entrypoint_object(
@@ -3151,11 +3149,7 @@ add_entrypoint_object(
   HDRS
     ../fdimbf16.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.fdimbf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fdim.cpp b/libc/src/math/generic/fdim.cpp
index 9ea121f327286..65aca3374e9c4 100644
--- a/libc/src/math/generic/fdim.cpp
+++ b/libc/src/math/generic/fdim.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fdim.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fdim.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, fdim, (double x, double y)) {
-  return fputil::fdim(x, y);
+  return math::fdim(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdimbf16.cpp b/libc/src/math/generic/fdimbf16.cpp
index 0f54055b763e1..e5a6504e98ed7 100644
--- a/libc/src/math/generic/fdimbf16.cpp
+++ b/libc/src/math/generic/fdimbf16.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fdimbf16.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/fdimbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, fdimbf16, (bfloat16 x, bfloat16 y)) {
-  return fputil::fdim(x, y);
+  return math::fdimbf16(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdimf.cpp b/libc/src/math/generic/fdimf.cpp
index 2c6bf7a51dc57..fda284b35e163 100644
--- a/libc/src/math/generic/fdimf.cpp
+++ b/libc/src/math/generic/fdimf.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fdimf.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fdimf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fdimf, (float x, float y)) {
-  return fputil::fdim(x, y);
+  return math::fdimf(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdimf128.cpp b/libc/src/math/generic/fdimf128.cpp
index f8b1499b35b0b..de705aa03a2a5 100644
--- a/libc/src/math/generic/fdimf128.cpp
+++ b/libc/src/math/generic/fdimf128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fdimf128.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fdimf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float128, fdimf128, (float128 x, float128 y)) {
-  return fputil::fdim(x, y);
+  return math::fdimf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdimf16.cpp b/libc/src/math/generic/fdimf16.cpp
index 6eac460a93c71..ce675d66a55cd 100644
--- a/libc/src/math/generic/fdimf16.cpp
+++ b/libc/src/math/generic/fdimf16.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fdimf16.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fdimf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, fdimf16, (float16 x, float16 y)) {
-  return fputil::fdim(x, y);
+  return math::fdimf16(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fdiml.cpp b/libc/src/math/generic/fdiml.cpp
index ebac58ea797ed..9ad2fa78bc593 100644
--- a/libc/src/math/generic/fdiml.cpp
+++ b/libc/src/math/generic/fdiml.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fdiml.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fdiml.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, fdiml, (long double x, long double y)) {
-  return fputil::fdim(x, y);
+  return math::fdiml(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 53ad309af4f30..19e70471b0b42 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -79,6 +79,12 @@ add_fp_unittest(
     libc.src.__support.math.f16sqrt
     libc.src.__support.math.f16sqrtf
     libc.src.__support.math.f16sqrtl
+    libc.src.__support.math.fdim
+    libc.src.__support.math.fdimbf16
+    libc.src.__support.math.fdimf
+    libc.src.__support.math.fdimf128
+    libc.src.__support.math.fdimf16
+    libc.src.__support.math.fdiml
     libc.src.__support.math.ffma
     libc.src.__support.math.ffmal
     libc.src.__support.math.frexpf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 7f2f39ac7a285..e1938bf7fda92 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -88,6 +88,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef16(&canonicalizef16_cx,
                                                        &canonicalizef16_x));
   EXPECT_FP_EQ(0x0p+0f16, canonicalizef16_cx);
+
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::fdimf16(0.0f16, 0.0f16));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT16
@@ -150,6 +152,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef(&canonicalizef_cx,
                                                      &canonicalizef_x));
   EXPECT_FP_EQ(0x0p+0f, canonicalizef_cx);
+
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fdimf(0.0f, 0.0f));
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
@@ -187,6 +191,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalize(&canonicalize_cx,
                                                     &canonicalize_x));
   EXPECT_FP_EQ(0.0, canonicalize_cx);
+
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fdim(0.0, 0.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {
@@ -203,6 +209,8 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizel(&canonicalizel_cx,
                                                      &canonicalizel_x));
   EXPECT_FP_EQ(0x0p+0L, canonicalizel_cx);
+
+  EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::fdiml(0.0L, 0.0L));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
@@ -238,6 +246,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef128(&canonicalizef128_cx,
                                                         &canonicalizef128_x));
   EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
+
+  EXPECT_FP_EQ(float128(0.0),
+               LIBC_NAMESPACE::shared::fdimf128(float128(0.0), float128(0.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
@@ -257,4 +268,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f));
   EXPECT_FP_EQ(bfloat16(10.0),
                LIBC_NAMESPACE::shared::bf16fmaf(2.0f, 3.0f, 4.0f));
+
+  EXPECT_FP_EQ(bfloat16(0.0),
+               LIBC_NAMESPACE::shared::fdimbf16(bfloat16(0.0), bfloat16(0.0)));
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index b90688af7a1d2..92c7140cda920 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3053,6 +3053,63 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fdim",
+    hdrs = ["src/__support/math/fdim.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fdimbf16",
+    hdrs = ["src/__support/math/fdimbf16.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fdimf",
+    hdrs = ["src/__support/math/fdimf.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fdimf128",
+    hdrs = ["src/__support/math/fdimf128.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fdimf16",
+    hdrs = ["src/__support/math/fdimf16.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fdiml",
+    hdrs = ["src/__support/math/fdiml.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_f16fma",
     hdrs = ["src/__support/math/f16fma.h"],
@@ -4975,15 +5032,47 @@ libc_math_function(name = "faddl")
 
 libc_math_function(name = "faddf128")
 
-libc_math_function(name = "fdim")
+libc_math_function(
+    name = "fdim",
+    additional_deps = [
+        ":__support_math_fdim",
+    ],
+)
 
-libc_math_function(name = "fdimf")
+libc_math_function(
+    name = "fdimbf16",
+    additional_deps = [
+        ":__support_math_fdimbf16",
+    ],
+)
 
-libc_math_function(name = "fdiml")
+libc_math_function(
+    name = "fdimf",
+    additional_deps = [
+        ":__support_math_fdimf",
+    ],
+)
 
-libc_math_function(name = "fdimf128")
+libc_math_function(
+    name = "fdiml",
+    additional_deps = [
+        ":__support_math_fdiml",
+    ],
+)
 
-libc_math_function(name = "fdimf16")
+libc_math_function(
+    name = "fdimf128",
+    additional_deps = [
+        ":__support_math_fdimf128",
+    ],
+)
+
+libc_math_function(
+    name = "fdimf16",
+    additional_deps = [
+        ":__support_math_fdimf16",
+    ],
+)
 
 libc_math_function(name = "fdiv")
 



More information about the libc-commits mailing list