HiRep 0.1
Loading...
Searching...
No Matches
gpu_complex.hpp
1/***************************************************************************\
2* Copyright (c) 2008-2024, Sofie Martins, Erik Kjellgren, Claudio Pica *
3* All rights reserved. *
4\***************************************************************************/
5
11#ifndef GPU_COMPLEX_H
12#define GPU_COMPLEX_H
13
14#include "Core/gpu.h"
15
16template <class T> struct __align__(sizeof(T)) hr_complex_t {
17 T re, im;
18
20 constexpr visible hr_complex_t(const T a = (T)0, const T b = (T)0)
21 : re(a)
22 , im(b) {
23 }
24
25 template <class L>
26 constexpr visible hr_complex_t(const hr_complex_t<L> a)
27 : re(a.re)
28 , im(a.im) {
29 }
30
32 template <class L> hr_complex_t visible inline __attribute__((always_inline)) &operator=(const L x) {
33 re = x;
34 im = 0;
35 return *this;
36 }
37
38 template <class L> hr_complex_t visible inline __attribute__((always_inline)) &operator=(const hr_complex_t<L> x) {
39 re = x.re;
40 im = x.im;
41 return *this;
42 }
43
44 hr_complex_t visible inline __attribute__((always_inline)) operator-(void) const {
45 return hr_complex_t(-re, -im);
46 }
47
49 template <class L> hr_complex_t visible inline __attribute__((always_inline)) &operator+=(const L x) {
50 re += x;
51 return *this;
52 }
53
54 template <class L> hr_complex_t visible inline __attribute__((always_inline)) &operator+=(const hr_complex_t<L> x) {
55 re += x.re;
56 im += x.im;
57 return *this;
58 }
59
61 template <class L> hr_complex_t visible inline __attribute__((always_inline)) &operator-=(const L x) {
62 re -= x;
63 return *this;
64 }
65
66 template <class L> auto visible inline __attribute__((always_inline)) &operator-=(const hr_complex_t<L> x) {
67 re -= x.re;
68 im -= x.im;
69 return *this;
70 }
71
73 template <class L> auto visible inline __attribute__((always_inline)) &operator*=(const L x) {
74 re *= x;
75 im *= x;
76 return *this;
77 }
78
79 template <class L> auto visible inline __attribute__((always_inline)) &operator*=(const hr_complex_t<L> x) {
80 T re_new = re * x.re - im * x.im;
81 T im_new = re * x.im + im * x.re;
82 re = re_new;
83 im = im_new;
84 return *this;
85 }
86
88 template <class L> auto visible inline __attribute__((always_inline)) &operator/=(const L x) {
89 re /= x;
90 im /= x;
91 return *this;
92 }
93
94 template <class L> auto visible inline __attribute__((always_inline)) &operator/=(const hr_complex_t<L> x) {
95 T re_new = (re * x.re + im * x.im) / (x.re * x.re + x.im * x.im);
96 T im_new = (im * x.re - re * x.im) / (x.re * x.re + x.im * x.im);
97 re = re_new;
98 im = im_new;
99 return *this;
100 }
101
103 hr_complex_t visible inline __attribute__((always_inline)) conj() {
104 return hr_complex_t(re, -im);
105 }
106};
107
109template <class L, class T> auto visible inline __attribute__((always_inline)) operator+(const hr_complex_t<L> &c, const T x) {
110 return hr_complex_t(c.re + x, c.im + (T)0);
111}
112
113template <class L, class T> auto visible inline __attribute__((always_inline)) operator+(const L x, const hr_complex_t<T> &c) {
114 return hr_complex_t(c.re + x, c.im + (L)0);
115}
116
117template <class L, class T>
118auto visible inline __attribute__((always_inline)) operator+(const hr_complex_t<L> &x, const hr_complex_t<T> &c) {
119 return hr_complex_t(c.re + x.re, c.im + x.im);
120}
121
123template <class T, class L> auto visible inline __attribute__((always_inline)) operator-(const hr_complex_t<T> &c, const L x) {
124 return hr_complex_t(c.re - x, c.im - (L)0);
125}
126
127template <class T, class L> auto visible inline __attribute__((always_inline)) operator-(const T x, const hr_complex_t<L> &c) {
128 return hr_complex_t(x - c.re, (T)0 - c.im);
129}
130
131template <class T, class L>
132auto visible inline __attribute__((always_inline)) operator-(const hr_complex_t<L> &x, const hr_complex_t<T> &c) {
133 return hr_complex_t(x.re - c.re, x.im - c.im);
134}
135
137template <class T, class L> auto visible inline __attribute__((always_inline)) operator*(const hr_complex_t<T> &c, const L x) {
138 return hr_complex_t(c.re * x, c.im * x);
139}
140
141template <class T, class L> auto visible inline __attribute__((always_inline)) operator*(const T x, const hr_complex_t<L> &c) {
142 return hr_complex_t(c.re * x, c.im * x);
143}
144
145template <class T, class L>
146auto visible inline __attribute__((always_inline)) operator*(const hr_complex_t<T> &a, const hr_complex_t<L> &b) {
147 return hr_complex_t(a.re * b.re - b.im * a.im, a.im * b.re + b.im * a.re);
148}
149
151template <class T, class L> auto visible inline __attribute__((always_inline)) operator/(const hr_complex_t<T> &c, const L x) {
152 return hr_complex_t(c.re / x, c.im / x);
153}
154
155template <class T, class L> auto visible inline __attribute__((always_inline)) operator/(const T x, const hr_complex_t<L> &c) {
156 return hr_complex_t(x * c.re / (c.re * c.re + c.im * c.im), -x * c.im / (c.re * c.re + c.im * c.im));
157}
158
159template <class T, class L>
160auto visible inline __attribute__((always_inline)) operator/(const hr_complex_t<T> x, const hr_complex_t<L> c) {
161 return hr_complex_t((x.re * c.re + x.im * c.im) / (c.re * c.re + c.im * c.im),
162 (x.im * c.re - x.re * c.im) / (c.re * c.re + c.im * c.im));
163}
164
165typedef struct hr_complex_t<double> hr_complex;
166typedef struct hr_complex_t<float> hr_complex_flt;
167
168#ifndef HIP
169#define I (hr_complex_flt(0.0f, 1.0f))
170#else
171constexpr hr_complex I = hr_complex_flt(0.0f, 1.0f);
172#endif
173#define creal(a) ((a).re)
174#define cimag(a) ((a).im)
175#define conj(a) ((a).conj())
176
177visible double carg(hr_complex c);
178
179visible hr_complex cpow(hr_complex c, double pow);
180
181#endif
Basic gpu imports and structs. Include this in files that define GPU logic.