[libc-commits] [libc] [llvm] [libc][math] Refactor fabs family to header-only (PR #182173)
via libc-commits
libc-commits at lists.llvm.org
Fri Apr 24 07:48:28 PDT 2026
https://github.com/hulxv updated https://github.com/llvm/llvm-project/pull/182173
>From 738fbe76c95543794b470ec343a11f40022f5191 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 19 Feb 2026 00:31:11 +0200
Subject: [PATCH 1/6] [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 | 6 ++
.../llvm-project-overlay/libc/BUILD.bazel | 93 ++++++++++++++++++-
24 files changed, 503 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 e73440121c0be..85ba88ea96403 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -133,6 +133,12 @@
#include "math/f16subf.h"
#include "math/f16subf128.h"
#include "math/f16subl.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/fadd.h"
#include "math/faddf128.h"
#include "math/faddl.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 0c62bfaf2ad6b..d59cc4fb285f9 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1217,6 +1217,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(
fadd
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 1774ead284769..7691ab3c3c868 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -470,9 +470,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(
@@ -482,9 +480,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(
@@ -494,7 +490,7 @@ add_entrypoint_object(
HDRS
../fabsl.h
DEPENDS
- libc.src.__support.FPUtil.basic_operations
+ libc.src.__support.math.fabsl
)
add_entrypoint_object(
@@ -504,12 +500,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(
@@ -519,8 +510,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(
@@ -530,10 +520,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 8c233e8326b77..061a439b0f9bf 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -130,6 +130,12 @@ add_fp_unittest(
libc.src.__support.math.f16subf
libc.src.__support.math.f16subf128
libc.src.__support.math.f16subl
+ libc.src.__support.math.fabs
+ libc.src.__support.math.fabsbf16
+ libc.src.__support.math.fabsf
+ libc.src.__support.math.fabsf128
+ libc.src.__support.math.fabsf16
+ libc.src.__support.math.fabsl
libc.src.__support.math.fadd
libc.src.__support.math.faddf128
libc.src.__support.math.faddl
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index f5a7d218b1592..029400925af8b 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -75,6 +75,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::ceilf16(0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16));
+ EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsf16(0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::fdimf16(0.0f16, 0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::floorf16(0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::fmaxf16(0.0f16, 0.0f16));
@@ -177,6 +178,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::ceilf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f));
+ EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fdimf(0.0f, 0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::floorf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fmaxf(0.0f, 0.0f));
@@ -259,6 +261,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::ceil(0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::copysign(0.0, 0.0));
+ EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabs(0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fadd(0.0, 0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fdim(0.0, 0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::floor(0.0));
@@ -320,6 +323,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::ceill(0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::daddl(0.0L, 0.0L));
+ EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsl(0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::faddl(0.0L, 0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::fdiml(0.0L, 0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::floorl(0.0L));
@@ -403,6 +407,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
float128(0.0), float128(0.0)));
EXPECT_FP_EQ(float128(0.0),
LIBC_NAMESPACE::shared::daddf128(float128(0.0), float128(0.0)));
+ EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsf128(float128(0.0)));
EXPECT_FP_EQ(float128(0.0),
LIBC_NAMESPACE::shared::faddf128(float128(0.0), float128(0.0)));
EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::floorf128(float128(0.0)));
@@ -468,6 +473,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::copysignbf16(
bfloat16(0.0), bfloat16(0.0)));
+ EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsbf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::floorbf16(bfloat16(0.0)));
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 6fa20fb0207bc..869a6cbb666fa 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4316,6 +4316,65 @@ 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",
+ ":__support_macros_properties_compiler",
+ ":__support_macros_properties_optimization",
+ ":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_fdim",
hdrs = ["src/__support/math/fdim.h"],
@@ -7471,21 +7530,45 @@ libc_math_function(
],
)
-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 = "fabsf",
+ additional_deps = [
+ ":__support_math_fabsf",
+ ],
+)
-libc_math_function(name = "fabsl")
+libc_math_function(
+ name = "fabsl",
+ additional_deps = [
+ ":__support_math_fabsl",
+ ],
+)
libc_math_function(
name = "fabsf128",
+ additional_deps = [
+ ":__support_math_fabsf128",
+ ],
)
libc_math_function(
name = "fabsf16",
additional_deps = [
- ":__support_macros_properties_architectures",
- ":__support_macros_properties_compiler",
+ ":__support_math_fabsf16",
],
)
>From 1b78acfc127461f164771fd3d4f31c3cf3c96d03 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sat, 14 Mar 2026 02:23:36 +0200
Subject: [PATCH 2/6] fix
---
libc/shared/math/fabs.h | 1 +
libc/shared/math/fabsbf16.h | 1 +
libc/shared/math/fabsf.h | 1 +
libc/shared/math/fabsf128.h | 1 +
libc/shared/math/fabsf16.h | 1 +
libc/shared/math/fabsl.h | 1 +
libc/src/__support/math/CMakeLists.txt | 6 ++++++
libc/src/__support/math/fabs.h | 1 -
libc/src/__support/math/fabsbf16.h | 1 -
libc/src/__support/math/fabsf.h | 1 -
libc/src/__support/math/fabsf128.h | 1 -
libc/src/__support/math/fabsf16.h | 1 -
libc/src/__support/math/fabsl.h | 1 -
13 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/libc/shared/math/fabs.h b/libc/shared/math/fabs.h
index 9c4c999a71a5a..f781d4755cfc6 100644
--- a/libc/shared/math/fabs.h
+++ b/libc/shared/math/fabs.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_MATH_FABS_H
#define LLVM_LIBC_SHARED_MATH_FABS_H
+#include "shared/libc_common.h"
#include "src/__support/math/fabs.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/math/fabsbf16.h b/libc/shared/math/fabsbf16.h
index 3ded6e83086fd..7e9fcec2e25f6 100644
--- a/libc/shared/math/fabsbf16.h
+++ b/libc/shared/math/fabsbf16.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_MATH_FABSBF16_H
#define LLVM_LIBC_SHARED_MATH_FABSBF16_H
+#include "shared/libc_common.h"
#include "src/__support/math/fabsbf16.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/math/fabsf.h b/libc/shared/math/fabsf.h
index e474608595571..3be98d5d66ff2 100644
--- a/libc/shared/math/fabsf.h
+++ b/libc/shared/math/fabsf.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_MATH_FABSF_H
#define LLVM_LIBC_SHARED_MATH_FABSF_H
+#include "shared/libc_common.h"
#include "src/__support/math/fabsf.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/math/fabsf128.h b/libc/shared/math/fabsf128.h
index 16ea29fcdb792..deea5f04b6267 100644
--- a/libc/shared/math/fabsf128.h
+++ b/libc/shared/math/fabsf128.h
@@ -10,6 +10,7 @@
#define LLVM_LIBC_SHARED_MATH_FABSF128_H
#include "include/llvm-libc-types/float128.h"
+#include "shared/libc_common.h"
#ifdef LIBC_TYPES_HAS_FLOAT128
diff --git a/libc/shared/math/fabsf16.h b/libc/shared/math/fabsf16.h
index 38584f4d98b1d..f4a1757e16b62 100644
--- a/libc/shared/math/fabsf16.h
+++ b/libc/shared/math/fabsf16.h
@@ -10,6 +10,7 @@
#define LLVM_LIBC_SHARED_MATH_FABSF16_H
#include "include/llvm-libc-macros/float16-macros.h"
+#include "shared/libc_common.h"
#ifdef LIBC_TYPES_HAS_FLOAT16
diff --git a/libc/shared/math/fabsl.h b/libc/shared/math/fabsl.h
index b0c23a61b555a..869a18a14ebf8 100644
--- a/libc/shared/math/fabsl.h
+++ b/libc/shared/math/fabsl.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_MATH_FABSL_H
#define LLVM_LIBC_SHARED_MATH_FABSL_H
+#include "shared/libc_common.h"
#include "src/__support/math/fabsl.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index d59cc4fb285f9..ff700bfb8f947 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1217,6 +1217,7 @@ add_header_library(
libc.src.__support.FPUtil.generic.add_sub
libc.src.__support.macros.config
)
+
add_header_library(
fabs
HDRS
@@ -1225,6 +1226,7 @@ add_header_library(
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
)
+
add_header_library(
fabsbf16
HDRS
@@ -1234,6 +1236,7 @@ add_header_library(
libc.src.__support.FPUtil.bfloat16
libc.src.__support.macros.config
)
+
add_header_library(
fabsf
HDRS
@@ -1242,6 +1245,7 @@ add_header_library(
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
)
+
add_header_library(
fabsf128
HDRS
@@ -1251,6 +1255,7 @@ add_header_library(
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
)
+
add_header_library(
fabsf16
HDRS
@@ -1260,6 +1265,7 @@ add_header_library(
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
)
+
add_header_library(
fabsl
HDRS
diff --git a/libc/src/__support/math/fabs.h b/libc/src/__support/math/fabs.h
index 28fef798dfcce..89f3109ce5642 100644
--- a/libc/src/__support/math/fabs.h
+++ b/libc/src/__support/math/fabs.h
@@ -13,7 +13,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE double fabs(double x) {
diff --git a/libc/src/__support/math/fabsbf16.h b/libc/src/__support/math/fabsbf16.h
index 27c9c5666f710..047989e0bee4e 100644
--- a/libc/src/__support/math/fabsbf16.h
+++ b/libc/src/__support/math/fabsbf16.h
@@ -14,7 +14,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE bfloat16 fabsbf16(bfloat16 x) { return fputil::abs(x); }
diff --git a/libc/src/__support/math/fabsf.h b/libc/src/__support/math/fabsf.h
index 5a6c5dbbbc59d..96521c645ad12 100644
--- a/libc/src/__support/math/fabsf.h
+++ b/libc/src/__support/math/fabsf.h
@@ -13,7 +13,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE float fabsf(float x) {
diff --git a/libc/src/__support/math/fabsf128.h b/libc/src/__support/math/fabsf128.h
index 4f46f206ca7a5..713234c4da076 100644
--- a/libc/src/__support/math/fabsf128.h
+++ b/libc/src/__support/math/fabsf128.h
@@ -17,7 +17,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE float128 fabsf128(float128 x) { return fputil::abs(x); }
diff --git a/libc/src/__support/math/fabsf16.h b/libc/src/__support/math/fabsf16.h
index d13fbb0dfeea7..9e91d798c4583 100644
--- a/libc/src/__support/math/fabsf16.h
+++ b/libc/src/__support/math/fabsf16.h
@@ -19,7 +19,6 @@
#include "src/__support/macros/properties/compiler.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE float16 fabsf16(float16 x) {
diff --git a/libc/src/__support/math/fabsl.h b/libc/src/__support/math/fabsl.h
index 1290f0a231e62..cb0ad024ab8da 100644
--- a/libc/src/__support/math/fabsl.h
+++ b/libc/src/__support/math/fabsl.h
@@ -13,7 +13,6 @@
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
-
namespace math {
LIBC_INLINE long double fabsl(long double x) { return fputil::abs(x); }
>From 66fb6f9b13f1b892df367ccefa0b0aac086bc923 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Wed, 22 Apr 2026 23:10:54 +0200
Subject: [PATCH 3/6] constexpr
---
libc/shared/math/fabsf128.h | 2 +-
libc/shared/math/fabsf16.h | 2 +-
libc/src/__support/FPUtil/BasicOperations.h | 2 +-
libc/src/__support/math/CMakeLists.txt | 12 ++++++++++++
libc/src/__support/math/fabs.h | 2 +-
libc/src/__support/math/fabsbf16.h | 2 +-
libc/src/__support/math/fabsf.h | 2 +-
libc/src/__support/math/fabsf128.h | 2 +-
libc/src/__support/math/fabsf16.h | 4 +++-
libc/src/__support/math/fabsl.h | 2 +-
libc/test/shared/CMakeLists.txt | 6 ++++++
libc/test/shared/shared_math_constexpr_test.cpp | 6 ++++++
12 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/libc/shared/math/fabsf128.h b/libc/shared/math/fabsf128.h
index deea5f04b6267..02b503642019e 100644
--- a/libc/shared/math/fabsf128.h
+++ b/libc/shared/math/fabsf128.h
@@ -10,10 +10,10 @@
#define LLVM_LIBC_SHARED_MATH_FABSF128_H
#include "include/llvm-libc-types/float128.h"
-#include "shared/libc_common.h"
#ifdef LIBC_TYPES_HAS_FLOAT128
+#include "shared/libc_common.h"
#include "src/__support/math/fabsf128.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/math/fabsf16.h b/libc/shared/math/fabsf16.h
index f4a1757e16b62..efbb6ad058641 100644
--- a/libc/shared/math/fabsf16.h
+++ b/libc/shared/math/fabsf16.h
@@ -10,10 +10,10 @@
#define LLVM_LIBC_SHARED_MATH_FABSF16_H
#include "include/llvm-libc-macros/float16-macros.h"
-#include "shared/libc_common.h"
#ifdef LIBC_TYPES_HAS_FLOAT16
+#include "shared/libc_common.h"
#include "src/__support/math/fabsf16.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index ca7be6676630a..ee5c90a8f290f 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -26,7 +26,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace fputil {
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
-LIBC_INLINE T abs(T x) {
+LIBC_INLINE constexpr T abs(T x) {
return FPBits<T>(x).abs().get_val();
}
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ff700bfb8f947..a4f1fb5a18aea 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1225,6 +1225,8 @@ add_header_library(
DEPENDS
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
+ FLAGS
+ MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
@@ -1235,6 +1237,8 @@ add_header_library(
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.bfloat16
libc.src.__support.macros.config
+ FLAGS
+ MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
@@ -1244,6 +1248,8 @@ add_header_library(
DEPENDS
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
+ FLAGS
+ MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
@@ -1254,6 +1260,8 @@ add_header_library(
libc.include.llvm-libc-types.float128
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
+ FLAGS
+ MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
@@ -1264,6 +1272,8 @@ add_header_library(
libc.include.llvm-libc-macros.float16_macros
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
+ FLAGS
+ MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
@@ -1273,6 +1283,8 @@ add_header_library(
DEPENDS
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
+ FLAGS
+ MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
diff --git a/libc/src/__support/math/fabs.h b/libc/src/__support/math/fabs.h
index 89f3109ce5642..ef63b40d3b48d 100644
--- a/libc/src/__support/math/fabs.h
+++ b/libc/src/__support/math/fabs.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE double fabs(double x) {
+LIBC_INLINE constexpr double fabs(double x) {
#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
return __builtin_fabs(x);
#else
diff --git a/libc/src/__support/math/fabsbf16.h b/libc/src/__support/math/fabsbf16.h
index 047989e0bee4e..fd6593b36c304 100644
--- a/libc/src/__support/math/fabsbf16.h
+++ b/libc/src/__support/math/fabsbf16.h
@@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE bfloat16 fabsbf16(bfloat16 x) { return fputil::abs(x); }
+LIBC_INLINE constexpr bfloat16 fabsbf16(bfloat16 x) { return fputil::abs(x); }
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/fabsf.h b/libc/src/__support/math/fabsf.h
index 96521c645ad12..5fe963c7efd17 100644
--- a/libc/src/__support/math/fabsf.h
+++ b/libc/src/__support/math/fabsf.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float fabsf(float x) {
+LIBC_INLINE constexpr float fabsf(float x) {
#ifdef __LIBC_MISC_MATH_BASIC_OPS_OPT
return __builtin_fabsf(x);
#else
diff --git a/libc/src/__support/math/fabsf128.h b/libc/src/__support/math/fabsf128.h
index 713234c4da076..ed9f9eba8116d 100644
--- a/libc/src/__support/math/fabsf128.h
+++ b/libc/src/__support/math/fabsf128.h
@@ -19,7 +19,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float128 fabsf128(float128 x) { return fputil::abs(x); }
+LIBC_INLINE constexpr float128 fabsf128(float128 x) { return fputil::abs(x); }
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/math/fabsf16.h b/libc/src/__support/math/fabsf16.h
index 9e91d798c4583..26634361adc73 100644
--- a/libc/src/__support/math/fabsf16.h
+++ b/libc/src/__support/math/fabsf16.h
@@ -21,7 +21,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE float16 fabsf16(float16 x) {
+LIBC_INLINE constexpr float16 fabsf16(float16 x) {
+ if (cpp::is_constant_evaluated())
+ return fputil::abs(x);
// For x86, GCC generates better code from the generic implementation.
// https://godbolt.org/z/K9orM4hTa
#if defined(__LIBC_MISC_MATH_BASIC_OPS_OPT) && \
diff --git a/libc/src/__support/math/fabsl.h b/libc/src/__support/math/fabsl.h
index cb0ad024ab8da..9924262af8077 100644
--- a/libc/src/__support/math/fabsl.h
+++ b/libc/src/__support/math/fabsl.h
@@ -15,7 +15,7 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE long double fabsl(long double x) { return fputil::abs(x); }
+LIBC_INLINE constexpr long double fabsl(long double x) { return fputil::abs(x); }
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 061a439b0f9bf..dd744ea35f24d 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -302,6 +302,12 @@ add_fp_unittest(
libc.src.__support.math.floorf128
libc.src.__support.math.floorf16
libc.src.__support.math.floorl
+ libc.src.__support.math.fabs
+ libc.src.__support.math.fabsbf16
+ libc.src.__support.math.fabsf
+ libc.src.__support.math.fabsf128
+ libc.src.__support.math.fabsf16
+ libc.src.__support.math.fabsl
libc.src.__support.math.log
libc.src.__support.math.logbbf16
)
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 5e69d981d0473..014bacca271fb 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -17,6 +17,7 @@
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));
static_assert(1.0 == LIBC_NAMESPACE::shared::floor(1.2));
static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
@@ -26,6 +27,7 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
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));
static_assert(0.0f == LIBC_NAMESPACE::shared::floorf(0.0f));
//===----------------------------------------------------------------------===//
@@ -36,6 +38,7 @@ static_assert(0.0f == LIBC_NAMESPACE::shared::floorf(0.0f));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::ceilf16(0.0f16));
static_assert(0.0f16 == LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16));
+static_assert(1.0f16 == LIBC_NAMESPACE::shared::fabsf16(-1.0f16));
static_assert(3.0f16 == LIBC_NAMESPACE::shared::floorf16(3.7f16));
#endif // LIBC_TYPES_HAS_FLOAT16
@@ -49,6 +52,7 @@ static_assert(3.0f16 == LIBC_NAMESPACE::shared::floorf16(3.7f16));
static_assert(0.0L == LIBC_NAMESPACE::shared::ceill(0.0L));
static_assert(0.0L == LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L));
+static_assert(1.0L == LIBC_NAMESPACE::shared::fabsl(-1.0L));
static_assert(0.0L == LIBC_NAMESPACE::shared::floorl(0.0L));
#endif
@@ -60,6 +64,7 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::floorl(0.0L));
#ifdef LIBC_TYPES_HAS_FLOAT128
static_assert(float128(0.0) == LIBC_NAMESPACE::shared::ceilf128(float128(0.0)));
+static_assert(float128(1.0) == LIBC_NAMESPACE::shared::fabsf128(float128(-1.0)));
static_assert(float128(0.0) ==
LIBC_NAMESPACE::shared::copysignf128(float128(0.0),
float128(0.0)));
@@ -75,6 +80,7 @@ static_assert(float128(0.0) ==
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) == LIBC_NAMESPACE::shared::fabsbf16(bfloat16(-1.0)));
static_assert(bfloat16(0.0) ==
LIBC_NAMESPACE::shared::copysignbf16(bfloat16(0.0),
bfloat16(0.0)));
>From acec535e421652d1a728b17b7461368c8e399380 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Thu, 23 Apr 2026 00:00:39 +0200
Subject: [PATCH 4/6] fix the ci
---
libc/test/shared/shared_math_constexpr_test.cpp | 6 ++++--
libc/test/shared/shared_math_test.cpp | 10 +++++-----
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 7 ++++++-
3 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index 014bacca271fb..48221d0b8ebc6 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -64,7 +64,8 @@ static_assert(0.0L == LIBC_NAMESPACE::shared::floorl(0.0L));
#ifdef LIBC_TYPES_HAS_FLOAT128
static_assert(float128(0.0) == LIBC_NAMESPACE::shared::ceilf128(float128(0.0)));
-static_assert(float128(1.0) == LIBC_NAMESPACE::shared::fabsf128(float128(-1.0)));
+static_assert(float128(1.0) ==
+ LIBC_NAMESPACE::shared::fabsf128(float128(-1.0)));
static_assert(float128(0.0) ==
LIBC_NAMESPACE::shared::copysignf128(float128(0.0),
float128(0.0)));
@@ -80,7 +81,8 @@ static_assert(float128(0.0) ==
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) == LIBC_NAMESPACE::shared::fabsbf16(bfloat16(-1.0)));
+static_assert(bfloat16(1.0) ==
+ LIBC_NAMESPACE::shared::fabsbf16(bfloat16(-1.0)));
static_assert(bfloat16(0.0) ==
LIBC_NAMESPACE::shared::copysignbf16(bfloat16(0.0),
bfloat16(0.0)));
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 029400925af8b..ddc9917a1387b 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -75,7 +75,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::ceilf16(0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::copysignf16(0.0f16, 0.0f16));
- EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsf16(0.0f16));
+ EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::fabsf16(0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::fdimf16(0.0f16, 0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::floorf16(0.0f16));
EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::shared::fmaxf16(0.0f16, 0.0f16));
@@ -261,7 +261,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::ceil(0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::copysign(0.0, 0.0));
- EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabs(0.0));
+ EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fabs(0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fadd(0.0, 0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fdim(0.0, 0.0));
EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::floor(0.0));
@@ -323,7 +323,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::ceill(0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::copysignl(0.0L, 0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::daddl(0.0L, 0.0L));
- EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsl(0.0L));
+ EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::fabsl(0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::faddl(0.0L, 0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::fdiml(0.0L, 0.0L));
EXPECT_FP_EQ(0.0L, LIBC_NAMESPACE::shared::floorl(0.0L));
@@ -407,7 +407,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
float128(0.0), float128(0.0)));
EXPECT_FP_EQ(float128(0.0),
LIBC_NAMESPACE::shared::daddf128(float128(0.0), float128(0.0)));
- EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsf128(float128(0.0)));
+ EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::fabsf128(float128(0.0)));
EXPECT_FP_EQ(float128(0.0),
LIBC_NAMESPACE::shared::faddf128(float128(0.0), float128(0.0)));
EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::floorf128(float128(0.0)));
@@ -473,7 +473,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::copysignbf16(
bfloat16(0.0), bfloat16(0.0)));
- EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fabsbf16(bfloat16(0.0)));
+ EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::fabsbf16(bfloat16(0.0)));
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::floorbf16(bfloat16(0.0)));
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 869a6cbb666fa..f6a0b8e9f64db 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2865,6 +2865,7 @@ libc_support_library(
":__support_fputil_sqrt",
":__support_macros_config",
":__support_macros_optimization",
+ ":__support_math_asin_utils",
":__support_math_inv_trigf_utils",
],
)
@@ -2954,6 +2955,7 @@ libc_support_library(
":__support_fputil_sqrt",
":__support_macros_config",
":__support_macros_optimization",
+ ":__support_math_atan_utils",
":__support_math_inv_trigf_utils",
],
)
@@ -3321,6 +3323,7 @@ libc_support_library(
":__support_fputil_multiply_add",
":__support_fputil_nearest_integer",
":__support_macros_optimization",
+ ":__support_math_atan_utils",
":__support_math_inv_trigf_utils",
":llvm_libc_macros_float16_macros",
],
@@ -3353,6 +3356,7 @@ libc_support_library(
":__support_fputil_rounding_mode",
":__support_macros_config",
":__support_macros_optimization",
+ ":__support_math_atan_utils",
":__support_math_inv_trigf_utils",
],
)
@@ -4360,8 +4364,8 @@ libc_support_library(
deps = [
":__support_fputil_basic_operations",
":__support_macros_config",
+ ":__support_macros_properties_architectures",
":__support_macros_properties_compiler",
- ":__support_macros_properties_optimization",
":llvm_libc_macros_float16_macros",
],
)
@@ -6315,6 +6319,7 @@ libc_support_library(
":__support_fputil_polyeval",
":__support_fputil_sqrt",
":__support_macros_optimization",
+ ":__support_math_atan_utils",
":hdr_errno_macros",
":hdr_fenv_macros",
":llvm_libc_macros_float16_macros",
>From 5d9de261d0e10d9346b601b425f213aaa925d170 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 24 Apr 2026 16:45:55 +0300
Subject: [PATCH 5/6] fix format
---
libc/src/__support/math/fabsl.h | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/libc/src/__support/math/fabsl.h b/libc/src/__support/math/fabsl.h
index 9924262af8077..8ae0e2d6f3b2f 100644
--- a/libc/src/__support/math/fabsl.h
+++ b/libc/src/__support/math/fabsl.h
@@ -15,7 +15,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace math {
-LIBC_INLINE constexpr long double fabsl(long double x) { return fputil::abs(x); }
+LIBC_INLINE constexpr long double fabsl(long double x) {
+ return fputil::abs(x);
+}
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
>From eba49b3f76be75ae3cc65ac25d22d43f93bb2dce Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Fri, 24 Apr 2026 17:48:11 +0300
Subject: [PATCH 6/6] remove wrong flags
---
libc/src/__support/math/CMakeLists.txt | 6 ------
1 file changed, 6 deletions(-)
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index a4f1fb5a18aea..505723797b047 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1237,8 +1237,6 @@ add_header_library(
libc.src.__support.FPUtil.basic_operations
libc.src.__support.FPUtil.bfloat16
libc.src.__support.macros.config
- FLAGS
- MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
@@ -1260,8 +1258,6 @@ add_header_library(
libc.include.llvm-libc-types.float128
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
- FLAGS
- MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
@@ -1283,8 +1279,6 @@ add_header_library(
DEPENDS
libc.src.__support.FPUtil.basic_operations
libc.src.__support.macros.config
- FLAGS
- MISC_MATH_BASIC_OPS_OPT
)
add_header_library(
More information about the libc-commits
mailing list