Skip to content

[libc][complex] Implement different flavors of the conj function #118671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.complex.cimag
libc.src.complex.cimagf
libc.src.complex.cimagl
libc.src.complex.conj
libc.src.complex.conjf
libc.src.complex.conjl

# fenv.h entrypoints
libc.src.fenv.feclearexcept
Expand Down Expand Up @@ -616,6 +619,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
# complex.h C23 _Complex _Float16 entrypoints
# libc.src.complex.crealf16
# libc.src.complex.cimagf16
# libc.src.complex.conjf16

# math.h C23 _Float16 entrypoints
libc.src.math.canonicalizef16
Expand Down Expand Up @@ -721,6 +725,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
# complex.h C23 _Complex _Float128 entrypoints
libc.src.complex.crealf128
libc.src.complex.cimagf128
libc.src.complex.conjf128

# math.h C23 _Float128 entrypoints
libc.src.math.canonicalizef128
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.complex.cimag
libc.src.complex.cimagf
libc.src.complex.cimagl
libc.src.complex.conj
libc.src.complex.conjf
libc.src.complex.conjl

# fenv.h entrypoints
libc.src.fenv.feclearexcept
Expand Down
4 changes: 4 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.complex.cimag
libc.src.complex.cimagf
libc.src.complex.cimagl
libc.src.complex.conj
libc.src.complex.conjf
libc.src.complex.conjl

# fenv.h entrypoints
libc.src.fenv.feclearexcept
Expand Down Expand Up @@ -618,6 +621,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
# complex.h C23 _Complex _Float128 entrypoints
libc.src.complex.crealf128
libc.src.complex.cimagf128
libc.src.complex.conjf128

# math.h C23 _Float128 entrypoints
libc.src.math.canonicalizef128
Expand Down
5 changes: 5 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.complex.cimag
libc.src.complex.cimagf
libc.src.complex.cimagl
libc.src.complex.conj
libc.src.complex.conjf
libc.src.complex.conjl

# fenv.h entrypoints
libc.src.fenv.feclearexcept
Expand Down Expand Up @@ -621,6 +624,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
# complex.h C23 _Complex _Float16 entrypoints
libc.src.complex.crealf16
libc.src.complex.cimagf16
libc.src.complex.conjf16

# math.h C23 _Float16 entrypoints
libc.src.math.canonicalizef16
Expand Down Expand Up @@ -730,6 +734,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
# complex.h C23 _Complex _Float128 entrypoints
# libc.src.complex.crealf128
# libc.src.complex.cimagf128
# libc.src.complex.conjf128

# math.h C23 _Float128 entrypoints
libc.src.math.canonicalizef128
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/headers/complex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Functions
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| cimag | |check| | |check| | |check| | |check| | |check| | 7.3.9.2 | N/A |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| conj | | | | | | 7.3.9.4 | N/A |
| conj | |check| | |check| | |check| | |check| | |check| | 7.3.9.4 | N/A |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| cproj | | | | | | 7.3.9.5 | N/A |
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
59 changes: 59 additions & 0 deletions libc/src/__support/complex_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,70 @@
#define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H

#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/complex_types.h"
#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE_DECL {
template <typename T> struct Complex {
T real;
T imag;
};

template <typename T> struct make_complex;

template <> struct make_complex<float> {
using type = _Complex float;
};
template <> struct make_complex<double> {
using type = _Complex double;
};
template <> struct make_complex<long double> {
using type = _Complex long double;
};

#if defined(LIBC_TYPES_HAS_CFLOAT16)
template <> struct make_complex<float16> {
using type = cfloat16;
};
#endif
#if defined(LIBC_TYPES_HAS_CFLOAT128)
template <> struct make_complex<float128> {
using type = cfloat128;
};
#endif

template <typename T> using make_complex_t = typename make_complex<T>::type;

template <typename T> struct make_real;

template <> struct make_real<_Complex float> {
using type = float;
};
template <> struct make_real<_Complex double> {
using type = double;
};
template <> struct make_real<_Complex long double> {
using type = long double;
};

#if defined(LIBC_TYPES_HAS_CFLOAT16)
template <> struct make_real<cfloat16> {
using type = float16;
};
#endif
#if defined(LIBC_TYPES_HAS_CFLOAT128)
template <> struct make_real<cfloat128> {
using type = float128;
};
#endif

template <typename T> using make_real_t = typename make_real<T>::type;

template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
c_c.imag = -c_c.imag;
return cpp::bit_cast<T>(c_c);
}

} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
6 changes: 6 additions & 0 deletions libc/src/complex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ add_complex_entrypoint_object(cimagf)
add_complex_entrypoint_object(cimagl)
add_complex_entrypoint_object(cimagf16)
add_complex_entrypoint_object(cimagf128)

add_complex_entrypoint_object(conj)
add_complex_entrypoint_object(conjf)
add_complex_entrypoint_object(conjl)
add_complex_entrypoint_object(conjf16)
add_complex_entrypoint_object(conjf128)
20 changes: 20 additions & 0 deletions libc/src/complex/conj.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for conj --------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_COMPLEX_CONJ_H
#define LLVM_LIBC_SRC_COMPLEX_CONJ_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

_Complex double conj(_Complex double x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_COMPLEX_CONJ_H
20 changes: 20 additions & 0 deletions libc/src/complex/conjf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for conjf -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_COMPLEX_CONJF_H
#define LLVM_LIBC_SRC_COMPLEX_CONJF_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

_Complex float conjf(_Complex float x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_COMPLEX_CONJF_H
27 changes: 27 additions & 0 deletions libc/src/complex/conjf128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Implementation header for conjf128 ----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/macros/properties/complex_types.h"
#include "src/__support/macros/properties/types.h"

#if defined(LIBC_TYPES_HAS_CFLOAT128)

#ifndef LLVM_LIBC_SRC_COMPLEX_CONJF128_H
#define LLVM_LIBC_SRC_COMPLEX_CONJF128_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

cfloat128 conjf128(cfloat128 x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_COMPLEX_CONJF128_H

#endif // LIBC_TYPES_HAS_CFLOAT128
27 changes: 27 additions & 0 deletions libc/src/complex/conjf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- Implementation header for conjf16 -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/macros/properties/complex_types.h"
#include "src/__support/macros/properties/types.h"

#if defined(LIBC_TYPES_HAS_CFLOAT16)

#ifndef LLVM_LIBC_SRC_COMPLEX_CONJF16_H
#define LLVM_LIBC_SRC_COMPLEX_CONJF16_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

cfloat16 conjf16(cfloat16 x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_COMPLEX_CONJF16_H

#endif // LIBC_TYPES_HAS_CFLOAT16
20 changes: 20 additions & 0 deletions libc/src/complex/conjl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for conjl -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_COMPLEX_CONJL_H
#define LLVM_LIBC_SRC_COMPLEX_CONJL_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

_Complex long double conjl(_Complex long double x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_COMPLEX_CONJL_H
69 changes: 69 additions & 0 deletions libc/src/complex/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,72 @@
add_entrypoint_object(
conj
SRCS
conj.cpp
HDRS
../conj.h
COMPILE_OPTIONS
${libc_opt_high_flag}
DEPENDS
libc.src.__support.CPP.bit
libc.src.__support.complex_type
)

add_entrypoint_object(
conjf
SRCS
conjf.cpp
HDRS
../conjf.h
COMPILE_OPTIONS
${libc_opt_high_flag}
DEPENDS
libc.src.__support.CPP.bit
libc.src.__support.complex_type
)

add_entrypoint_object(
conjl
SRCS
conjl.cpp
HDRS
../conjl.h
COMPILE_OPTIONS
${libc_opt_high_flag}
DEPENDS
libc.src.__support.CPP.bit
libc.src.__support.complex_type
)

add_entrypoint_object(
conjf16
SRCS
conjf16.cpp
HDRS
../conjf16.h
COMPILE_OPTIONS
${libc_opt_high_flag}
DEPENDS
libc.src.__support.CPP.bit
libc.src.__support.complex_type
libc.src.__support.macros.properties.types
libc.src.__support.macros.properties.complex_types
)

add_entrypoint_object(
conjf128
SRCS
conjf128.cpp
HDRS
../conjf128.h
COMPILE_OPTIONS
${libc_opt_high_flag}
DEPENDS
libc.src.__support.CPP.bit
libc.src.__support.complex_type
libc.src.__support.macros.properties.types
libc.src.__support.macros.properties.complex_types
)

add_entrypoint_object(
creal
SRCS
Expand Down
20 changes: 20 additions & 0 deletions libc/src/complex/generic/conj.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of conj function -----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/complex/conj.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
#include "src/__support/complex_type.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(_Complex double, conj, (_Complex double x)) {
return conjugate<_Complex double>(x);
}

} // namespace LIBC_NAMESPACE_DECL
Loading
Loading