• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- complex basic operations --------------------------------*- 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_BASIC_OPERATIONS_H
10 #define LLVM_LIBC_SRC___SUPPORT_COMPLEX_BASIC_OPERATIONS_H
11 
12 #include "complex_type.h"
13 #include "src/__support/CPP/bit.h"
14 #include "src/__support/FPUtil/FPBits.h"
15 
16 namespace LIBC_NAMESPACE_DECL {
17 
conjugate(T c)18 template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
19   Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
20   c_c.imag = -c_c.imag;
21   return cpp::bit_cast<T>(c_c);
22 }
23 
project(T c)24 template <typename T> LIBC_INLINE constexpr T project(T c) {
25   using real_t = make_real_t<T>;
26   Complex<real_t> c_c = cpp::bit_cast<Complex<real_t>>(c);
27   if (fputil::FPBits<real_t>(c_c.real).is_inf() ||
28       fputil::FPBits<real_t>(c_c.imag).is_inf())
29     return cpp::bit_cast<T>(
30         Complex<real_t>{(fputil::FPBits<real_t>::inf(Sign::POS).get_val()),
31                         static_cast<real_t>(c_c.imag > 0 ? 0.0 : -0.0)});
32   return c;
33 }
34 
35 } // namespace LIBC_NAMESPACE_DECL
36 #endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_BASIC_OPERATIONS_H
37