Skip to content

Commit c98e79d

Browse files
authored
[libc][complex] Implement different flavors of the cproj function (#119722)
Refer section 7.3.9.5 of ISO/IEC 9899:2023
1 parent e8ce6c4 commit c98e79d

34 files changed

+619
-13
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ set(TARGET_LIBM_ENTRYPOINTS
370370
libc.src.complex.conj
371371
libc.src.complex.conjf
372372
libc.src.complex.conjl
373+
libc.src.complex.cproj
374+
libc.src.complex.cprojf
375+
libc.src.complex.cprojl
373376

374377
# fenv.h entrypoints
375378
libc.src.fenv.feclearexcept
@@ -622,6 +625,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
622625
# libc.src.complex.crealf16
623626
# libc.src.complex.cimagf16
624627
# libc.src.complex.conjf16
628+
# libc.src.complex.cprojf16
625629

626630
# math.h C23 _Float16 entrypoints
627631
libc.src.math.canonicalizef16
@@ -728,6 +732,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
728732
libc.src.complex.crealf128
729733
libc.src.complex.cimagf128
730734
libc.src.complex.conjf128
735+
libc.src.complex.cprojf128
731736

732737
# math.h C23 _Float128 entrypoints
733738
libc.src.math.canonicalizef128

libc/config/linux/arm/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ set(TARGET_LIBM_ENTRYPOINTS
212212
libc.src.complex.conj
213213
libc.src.complex.conjf
214214
libc.src.complex.conjl
215+
libc.src.complex.cproj
216+
libc.src.complex.cprojf
217+
libc.src.complex.cprojl
215218

216219
# fenv.h entrypoints
217220
libc.src.fenv.feclearexcept

libc/config/linux/riscv/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ set(TARGET_LIBM_ENTRYPOINTS
367367
libc.src.complex.conj
368368
libc.src.complex.conjf
369369
libc.src.complex.conjl
370+
libc.src.complex.cproj
371+
libc.src.complex.cprojf
372+
libc.src.complex.cprojl
370373

371374
# fenv.h entrypoints
372375
libc.src.fenv.feclearexcept
@@ -623,6 +626,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
623626
libc.src.complex.crealf128
624627
libc.src.complex.cimagf128
625628
libc.src.complex.conjf128
629+
libc.src.complex.cprojf128
626630

627631
# math.h C23 _Float128 entrypoints
628632
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
@@ -370,6 +370,9 @@ set(TARGET_LIBM_ENTRYPOINTS
370370
libc.src.complex.conj
371371
libc.src.complex.conjf
372372
libc.src.complex.conjl
373+
libc.src.complex.cproj
374+
libc.src.complex.cprojf
375+
libc.src.complex.cprojl
373376

374377
# fenv.h entrypoints
375378
libc.src.fenv.feclearexcept
@@ -627,6 +630,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
627630
libc.src.complex.crealf16
628631
libc.src.complex.cimagf16
629632
libc.src.complex.conjf16
633+
libc.src.complex.cprojf16
630634

631635
# math.h C23 _Float16 entrypoints
632636
libc.src.math.canonicalizef16
@@ -738,6 +742,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
738742
# libc.src.complex.crealf128
739743
# libc.src.complex.cimagf128
740744
# libc.src.complex.conjf128
745+
# libc.src.complex.cprojf128
741746

742747
# math.h C23 _Float128 entrypoints
743748
libc.src.math.canonicalizef128

libc/docs/headers/complex.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Functions
5959
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
6060
| conj | |check| | |check| | |check| | |check| | |check| | 7.3.9.4 | N/A |
6161
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
62-
| cproj | | | | | | 7.3.9.5 | N/A |
62+
| cproj | |check| | |check| | |check| | |check| | |check| | 7.3.9.5 | N/A |
6363
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
6464
| creal | |check| | |check| | |check| | |check| | |check| | 7.3.9.6 | N/A |
6565
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/hdrgen/yaml/complex.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,35 @@ functions:
103103
arguments:
104104
- type: cfloat128
105105
guard: LIBC_TYPES_HAS_CFLOAT128
106+
- name: cproj
107+
standards:
108+
- stdc
109+
return_type: _Complex double
110+
arguments:
111+
- type: _Complex double
112+
- name: cprojf
113+
standards:
114+
- stdc
115+
return_type: _Complex float
116+
arguments:
117+
- type: _Complex float
118+
- name: cprojl
119+
standards:
120+
- stdc
121+
return_type: _Complex long double
122+
arguments:
123+
- type: _Complex long double
124+
- name: cprojf16
125+
standards:
126+
- stdc
127+
return_type: cfloat16
128+
arguments:
129+
- type: cfloat16
130+
guard: LIBC_TYPES_HAS_CFLOAT16
131+
- name: cprojf128
132+
standards:
133+
- stdc
134+
return_type: cfloat128
135+
arguments:
136+
- type: cfloat128
137+
guard: LIBC_TYPES_HAS_CFLOAT128

libc/src/__support/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ add_header_library(
241241
complex_type
242242
HDRS
243243
complex_type.h
244+
DEPENDS
245+
libc.src.__support.CPP.bit
246+
libc.src.__support.FPUtil.fp_bits
247+
libc.src.__support.macros.properties.types
248+
libc.src.__support.macros.properties.complex_types
244249
)
245250

246251
add_header_library(

libc/src/__support/complex_type.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
1010
#define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
1111

12+
#include "src/__support/CPP/bit.h"
13+
#include "src/__support/FPUtil/FPBits.h"
1214
#include "src/__support/macros/config.h"
1315
#include "src/__support/macros/properties/complex_types.h"
1416
#include "src/__support/macros/properties/types.h"
@@ -75,5 +77,18 @@ template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
7577
return cpp::bit_cast<T>(c_c);
7678
}
7779

80+
template <typename T> LIBC_INLINE constexpr T project(T c) {
81+
using real_t = make_real_t<T>;
82+
Complex<real_t> c_c = cpp::bit_cast<Complex<real_t>>(c);
83+
if (fputil::FPBits<real_t>(c_c.real).is_inf() ||
84+
fputil::FPBits<real_t>(c_c.imag).is_inf()) {
85+
return cpp::bit_cast<T>(
86+
Complex<real_t>{(fputil::FPBits<real_t>::inf(Sign::POS).get_val()),
87+
static_cast<real_t>(c_c.imag > 0 ? 0.0 : -0.0)});
88+
} else {
89+
return c;
90+
}
91+
}
92+
7893
} // namespace LIBC_NAMESPACE_DECL
7994
#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
@@ -30,3 +30,9 @@ add_complex_entrypoint_object(conjf)
3030
add_complex_entrypoint_object(conjl)
3131
add_complex_entrypoint_object(conjf16)
3232
add_complex_entrypoint_object(conjf128)
33+
34+
add_complex_entrypoint_object(cproj)
35+
add_complex_entrypoint_object(cprojf)
36+
add_complex_entrypoint_object(cprojl)
37+
add_complex_entrypoint_object(cprojf16)
38+
add_complex_entrypoint_object(cprojf128)

libc/src/complex/conjf128.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/macros/properties/complex_types.h"
10-
#include "src/__support/macros/properties/types.h"
1110

1211
#if defined(LIBC_TYPES_HAS_CFLOAT128)
1312

libc/src/complex/conjf16.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/macros/properties/complex_types.h"
10-
#include "src/__support/macros/properties/types.h"
1110

1211
#if defined(LIBC_TYPES_HAS_CFLOAT16)
1312

libc/src/complex/cproj.h

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

libc/src/complex/cprojf.h

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

libc/src/complex/cprojf128.h

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

libc/src/complex/cprojf16.h

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

libc/src/complex/cprojl.h

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

0 commit comments

Comments
 (0)