Skip to content

Commit 78c2b6d

Browse files
authored
[libc][complex] Implement different flavors of the conj function (#118671)
Refer section 7.3.9.4 of ISO/IEC 9899:2023
1 parent 2c0b8b1 commit 78c2b6d

27 files changed

+654
-6
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ set(TARGET_LIBM_ENTRYPOINTS
365365
libc.src.complex.cimag
366366
libc.src.complex.cimagf
367367
libc.src.complex.cimagl
368+
libc.src.complex.conj
369+
libc.src.complex.conjf
370+
libc.src.complex.conjl
368371

369372
# fenv.h entrypoints
370373
libc.src.fenv.feclearexcept
@@ -616,6 +619,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
616619
# complex.h C23 _Complex _Float16 entrypoints
617620
# libc.src.complex.crealf16
618621
# libc.src.complex.cimagf16
622+
# libc.src.complex.conjf16
619623

620624
# math.h C23 _Float16 entrypoints
621625
libc.src.math.canonicalizef16
@@ -721,6 +725,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
721725
# complex.h C23 _Complex _Float128 entrypoints
722726
libc.src.complex.crealf128
723727
libc.src.complex.cimagf128
728+
libc.src.complex.conjf128
724729

725730
# math.h C23 _Float128 entrypoints
726731
libc.src.math.canonicalizef128

libc/config/linux/arm/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ set(TARGET_LIBM_ENTRYPOINTS
207207
libc.src.complex.cimag
208208
libc.src.complex.cimagf
209209
libc.src.complex.cimagl
210+
libc.src.complex.conj
211+
libc.src.complex.conjf
212+
libc.src.complex.conjl
210213

211214
# fenv.h entrypoints
212215
libc.src.fenv.feclearexcept

libc/config/linux/riscv/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ set(TARGET_LIBM_ENTRYPOINTS
363363
libc.src.complex.cimag
364364
libc.src.complex.cimagf
365365
libc.src.complex.cimagl
366+
libc.src.complex.conj
367+
libc.src.complex.conjf
368+
libc.src.complex.conjl
366369

367370
# fenv.h entrypoints
368371
libc.src.fenv.feclearexcept
@@ -618,6 +621,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
618621
# complex.h C23 _Complex _Float128 entrypoints
619622
libc.src.complex.crealf128
620623
libc.src.complex.cimagf128
624+
libc.src.complex.conjf128
621625

622626
# math.h C23 _Float128 entrypoints
623627
libc.src.math.canonicalizef128

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ set(TARGET_LIBM_ENTRYPOINTS
365365
libc.src.complex.cimag
366366
libc.src.complex.cimagf
367367
libc.src.complex.cimagl
368+
libc.src.complex.conj
369+
libc.src.complex.conjf
370+
libc.src.complex.conjl
368371

369372
# fenv.h entrypoints
370373
libc.src.fenv.feclearexcept
@@ -621,6 +624,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
621624
# complex.h C23 _Complex _Float16 entrypoints
622625
libc.src.complex.crealf16
623626
libc.src.complex.cimagf16
627+
libc.src.complex.conjf16
624628

625629
# math.h C23 _Float16 entrypoints
626630
libc.src.math.canonicalizef16
@@ -730,6 +734,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
730734
# complex.h C23 _Complex _Float128 entrypoints
731735
# libc.src.complex.crealf128
732736
# libc.src.complex.cimagf128
737+
# libc.src.complex.conjf128
733738

734739
# math.h C23 _Float128 entrypoints
735740
libc.src.math.canonicalizef128

libc/docs/headers/complex.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Functions
5757
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
5858
| cimag | |check| | |check| | |check| | |check| | |check| | 7.3.9.2 | N/A |
5959
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
60-
| conj | | | | | | 7.3.9.4 | N/A |
60+
| conj | |check| | |check| | |check| | |check| | |check| | 7.3.9.4 | N/A |
6161
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
6262
| cproj | | | | | | 7.3.9.5 | N/A |
6363
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/src/__support/complex_type.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,70 @@
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
};
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+
1978
} // namespace LIBC_NAMESPACE_DECL
2079
#endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H

libc/src/complex/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ add_complex_entrypoint_object(cimagf)
2424
add_complex_entrypoint_object(cimagl)
2525
add_complex_entrypoint_object(cimagf16)
2626
add_complex_entrypoint_object(cimagf128)
27+
28+
add_complex_entrypoint_object(conj)
29+
add_complex_entrypoint_object(conjf)
30+
add_complex_entrypoint_object(conjl)
31+
add_complex_entrypoint_object(conjf16)
32+
add_complex_entrypoint_object(conjf128)

libc/src/complex/conj.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for conj --------------------------*- 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+
9+
#ifndef LLVM_LIBC_SRC_COMPLEX_CONJ_H
10+
#define LLVM_LIBC_SRC_COMPLEX_CONJ_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
_Complex double conj(_Complex double x);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_COMPLEX_CONJ_H

libc/src/complex/conjf.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for conjf -------------------------*- 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+
9+
#ifndef LLVM_LIBC_SRC_COMPLEX_CONJF_H
10+
#define LLVM_LIBC_SRC_COMPLEX_CONJF_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
_Complex float conjf(_Complex float x);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_COMPLEX_CONJF_H

libc/src/complex/conjf128.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation header for conjf128 ----------------------*- 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+
9+
#include "src/__support/macros/properties/complex_types.h"
10+
#include "src/__support/macros/properties/types.h"
11+
12+
#if defined(LIBC_TYPES_HAS_CFLOAT128)
13+
14+
#ifndef LLVM_LIBC_SRC_COMPLEX_CONJF128_H
15+
#define LLVM_LIBC_SRC_COMPLEX_CONJF128_H
16+
17+
#include "src/__support/macros/config.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
cfloat128 conjf128(cfloat128 x);
22+
23+
} // namespace LIBC_NAMESPACE_DECL
24+
25+
#endif // LLVM_LIBC_SRC_COMPLEX_CONJF128_H
26+
27+
#endif // LIBC_TYPES_HAS_CFLOAT128

libc/src/complex/conjf16.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation header for conjf16 -----------------------*- 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+
9+
#include "src/__support/macros/properties/complex_types.h"
10+
#include "src/__support/macros/properties/types.h"
11+
12+
#if defined(LIBC_TYPES_HAS_CFLOAT16)
13+
14+
#ifndef LLVM_LIBC_SRC_COMPLEX_CONJF16_H
15+
#define LLVM_LIBC_SRC_COMPLEX_CONJF16_H
16+
17+
#include "src/__support/macros/config.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
cfloat16 conjf16(cfloat16 x);
22+
23+
} // namespace LIBC_NAMESPACE_DECL
24+
25+
#endif // LLVM_LIBC_SRC_COMPLEX_CONJF16_H
26+
27+
#endif // LIBC_TYPES_HAS_CFLOAT16

libc/src/complex/conjl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for conjl -------------------------*- 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+
9+
#ifndef LLVM_LIBC_SRC_COMPLEX_CONJL_H
10+
#define LLVM_LIBC_SRC_COMPLEX_CONJL_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
_Complex long double conjl(_Complex long double x);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_COMPLEX_CONJL_H

libc/src/complex/generic/CMakeLists.txt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,72 @@
1+
add_entrypoint_object(
2+
conj
3+
SRCS
4+
conj.cpp
5+
HDRS
6+
../conj.h
7+
COMPILE_OPTIONS
8+
${libc_opt_high_flag}
9+
DEPENDS
10+
libc.src.__support.CPP.bit
11+
libc.src.__support.complex_type
12+
)
13+
14+
add_entrypoint_object(
15+
conjf
16+
SRCS
17+
conjf.cpp
18+
HDRS
19+
../conjf.h
20+
COMPILE_OPTIONS
21+
${libc_opt_high_flag}
22+
DEPENDS
23+
libc.src.__support.CPP.bit
24+
libc.src.__support.complex_type
25+
)
26+
27+
add_entrypoint_object(
28+
conjl
29+
SRCS
30+
conjl.cpp
31+
HDRS
32+
../conjl.h
33+
COMPILE_OPTIONS
34+
${libc_opt_high_flag}
35+
DEPENDS
36+
libc.src.__support.CPP.bit
37+
libc.src.__support.complex_type
38+
)
39+
40+
add_entrypoint_object(
41+
conjf16
42+
SRCS
43+
conjf16.cpp
44+
HDRS
45+
../conjf16.h
46+
COMPILE_OPTIONS
47+
${libc_opt_high_flag}
48+
DEPENDS
49+
libc.src.__support.CPP.bit
50+
libc.src.__support.complex_type
51+
libc.src.__support.macros.properties.types
52+
libc.src.__support.macros.properties.complex_types
53+
)
54+
55+
add_entrypoint_object(
56+
conjf128
57+
SRCS
58+
conjf128.cpp
59+
HDRS
60+
../conjf128.h
61+
COMPILE_OPTIONS
62+
${libc_opt_high_flag}
63+
DEPENDS
64+
libc.src.__support.CPP.bit
65+
libc.src.__support.complex_type
66+
libc.src.__support.macros.properties.types
67+
libc.src.__support.macros.properties.complex_types
68+
)
69+
170
add_entrypoint_object(
271
creal
372
SRCS

libc/src/complex/generic/conj.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of conj function -----------------------------------===//
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+
#include "src/complex/conj.h"
10+
#include "src/__support/CPP/bit.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/complex_type.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(_Complex double, conj, (_Complex double x)) {
17+
return conjugate<_Complex double>(x);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)