[libc-commits] [libc] [libc][math][c23] Add fabsf16 C23 math function (PR #93567)

via libc-commits libc-commits at lists.llvm.org
Wed May 29 10:31:26 PDT 2024


https://github.com/overmighty updated https://github.com/llvm/llvm-project/pull/93567

>From f5f64f970f52d8c99cb0baadeadb7bccd2292a92 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 28 May 2024 18:01:31 +0200
Subject: [PATCH 01/10] [libc][math][c23] Add fabsf16 C23 math function

---
 .../cmake/modules/CheckCompilerFeatures.cmake |  6 ++-
 .../compiler_features/check_float16.cpp       |  5 +++
 libc/config/linux/x86_64/entrypoints.txt      |  7 ++++
 libc/include/llvm-libc-types/CMakeLists.txt   |  1 +
 libc/include/llvm-libc-types/float16.h        | 38 +++++++++++++++++++
 libc/spec/spec.td                             |  1 +
 libc/spec/stdc.td                             |  2 +
 .../CPP/type_traits/is_floating_point.h       |  9 ++++-
 libc/src/__support/FPUtil/FPBits.h            |  2 +-
 libc/src/__support/macros/properties/types.h  | 25 ++----------
 libc/src/math/CMakeLists.txt                  |  1 +
 libc/src/math/fabsf16.h                       | 20 ++++++++++
 libc/src/math/generic/CMakeLists.txt          | 13 +++++++
 libc/src/math/generic/fabsf16.cpp             | 17 +++++++++
 libc/test/src/math/smoke/CMakeLists.txt       | 13 +++++++
 libc/test/src/math/smoke/fabsf16_test.cpp     | 13 +++++++
 16 files changed, 147 insertions(+), 26 deletions(-)
 create mode 100644 libc/cmake/modules/compiler_features/check_float16.cpp
 create mode 100644 libc/include/llvm-libc-types/float16.h
 create mode 100644 libc/src/math/fabsf16.h
 create mode 100644 libc/src/math/generic/fabsf16.cpp
 create mode 100644 libc/test/src/math/smoke/fabsf16_test.cpp

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake
index 3a9e1e3b1cf8b..17806588550eb 100644
--- a/libc/cmake/modules/CheckCompilerFeatures.cmake
+++ b/libc/cmake/modules/CheckCompilerFeatures.cmake
@@ -2,7 +2,7 @@
 # Compiler features definition and flags
 # ------------------------------------------------------------------------------
 
-set(ALL_COMPILER_FEATURES "float128" "fixed_point")
+set(ALL_COMPILER_FEATURES "float16" "float128" "fixed_point")
 
 # Making sure ALL_COMPILER_FEATURES is sorted.
 list(SORT ALL_COMPILER_FEATURES)
@@ -54,7 +54,9 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
   )
   if(has_feature)
     list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})
-    if(${feature} STREQUAL "float128")
+    if(${feature} STREQUAL "float16")
+      set(LIBC_TYPES_HAS_FLOAT16 TRUE)
+    elseif(${feature} STREQUAL "float128")
       set(LIBC_TYPES_HAS_FLOAT128 TRUE)
     elseif(${feature} STREQUAL "fixed_point")
       set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)
diff --git a/libc/cmake/modules/compiler_features/check_float16.cpp b/libc/cmake/modules/compiler_features/check_float16.cpp
new file mode 100644
index 0000000000000..133c559eed219
--- /dev/null
+++ b/libc/cmake/modules/compiler_features/check_float16.cpp
@@ -0,0 +1,5 @@
+#include "src/__support/macros/properties/types.h"
+
+#ifndef LIBC_TYPES_HAS_FLOAT16
+#error unsupported
+#endif
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5e3ddd34fb4dc..03cd17a8090cb 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -528,6 +528,13 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.ufromfpxl
 )
 
+if(LIBC_TYPES_HAS_FLOAT16)
+  list(APPEND TARGET_LIBM_ENTRYPOINTS
+    # math.h C23 _Float16 entrypoints
+    libc.src.math.fabsf16
+  )
+endif()
+
 if(LIBC_TYPES_HAS_FLOAT128)
   list(APPEND TARGET_LIBM_ENTRYPOINTS
     # math.h C23 _Float128 entrypoints
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 018b6c58316c3..6f3e123f78d8f 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -119,6 +119,7 @@ add_header(ENTRY HDR ENTRY.h)
 add_header(struct_hsearch_data HDR struct_hsearch_data.h)
 add_header(struct_epoll_event HDR struct_epoll_event.h)
 add_header(struct_epoll_data HDR struct_epoll_data.h)
+add_header(float16 HDR float16.h)
 add_header(
   float128
   HDR
diff --git a/libc/include/llvm-libc-types/float16.h b/libc/include/llvm-libc-types/float16.h
new file mode 100644
index 0000000000000..671d9c1b97b7f
--- /dev/null
+++ b/libc/include/llvm-libc-types/float16.h
@@ -0,0 +1,38 @@
+//===-- Definition of float16 type ----------------------------------------===//
+//
+// 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_TYPES_FLOAT16_H
+#define LLVM_LIBC_TYPES_FLOAT16_H
+
+#include "src/__support/macros/properties/architectures.h"
+#include "src/__support/macros/properties/compiler.h"
+#include "src/__support/macros/properties/cpu_features.h"
+
+#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2)
+#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1500)) || \
+    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1201))
+#define LIBC_TYPES_HAS_FLOAT16
+using float16 = _Float16;
+#endif
+#endif
+#if defined(LIBC_TARGET_ARCH_IS_AARCH64)
+#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 900)) ||  \
+    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
+#define LIBC_TYPES_HAS_FLOAT16
+using float16 = _Float16;
+#endif
+#endif
+#if defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
+#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1300)) || \
+    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
+#define LIBC_TYPES_HAS_FLOAT16
+using float16 = _Float16;
+#endif
+#endif
+
+#endif // LLVM_LIBC_TYPES_FLOAT16_H
diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index ea8fa4cd373cf..966e1f5df47c1 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -53,6 +53,7 @@ def UnsignedCharType : NamedType<"unsigned char">;
 def UnsignedShortType : NamedType<"unsigned short">;
 def BoolType : NamedType<"bool">;
 
+def Float16Type : NamedType<"float16">;
 def Float128Type : NamedType<"float128">;
 
 // Common types
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index eb67c9b0b009b..6034a6e1aa703 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -378,6 +378,7 @@ def StdC : StandardSpec<"stdc"> {
       [
           NamedType<"float_t">,
           NamedType<"double_t">,
+          Float16Type,
           NamedType<"float128">,
       ],
       [], // Enumerations
@@ -395,6 +396,7 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"fabs", RetValSpec<DoubleType>, [ArgSpec<DoubleType>], [ConstAttr]>,
           FunctionSpec<"fabsf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
           FunctionSpec<"fabsl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
+          GuardedFunctionSpec<"fabsf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
           GuardedFunctionSpec<"fabsf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
 
           FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
diff --git a/libc/src/__support/CPP/type_traits/is_floating_point.h b/libc/src/__support/CPP/type_traits/is_floating_point.h
index 4c8f50f4e91f9..39150d64b7876 100644
--- a/libc/src/__support/CPP/type_traits/is_floating_point.h
+++ b/libc/src/__support/CPP/type_traits/is_floating_point.h
@@ -24,7 +24,14 @@ template <typename T> struct is_floating_point {
   }
 
 public:
-#if defined(LIBC_TYPES_HAS_FLOAT128)
+#if defined(LIBC_TYPES_HAS_FLOAT16) && defined(LIBC_TYPES_HAS_FLOAT128)
+  LIBC_INLINE_VAR static constexpr bool value =
+      __is_unqualified_any_of<T, float, double, long double, float16,
+                              float128>();
+#elif defined(LIBC_TYPES_HAS_FLOAT16)
+  LIBC_INLINE_VAR static constexpr bool value =
+      __is_unqualified_any_of<T, float, double, long double, float16>();
+#elif defined(LIBC_TYPES_HAS_FLOAT128)
   LIBC_INLINE_VAR static constexpr bool value =
       __is_unqualified_any_of<T, float, double, long double, float128>();
 #else
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index ab050360c353b..d3c96d2d613d6 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -651,7 +651,7 @@ struct FPRepImpl : public FPRepSem<fp_type, RetT> {
 
   // Modifiers
   LIBC_INLINE constexpr RetT abs() const {
-    return RetT(bits & UP::EXP_SIG_MASK);
+    return RetT(static_cast<StorageType>(bits & UP::EXP_SIG_MASK));
   }
 
   // Observers
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index 781cf1b7a2b62..6a81b33503e00 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -12,6 +12,7 @@
 
 #include "hdr/float_macros.h"                      // LDBL_MANT_DIG
 #include "include/llvm-libc-types/float128.h"      // float128
+#include "include/llvm-libc-types/float16.h"       // float16
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
 #include "src/__support/macros/properties/cpu_features.h"
@@ -39,28 +40,8 @@
 #endif // defined(__SIZEOF_INT128__)
 
 // -- float16 support ---------------------------------------------------------
-// TODO: move this logic to "llvm-libc-types/float16.h"
-#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2)
-#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1500)) || \
-    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1201))
-#define LIBC_TYPES_HAS_FLOAT16
-using float16 = _Float16;
-#endif
-#endif
-#if defined(LIBC_TARGET_ARCH_IS_AARCH64)
-#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 900)) ||  \
-    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
-#define LIBC_TYPES_HAS_FLOAT16
-using float16 = _Float16;
-#endif
-#endif
-#if defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
-#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1300)) || \
-    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
-#define LIBC_TYPES_HAS_FLOAT16
-using float16 = _Float16;
-#endif
-#endif
+// LIBC_TYPES_HAS_FLOAT16 and 'float16' type are provided by
+// "include/llvm-libc-types/float16.h"
 
 // -- float128 support --------------------------------------------------------
 // LIBC_TYPES_HAS_FLOAT128 and 'float128' type are provided by
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index c34c58575441d..31df5d0ab8809 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -99,6 +99,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(fabsf16)
 add_math_entrypoint_object(fabsf128)
 
 add_math_entrypoint_object(fdim)
diff --git a/libc/src/math/fabsf16.h b/libc/src/math/fabsf16.h
new file mode 100644
index 0000000000000..532662a77e9a6
--- /dev/null
+++ b/libc/src/math/fabsf16.h
@@ -0,0 +1,20 @@
+//===-- 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_MATH_FABSF16_H
+#define LLVM_LIBC_SRC_MATH_FABSF16_H
+
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE {
+
+float16 fabsf16(float16 x);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_MATH_FABSF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 269bc6be5d834..04656e3186181 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -241,6 +241,19 @@ add_entrypoint_object(
     -O2
 )
 
+add_entrypoint_object(
+  fabsf16
+  SRCS
+    fabsf16.cpp
+  HDRS
+    ../fabsf16.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.basic_operations
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   fabsf128
   SRCS
diff --git a/libc/src/math/generic/fabsf16.cpp b/libc/src/math/generic/fabsf16.cpp
new file mode 100644
index 0000000000000..4de84f35da302
--- /dev/null
+++ b/libc/src/math/generic/fabsf16.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of fabsf16 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/fabsf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(float16, fabsf16, (float16 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 112b2985829ca..c74f68daeb082 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -92,6 +92,19 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  fabsf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    fabsf16_test.cpp
+  HDRS
+    FAbsTest.h
+  DEPENDS
+    libc.src.math.fabsf16
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   fabsf128_test
   SUITE
diff --git a/libc/test/src/math/smoke/fabsf16_test.cpp b/libc/test/src/math/smoke/fabsf16_test.cpp
new file mode 100644
index 0000000000000..c43bd5090f90b
--- /dev/null
+++ b/libc/test/src/math/smoke/fabsf16_test.cpp
@@ -0,0 +1,13 @@
+//===-- Unittests for fabsf16 ---------------------------------------------===//
+//
+// 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/fabsf16.h"
+
+LIST_FABS_TESTS(float16, LIBC_NAMESPACE::fabsf16)

>From e2ec1d5978292cc250f610ff83f4905dac6cbf7e Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 28 May 2024 18:10:22 +0200
Subject: [PATCH 02/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Fix usage of internal float16 alias in generated headers.
---
 libc/spec/spec.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index 966e1f5df47c1..056a3143c5a71 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -53,7 +53,7 @@ def UnsignedCharType : NamedType<"unsigned char">;
 def UnsignedShortType : NamedType<"unsigned short">;
 def BoolType : NamedType<"bool">;
 
-def Float16Type : NamedType<"float16">;
+def Float16Type : NamedType<"_Float16">;
 def Float128Type : NamedType<"float128">;
 
 // Common types

>From 04c701cf0835e109f3a0d47ce8fc7180d8fafb6d Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 28 May 2024 18:17:42 +0200
Subject: [PATCH 03/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Add check mark for fabsf16 in docs.
---
 libc/docs/math/index.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 28503e1d13ab5..cd90b6ae85769 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -124,7 +124,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | dsub             | N/A              | N/A             |                        | N/A                  |                        | 7.12.14.2              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| fabs             | |check|          | |check|         | |check|                |                      | |check|                | 7.12.7.3               | F.10.4.3                   |
+| fabs             | |check|          | |check|         | |check|                | |check|              | |check|                | 7.12.7.3               | F.10.4.3                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | fadd             | N/A              |                 |                        | N/A                  |                        | 7.12.14.1              | F.10.11                    |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

>From e1c6035ee38e9621c7d4a882cc5c9d5ec35c3af1 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 28 May 2024 19:39:18 +0200
Subject: [PATCH 04/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Fix missing include in generated math.h public header.
---
 libc/config/linux/api.td    | 2 +-
 libc/include/CMakeLists.txt | 1 +
 libc/spec/stdc.td           | 2 +-
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 902839b3e5b8f..69167ed6907e8 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -60,7 +60,7 @@ def IntTypesAPI : PublicAPI<"inttypes.h"> {
 }
 
 def MathAPI : PublicAPI<"math.h"> {
-  let Types = ["double_t", "float_t", "float128"];
+  let Types = ["double_t", "float_t", "float16", "float128"];
 }
 
 def FenvAPI: PublicAPI<"fenv.h"> {
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index be876c9090d33..92708cc3f7244 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -114,6 +114,7 @@ add_gen_header(
     .llvm-libc-macros.math_macros
     .llvm-libc-types.double_t
     .llvm-libc-types.float_t
+    .llvm-libc-types.float16
     .llvm-libc-types.float128
 )
 
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 6034a6e1aa703..41fe8f9aeca29 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -378,7 +378,7 @@ def StdC : StandardSpec<"stdc"> {
       [
           NamedType<"float_t">,
           NamedType<"double_t">,
-          Float16Type,
+          NamedType<"float16">,
           NamedType<"float128">,
       ],
       [], // Enumerations

>From 079c7addae910e8a7a217f9f9ac3162dae2033f1 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 28 May 2024 19:52:21 +0200
Subject: [PATCH 05/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Replace includes for float16 alias with "include/llvm-libc-types/float16.h".
---
 libc/cmake/modules/compiler_features/check_float16.cpp | 2 +-
 libc/src/math/fabsf16.h                                | 2 +-
 libc/src/math/generic/CMakeLists.txt                   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/cmake/modules/compiler_features/check_float16.cpp b/libc/cmake/modules/compiler_features/check_float16.cpp
index 133c559eed219..8780346eb4f35 100644
--- a/libc/cmake/modules/compiler_features/check_float16.cpp
+++ b/libc/cmake/modules/compiler_features/check_float16.cpp
@@ -1,4 +1,4 @@
-#include "src/__support/macros/properties/types.h"
+#include "include/llvm-libc-types/float16.h"
 
 #ifndef LIBC_TYPES_HAS_FLOAT16
 #error unsupported
diff --git a/libc/src/math/fabsf16.h b/libc/src/math/fabsf16.h
index 532662a77e9a6..19e022ab83b3b 100644
--- a/libc/src/math/fabsf16.h
+++ b/libc/src/math/fabsf16.h
@@ -9,7 +9,7 @@
 #ifndef LLVM_LIBC_SRC_MATH_FABSF16_H
 #define LLVM_LIBC_SRC_MATH_FABSF16_H
 
-#include "src/__support/macros/properties/types.h"
+#include "include/llvm-libc-types/float16.h"
 
 namespace LIBC_NAMESPACE {
 
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 04656e3186181..35bbf71f7e059 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -248,7 +248,7 @@ add_entrypoint_object(
   HDRS
     ../fabsf16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
+    libc.include.llvm-libc-types.float16
     libc.src.__support.FPUtil.basic_operations
   COMPILE_OPTIONS
     -O3

>From 304f7d7722e90e66e43d38f5a9f377e3cb39b5de Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 28 May 2024 19:55:58 +0200
Subject: [PATCH 06/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Simplify cpp::is_floating_point.
---
 .../CPP/type_traits/is_floating_point.h       | 23 ++++++++-----------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/libc/src/__support/CPP/type_traits/is_floating_point.h b/libc/src/__support/CPP/type_traits/is_floating_point.h
index 39150d64b7876..49b51dc83ea0a 100644
--- a/libc/src/__support/CPP/type_traits/is_floating_point.h
+++ b/libc/src/__support/CPP/type_traits/is_floating_point.h
@@ -24,20 +24,17 @@ template <typename T> struct is_floating_point {
   }
 
 public:
-#if defined(LIBC_TYPES_HAS_FLOAT16) && defined(LIBC_TYPES_HAS_FLOAT128)
   LIBC_INLINE_VAR static constexpr bool value =
-      __is_unqualified_any_of<T, float, double, long double, float16,
-                              float128>();
-#elif defined(LIBC_TYPES_HAS_FLOAT16)
-  LIBC_INLINE_VAR static constexpr bool value =
-      __is_unqualified_any_of<T, float, double, long double, float16>();
-#elif defined(LIBC_TYPES_HAS_FLOAT128)
-  LIBC_INLINE_VAR static constexpr bool value =
-      __is_unqualified_any_of<T, float, double, long double, float128>();
-#else
-  LIBC_INLINE_VAR static constexpr bool value =
-      __is_unqualified_any_of<T, float, double, long double>();
-#endif // LIBC_TYPES_HAS_FLOAT128
+      __is_unqualified_any_of<T, float, double, long double
+#ifdef LIBC_TYPES_HAS_FLOAT16
+                              ,
+                              float16
+#endif
+#ifdef LIBC_TYPES_HAS_FLOAT128
+                              ,
+                              float128
+#endif
+                              >();
 };
 template <typename T>
 LIBC_INLINE_VAR constexpr bool is_floating_point_v =

>From c651c80add31a046787f3fb8b2268612c3535305 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Wed, 29 May 2024 13:34:46 +0200
Subject: [PATCH 07/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Simplify check for _Float16 support.
---
 libc/include/llvm-libc-types/float16.h | 22 ++--------------------
 1 file changed, 2 insertions(+), 20 deletions(-)

diff --git a/libc/include/llvm-libc-types/float16.h b/libc/include/llvm-libc-types/float16.h
index 671d9c1b97b7f..99c4b904cac30 100644
--- a/libc/include/llvm-libc-types/float16.h
+++ b/libc/include/llvm-libc-types/float16.h
@@ -9,30 +9,12 @@
 #ifndef LLVM_LIBC_TYPES_FLOAT16_H
 #define LLVM_LIBC_TYPES_FLOAT16_H
 
-#include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
-#include "src/__support/macros/properties/cpu_features.h"
 
-#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2)
-#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1500)) || \
-    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1201))
+#if defined(__FLT16_MANT_DIG__) &&                                             \
+    (!defined(LIBC_COMPILER_IS_GCC) || LIBC_COMPILER_GCC_VER >= 1301)
 #define LIBC_TYPES_HAS_FLOAT16
 using float16 = _Float16;
 #endif
-#endif
-#if defined(LIBC_TARGET_ARCH_IS_AARCH64)
-#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 900)) ||  \
-    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
-#define LIBC_TYPES_HAS_FLOAT16
-using float16 = _Float16;
-#endif
-#endif
-#if defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
-#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1300)) || \
-    (defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
-#define LIBC_TYPES_HAS_FLOAT16
-using float16 = _Float16;
-#endif
-#endif
 
 #endif // LLVM_LIBC_TYPES_FLOAT16_H

>From d5d24c69124fa49435668c326023b4dc8e2a6423 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Wed, 29 May 2024 19:02:40 +0200
Subject: [PATCH 08/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Fix llvm-libc-types/float16.h including internal header and exposing internal C++ using float16 stmt.
---
 libc/include/llvm-libc-types/float16.h       | 7 ++-----
 libc/src/__support/macros/properties/types.h | 9 ++++++---
 libc/src/math/fabsf16.h                      | 2 +-
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/libc/include/llvm-libc-types/float16.h b/libc/include/llvm-libc-types/float16.h
index 99c4b904cac30..8da49851112a3 100644
--- a/libc/include/llvm-libc-types/float16.h
+++ b/libc/include/llvm-libc-types/float16.h
@@ -1,4 +1,4 @@
-//===-- Definition of float16 type ----------------------------------------===//
+//===-- Detection of _Float16 compiler builtin type -----------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -9,12 +9,9 @@
 #ifndef LLVM_LIBC_TYPES_FLOAT16_H
 #define LLVM_LIBC_TYPES_FLOAT16_H
 
-#include "src/__support/macros/properties/compiler.h"
-
 #if defined(__FLT16_MANT_DIG__) &&                                             \
-    (!defined(LIBC_COMPILER_IS_GCC) || LIBC_COMPILER_GCC_VER >= 1301)
+    (!defined(__GNUC__) || __GNUC__ >= 13 || defined(__clang__))
 #define LIBC_TYPES_HAS_FLOAT16
-using float16 = _Float16;
 #endif
 
 #endif // LLVM_LIBC_TYPES_FLOAT16_H
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index 6a81b33503e00..e740a6b4d3aa1 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -12,7 +12,7 @@
 
 #include "hdr/float_macros.h"                      // LDBL_MANT_DIG
 #include "include/llvm-libc-types/float128.h"      // float128
-#include "include/llvm-libc-types/float16.h"       // float16
+#include "include/llvm-libc-types/float16.h"       // LIBC_TYPES_HAS_FLOAT16
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
 #include "src/__support/macros/properties/cpu_features.h"
@@ -40,8 +40,11 @@
 #endif // defined(__SIZEOF_INT128__)
 
 // -- float16 support ---------------------------------------------------------
-// LIBC_TYPES_HAS_FLOAT16 and 'float16' type are provided by
-// "include/llvm-libc-types/float16.h"
+// LIBC_TYPES_HAS_FLOAT16 is provided by "include/llvm-libc-types/float16.h"
+#ifdef LIBC_TYPES_HAS_FLOAT16
+// Type alias for internal use.
+using float16 = _Float16;
+#endif // LIBC_TYPES_HAS_FLOAT16
 
 // -- float128 support --------------------------------------------------------
 // LIBC_TYPES_HAS_FLOAT128 and 'float128' type are provided by
diff --git a/libc/src/math/fabsf16.h b/libc/src/math/fabsf16.h
index 19e022ab83b3b..532662a77e9a6 100644
--- a/libc/src/math/fabsf16.h
+++ b/libc/src/math/fabsf16.h
@@ -9,7 +9,7 @@
 #ifndef LLVM_LIBC_SRC_MATH_FABSF16_H
 #define LLVM_LIBC_SRC_MATH_FABSF16_H
 
-#include "include/llvm-libc-types/float16.h"
+#include "src/__support/macros/properties/types.h"
 
 namespace LIBC_NAMESPACE {
 

>From 41220f57e1c00cc60d633af164e0650e2d292760 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Wed, 29 May 2024 19:11:23 +0200
Subject: [PATCH 09/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Remove unused direct dependency from fabsf16_test target.
---
 libc/test/src/math/smoke/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index c74f68daeb082..b46fe5e902c67 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -102,7 +102,6 @@ add_fp_unittest(
     FAbsTest.h
   DEPENDS
     libc.src.math.fabsf16
-    libc.src.__support.FPUtil.fp_bits
 )
 
 add_fp_unittest(

>From 893147769ae56744ce1a71582057cc7d48a8d870 Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Wed, 29 May 2024 19:22:08 +0200
Subject: [PATCH 10/10] fixup! [libc][math][c23] Add fabsf16 C23 math function

Move _Float16 detection header to llvm-libc-macros/float16-macros.h.
---
 libc/config/linux/api.td                        |  2 +-
 libc/include/llvm-libc-macros/CMakeLists.txt    |  6 ++++++
 libc/include/llvm-libc-macros/float16-macros.h  | 17 +++++++++++++++++
 libc/include/math.h.def                         |  1 +
 libc/spec/stdc.td                               |  1 -
 .../__support/macros/properties/CMakeLists.txt  |  1 +
 libc/src/__support/macros/properties/types.h    |  7 ++++---
 7 files changed, 30 insertions(+), 5 deletions(-)
 create mode 100644 libc/include/llvm-libc-macros/float16-macros.h

diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 69167ed6907e8..902839b3e5b8f 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -60,7 +60,7 @@ def IntTypesAPI : PublicAPI<"inttypes.h"> {
 }
 
 def MathAPI : PublicAPI<"math.h"> {
-  let Types = ["double_t", "float_t", "float16", "float128"];
+  let Types = ["double_t", "float_t", "float128"];
 }
 
 def FenvAPI: PublicAPI<"fenv.h"> {
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 961830ef97668..a4c2ca0df74d3 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -91,6 +91,12 @@ add_macro_header(
     float-macros.h
 )
 
+add_macro_header(
+  float16_macros
+  HDR
+    float16-macros.h
+)
+
 add_macro_header(
   limits_macros
   HDR
diff --git a/libc/include/llvm-libc-macros/float16-macros.h b/libc/include/llvm-libc-macros/float16-macros.h
new file mode 100644
index 0000000000000..9f17503d85130
--- /dev/null
+++ b/libc/include/llvm-libc-macros/float16-macros.h
@@ -0,0 +1,17 @@
+//===-- Detection of _Float16 compiler builtin type -----------------------===//
+//
+// 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_MACROS_FLOAT16_MACROS_H
+#define LLVM_LIBC_MACROS_FLOAT16_MACROS_H
+
+#if defined(__FLT16_MANT_DIG__) &&                                             \
+    (!defined(__GNUC__) || __GNUC__ >= 13 || defined(__clang__))
+#define LIBC_TYPES_HAS_FLOAT16
+#endif
+
+#endif // LLVM_LIBC_MACROS_FLOAT16_MACROS_H
diff --git a/libc/include/math.h.def b/libc/include/math.h.def
index cd2fe76f40bf2..454b8f2980514 100644
--- a/libc/include/math.h.def
+++ b/libc/include/math.h.def
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_MATH_H
 
 #include "__llvm-libc-common.h"
+#include "llvm-libc-macros/float16-macros.h"
 #include "llvm-libc-macros/math-macros.h"
 #include "llvm-libc-types/float128.h"
 
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 41fe8f9aeca29..ff700255144e6 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -378,7 +378,6 @@ def StdC : StandardSpec<"stdc"> {
       [
           NamedType<"float_t">,
           NamedType<"double_t">,
-          NamedType<"float16">,
           NamedType<"float128">,
       ],
       [], // Enumerations
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index 7718aeaa3de5a..c69f3a85d7287 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -34,5 +34,6 @@ add_header_library(
     .cpu_features
     .os
     libc.hdr.float_macros
+    libc.include.llvm-libc-macros.float16_macros
     libc.include.llvm-libc-types.float128
 )
diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h
index e740a6b4d3aa1..69ddc912238e7 100644
--- a/libc/src/__support/macros/properties/types.h
+++ b/libc/src/__support/macros/properties/types.h
@@ -11,8 +11,8 @@
 #define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_TYPES_H
 
 #include "hdr/float_macros.h"                      // LDBL_MANT_DIG
-#include "include/llvm-libc-types/float128.h"      // float128
-#include "include/llvm-libc-types/float16.h"       // LIBC_TYPES_HAS_FLOAT16
+#include "include/llvm-libc-macros/float16-macros.h" // LIBC_TYPES_HAS_FLOAT16
+#include "include/llvm-libc-types/float128.h"        // float128
 #include "src/__support/macros/properties/architectures.h"
 #include "src/__support/macros/properties/compiler.h"
 #include "src/__support/macros/properties/cpu_features.h"
@@ -40,7 +40,8 @@
 #endif // defined(__SIZEOF_INT128__)
 
 // -- float16 support ---------------------------------------------------------
-// LIBC_TYPES_HAS_FLOAT16 is provided by "include/llvm-libc-types/float16.h"
+// LIBC_TYPES_HAS_FLOAT16 is provided by
+// "include/llvm-libc-macros/float16-macros.h"
 #ifdef LIBC_TYPES_HAS_FLOAT16
 // Type alias for internal use.
 using float16 = _Float16;



More information about the libc-commits mailing list