Skip to content

Commit ffe04e0

Browse files
authored
[libc][complex] implement different flavors of creal and cimag functions (#113300)
I have commented out the test for `neg_zero`(creal) because : 1. real(neg_zero + 0.0i) equals zero. 2. real(neg_zero - 0.0i) equals neg_zero. I am not sure if this is the intended behaviour. [EDIT] I have updated tests for `neg_zero` (creal) to be : ``` EXPECT_FP_EQ(func(CFPT(neg_zero - zero * 1.0i)), neg_zero); EXPECT_FP_EQ(func(CFPT(neg_zero + zero * 1.0i)), zero); ``` because all three [gcc, clang and GNU MPC] also give the same result. https://godbolt.org/z/hxhcn6aof and it seems that it is indeed the correct behaviour since Imaginary types are not supported yet, refer #113671
1 parent bf099f4 commit ffe04e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1112
-2
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
356356
endif()
357357

358358
set(TARGET_LIBM_ENTRYPOINTS
359+
# complex.h entrypoints
360+
libc.src.complex.creal
361+
libc.src.complex.crealf
362+
libc.src.complex.creall
363+
libc.src.complex.cimag
364+
libc.src.complex.cimagf
365+
libc.src.complex.cimagl
366+
359367
# fenv.h entrypoints
360368
libc.src.fenv.feclearexcept
361369
libc.src.fenv.fedisableexcept
@@ -603,6 +611,10 @@ set(TARGET_LIBM_ENTRYPOINTS
603611

604612
if(LIBC_TYPES_HAS_FLOAT16)
605613
list(APPEND TARGET_LIBM_ENTRYPOINTS
614+
# complex.h C23 _Complex _Float16 entrypoints
615+
libc.src.complex.crealf16
616+
libc.src.complex.cimagf16
617+
606618
# math.h C23 _Float16 entrypoints
607619
libc.src.math.canonicalizef16
608620
libc.src.math.ceilf16
@@ -704,6 +716,10 @@ endif()
704716

705717
if(LIBC_TYPES_HAS_FLOAT128)
706718
list(APPEND TARGET_LIBM_ENTRYPOINTS
719+
# complex.h C23 _Complex _Float128 entrypoints
720+
libc.src.complex.crealf128
721+
libc.src.complex.cimagf128
722+
707723
# math.h C23 _Float128 entrypoints
708724
libc.src.math.canonicalizef128
709725
libc.src.math.ceilf128

libc/config/linux/arm/entrypoints.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
197197
endif()
198198

199199
set(TARGET_LIBM_ENTRYPOINTS
200+
# complex.h entrypoints
201+
libc.src.complex.creal
202+
libc.src.complex.crealf
203+
libc.src.complex.creall
204+
libc.src.complex.cimag
205+
libc.src.complex.cimagf
206+
libc.src.complex.cimagl
207+
200208
# fenv.h entrypoints
201209
libc.src.fenv.feclearexcept
202210
libc.src.fenv.fedisableexcept

libc/config/linux/riscv/entrypoints.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
355355
endif()
356356

357357
set(TARGET_LIBM_ENTRYPOINTS
358+
# complex.h entrypoints
359+
libc.src.complex.creal
360+
libc.src.complex.crealf
361+
libc.src.complex.creall
362+
libc.src.complex.cimag
363+
libc.src.complex.cimagf
364+
libc.src.complex.cimagl
365+
358366
# fenv.h entrypoints
359367
libc.src.fenv.feclearexcept
360368
libc.src.fenv.fedisableexcept
@@ -606,6 +614,10 @@ set(TARGET_LIBM_ENTRYPOINTS
606614

607615
if(LIBC_TYPES_HAS_FLOAT128)
608616
list(APPEND TARGET_LIBM_ENTRYPOINTS
617+
# complex.h C23 _Complex _Float128 entrypoints
618+
libc.src.complex.crealf128
619+
libc.src.complex.cimagf128
620+
609621
# math.h C23 _Float128 entrypoints
610622
libc.src.math.canonicalizef128
611623
libc.src.math.ceilf128

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
355355
endif()
356356

357357
set(TARGET_LIBM_ENTRYPOINTS
358+
# complex.h entrypoints
359+
libc.src.complex.creal
360+
libc.src.complex.crealf
361+
libc.src.complex.creall
362+
libc.src.complex.cimag
363+
libc.src.complex.cimagf
364+
libc.src.complex.cimagl
365+
358366
# fenv.h entrypoints
359367
libc.src.fenv.feclearexcept
360368
libc.src.fenv.fedisableexcept
@@ -606,6 +614,10 @@ set(TARGET_LIBM_ENTRYPOINTS
606614

607615
if(LIBC_TYPES_HAS_FLOAT16)
608616
list(APPEND TARGET_LIBM_ENTRYPOINTS
617+
# complex.h C23 _Complex _Float16 entrypoints
618+
libc.src.complex.crealf16
619+
libc.src.complex.cimagf16
620+
609621
# math.h C23 _Float16 entrypoints
610622
libc.src.math.canonicalizef16
611623
libc.src.math.ceilf16
@@ -709,6 +721,10 @@ endif()
709721

710722
if(LIBC_TYPES_HAS_FLOAT128)
711723
list(APPEND TARGET_LIBM_ENTRYPOINTS
724+
# complex.h C23 _Complex _Float128 entrypoints
725+
libc.src.complex.crealf128
726+
libc.src.complex.cimagf128
727+
712728
# math.h C23 _Float128 entrypoints
713729
libc.src.math.canonicalizef128
714730
libc.src.math.ceilf128

libc/docs/complex.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ Functions
5555
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
5656
| carg | | | | | | 7.3.9.1 | N/A |
5757
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
58-
| cimag | | | | | | 7.3.9.2 | N/A |
58+
| cimag | |check| | |check| | |check| | |check| | |check| | 7.3.9.2 | N/A |
5959
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
6060
| conj | | | | | | 7.3.9.4 | N/A |
6161
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
6262
| cproj | | | | | | 7.3.9.5 | N/A |
6363
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
64-
| creal | | | | | | 7.3.9.6 | N/A |
64+
| creal | |check| | |check| | |check| | |check| | |check| | 7.3.9.6 | N/A |
6565
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_subdirectory(__support)
22

3+
add_subdirectory(complex)
34
add_subdirectory(ctype)
45
add_subdirectory(dlfcn)
56
add_subdirectory(errno)

libc/src/__support/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ add_header_library(
209209
libc.src.__support.CPP.limits
210210
)
211211

212+
add_header_library(
213+
complex_type
214+
HDRS
215+
complex_type.h
216+
)
217+
212218
add_header_library(
213219
integer_operations
214220
HDRS

libc/src/__support/complex_type.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- complex type --------------------------------------------*- 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___SUPPORT_COMPLEX_TYPE_H
10+
#define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
template <typename T> struct Complex {
16+
T real;
17+
T imag;
18+
};
19+
} // namespace LIBC_NAMESPACE_DECL
20+
#endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H

libc/src/complex/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
add_subdirectory(generic)
2+
3+
function(add_complex_entrypoint_object name)
4+
get_fq_target_name("generic.${name}" fq_generic_target_name)
5+
if(TARGET ${fq_generic_target_name})
6+
add_entrypoint_object(
7+
${name}
8+
ALIAS
9+
DEPENDS
10+
.generic.${name}
11+
)
12+
return()
13+
endif()
14+
endfunction()
15+
16+
add_complex_entrypoint_object(creal)
17+
add_complex_entrypoint_object(crealf)
18+
add_complex_entrypoint_object(creall)
19+
add_complex_entrypoint_object(crealf16)
20+
add_complex_entrypoint_object(crealf128)
21+
22+
add_complex_entrypoint_object(cimag)
23+
add_complex_entrypoint_object(cimagf)
24+
add_complex_entrypoint_object(cimagl)
25+
add_complex_entrypoint_object(cimagf16)
26+
add_complex_entrypoint_object(cimagf128)

libc/src/complex/cimag.h

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

libc/src/complex/cimagf.h

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

libc/src/complex/cimagf128.h

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

libc/src/complex/cimagf16.h

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

libc/src/complex/cimagl.h

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

libc/src/complex/creal.h

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

libc/src/complex/crealf.h

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

libc/src/complex/crealf128.h

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

0 commit comments

Comments
 (0)