[libc-commits] [libc] [libc][complex] Added support for CFP16 and CFP128 (PR #112594)
Shourya Goel via libc-commits
libc-commits at lists.llvm.org
Wed Oct 16 12:24:53 PDT 2024
https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/112594
>From 7a384d21e3f2d6e098a8bc5fd90ab38f3251a8e5 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 17 Oct 2024 00:01:12 +0530
Subject: [PATCH 1/4] Added support for CF16 and CF128
---
libc/include/llvm-libc-types/CMakeLists.txt | 8 +++++
libc/include/llvm-libc-types/cfloat128.h | 36 +++++++++++++++++++
libc/include/llvm-libc-types/cfloat16.h | 20 +++++++++++
libc/src/__support/CPP/CMakeLists.txt | 2 ++
.../__support/CPP/type_traits/is_complex.h | 15 +++++++-
.../macros/properties/CMakeLists.txt | 10 ++++++
.../macros/properties/complex_types.h | 25 +++++++++++++
libc/test/UnitTest/FPMatcher.h | 16 +++++++++
8 files changed, 131 insertions(+), 1 deletion(-)
create mode 100644 libc/include/llvm-libc-types/cfloat128.h
create mode 100644 libc/include/llvm-libc-types/cfloat16.h
create mode 100644 libc/src/__support/macros/properties/complex_types.h
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index a4cf4631c8470e..836e8a507bd6f2 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -134,6 +134,14 @@ add_header(
DEPENDS
libc.include.llvm-libc-macros.float_macros
)
+add_header(
+ cfloat128
+ HDR
+ cfloat128.h
+ DEPENDS
+ libc.include.llvm-libc-macros.float_macros
+)
+add_header(cfloat16 HDR cfloat16.h)
add_header(fsblkcnt_t HDR fsblkcnt_t.h)
add_header(fsfilcnt_t HDR fsfilcnt_t.h)
add_header(
diff --git a/libc/include/llvm-libc-types/cfloat128.h b/libc/include/llvm-libc-types/cfloat128.h
new file mode 100644
index 00000000000000..1f5ef09d6cc8a0
--- /dev/null
+++ b/libc/include/llvm-libc-types/cfloat128.h
@@ -0,0 +1,36 @@
+//===-- Definition of cfloat128 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_CFLOAT128_H
+#define LLVM_LIBC_TYPES_CFLOAT128_H
+
+#include "../llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
+
+// Currently, the complex variant of C23 `_Float128` type is only defined as a built-in type in GCC 7
+// or later, and only for C. For C++, or for clang, `__float128` is defined
+// instead, and only on x86-64 targets.
+//
+// TODO: Update the complex variant of C23 `_Float128` type detection again when clang supports it.
+// https://github.com/llvm/llvm-project/issues/80195
+#if defined(__STDC_IEC_60559_BFP__) && !defined(__clang__) && \
+ !defined(__cplusplus)
+#define LIBC_TYPES_HAS_CFLOAT128
+typedef _Complex _Float128 cfloat128;
+#elif defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__)
+// Use __float128 type. gcc and clang sometime use __SIZEOF_FLOAT128__ to
+// notify the availability of __float128.
+// clang also uses __FLOAT128__ macro to notify the availability of __float128
+// type: https://reviews.llvm.org/D15120
+#define LIBC_TYPES_HAS_CFLOAT128
+typedef _Complex __float128 cfloat128;
+#elif (LDBL_MANT_DIG == 113)
+#define LIBC_TYPES_HAS_CFLOAT128
+typedef _Complex long double cfloat128;
+#endif
+
+#endif // LLVM_LIBC_TYPES_CFLOAT128_H
diff --git a/libc/include/llvm-libc-types/cfloat16.h b/libc/include/llvm-libc-types/cfloat16.h
new file mode 100644
index 00000000000000..e7e5631e025074
--- /dev/null
+++ b/libc/include/llvm-libc-types/cfloat16.h
@@ -0,0 +1,20 @@
+//===-- Definition of cfloat16 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_CFLOAT16_H
+#define LLVM_LIBC_TYPES_CFLOAT16_H
+
+#if defined(__FLT16_MANT_DIG__) && \
+ (!defined(__GNUC__) || __GNUC__ >= 13 || defined(__clang__)) && \
+ !defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) && \
+ !defined(_WIN32)
+#define LIBC_TYPES_HAS_CFLOAT16
+typedef _Complex _Float16 cfloat16;
+#endif
+
+#endif // LLVM_LIBC_TYPES_CFLOAT16_H
diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index c1981b827042ca..774668be42e56d 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -126,6 +126,7 @@ add_header_library(
type_traits/is_array.h
type_traits/is_base_of.h
type_traits/is_class.h
+ type_traits/is_complex.h
type_traits/is_const.h
type_traits/is_constant_evaluated.h
type_traits/is_convertible.h
@@ -165,6 +166,7 @@ add_header_library(
libc.include.llvm-libc-macros.stdfix_macros
libc.src.__support.macros.attributes
libc.src.__support.macros.properties.types
+ libc.src.__support.macros.properties.complex_types
)
add_header_library(
diff --git a/libc/src/__support/CPP/type_traits/is_complex.h b/libc/src/__support/CPP/type_traits/is_complex.h
index 4f5ee9abdb33a5..3769a1a83e4a8a 100644
--- a/libc/src/__support/CPP/type_traits/is_complex.h
+++ b/libc/src/__support/CPP/type_traits/is_complex.h
@@ -10,6 +10,10 @@
#include "src/__support/CPP/type_traits/is_same.h"
#include "src/__support/CPP/type_traits/remove_cv.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+// LIBC_TYPES_HAS_CFLOAT16 && LIBC_TYPES_HAS_CFLOAT128
+#include "src/__support/macros/properties/complex_types.h"
namespace LIBC_NAMESPACE_DECL {
namespace cpp {
@@ -25,7 +29,16 @@ template <typename T> struct is_complex {
public:
LIBC_INLINE_VAR static constexpr bool value =
__is_unqualified_any_of<T, _Complex float, _Complex double,
- _Complex long double>();
+ _Complex long double
+#ifdef LIBC_TYPES_HAS_CFLOAT16
+ ,
+ cfloat16
+#endif
+#ifdef LIBC_TYPES_HAS_CFLOAT128
+ ,
+ cfloat128
+#endif
+ >();
};
template <typename T>
LIBC_INLINE_VAR constexpr bool is_complex_v = is_complex<T>::value;
diff --git a/libc/src/__support/macros/properties/CMakeLists.txt b/libc/src/__support/macros/properties/CMakeLists.txt
index c69f3a85d7287a..80ed63a2fbcf70 100644
--- a/libc/src/__support/macros/properties/CMakeLists.txt
+++ b/libc/src/__support/macros/properties/CMakeLists.txt
@@ -37,3 +37,13 @@ add_header_library(
libc.include.llvm-libc-macros.float16_macros
libc.include.llvm-libc-types.float128
)
+
+add_header_library(
+ complex_types
+ HDRS
+ complex_types.h
+ DEPENDS
+ .types
+ libc.include.llvm-libc-types.cfloat16
+ libc.include.llvm-libc-types.cfloat128
+)
diff --git a/libc/src/__support/macros/properties/complex_types.h b/libc/src/__support/macros/properties/complex_types.h
new file mode 100644
index 00000000000000..af68b61e5080ba
--- /dev/null
+++ b/libc/src/__support/macros/properties/complex_types.h
@@ -0,0 +1,25 @@
+//===-- Complex Types support -----------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+// Complex Types detection and support.
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H
+#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H
+
+#include "include/llvm-libc-types/cfloat16.h"
+#include "include/llvm-libc-types/cfloat128.h"
+#include "types.h"
+
+// -- cfloat16 support --------------------------------------------------------
+// LIBC_TYPES_HAS_CFLOAT16 and 'cfloat16' type is provided by
+// "include/llvm-libc-types/cfloat16.h"
+
+// -- cfloat128 support -------------------------------------------------------
+// LIBC_TYPES_HAS_CFLOAT128 and 'cfloat128' type are provided by
+// "include/llvm-libc-types/cfloat128.h"
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H
diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h
index 5220b1245bf3a5..5a713925a65117 100644
--- a/libc/test/UnitTest/FPMatcher.h
+++ b/libc/test/UnitTest/FPMatcher.h
@@ -128,6 +128,14 @@ template <typename T, TestCond Condition> class CFPMatcher : public Matcher<T> {
return matchComplex<double>();
else if (cpp::is_complex_type_same<T, _Complex long double>())
return matchComplex<long double>();
+#ifdef LIBC_TYPES_HAS_CFLOAT16
+ else if (cpp::is_complex_type_same<T, cfloat16>)
+ return matchComplex<float16>();
+#endif
+#ifdef LIBC_TYPES_HAS_CFLOAT128
+ else if (cpp::is_complex_type_same<T, cfloat128>)
+ return matchComplex<float128>();
+#endif
}
void explainError() override {
@@ -137,6 +145,14 @@ template <typename T, TestCond Condition> class CFPMatcher : public Matcher<T> {
return explainErrorComplex<double>();
else if (cpp::is_complex_type_same<T, _Complex long double>())
return explainErrorComplex<long double>();
+#ifdef LIBC_TYPES_HAS_CFLOAT16
+ else if (cpp::is_complex_type_same<T, cfloat16>)
+ return explainErrorComplex<float16>();
+#endif
+#ifdef LIBC_TYPES_HAS_CFLOAT128
+ else if (cpp::is_complex_type_same<T, cfloat128>)
+ return explainErrorComplex<float128>();
+#endif
}
};
>From 7dd93541b74f9fbf7d80c734b8c0ddf9469da834 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 17 Oct 2024 00:02:59 +0530
Subject: [PATCH 2/4] fmt
---
libc/include/llvm-libc-types/cfloat128.h | 13 +++++++------
libc/src/__support/CPP/type_traits/is_complex.h | 2 +-
.../src/__support/macros/properties/complex_types.h | 2 +-
libc/test/UnitTest/FPMatcher.h | 2 +-
4 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/libc/include/llvm-libc-types/cfloat128.h b/libc/include/llvm-libc-types/cfloat128.h
index 1f5ef09d6cc8a0..da2123dc16a6a6 100644
--- a/libc/include/llvm-libc-types/cfloat128.h
+++ b/libc/include/llvm-libc-types/cfloat128.h
@@ -11,18 +11,19 @@
#include "../llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
-// Currently, the complex variant of C23 `_Float128` type is only defined as a built-in type in GCC 7
-// or later, and only for C. For C++, or for clang, `__float128` is defined
-// instead, and only on x86-64 targets.
+// Currently, the complex variant of C23 `_Float128` type is only defined as a
+// built-in type in GCC 7 or later, and only for C. For C++, or for clang,
+// the complex variant of `__float128` is defined instead, and only on x86-64 targets.
//
-// TODO: Update the complex variant of C23 `_Float128` type detection again when clang supports it.
+// TODO: Update the complex variant of C23 `_Float128` type detection again when
+// clang supports it.
// https://github.com/llvm/llvm-project/issues/80195
-#if defined(__STDC_IEC_60559_BFP__) && !defined(__clang__) && \
+#if defined(__STDC_IEC_60559_BFP__) && !defined(__clang__) && \
!defined(__cplusplus)
#define LIBC_TYPES_HAS_CFLOAT128
typedef _Complex _Float128 cfloat128;
#elif defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__)
-// Use __float128 type. gcc and clang sometime use __SIZEOF_FLOAT128__ to
+// Use _Complex __float128 type. gcc and clang sometime use __SIZEOF_FLOAT128__ to
// notify the availability of __float128.
// clang also uses __FLOAT128__ macro to notify the availability of __float128
// type: https://reviews.llvm.org/D15120
diff --git a/libc/src/__support/CPP/type_traits/is_complex.h b/libc/src/__support/CPP/type_traits/is_complex.h
index 3769a1a83e4a8a..23f05c08ccab5a 100644
--- a/libc/src/__support/CPP/type_traits/is_complex.h
+++ b/libc/src/__support/CPP/type_traits/is_complex.h
@@ -37,7 +37,7 @@ template <typename T> struct is_complex {
#ifdef LIBC_TYPES_HAS_CFLOAT128
,
cfloat128
-#endif
+#endif
>();
};
template <typename T>
diff --git a/libc/src/__support/macros/properties/complex_types.h b/libc/src/__support/macros/properties/complex_types.h
index af68b61e5080ba..3f4a7646649c64 100644
--- a/libc/src/__support/macros/properties/complex_types.h
+++ b/libc/src/__support/macros/properties/complex_types.h
@@ -10,8 +10,8 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H
-#include "include/llvm-libc-types/cfloat16.h"
#include "include/llvm-libc-types/cfloat128.h"
+#include "include/llvm-libc-types/cfloat16.h"
#include "types.h"
// -- cfloat16 support --------------------------------------------------------
diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h
index 5a713925a65117..181fb353febd0b 100644
--- a/libc/test/UnitTest/FPMatcher.h
+++ b/libc/test/UnitTest/FPMatcher.h
@@ -135,7 +135,7 @@ template <typename T, TestCond Condition> class CFPMatcher : public Matcher<T> {
#ifdef LIBC_TYPES_HAS_CFLOAT128
else if (cpp::is_complex_type_same<T, cfloat128>)
return matchComplex<float128>();
-#endif
+#endif
}
void explainError() override {
>From ee60f1e45bda9380284c26751ab0e19e0a171f79 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 17 Oct 2024 00:03:45 +0530
Subject: [PATCH 3/4] fmt
---
libc/include/llvm-libc-types/cfloat128.h | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/libc/include/llvm-libc-types/cfloat128.h b/libc/include/llvm-libc-types/cfloat128.h
index da2123dc16a6a6..0cc8ed3041d6f0 100644
--- a/libc/include/llvm-libc-types/cfloat128.h
+++ b/libc/include/llvm-libc-types/cfloat128.h
@@ -13,20 +13,21 @@
// Currently, the complex variant of C23 `_Float128` type is only defined as a
// built-in type in GCC 7 or later, and only for C. For C++, or for clang,
-// the complex variant of `__float128` is defined instead, and only on x86-64 targets.
+// the complex variant of `__float128` is defined instead, and only on x86-64
+// targets.
//
// TODO: Update the complex variant of C23 `_Float128` type detection again when
// clang supports it.
// https://github.com/llvm/llvm-project/issues/80195
-#if defined(__STDC_IEC_60559_BFP__) && !defined(__clang__) && \
+#if defined(__STDC_IEC_60559_COMPLEX__) && !defined(__clang__) && \
!defined(__cplusplus)
#define LIBC_TYPES_HAS_CFLOAT128
typedef _Complex _Float128 cfloat128;
#elif defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__)
-// Use _Complex __float128 type. gcc and clang sometime use __SIZEOF_FLOAT128__ to
-// notify the availability of __float128.
-// clang also uses __FLOAT128__ macro to notify the availability of __float128
-// type: https://reviews.llvm.org/D15120
+// Use _Complex __float128 type. gcc and clang sometime use __SIZEOF_FLOAT128__
+// to notify the availability of __float128. clang also uses __FLOAT128__ macro
+// to notify the availability of __float128 type:
+// https://reviews.llvm.org/D15120
#define LIBC_TYPES_HAS_CFLOAT128
typedef _Complex __float128 cfloat128;
#elif (LDBL_MANT_DIG == 113)
>From 326ea426068068f3cffd19b9851793b8476edec1 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Thu, 17 Oct 2024 00:54:36 +0530
Subject: [PATCH 4/4] Add complex as a dependency of type_traits
---
libc/src/__support/CPP/type_traits.h | 1 -
libc/test/UnitTest/FPMatcher.h | 1 +
2 files changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h
index cef4e5d1f0b139..d50b6612656dbb 100644
--- a/libc/src/__support/CPP/type_traits.h
+++ b/libc/src/__support/CPP/type_traits.h
@@ -25,7 +25,6 @@
#include "src/__support/CPP/type_traits/is_array.h"
#include "src/__support/CPP/type_traits/is_base_of.h"
#include "src/__support/CPP/type_traits/is_class.h"
-#include "src/__support/CPP/type_traits/is_complex.h"
#include "src/__support/CPP/type_traits/is_const.h"
#include "src/__support/CPP/type_traits/is_constant_evaluated.h"
#include "src/__support/CPP/type_traits/is_convertible.h"
diff --git a/libc/test/UnitTest/FPMatcher.h b/libc/test/UnitTest/FPMatcher.h
index 181fb353febd0b..07e2cd5df18cbb 100644
--- a/libc/test/UnitTest/FPMatcher.h
+++ b/libc/test/UnitTest/FPMatcher.h
@@ -11,6 +11,7 @@
#include "src/__support/CPP/array.h"
#include "src/__support/CPP/type_traits.h"
+#include "src/__support/CPP/type_traits/is_complex.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/fpbits_str.h"
More information about the libc-commits
mailing list