[libc] [llvm] [libc][math] Refactor fabs family to header-only (PR #182173)

Mohamed Emad via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 4 13:03:21 PST 2026


https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/182173

>From d4c2bf11fdb8e728eb857fd1bafbb3bdba24a3f5 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 19 Feb 2026 00:31:11 +0200
Subject: [PATCH] [libc][math] Refactor fabs family to header-only

Refactored functions:
  - fabs
  - fabsbf16
  - fabsf
  - fabsf128
  - fabsf16
  - fabsl
---
 libc/shared/math.h                            |  6 ++
 libc/shared/math/fabs.h                       | 22 +++++
 libc/shared/math/fabsbf16.h                   | 22 +++++
 libc/shared/math/fabsf.h                      | 22 +++++
 libc/shared/math/fabsf128.h                   | 28 ++++++
 libc/shared/math/fabsf16.h                    | 28 ++++++
 libc/shared/math/fabsl.h                      | 22 +++++
 libc/src/__support/math/CMakeLists.txt        | 51 ++++++++++
 libc/src/__support/math/fabs.h                | 30 ++++++
 libc/src/__support/math/fabsbf16.h            | 25 +++++
 libc/src/__support/math/fabsf.h               | 30 ++++++
 libc/src/__support/math/fabsf128.h            | 30 ++++++
 libc/src/__support/math/fabsf16.h             | 41 ++++++++
 libc/src/__support/math/fabsl.h               | 24 +++++
 libc/src/math/generic/CMakeLists.txt          | 25 ++---
 libc/src/math/generic/fabs.cpp                | 12 +--
 libc/src/math/generic/fabsbf16.cpp            |  9 +-
 libc/src/math/generic/fabsf.cpp               | 12 +--
 libc/src/math/generic/fabsf128.cpp            |  8 +-
 libc/src/math/generic/fabsf16.cpp             | 17 +---
 libc/src/math/generic/fabsl.cpp               |  6 +-
 libc/test/shared/CMakeLists.txt               |  6 ++
 libc/test/shared/shared_math_test.cpp         | 12 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 99 ++++++++++++++++++-
 24 files changed, 515 insertions(+), 72 deletions(-)
 create mode 100644 libc/shared/math/fabs.h
 create mode 100644 libc/shared/math/fabsbf16.h
 create mode 100644 libc/shared/math/fabsf.h
 create mode 100644 libc/shared/math/fabsf128.h
 create mode 100644 libc/shared/math/fabsf16.h
 create mode 100644 libc/shared/math/fabsl.h
 create mode 100644 libc/src/__support/math/fabs.h
 create mode 100644 libc/src/__support/math/fabsbf16.h
 create mode 100644 libc/src/__support/math/fabsf.h
 create mode 100644 libc/src/__support/math/fabsf128.h
 create mode 100644 libc/src/__support/math/fabsf16.h
 create mode 100644 libc/src/__support/math/fabsl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 8a5aca82c6ec3..b7b27f21a4f50 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/fabs.h"
+#include "math/fabsbf16.h"
+#include "math/fabsf.h"
+#include "math/fabsf128.h"
+#include "math/fabsf16.h"
+#include "math/fabsl.h"
 #include "math/ffma.h"
 #include "math/ffmal.h"
 #include "math/frexpf.h"
diff --git a/libc/shared/math/fabs.h b/libc/shared/math/fabs.h
new file mode 100644
index 0000000000000..9c4c999a71a5a
--- /dev/null
+++ b/libc/shared/math/fabs.h
@@ -0,0 +1,22 @@
+//===-- Shared fabs 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_FABS_H
+#define LLVM_LIBC_SHARED_MATH_FABS_H
+
+#include "src/__support/math/fabs.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fabs;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FABS_H
diff --git a/libc/shared/math/fabsbf16.h b/libc/shared/math/fabsbf16.h
new file mode 100644
index 0000000000000..3ded6e83086fd
--- /dev/null
+++ b/libc/shared/math/fabsbf16.h
@@ -0,0 +1,22 @@
+//===-- Shared fabsbf16 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_FABSBF16_H
+#define LLVM_LIBC_SHARED_MATH_FABSBF16_H
+
+#include "src/__support/math/fabsbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fabsbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FABSBF16_H
diff --git a/libc/shared/math/fabsf.h b/libc/shared/math/fabsf.h
new file mode 100644
index 0000000000000..e474608595571
--- /dev/null
+++ b/libc/shared/math/fabsf.h
@@ -0,0 +1,22 @@
+//===-- Shared fabsf 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_FABSF_H
+#define LLVM_LIBC_SHARED_MATH_FABSF_H
+
+#include "src/__support/math/fabsf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fabsf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FABSF_H
diff --git a/libc/shared/math/fabsf128.h b/libc/shared/math/fabsf128.h
new file mode 100644
index 0000000000000..16ea29fcdb792
--- /dev/null
+++ b/libc/shared/math/fabsf128.h
@@ -0,0 +1,28 @@
+//===-- Shared fabsf128 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_FABSF128_H
+#define LLVM_LIBC_SHARED_MATH_FABSF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/math/fabsf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fabsf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_FABSF128_H
diff --git a/libc/shared/math/fabsf16.h b/libc/shared/math/fabsf16.h
new file mode 100644
index 0000000000000..38584f4d98b1d
--- /dev/null
+++ b/libc/shared/math/fabsf16.h
@@ -0,0 +1,28 @@
+//===-- Shared fabsf16 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_FABSF16_H
+#define LLVM_LIBC_SHARED_MATH_FABSF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/fabsf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fabsf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_FABSF16_H
diff --git a/libc/shared/math/fabsl.h b/libc/shared/math/fabsl.h
new file mode 100644
index 0000000000000..b0c23a61b555a
--- /dev/null
+++ b/libc/shared/math/fabsl.h
@@ -0,0 +1,22 @@
+//===-- Shared fabsl 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_FABSL_H
+#define LLVM_LIBC_SHARED_MATH_FABSL_H
+
+#include "src/__support/math/fabsl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fabsl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FABSL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e21fe8ef0ab93..a315b06f7948b 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(
+  fabs
+  HDRS
+    fabs.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fabsbf16
+  HDRS
+    fabsbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fabsf
+  HDRS
+    fabsf.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fabsf128
+  HDRS
+    fabsf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fabsf16
+  HDRS
+    fabsf16.h
+  DEPENDS
+    libc.include.llvm-libc-macros.float16_macros
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+add_header_library(
+  fabsl
+  HDRS
+    fabsl.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
 
 add_header_library(
   ffmal
diff --git a/libc/src/__support/math/fabs.h b/libc/src/__support/math/fabs.h
new file mode 100644
index 0000000000000..28fef798dfcce
--- /dev/null
+++ b/libc/src/__support/math/fabs.h
@@ -0,0 +1,30 @@
+//===-- Implementation header for fabs --------------------------*- 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_FABS_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FABS_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double fabs(double x) {
+#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
+  return __builtin_fabs(x);
+#else
+  return fputil::abs(x);
+#endif
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABS_H
diff --git a/libc/src/__support/math/fabsbf16.h b/libc/src/__support/math/fabsbf16.h
new file mode 100644
index 0000000000000..27c9c5666f710
--- /dev/null
+++ b/libc/src/__support/math/fabsbf16.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for fabsbf16 ----------------------*- 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_FABSBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FABSBF16_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 fabsbf16(bfloat16 x) { return fputil::abs(x); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABSBF16_H
diff --git a/libc/src/__support/math/fabsf.h b/libc/src/__support/math/fabsf.h
new file mode 100644
index 0000000000000..5a6c5dbbbc59d
--- /dev/null
+++ b/libc/src/__support/math/fabsf.h
@@ -0,0 +1,30 @@
+//===-- Implementation header for fabsf -------------------------*- 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_FABSF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FABSF_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float fabsf(float x) {
+#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
+  return __builtin_fabsf(x);
+#else
+  return fputil::abs(x);
+#endif
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABSF_H
diff --git a/libc/src/__support/math/fabsf128.h b/libc/src/__support/math/fabsf128.h
new file mode 100644
index 0000000000000..4f46f206ca7a5
--- /dev/null
+++ b/libc/src/__support/math/fabsf128.h
@@ -0,0 +1,30 @@
+//===-- Implementation header for fabsf128 ----------------------*- 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_FABSF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FABSF128_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 fabsf128(float128 x) { return fputil::abs(x); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABSF128_H
diff --git a/libc/src/__support/math/fabsf16.h b/libc/src/__support/math/fabsf16.h
new file mode 100644
index 0000000000000..d13fbb0dfeea7
--- /dev/null
+++ b/libc/src/__support/math/fabsf16.h
@@ -0,0 +1,41 @@
+//===-- Implementation header for fabsf16 -----------------------*- 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_FABSF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FABSF16_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"
+#include "src/__support/macros/properties/architectures.h"
+#include "src/__support/macros/properties/compiler.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float16 fabsf16(float16 x) {
+  // For x86, GCC generates better code from the generic implementation.
+  // https://godbolt.org/z/K9orM4hTa
+#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) &&                                 \
+    !(defined(LIBC_TARGET_ARCH_IS_X86) && defined(LIBC_COMPILER_IS_GCC))
+  return __builtin_fabsf16(x);
+#else
+  return fputil::abs(x);
+#endif
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABSF16_H
diff --git a/libc/src/__support/math/fabsl.h b/libc/src/__support/math/fabsl.h
new file mode 100644
index 0000000000000..1290f0a231e62
--- /dev/null
+++ b/libc/src/__support/math/fabsl.h
@@ -0,0 +1,24 @@
+//===-- Implementation header for fabsl -------------------------*- 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_FABSL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FABSL_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE long double fabsl(long double x) { return fputil::abs(x); }
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FABSL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 57a29665318a3..5ff7265ea17d4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -491,9 +491,7 @@ add_entrypoint_object(
   HDRS
     ../fabs.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
-  FLAGS
-    MISC_MATH_BASIC_OPS_OPT
+    libc.src.__support.math.fabs
 )
 
 add_entrypoint_object(
@@ -503,9 +501,7 @@ add_entrypoint_object(
   HDRS
     ../fabsf.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
-  FLAGS
-    MISC_MATH_BASIC_OPS_OPT
+    libc.src.__support.math.fabsf
 )
 
 add_entrypoint_object(
@@ -515,7 +511,7 @@ add_entrypoint_object(
   HDRS
     ../fabsl.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fabsl
 )
 
 add_entrypoint_object(
@@ -525,12 +521,7 @@ add_entrypoint_object(
   HDRS
     ../fabsf16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.basic_operations
-    libc.src.__support.macros.properties.architectures
-    libc.src.__support.macros.properties.compiler
-  FLAGS
-    MISC_MATH_BASIC_OPS_OPT
+    libc.src.__support.math.fabsf16
 )
 
 add_entrypoint_object(
@@ -540,8 +531,7 @@ add_entrypoint_object(
   HDRS
     ../fabsf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fabsf128
 )
 
 add_entrypoint_object(
@@ -551,10 +541,7 @@ add_entrypoint_object(
   HDRS
     ../fabsbf16.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.fabsbf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fabs.cpp b/libc/src/math/generic/fabs.cpp
index 55fa958cd7c00..37d5acf0292f9 100644
--- a/libc/src/math/generic/fabs.cpp
+++ b/libc/src/math/generic/fabs.cpp
@@ -7,18 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fabs.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fabs.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(double, fabs, (double x)) {
-#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
-  return __builtin_fabs(x);
-#else
-  return fputil::abs(x);
-#endif
-}
+LLVM_LIBC_FUNCTION(double, fabs, (double x)) { return math::fabs(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fabsbf16.cpp b/libc/src/math/generic/fabsbf16.cpp
index ea39719e82d42..1c311adacadd3 100644
--- a/libc/src/math/generic/fabsbf16.cpp
+++ b/libc/src/math/generic/fabsbf16.cpp
@@ -7,13 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fabsbf16.h"
-
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fabsbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(bfloat16, fabsbf16, (bfloat16 x)) { return fputil::abs(x); }
+LLVM_LIBC_FUNCTION(bfloat16, fabsbf16, (bfloat16 x)) {
+  return math::fabsbf16(x);
+}
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fabsf.cpp b/libc/src/math/generic/fabsf.cpp
index 2ba18d09bbd5b..dfad15b76749e 100644
--- a/libc/src/math/generic/fabsf.cpp
+++ b/libc/src/math/generic/fabsf.cpp
@@ -7,18 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fabsf.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fabsf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float, fabsf, (float x)) {
-#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
-  return __builtin_fabsf(x);
-#else
-  return fputil::abs(x);
-#endif
-}
+LLVM_LIBC_FUNCTION(float, fabsf, (float x)) { return math::fabsf(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fabsf128.cpp b/libc/src/math/generic/fabsf128.cpp
index 795d907e881f6..1f54d141600f5 100644
--- a/libc/src/math/generic/fabsf128.cpp
+++ b/libc/src/math/generic/fabsf128.cpp
@@ -7,12 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fabsf128.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fabsf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float128, fabsf128, (float128 x)) { return fputil::abs(x); }
+LLVM_LIBC_FUNCTION(float128, fabsf128, (float128 x)) {
+  return math::fabsf128(x);
+}
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fabsf16.cpp b/libc/src/math/generic/fabsf16.cpp
index 02e11330db718..8e84f78ed564e 100644
--- a/libc/src/math/generic/fabsf16.cpp
+++ b/libc/src/math/generic/fabsf16.cpp
@@ -7,23 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fabsf16.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/properties/architectures.h"
-#include "src/__support/macros/properties/compiler.h"
+#include "src/__support/math/fabsf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float16, fabsf16, (float16 x)) {
-  // For x86, GCC generates better code from the generic implementation.
-  // https://godbolt.org/z/K9orM4hTa
-#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) &&                                 \
-    !(defined(LIBC_TARGET_ARCH_IS_X86) && defined(LIBC_COMPILER_IS_GCC))
-  return __builtin_fabsf16(x);
-#else
-  return fputil::abs(x);
-#endif
-}
+LLVM_LIBC_FUNCTION(float16, fabsf16, (float16 x)) { return math::fabsf16(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fabsl.cpp b/libc/src/math/generic/fabsl.cpp
index ea860549abd1e..6905129e810f5 100644
--- a/libc/src/math/generic/fabsl.cpp
+++ b/libc/src/math/generic/fabsl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fabsl.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fabsl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, fabsl, (long double x)) {
-  return fputil::abs(x);
+  return math::fabsl(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 53ad309af4f30..2636cf954fb75 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.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.fabsl
     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..93b3960701b27 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::fabsf16(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::fabsf(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::fabs(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::fabsl(0.0L));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
@@ -238,6 +246,8 @@ 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::fabsf128(float128(0.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
@@ -257,4 +267,6 @@ 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::fabsbf16(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..66f647faa5854 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_fabs",
+    hdrs = ["src/__support/math/fabs.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fabsbf16",
+    hdrs = ["src/__support/math/fabsbf16.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fabsf",
+    hdrs = ["src/__support/math/fabsf.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fabsf128",
+    hdrs = ["src/__support/math/fabsf128.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fabsf16",
+    hdrs = ["src/__support/math/fabsf16.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fabsl",
+    hdrs = ["src/__support/math/fabsl.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_f16fma",
     hdrs = ["src/__support/math/f16fma.h"],
@@ -4959,15 +5016,47 @@ libc_math_function(name = "f16subf128")
 
 libc_math_function(name = "f16subl")
 
-libc_math_function(name = "fabs")
+libc_math_function(
+    name = "fabs",
+    additional_deps = [
+        ":__support_math_fabs",
+    ],
+)
 
-libc_math_function(name = "fabsf")
+libc_math_function(
+    name = "fabsbf16",
+    additional_deps = [
+        ":__support_math_fabsbf16",
+    ],
+)
 
-libc_math_function(name = "fabsl")
+libc_math_function(
+    name = "fabsf",
+    additional_deps = [
+        ":__support_math_fabsf",
+    ],
+)
 
-libc_math_function(name = "fabsf128")
+libc_math_function(
+    name = "fabsl",
+    additional_deps = [
+        ":__support_math_fabsl",
+    ],
+)
 
-libc_math_function(name = "fabsf16")
+libc_math_function(
+    name = "fabsf128",
+    additional_deps = [
+        ":__support_math_fabsf128",
+    ],
+)
+
+libc_math_function(
+    name = "fabsf16",
+    additional_deps = [
+        ":__support_math_fabsf16",
+    ],
+)
 
 libc_math_function(name = "fadd")
 



More information about the llvm-commits mailing list