xref: /llvm-project/libc/src/__support/complex_basic_ops.h (revision 7f37b34d31914120a5bb6bd341e7616773df7613)
1*7f37b34dSShourya Goel //===-- complex basic operations --------------------------------*- C++ -*-===//
2*7f37b34dSShourya Goel //
3*7f37b34dSShourya Goel // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7f37b34dSShourya Goel // See https://llvm.org/LICENSE.txt for license information.
5*7f37b34dSShourya Goel // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7f37b34dSShourya Goel //
7*7f37b34dSShourya Goel //===----------------------------------------------------------------------===//
8*7f37b34dSShourya Goel 
9*7f37b34dSShourya Goel #ifndef LLVM_LIBC_SRC___SUPPORT_COMPLEX_BASIC_OPERATIONS_H
10*7f37b34dSShourya Goel #define LLVM_LIBC_SRC___SUPPORT_COMPLEX_BASIC_OPERATIONS_H
11*7f37b34dSShourya Goel 
12*7f37b34dSShourya Goel #include "complex_type.h"
13*7f37b34dSShourya Goel #include "src/__support/CPP/bit.h"
14*7f37b34dSShourya Goel #include "src/__support/FPUtil/FPBits.h"
15*7f37b34dSShourya Goel 
16*7f37b34dSShourya Goel namespace LIBC_NAMESPACE_DECL {
17*7f37b34dSShourya Goel 
18*7f37b34dSShourya Goel template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
19*7f37b34dSShourya Goel   Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
20*7f37b34dSShourya Goel   c_c.imag = -c_c.imag;
21*7f37b34dSShourya Goel   return cpp::bit_cast<T>(c_c);
22*7f37b34dSShourya Goel }
23*7f37b34dSShourya Goel 
24*7f37b34dSShourya Goel template <typename T> LIBC_INLINE constexpr T project(T c) {
25*7f37b34dSShourya Goel   using real_t = make_real_t<T>;
26*7f37b34dSShourya Goel   Complex<real_t> c_c = cpp::bit_cast<Complex<real_t>>(c);
27*7f37b34dSShourya Goel   if (fputil::FPBits<real_t>(c_c.real).is_inf() ||
28*7f37b34dSShourya Goel       fputil::FPBits<real_t>(c_c.imag).is_inf())
29*7f37b34dSShourya Goel     return cpp::bit_cast<T>(
30*7f37b34dSShourya Goel         Complex<real_t>{(fputil::FPBits<real_t>::inf(Sign::POS).get_val()),
31*7f37b34dSShourya Goel                         static_cast<real_t>(c_c.imag > 0 ? 0.0 : -0.0)});
32*7f37b34dSShourya Goel   return c;
33*7f37b34dSShourya Goel }
34*7f37b34dSShourya Goel 
35*7f37b34dSShourya Goel } // namespace LIBC_NAMESPACE_DECL
36*7f37b34dSShourya Goel #endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_BASIC_OPERATIONS_H
37