xref: /petsc/include/petsccxxcomplexfix.h (revision 6524c165f7ddaf30fd7457737f668f984c8ababf)
1*6524c165SJacob Faibussowitsch #ifndef PETSCCXXCOMPLEXFIX_H
239829747SLisandro Dalcin #define PETSCCXXCOMPLEXFIX_H
339829747SLisandro Dalcin 
4f636a84aSBarry Smith /*
54cdc28b3SLisandro Dalcin     The pragma below silence all compiler warnings comming from code in this header file.
64cdc28b3SLisandro Dalcin     In particular, it silences `-Wfloat-equal` warnings in `operator==()` and `operator!=` below.
74cdc28b3SLisandro Dalcin     Other compilers beyond GCC support this pragma.
84cdc28b3SLisandro Dalcin */
977ba14dbSStefano Zampini #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__NEC__)
104cdc28b3SLisandro Dalcin #pragma GCC system_header
114cdc28b3SLisandro Dalcin #endif
124cdc28b3SLisandro Dalcin 
134cdc28b3SLisandro Dalcin /*
14f636a84aSBarry Smith      Defines additional operator overloading for the C++ complex class that are "missing" in the standard
15f636a84aSBarry Smith      include files. For example, the code fragment
16f636a84aSBarry Smith 
17f636a84aSBarry Smith      std::complex<double> c = 22.0;
18f636a84aSBarry Smith      c = 11 + c;
19f636a84aSBarry Smith 
20f636a84aSBarry Smith      will produce a compile time error such as
21f636a84aSBarry Smith 
22f636a84aSBarry Smith      error: no match for 'operator+' (operand types are 'int' and 'std::complex<double>')
23f636a84aSBarry Smith 
24f636a84aSBarry Smith      The code fragment
25f636a84aSBarry Smith 
26f636a84aSBarry Smith      std::complex<float> c = 22.0;
27f636a84aSBarry Smith      c = 11.0 + c;
28f636a84aSBarry Smith 
29f636a84aSBarry Smith      will produce a compile time error such as
30f636a84aSBarry Smith 
31f636a84aSBarry Smith      error: no match for 'operator+' (operand types are 'double' and 'std::complex<float>')
32f636a84aSBarry Smith 
33f636a84aSBarry Smith      This deficiency means one may need to write cumbersome code while working with the C++ complex classes.
34f636a84aSBarry Smith 
35f636a84aSBarry Smith      This include file defines a few additional operator overload methods for the C++ complex classes to handle
36f636a84aSBarry Smith      these cases naturally within PETSc code.
37f636a84aSBarry Smith 
38a966371cSJunchao Zhang      This file is included in petscsystypes.h when feasible. In the small number of cases where these additional methods
39a966371cSJunchao Zhang      may conflict with other code one may add '#define PETSC_SKIP_CXX_COMPLEX_FIX 1' before including any PETSc include
40a966371cSJunchao Zhang      files to prevent these methods from being provided.
41f636a84aSBarry Smith */
42f636a84aSBarry Smith 
4339829747SLisandro Dalcin #define PETSC_CXX_COMPLEX_FIX(Type) \
449371c9d4SSatish Balay   static inline PetscComplex operator+(const PetscComplex &lhs, const Type &rhs) { \
459371c9d4SSatish Balay     return const_cast<PetscComplex &>(lhs) + PetscReal(rhs); \
469371c9d4SSatish Balay   } \
479371c9d4SSatish Balay   static inline PetscComplex operator+(const Type &lhs, const PetscComplex &rhs) { \
489371c9d4SSatish Balay     return PetscReal(lhs) + const_cast<PetscComplex &>(rhs); \
499371c9d4SSatish Balay   } \
509371c9d4SSatish Balay   static inline PetscComplex operator-(const PetscComplex &lhs, const Type &rhs) { \
519371c9d4SSatish Balay     return const_cast<PetscComplex &>(lhs) - PetscReal(rhs); \
529371c9d4SSatish Balay   } \
539371c9d4SSatish Balay   static inline PetscComplex operator-(const Type &lhs, const PetscComplex &rhs) { \
549371c9d4SSatish Balay     return PetscReal(lhs) - const_cast<PetscComplex &>(rhs); \
559371c9d4SSatish Balay   } \
569371c9d4SSatish Balay   static inline PetscComplex operator*(const PetscComplex &lhs, const Type &rhs) { \
579371c9d4SSatish Balay     return const_cast<PetscComplex &>(lhs) * PetscReal(rhs); \
589371c9d4SSatish Balay   } \
599371c9d4SSatish Balay   static inline PetscComplex operator*(const Type &lhs, const PetscComplex &rhs) { \
609371c9d4SSatish Balay     return PetscReal(lhs) * const_cast<PetscComplex &>(rhs); \
619371c9d4SSatish Balay   } \
629371c9d4SSatish Balay   static inline PetscComplex operator/(const PetscComplex &lhs, const Type &rhs) { \
639371c9d4SSatish Balay     return const_cast<PetscComplex &>(lhs) / PetscReal(rhs); \
649371c9d4SSatish Balay   } \
659371c9d4SSatish Balay   static inline PetscComplex operator/(const Type &lhs, const PetscComplex &rhs) { \
669371c9d4SSatish Balay     return PetscReal(lhs) / const_cast<PetscComplex &>(rhs); \
679371c9d4SSatish Balay   } \
689371c9d4SSatish Balay   static inline bool operator==(const PetscComplex &lhs, const Type &rhs) { \
699371c9d4SSatish Balay     return const_cast<PetscComplex &>(lhs).imag() == PetscReal(0) && const_cast<PetscComplex &>(lhs).real() == PetscReal(rhs); \
709371c9d4SSatish Balay   } \
719371c9d4SSatish Balay   static inline bool operator==(const Type &lhs, const PetscComplex &rhs) { \
729371c9d4SSatish Balay     return const_cast<PetscComplex &>(rhs).imag() == PetscReal(0) && const_cast<PetscComplex &>(rhs).real() == PetscReal(lhs); \
739371c9d4SSatish Balay   } \
749371c9d4SSatish Balay   static inline bool operator!=(const PetscComplex &lhs, const Type &rhs) { \
759371c9d4SSatish Balay     return const_cast<PetscComplex &>(lhs).imag() != PetscReal(0) || const_cast<PetscComplex &>(lhs).real() != PetscReal(rhs); \
769371c9d4SSatish Balay   } \
779371c9d4SSatish Balay   static inline bool operator!=(const Type &lhs, const PetscComplex &rhs) { \
789371c9d4SSatish Balay     return const_cast<PetscComplex &>(rhs).imag() != PetscReal(0) || const_cast<PetscComplex &>(rhs).real() != PetscReal(lhs); \
799371c9d4SSatish Balay   } \
8039829747SLisandro Dalcin /* PETSC_CXX_COMPLEX_FIX */
8139829747SLisandro Dalcin 
82f636a84aSBarry Smith /*
83f636a84aSBarry Smith     Due to the C++ automatic promotion rules for floating point and integer values only the two cases below
84f636a84aSBarry Smith     need to be handled.
85f636a84aSBarry Smith */
8639829747SLisandro Dalcin #if defined(PETSC_USE_REAL_SINGLE)
8739829747SLisandro Dalcin PETSC_CXX_COMPLEX_FIX(double)
8839829747SLisandro Dalcin #elif defined(PETSC_USE_REAL_DOUBLE)
8939829747SLisandro Dalcin PETSC_CXX_COMPLEX_FIX(PetscInt)
9039829747SLisandro Dalcin #endif /* PETSC_USE_REAL_* */
9139829747SLisandro Dalcin 
9239829747SLisandro Dalcin #endif
93