Skip to content

Commit bbd8749

Browse files
committed
add make_complex and make_real
1 parent a7a54f3 commit bbd8749

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

libc/src/__support/complex_type.h

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,47 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
1111

1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/complex_types.h"
14+
#include "src/__support/macros/properties/types.h"
1315

1416
namespace LIBC_NAMESPACE_DECL {
1517
template <typename T> struct Complex {
1618
T real;
1719
T imag;
1820
};
1921

20-
template <typename T, typename U> T conjugate(T c) {
21-
Complex<U> c_c = cpp::bit_cast<Complex<U>>(c);
22+
template <typename T> struct make_complex;
23+
24+
template<> struct make_complex<float> { using type = _Complex float; };
25+
template<> struct make_complex<double> { using type = _Complex double; };
26+
template<> struct make_complex<long double> { using type = _Complex long double; };
27+
28+
#if defined(LIBC_TYPES_HAS_CFLOAT16)
29+
template<> struct make_complex<float16> { using type = cfloat16; };
30+
#endif
31+
#if defined(LIBC_TYPES_HAS_CFLOAT128)
32+
template<> struct make_complex<float128> { using type = cfloat128; };
33+
#endif
34+
35+
template<typename T> using make_complex_t = typename make_complex<T>::type;
36+
37+
template <typename T> struct make_real;
38+
39+
template<> struct make_real<_Complex float> { using type = float; };
40+
template<> struct make_real<_Complex double> { using type = double; };
41+
template<> struct make_real<_Complex long double> { using type = long double; };
42+
43+
#if defined(LIBC_TYPES_HAS_CFLOAT16)
44+
template<> struct make_real<cfloat16> { using type = float16; };
45+
#endif
46+
#if defined(LIBC_TYPES_HAS_CFLOAT128)
47+
template<> struct make_real<cfloat128> { using type = float128; };
48+
#endif
49+
50+
template<typename T> using make_real_t = typename make_real<T>::type;
51+
52+
template <typename T> T conjugate(T c) {
53+
Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
2254
c_c.imag = -c_c.imag;
2355
return cpp::bit_cast<T>(c_c);
2456
}

libc/src/complex/generic/conj.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(_Complex double, conj, (_Complex double x)) {
17-
return conjugate<_Complex double, double>(x);
17+
return conjugate<_Complex double>(x);
1818
}
1919

2020
} // namespace LIBC_NAMESPACE_DECL

libc/src/complex/generic/conjf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(_Complex float, conjf, (_Complex float x)) {
17-
return conjugate<_Complex float, float>(x);
17+
return conjugate<_Complex float>(x);
1818
}
1919

2020
} // namespace LIBC_NAMESPACE_DECL

libc/src/complex/generic/conjf128.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace LIBC_NAMESPACE_DECL {
1717

1818
LLVM_LIBC_FUNCTION(cfloat128, conjf128, (cfloat128 x)) {
19-
return conjugate<cfloat128, float128>(x);
19+
return conjugate<cfloat128>(x);
2020
}
2121

2222
} // namespace LIBC_NAMESPACE_DECL

libc/src/complex/generic/conjf16.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace LIBC_NAMESPACE_DECL {
1717

1818
LLVM_LIBC_FUNCTION(cfloat16, conjf16, (cfloat16 x)) {
19-
return conjugate<cfloat16, float16>(x);
19+
return conjugate<cfloat16>(x);
2020
}
2121

2222
} // namespace LIBC_NAMESPACE_DECL

libc/src/complex/generic/conjl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(_Complex long double, conjl, (_Complex long double x)) {
17-
return conjugate<_Complex long double, long double>(x);
17+
return conjugate<_Complex long double>(x);
1818
}
1919

2020
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)