Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc][math] Add C23 math function fabsf128. #77825

Merged
merged 2 commits into from Jan 12, 2024
Merged

[libc][math] Add C23 math function fabsf128. #77825

merged 2 commits into from Jan 12, 2024

Conversation

lntue
Copy link
Contributor

@lntue lntue commented Jan 11, 2024

No description provided.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 11, 2024

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

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

10 Files Affected:

  • (modified) libc/config/linux/aarch64/entrypoints.txt (+9)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
  • (modified) libc/docs/math/index.rst (+2)
  • (modified) libc/spec/stdc.td (+1)
  • (modified) libc/src/math/CMakeLists.txt (+1)
  • (added) libc/src/math/fabsf128.h (+20)
  • (modified) libc/src/math/generic/CMakeLists.txt (+12)
  • (added) libc/src/math/generic/fabsf128.cpp (+17)
  • (modified) libc/test/src/math/smoke/CMakeLists.txt (+16)
  • (added) libc/test/src/math/smoke/fabsf128_test.cpp (+13)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index ce3f5eb40e38aa..81a6217ba680d6 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -260,6 +260,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fabs
     libc.src.math.fabsf
     libc.src.math.fabsl
+    libc.src.math.fabs128
     libc.src.math.fdim
     libc.src.math.fdimf
     libc.src.math.fdiml
@@ -354,6 +355,14 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.truncl
 )
 
+if(LIBC_COMPILER_HAS_FLOAT128)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    # math.h C23 _Float128 entrypoints
+    libc.src.math.copysignf128
+    libc.src.math.fabsf128
+  )
+endif()
+
 if(LLVM_LIBC_FULL_BUILD)
   list(APPEND TARGET_LIBC_ENTRYPOINTS
     # compiler entrypoints (no corresponding header)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 30900de365bf95..094bdde2e1589c 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -370,6 +370,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C23 _Float128 entrypoints
     libc.src.math.copysignf128
+    libc.src.math.fabsf128
   )
 endif()
 
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 3668524af03c27..724ad19dbe4426 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -128,6 +128,8 @@ Basic Operations
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fabsl        | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| |         |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
+| fabsf128     | |check| | |check| |         |         |         |         |         |         |         |         |         |         |
++--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fdim         | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| |         |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fdimf        | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| |         |         |         |
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 78095eb23f0711..714dc21f95ba54 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -367,6 +367,7 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"fabs", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
           FunctionSpec<"fabsf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
           FunctionSpec<"fabsl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+          FunctionSpec<"fabsf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>]>,
 
           FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
           FunctionSpec<"fdimf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index e2b1026fcad7e5..a8d3fbd475f07f 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -105,6 +105,7 @@ add_math_entrypoint_object(expm1f)
 add_math_entrypoint_object(fabs)
 add_math_entrypoint_object(fabsf)
 add_math_entrypoint_object(fabsl)
+add_math_entrypoint_object(fabsf128)
 
 add_math_entrypoint_object(fdim)
 add_math_entrypoint_object(fdimf)
diff --git a/libc/src/math/fabsf128.h b/libc/src/math/fabsf128.h
new file mode 100644
index 00000000000000..5999757decfdab
--- /dev/null
+++ b/libc/src/math/fabsf128.h
@@ -0,0 +1,20 @@
+//===-- 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_MATH_FABSF128_H
+#define LLVM_LIBC_SRC_MATH_FABSF128_H
+
+#include "src/__support/macros/properties/float.h"
+
+namespace LIBC_NAMESPACE {
+
+float128 fabsf128(float128 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_FABSF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index eeb09652961fd5..887a8e6038a49d 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -196,6 +196,18 @@ add_entrypoint_object(
     -O2
 )
 
+add_entrypoint_object(
+  fabsf128
+  SRCS
+    fabsf128.cpp
+  HDRS
+    ../fabsf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   trunc
   SRCS
diff --git a/libc/src/math/generic/fabsf128.cpp b/libc/src/math/generic/fabsf128.cpp
new file mode 100644
index 00000000000000..615b13f8623993
--- /dev/null
+++ b/libc/src/math/generic/fabsf128.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of fabsf128 function -------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/fabsf128.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float128, fabsf128, (float128 x)) { return fputil::abs(x); }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 87b72e2a8eca2e..163fa924b243ac 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -87,6 +87,8 @@ add_fp_unittest(
 
 add_fp_unittest(
   fabsl_test
+  # FIXME: Currently fails on the GPU build.
+  UNIT_TEST_ONLY
   SUITE
     libc-math-smoke-tests
   SRCS
@@ -97,8 +99,22 @@ add_fp_unittest(
     libc.include.math
     libc.src.math.fabsl
     libc.src.__support.FPUtil.fp_bits
+)
+
+add_fp_unittest(
+  fabsf128_test
   # FIXME: Currently fails on the GPU build.
   UNIT_TEST_ONLY
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    fabsf128_test.cpp
+  HDRS
+    FAbsTest.h
+  DEPENDS
+    libc.include.math
+    libc.src.math.fabsf128
+    libc.src.__support.FPUtil.fp_bits
 )
 
 add_fp_unittest(
diff --git a/libc/test/src/math/smoke/fabsf128_test.cpp b/libc/test/src/math/smoke/fabsf128_test.cpp
new file mode 100644
index 00000000000000..9807179efa9904
--- /dev/null
+++ b/libc/test/src/math/smoke/fabsf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fabsf128 --------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "FAbsTest.h"
+
+#include "src/math/fabsf128.h"
+
+LIST_FABS_TESTS(float128, LIBC_NAMESPACE::fabsf128)

@nickdesaulniers
Copy link
Member

LGTM; but can you share the details of the GPU failure? cc @jhuber6

if(LIBC_COMPILER_HAS_FLOAT128)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float128 entrypoints
libc.src.math.copysignf128
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should copysign be a distinct commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@jhuber6
Copy link
Contributor

jhuber6 commented Jan 12, 2024

LGTM; but can you share the details of the GPU failure? cc @jhuber6

This is related to b1af3c0 which contains the commit that made all the math tests suddenly start failing on the GPU. We still need to figure out what that is.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jhuber6
Copy link
Contributor

jhuber6 commented Jan 12, 2024

C23 math functions will be very interesting for the GPU considering that they have first-class support for f16, see https://godbolt.org/z/5aG3z15eb. Looking forward to it.

@lntue lntue merged commit 1048b59 into llvm:main Jan 12, 2024
5 checks passed
@lntue lntue deleted the fabsf128 branch January 12, 2024 20:00
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants