Skip to content

Commit 7a384d2

Browse files
committed
Added support for CF16 and CF128
1 parent b333edd commit 7a384d2

File tree

8 files changed

+131
-1
lines changed

8 files changed

+131
-1
lines changed

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ add_header(
134134
DEPENDS
135135
libc.include.llvm-libc-macros.float_macros
136136
)
137+
add_header(
138+
cfloat128
139+
HDR
140+
cfloat128.h
141+
DEPENDS
142+
libc.include.llvm-libc-macros.float_macros
143+
)
144+
add_header(cfloat16 HDR cfloat16.h)
137145
add_header(fsblkcnt_t HDR fsblkcnt_t.h)
138146
add_header(fsfilcnt_t HDR fsfilcnt_t.h)
139147
add_header(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- Definition of cfloat128 type --------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_CFLOAT128_H
10+
#define LLVM_LIBC_TYPES_CFLOAT128_H
11+
12+
#include "../llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG
13+
14+
// Currently, the complex variant of C23 `_Float128` type is only defined as a built-in type in GCC 7
15+
// or later, and only for C. For C++, or for clang, `__float128` is defined
16+
// instead, and only on x86-64 targets.
17+
//
18+
// TODO: Update the complex variant of C23 `_Float128` type detection again when clang supports it.
19+
// https://github.com/llvm/llvm-project/issues/80195
20+
#if defined(__STDC_IEC_60559_BFP__) && !defined(__clang__) && \
21+
!defined(__cplusplus)
22+
#define LIBC_TYPES_HAS_CFLOAT128
23+
typedef _Complex _Float128 cfloat128;
24+
#elif defined(__FLOAT128__) || defined(__SIZEOF_FLOAT128__)
25+
// Use __float128 type. gcc and clang sometime use __SIZEOF_FLOAT128__ to
26+
// notify the availability of __float128.
27+
// clang also uses __FLOAT128__ macro to notify the availability of __float128
28+
// type: https://reviews.llvm.org/D15120
29+
#define LIBC_TYPES_HAS_CFLOAT128
30+
typedef _Complex __float128 cfloat128;
31+
#elif (LDBL_MANT_DIG == 113)
32+
#define LIBC_TYPES_HAS_CFLOAT128
33+
typedef _Complex long double cfloat128;
34+
#endif
35+
36+
#endif // LLVM_LIBC_TYPES_CFLOAT128_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Definition of cfloat16 type ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_CFLOAT16_H
10+
#define LLVM_LIBC_TYPES_CFLOAT16_H
11+
12+
#if defined(__FLT16_MANT_DIG__) && \
13+
(!defined(__GNUC__) || __GNUC__ >= 13 || defined(__clang__)) && \
14+
!defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) && \
15+
!defined(_WIN32)
16+
#define LIBC_TYPES_HAS_CFLOAT16
17+
typedef _Complex _Float16 cfloat16;
18+
#endif
19+
20+
#endif // LLVM_LIBC_TYPES_CFLOAT16_H

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ add_header_library(
126126
type_traits/is_array.h
127127
type_traits/is_base_of.h
128128
type_traits/is_class.h
129+
type_traits/is_complex.h
129130
type_traits/is_const.h
130131
type_traits/is_constant_evaluated.h
131132
type_traits/is_convertible.h
@@ -165,6 +166,7 @@ add_header_library(
165166
libc.include.llvm-libc-macros.stdfix_macros
166167
libc.src.__support.macros.attributes
167168
libc.src.__support.macros.properties.types
169+
libc.src.__support.macros.properties.complex_types
168170
)
169171

170172
add_header_library(

libc/src/__support/CPP/type_traits/is_complex.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
#include "src/__support/CPP/type_traits/is_same.h"
1212
#include "src/__support/CPP/type_traits/remove_cv.h"
13+
#include "src/__support/macros/attributes.h"
14+
#include "src/__support/macros/config.h"
15+
// LIBC_TYPES_HAS_CFLOAT16 && LIBC_TYPES_HAS_CFLOAT128
16+
#include "src/__support/macros/properties/complex_types.h"
1317

1418
namespace LIBC_NAMESPACE_DECL {
1519
namespace cpp {
@@ -25,7 +29,16 @@ template <typename T> struct is_complex {
2529
public:
2630
LIBC_INLINE_VAR static constexpr bool value =
2731
__is_unqualified_any_of<T, _Complex float, _Complex double,
28-
_Complex long double>();
32+
_Complex long double
33+
#ifdef LIBC_TYPES_HAS_CFLOAT16
34+
,
35+
cfloat16
36+
#endif
37+
#ifdef LIBC_TYPES_HAS_CFLOAT128
38+
,
39+
cfloat128
40+
#endif
41+
>();
2942
};
3043
template <typename T>
3144
LIBC_INLINE_VAR constexpr bool is_complex_v = is_complex<T>::value;

libc/src/__support/macros/properties/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,13 @@ add_header_library(
3737
libc.include.llvm-libc-macros.float16_macros
3838
libc.include.llvm-libc-types.float128
3939
)
40+
41+
add_header_library(
42+
complex_types
43+
HDRS
44+
complex_types.h
45+
DEPENDS
46+
.types
47+
libc.include.llvm-libc-types.cfloat16
48+
libc.include.llvm-libc-types.cfloat128
49+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Complex Types support -----------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// Complex Types detection and support.
9+
10+
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H
11+
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H
12+
13+
#include "include/llvm-libc-types/cfloat16.h"
14+
#include "include/llvm-libc-types/cfloat128.h"
15+
#include "types.h"
16+
17+
// -- cfloat16 support --------------------------------------------------------
18+
// LIBC_TYPES_HAS_CFLOAT16 and 'cfloat16' type is provided by
19+
// "include/llvm-libc-types/cfloat16.h"
20+
21+
// -- cfloat128 support -------------------------------------------------------
22+
// LIBC_TYPES_HAS_CFLOAT128 and 'cfloat128' type are provided by
23+
// "include/llvm-libc-types/cfloat128.h"
24+
25+
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_CTYPES_H

libc/test/UnitTest/FPMatcher.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ template <typename T, TestCond Condition> class CFPMatcher : public Matcher<T> {
128128
return matchComplex<double>();
129129
else if (cpp::is_complex_type_same<T, _Complex long double>())
130130
return matchComplex<long double>();
131+
#ifdef LIBC_TYPES_HAS_CFLOAT16
132+
else if (cpp::is_complex_type_same<T, cfloat16>)
133+
return matchComplex<float16>();
134+
#endif
135+
#ifdef LIBC_TYPES_HAS_CFLOAT128
136+
else if (cpp::is_complex_type_same<T, cfloat128>)
137+
return matchComplex<float128>();
138+
#endif
131139
}
132140

133141
void explainError() override {
@@ -137,6 +145,14 @@ template <typename T, TestCond Condition> class CFPMatcher : public Matcher<T> {
137145
return explainErrorComplex<double>();
138146
else if (cpp::is_complex_type_same<T, _Complex long double>())
139147
return explainErrorComplex<long double>();
148+
#ifdef LIBC_TYPES_HAS_CFLOAT16
149+
else if (cpp::is_complex_type_same<T, cfloat16>)
150+
return explainErrorComplex<float16>();
151+
#endif
152+
#ifdef LIBC_TYPES_HAS_CFLOAT128
153+
else if (cpp::is_complex_type_same<T, cfloat128>)
154+
return explainErrorComplex<float128>();
155+
#endif
140156
}
141157
};
142158

0 commit comments

Comments
 (0)