|
10 | 10 | #define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H |
11 | 11 |
|
12 | 12 | #include "src/__support/macros/config.h" |
| 13 | +#include "src/__support/macros/properties/complex_types.h" |
| 14 | +#include "src/__support/macros/properties/types.h" |
13 | 15 |
|
14 | 16 | namespace LIBC_NAMESPACE_DECL { |
15 | 17 | template <typename T> struct Complex { |
16 | 18 | T real; |
17 | 19 | T imag; |
18 | 20 | }; |
| 21 | + |
| 22 | +template <typename T> struct make_complex; |
| 23 | + |
| 24 | +template <> struct make_complex<float> { |
| 25 | + using type = _Complex float; |
| 26 | +}; |
| 27 | +template <> struct make_complex<double> { |
| 28 | + using type = _Complex double; |
| 29 | +}; |
| 30 | +template <> struct make_complex<long double> { |
| 31 | + using type = _Complex long double; |
| 32 | +}; |
| 33 | + |
| 34 | +#if defined(LIBC_TYPES_HAS_CFLOAT16) |
| 35 | +template <> struct make_complex<float16> { |
| 36 | + using type = cfloat16; |
| 37 | +}; |
| 38 | +#endif |
| 39 | +#if defined(LIBC_TYPES_HAS_CFLOAT128) |
| 40 | +template <> struct make_complex<float128> { |
| 41 | + using type = cfloat128; |
| 42 | +}; |
| 43 | +#endif |
| 44 | + |
| 45 | +template <typename T> using make_complex_t = typename make_complex<T>::type; |
| 46 | + |
| 47 | +template <typename T> struct make_real; |
| 48 | + |
| 49 | +template <> struct make_real<_Complex float> { |
| 50 | + using type = float; |
| 51 | +}; |
| 52 | +template <> struct make_real<_Complex double> { |
| 53 | + using type = double; |
| 54 | +}; |
| 55 | +template <> struct make_real<_Complex long double> { |
| 56 | + using type = long double; |
| 57 | +}; |
| 58 | + |
| 59 | +#if defined(LIBC_TYPES_HAS_CFLOAT16) |
| 60 | +template <> struct make_real<cfloat16> { |
| 61 | + using type = float16; |
| 62 | +}; |
| 63 | +#endif |
| 64 | +#if defined(LIBC_TYPES_HAS_CFLOAT128) |
| 65 | +template <> struct make_real<cfloat128> { |
| 66 | + using type = float128; |
| 67 | +}; |
| 68 | +#endif |
| 69 | + |
| 70 | +template <typename T> using make_real_t = typename make_real<T>::type; |
| 71 | + |
| 72 | +template <typename T> LIBC_INLINE constexpr T conjugate(T c) { |
| 73 | + Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c); |
| 74 | + c_c.imag = -c_c.imag; |
| 75 | + return cpp::bit_cast<T>(c_c); |
| 76 | +} |
| 77 | + |
19 | 78 | } // namespace LIBC_NAMESPACE_DECL |
20 | 79 | #endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H |
0 commit comments