[libc-commits] [libc] [libc] Add fminf128 and fmaxf128 implementations for Linux x86_64. (PR #79307)

via libc-commits libc-commits at lists.llvm.org
Wed Jan 24 07:29:05 PST 2024


https://github.com/felixh5678 created https://github.com/llvm/llvm-project/pull/79307

None

>From 5dc153ecf889840855c69b97aa7e09bf4e115f8e Mon Sep 17 00:00:00 2001
From: Felix Hentschel <felix.hentschel1 at icloud.com>
Date: Wed, 24 Jan 2024 10:26:34 -0500
Subject: [PATCH] [libc] Add fminf128 and fmaxf128 implementations for Linux
 x86_64.

---
 libc/config/linux/x86_64/entrypoints.txt   |  4 +++
 libc/docs/math/index.rst                   |  4 +++
 libc/spec/stdc.td                          |  2 ++
 libc/src/math/CMakeLists.txt               |  2 ++
 libc/src/math/fmaxf128.h                   | 20 ++++++++++++++
 libc/src/math/fminf128.h                   | 20 ++++++++++++++
 libc/src/math/generic/CMakeLists.txt       | 24 ++++++++++++++++
 libc/src/math/generic/fmaxf128.cpp         | 19 +++++++++++++
 libc/src/math/generic/fminf128.cpp         | 19 +++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt    | 32 ++++++++++++++++++----
 libc/test/src/math/smoke/FMaxTest.h        | 10 +++----
 libc/test/src/math/smoke/FMinTest.h        | 10 +++----
 libc/test/src/math/smoke/fmaxf128_test.cpp | 13 +++++++++
 libc/test/src/math/smoke/fminf128_test.cpp | 13 +++++++++
 14 files changed, 176 insertions(+), 16 deletions(-)
 create mode 100644 libc/src/math/fmaxf128.h
 create mode 100644 libc/src/math/fminf128.h
 create mode 100644 libc/src/math/generic/fmaxf128.cpp
 create mode 100644 libc/src/math/generic/fminf128.cpp
 create mode 100644 libc/test/src/math/smoke/fmaxf128_test.cpp
 create mode 100644 libc/test/src/math/smoke/fminf128_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a1486ac689812b4..4fbb6f0f5872ae9 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -284,9 +284,11 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.fmin
     libc.src.math.fminf
     libc.src.math.fminl
+    libc.src.math.fminf128
     libc.src.math.fmax
     libc.src.math.fmaxf
     libc.src.math.fmaxl
+    libc.src.math.fmaxf128
     libc.src.math.fmod
     libc.src.math.fmodf
     libc.src.math.frexp
@@ -375,6 +377,8 @@ if(LIBC_COMPILER_HAS_FLOAT128)
     libc.src.math.copysignf128
     libc.src.math.fabsf128
     libc.src.math.sqrtf128
+    libc.src.math.fmaxf128
+    libc.src.math.fminf128
   )
 endif()
 
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index e28b8e87988441a..748247bb85d79c7 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -146,12 +146,16 @@ Basic Operations
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fmaxf        | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
+| fmaxf128     | |check| |         |         |         |         |         |         |         |         |         |         |         |
++--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fmaxl        | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fmin         | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fminf        | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
+| fminf128     | |check| |         |         |         |         |         |         |         |         |         |         |         |
++--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fminl        | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
 +--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
 | fmod         | |check| | |check| | |check| | |check| | |check| |         |         | |check| | |check| | |check| |         |         |
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 8c2feba23adbf3a..5b6f344588b7871 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -380,10 +380,12 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"fmin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
           FunctionSpec<"fminf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
           FunctionSpec<"fminl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
+          FunctionSpec<"fminf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>]>,
 
           FunctionSpec<"fmax", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
           FunctionSpec<"fmaxf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
           FunctionSpec<"fmaxl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
+          FunctionSpec<"fmaxf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>]>,
 
           FunctionSpec<"fma", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
           FunctionSpec<"fmaf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>, ArgSpec<FloatType>]>,
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 3097c4b3a82cc48..2b7eb3a05396b59 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -121,10 +121,12 @@ add_math_entrypoint_object(fmaf)
 add_math_entrypoint_object(fmax)
 add_math_entrypoint_object(fmaxf)
 add_math_entrypoint_object(fmaxl)
+add_math_entrypoint_object(fmaxf128)
 
 add_math_entrypoint_object(fmin)
 add_math_entrypoint_object(fminf)
 add_math_entrypoint_object(fminl)
+add_math_entrypoint_object(fminf128)
 
 add_math_entrypoint_object(fmod)
 add_math_entrypoint_object(fmodf)
diff --git a/libc/src/math/fmaxf128.h b/libc/src/math/fmaxf128.h
new file mode 100644
index 000000000000000..39eaaf616dd5d8e
--- /dev/null
+++ b/libc/src/math/fmaxf128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fmaxf128 ----------------------*- 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_FMAXF128_H
+#define LLVM_LIBC_SRC_MATH_FMAXF128_H
+
+#include "src/__support/macros/properties/float.h"
+
+namespace LIBC_NAMESPACE {
+
+float128 fmaxf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_FMAXF128_H
diff --git a/libc/src/math/fminf128.h b/libc/src/math/fminf128.h
new file mode 100644
index 000000000000000..b3d1bec8e2ad925
--- /dev/null
+++ b/libc/src/math/fminf128.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for fminf128 ----------------------*- 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_FMINF128_H
+#define LLVM_LIBC_SRC_MATH_FMINF128_H
+
+#include "src/__support/macros/properties/float.h"
+
+namespace LIBC_NAMESPACE {
+
+float128 fminf128(float128 x, float128 y);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_FMINF128_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 8b32a3296bb87f6..edc04165a9879f4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1243,6 +1243,18 @@ add_entrypoint_object(
     -O2
 )
 
+add_entrypoint_object(
+  fminf128
+  SRCS
+    fminf128.cpp
+  HDRS
+    ../fminf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+  COMPILE_OPTIONS
+    -O2
+)
+
 add_entrypoint_object(
   fmax
   SRCS
@@ -1279,6 +1291,18 @@ add_entrypoint_object(
     -O2
 )
 
+add_entrypoint_object(
+  fmaxf128
+  SRCS
+    fmaxf128.cpp
+  HDRS
+    ../fmaxf128.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+  COMPILE_OPTIONS
+    -O2
+)
+
 add_entrypoint_object(
   sqrt
   SRCS
diff --git a/libc/src/math/generic/fmaxf128.cpp b/libc/src/math/generic/fmaxf128.cpp
new file mode 100644
index 000000000000000..096e4befeb79a49
--- /dev/null
+++ b/libc/src/math/generic/fmaxf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of fmaxf128 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/fmaxf128.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float128, fmaxf128, (float128 x, float128 y)) {
+  return fputil::fmax(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/math/generic/fminf128.cpp b/libc/src/math/generic/fminf128.cpp
new file mode 100644
index 000000000000000..084ed4c9931bc28
--- /dev/null
+++ b/libc/src/math/generic/fminf128.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of fminf128 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/fminf128.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float128, fminf128, (float128 x, float128 y)) {
+  return fputil::fmin(x, y);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 03526e019c3e9ff..05f5a39beed5b8c 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -984,7 +984,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
     HDRS
       FMinTest.h
     DEPENDS
-      libc.include.math
       libc.src.math.fminf
       libc.src.__support.FPUtil.fp_bits
   )
@@ -998,7 +997,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
     HDRS
       FMinTest.h
     DEPENDS
-      libc.include.math
       libc.src.math.fmin
       libc.src.__support.FPUtil.fp_bits
   )
@@ -1012,11 +1010,23 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
     HDRS
       FMinTest.h
     DEPENDS
-      libc.include.math
       libc.src.math.fminl
       libc.src.__support.FPUtil.fp_bits
   )
 
+  add_fp_unittest(
+    fminf128_test
+    SUITE
+      libc-math-smoke-tests
+    SRCS
+      fminf128_test.cpp
+    HDRS
+      FMinTest.h
+    DEPENDS
+      libc.src.math.fminf128
+      libc.src.__support.FPUtil.fp_bits
+  )
+
   add_fp_unittest(
     fmaxf_test
     SUITE
@@ -1026,7 +1036,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
     HDRS
       FMaxTest.h
     DEPENDS
-      libc.include.math
       libc.src.math.fmaxf
       libc.src.__support.FPUtil.fp_bits
   )
@@ -1040,7 +1049,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
     HDRS
       FMaxTest.h
     DEPENDS
-      libc.include.math
       libc.src.math.fmax
       libc.src.__support.FPUtil.fp_bits
   )
@@ -1054,10 +1062,22 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_GPU)
     HDRS
       FMaxTest.h
     DEPENDS
-      libc.include.math
       libc.src.math.fmaxl
       libc.src.__support.FPUtil.fp_bits
   )
+  
+  add_fp_unittest(
+    fmaxf128_test
+    SUITE
+      libc-math-smoke-tests
+    SRCS
+      fmaxf128_test.cpp
+    HDRS
+      FMaxTest.h
+    DEPENDS
+      libc.src.math.fmaxf128
+      libc.src.__support.FPUtil.fp_bits
+  )
 endif()
 
 add_fp_unittest(
diff --git a/libc/test/src/math/smoke/FMaxTest.h b/libc/test/src/math/smoke/FMaxTest.h
index 98edc8e971e8167..1a376af2e0b7b4b 100644
--- a/libc/test/src/math/smoke/FMaxTest.h
+++ b/libc/test/src/math/smoke/FMaxTest.h
@@ -9,8 +9,6 @@
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <math.h>
-
 template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {
 
   DECLARE_SPECIAL_CONSTANTS(T)
@@ -56,11 +54,13 @@ template <typename T> class FMaxTest : public LIBC_NAMESPACE::testing::Test {
     constexpr StorageType STEP = STORAGE_MAX / COUNT;
     for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
          ++i, v += STEP, w -= STEP) {
-      T x = FPBits(v).get_val(), y = FPBits(w).get_val();
-      if (isnan(x) || isinf(x))
+      FPBits xbits(v), ybits(w);
+      if (xbits.is_inf_or_nan())
         continue;
-      if (isnan(y) || isinf(y))
+      if (ybits.is_inf_or_nan())
         continue;
+      T x = xbits.get_val();
+      T y = ybits.get_val();
       if ((x == 0) && (y == 0))
         continue;
 
diff --git a/libc/test/src/math/smoke/FMinTest.h b/libc/test/src/math/smoke/FMinTest.h
index 834d757c85d047d..add2544424a016b 100644
--- a/libc/test/src/math/smoke/FMinTest.h
+++ b/libc/test/src/math/smoke/FMinTest.h
@@ -9,8 +9,6 @@
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <math.h>
-
 template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {
 
   DECLARE_SPECIAL_CONSTANTS(T)
@@ -56,11 +54,13 @@ template <typename T> class FMinTest : public LIBC_NAMESPACE::testing::Test {
     constexpr StorageType STEP = STORAGE_MAX / COUNT;
     for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
          ++i, v += STEP, w -= STEP) {
-      T x = FPBits(v).get_val(), y = FPBits(w).get_val();
-      if (isnan(x) || isinf(x))
+      FPBits xbits(v), ybits(w);
+      if (xbits.is_inf_or_nan())
         continue;
-      if (isnan(y) || isinf(y))
+      if (ybits.is_inf_or_nan())
         continue;
+      T x = xbits.get_val();
+      T y = ybits.get_val();
       if ((x == 0) && (y == 0))
         continue;
 
diff --git a/libc/test/src/math/smoke/fmaxf128_test.cpp b/libc/test/src/math/smoke/fmaxf128_test.cpp
new file mode 100644
index 000000000000000..497cd05c623911f
--- /dev/null
+++ b/libc/test/src/math/smoke/fmaxf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fmaxf128 --------------------------------------------===//
+//
+// 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 "FMaxTest.h"
+
+#include "src/math/fmaxf128.h"
+
+LIST_FMAX_TESTS(float128, LIBC_NAMESPACE::fmaxf128)
diff --git a/libc/test/src/math/smoke/fminf128_test.cpp b/libc/test/src/math/smoke/fminf128_test.cpp
new file mode 100644
index 000000000000000..f1e9ed4e6ac8ee7
--- /dev/null
+++ b/libc/test/src/math/smoke/fminf128_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fminf128 --------------------------------------------===//
+//
+// 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 "FMinTest.h"
+
+#include "src/math/fminf128.h"
+
+LIST_FMIN_TESTS(float128, LIBC_NAMESPACE::fminf128)



More information about the libc-commits mailing list