23 #ifndef HIP_INCLUDE_HIP_AMD_DETAIL_HIP_COMPLEX_H
24 #define HIP_INCLUDE_HIP_AMD_DETAIL_HIP_COMPLEX_H
28 #if defined(__HIPCC_RTC__)
29 #define __HOST_DEVICE__ __device__
31 #define __HOST_DEVICE__ __host__ __device__
40 #endif // !defined(__HIPCC_RTC__)
43 #define COMPLEX_NEG_OP_OVERLOAD(type) \
44 __HOST_DEVICE__ static inline type operator-(const type& op) { \
51 #define COMPLEX_EQ_OP_OVERLOAD(type) \
52 __HOST_DEVICE__ static inline bool operator==(const type& lhs, const type& rhs) { \
53 return lhs.x == rhs.x && lhs.y == rhs.y; \
56 #define COMPLEX_NE_OP_OVERLOAD(type) \
57 __HOST_DEVICE__ static inline bool operator!=(const type& lhs, const type& rhs) { \
58 return !(lhs == rhs); \
61 #define COMPLEX_ADD_OP_OVERLOAD(type) \
62 __HOST_DEVICE__ static inline type operator+(const type& lhs, const type& rhs) { \
64 ret.x = lhs.x + rhs.x; \
65 ret.y = lhs.y + rhs.y; \
69 #define COMPLEX_SUB_OP_OVERLOAD(type) \
70 __HOST_DEVICE__ static inline type operator-(const type& lhs, const type& rhs) { \
72 ret.x = lhs.x - rhs.x; \
73 ret.y = lhs.y - rhs.y; \
77 #define COMPLEX_MUL_OP_OVERLOAD(type) \
78 __HOST_DEVICE__ static inline type operator*(const type& lhs, const type& rhs) { \
80 ret.x = lhs.x * rhs.x - lhs.y * rhs.y; \
81 ret.y = lhs.x * rhs.y + lhs.y * rhs.x; \
85 #define COMPLEX_DIV_OP_OVERLOAD(type) \
86 __HOST_DEVICE__ static inline type operator/(const type& lhs, const type& rhs) { \
88 ret.x = (lhs.x * rhs.x + lhs.y * rhs.y); \
89 ret.y = (rhs.x * lhs.y - lhs.x * rhs.y); \
90 ret.x = ret.x / (rhs.x * rhs.x + rhs.y * rhs.y); \
91 ret.y = ret.y / (rhs.x * rhs.x + rhs.y * rhs.y); \
95 #define COMPLEX_ADD_PREOP_OVERLOAD(type) \
96 __HOST_DEVICE__ static inline type& operator+=(type& lhs, const type& rhs) { \
102 #define COMPLEX_SUB_PREOP_OVERLOAD(type) \
103 __HOST_DEVICE__ static inline type& operator-=(type& lhs, const type& rhs) { \
109 #define COMPLEX_MUL_PREOP_OVERLOAD(type) \
110 __HOST_DEVICE__ static inline type& operator*=(type& lhs, const type& rhs) { \
115 #define COMPLEX_DIV_PREOP_OVERLOAD(type) \
116 __HOST_DEVICE__ static inline type& operator/=(type& lhs, const type& rhs) { \
121 #define COMPLEX_SCALAR_PRODUCT(type, type1) \
122 __HOST_DEVICE__ static inline type operator*(const type& lhs, type1 rhs) { \
124 ret.x = lhs.x * rhs; \
125 ret.y = lhs.y * rhs; \
133 __HOST_DEVICE__
static inline float hipCrealf(
hipFloatComplex z) {
return z.x; }
135 __HOST_DEVICE__
static inline float hipCimagf(
hipFloatComplex z) {
return z.y; }
137 __HOST_DEVICE__
static inline hipFloatComplex make_hipFloatComplex(
float a,
float b) {
152 return z.x * z.x + z.y * z.y;
156 return make_hipFloatComplex(p.x + q.x, p.y + q.y);
160 return make_hipFloatComplex(p.x - q.x, p.y - q.y);
164 return make_hipFloatComplex(p.x * q.x - p.y * q.y, p.y * q.x + p.x * q.y);
168 float sqabs = hipCsqabsf(q);
170 ret.x = (p.x * q.x + p.y * q.y) / sqabs;
171 ret.y = (p.y * q.x - p.x * q.y) / sqabs;
175 __HOST_DEVICE__
static inline float hipCabsf(
hipFloatComplex z) {
return sqrtf(hipCsqabsf(z)); }
180 __HOST_DEVICE__
static inline double hipCreal(
hipDoubleComplex z) {
return z.x; }
182 __HOST_DEVICE__
static inline double hipCimag(
hipDoubleComplex z) {
return z.y; }
184 __HOST_DEVICE__
static inline hipDoubleComplex make_hipDoubleComplex(
double a,
double b) {
199 return z.x * z.x + z.y * z.y;
203 return make_hipDoubleComplex(p.x + q.x, p.y + q.y);
207 return make_hipDoubleComplex(p.x - q.x, p.y - q.y);
211 return make_hipDoubleComplex(p.x * q.x - p.y * q.y, p.y * q.x + p.x * q.y);
215 double sqabs = hipCsqabs(q);
217 ret.x = (p.x * q.x + p.y * q.y) / sqabs;
218 ret.y = (p.y * q.x - p.x * q.y) / sqabs;
222 __HOST_DEVICE__
static inline double hipCabs(
hipDoubleComplex z) {
return sqrt(hipCsqabs(z)); }
276 __HOST_DEVICE__
static inline hipComplex make_hipComplex(
float x,
float y) {
277 return make_hipFloatComplex(x, y);
281 return make_hipFloatComplex((
float)z.x, (
float)z.y);
285 return make_hipDoubleComplex((
double)z.x, (
double)z.y);
289 float real = (p.x * q.x) + r.x;
290 float imag = (q.x * p.y) + r.y;
292 real = -(p.y * q.y) + real;
293 imag = (p.x * q.y) + imag;
295 return make_hipComplex(real, imag);
300 double real = (p.x * q.x) + r.x;
301 double imag = (q.x * p.y) + r.y;
303 real = -(p.y * q.y) + real;
304 imag = (p.x * q.y) + imag;
306 return make_hipDoubleComplex(real, imag);
309 #endif //HIP_INCLUDE_HIP_AMD_DETAIL_HIP_COMPLEX_H