Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

ComplexDouble.h
Go to the documentation of this file.
1 
32 #pragma once
33 
35 
36 namespace ogdf {
37 
38 namespace sse {
39 
41 #ifdef OGDF_FME_KERNEL_USE_SSE
42 class ComplexDouble
43 {
44 public:
45  __m128d reg;
46 
47  inline ComplexDouble()
48  {
49  reg = _mm_setzero_pd();
50  }
51 
52  inline ComplexDouble(const ComplexDouble& other)
53  {
54  reg = other.reg;
55  }
56 
57  inline ComplexDouble(double x)
58  {
59  reg = _mm_setr_pd((x), (0));
60  }
61 
62  inline ComplexDouble(double x, double y)
63  {
64  reg = _mm_setr_pd((x), (y));
65  }
66 
67  inline ComplexDouble(const double* ptr)
68  {
69  reg = _mm_load_pd(ptr);
70  }
71 
72 
73  inline ComplexDouble(__m128d r) : reg(r)
74  {
75  }
76 
77  inline ComplexDouble(float x, float y)
78  {
79  reg = _mm_cvtps_pd(_mm_setr_ps((x), (y), 0, 0));
80  }
81 
84 
85  inline ComplexDouble operator+(const ComplexDouble& other) const
86  {
87  return ComplexDouble( _mm_add_pd(reg, other.reg) );
88  }
89 
90  inline ComplexDouble operator-(const ComplexDouble& other) const
91  {
92  return ComplexDouble( _mm_sub_pd(reg, other.reg) );
93  }
94 
95  inline ComplexDouble operator-(void) const
96  {
97  return ComplexDouble( _mm_sub_pd(_mm_setzero_pd(), reg) );
98  }
99 
100  inline ComplexDouble operator*(const ComplexDouble& other) const
101  {
102  // ---------------------------------
103  // | a0*b0 - a1*b1 | a0*b1 + a1*b0 |
104  // ---------------------------------
105  // bt = | b1 | b0 |
106  __m128d b_t = _mm_shuffle_pd(other.reg, other.reg, _MM_SHUFFLE2(0, 1));
107  // left = | a0*b0 | a1*b1 |
108  __m128d left = _mm_mul_pd(reg, other.reg);
109  // right = | a0*b1 | a1*b0 |
110  __m128d right = _mm_mul_pd(reg, b_t);
111  // left = | a0*b0 | -a1*b1 |
112  left = _mm_mul_pd(left, _mm_setr_pd(1.0, -1.0) ) ;
113  // left = | a0*b0 + (-a1*b1) | a0*b1 + a1*b0 |
114  return ComplexDouble( _mm_hadd_pd ( left, right ) );
115  }
116 
117  inline ComplexDouble operator/(const ComplexDouble& other) const
118  {
119  // 1/(length(other)^2 * this * other.conj;
120  // bt = | b0 | -b1 |
121  __m128d conj_reg = _mm_mul_pd(other.reg, _mm_setr_pd(1.0, -1.0) ) ;
122  // bt = | b1 | b0 |
123  __m128d b_t = _mm_shuffle_pd(conj_reg, conj_reg, _MM_SHUFFLE2(0, 1));
124  // left = | a0*b0 | a1*b1 |
125  __m128d left = _mm_mul_pd(reg, conj_reg);
126  // right = | a0*b1 | a1*b0 |
127  __m128d right = _mm_mul_pd(reg, b_t);
128  // left = | a0*b0 | -a1*b1 |
129  left = _mm_mul_pd(left, _mm_setr_pd(1.0, -1.0) ) ;
130  // left = | a0*b0 + (-a1*b1) | a0*b1 + a1*b0 |
131  __m128d product = _mm_hadd_pd ( left, right ) ;
132  // product = reg*other.reg.conj
133  // ell = b0*b0 | b1*b1
134  __m128d ell = _mm_mul_pd(conj_reg, conj_reg );
135  // ell = b0*b0 + b1*b1 | b0*b0 + b1*b1
136  ell = _mm_hadd_pd(ell, ell);
137  // ell = length^2 | length^2
138  return ComplexDouble( _mm_div_pd(product, ell));
139  }
140 
141  inline ComplexDouble operator*(double scalar) const
142  {
143  return ComplexDouble( _mm_mul_pd(reg, _mm_setr_pd(scalar, scalar)) );
144  }
145 
146  inline ComplexDouble operator/(double scalar) const
147  {
148  //double rcp = 1.0/scalar;
149  return ComplexDouble( _mm_div_pd(reg, _mm_setr_pd(scalar, scalar)) );
150  }
151 
152  inline ComplexDouble operator*(unsigned int scalar) const
153  {
154  return ComplexDouble( _mm_mul_pd(reg, _mm_setr_pd((double)scalar, (double)scalar)) );
155  }
156 
157  inline void operator+=(const ComplexDouble& other)
158  {
159  reg = _mm_add_pd(reg, other.reg);
160  }
161 
162  inline void operator-=(const ComplexDouble& other)
163  {
164  reg = _mm_sub_pd(reg, other.reg);
165  }
166 
167  inline void operator*=(const ComplexDouble& other)
168  {
169  // bt = | b1 | b0 |
170  __m128d b_t = _mm_shuffle_pd(other.reg, other.reg, _MM_SHUFFLE2(0, 1));
171  // left = | a0*b0 | a1*b1 |
172  __m128d left = _mm_mul_pd(reg, other.reg);
173  // right = | a0*b1 | a1*b0 |
174  __m128d right = _mm_mul_pd(reg, b_t);
175  // left = | a0*b0 | -a1*b1 |
176  left = _mm_mul_pd(left, _mm_setr_pd(1.0, -1.0) ) ;
177  // left = | a0*b0 + (-a1*b1) | a0*b1 + a1*b0 |
178  reg = _mm_hadd_pd ( left, right ) ;
179  }
180 
181  inline void operator*=(double scalar)
182  {
183  // (real*scalar, imag*scalar)
184  reg = _mm_mul_pd(reg, _mm_setr_pd(scalar, scalar));
185  }
186 
187  inline void operator/=(const ComplexDouble& other)
188  {
189  // 1/(length(other)^2 * this * other.conj;
190  // bt = | b0 | -b1 |
191  __m128d conj_reg = _mm_mul_pd(other.reg, _mm_setr_pd(1.0, -1.0) ) ;
192  // bt = | b1 | b0 |
193  __m128d b_t = _mm_shuffle_pd(conj_reg, conj_reg, _MM_SHUFFLE2(0, 1));
194  // left = | a0*b0 | a1*b1 |
195  __m128d left = _mm_mul_pd(reg, conj_reg);
196  // right = | a0*b1 | a1*b0 |
197  __m128d right = _mm_mul_pd(reg, b_t);
198  // left = | a0*b0 | -a1*b1 |
199  left = _mm_mul_pd(left, _mm_setr_pd(1.0, -1.0) ) ;
200  // left = | a0*b0 + (-a1*b1) | a0*b1 + a1*b0 |
201  __m128d product = _mm_hadd_pd ( left, right ) ;
202  // ell = b0*b0 | b1*b1
203  __m128d ell = _mm_mul_pd(conj_reg, conj_reg );
204  // ell = b0*b0 + b1*b1 | b0*b0 + b1*b1
205  ell = _mm_hadd_pd(ell, ell);
206  // ell = length^2 | length^2
207  reg = _mm_div_pd(product, ell);
208  }
209 
213 
214  inline double length() const
215  {
216  // sqrt(real*real + imag*imag)
217  double res;
218  __m128d r = _mm_mul_pd(reg, reg );
219  r = _mm_hadd_pd(r, _mm_setzero_pd());
220  r = _mm_sqrt_sd(r, r);
221  _mm_store_sd(&res, r);
222  return res;
223  }
224 
225  inline ComplexDouble conj() const
226  {
227  // (real, -imag)
228  return ComplexDouble( _mm_mul_pd(reg, _mm_setr_pd(1.0, -1.0) ) );
229  }
230 
234 
235  inline void operator=(const ComplexDouble& other)
236  {
237  reg = other.reg;
238  }
239 
241  inline void operator=(double* ptr)
242  {
243  reg = _mm_load_pd(ptr);
244  }
245 
249 
251  inline void load(const double* ptr)
252  {
253  reg = _mm_load_pd(ptr);
254  }
255 
257  inline void load_unaligned(const double* ptr)
258  {
259  reg = _mm_loadu_pd(ptr);
260  }
261 
263  inline void store(double* ptr) const
264  {
265  _mm_store_pd(ptr, reg);
266  }
267 
269  inline void store_unaligned(double* ptr) const
270  {
271  _mm_storeu_pd(ptr, reg);
272  }
273 
275 };
276 
277 #else
279 {
280 public:
281  double reg[2];
282 
283  inline ComplexDouble( )
284  {
285  reg[0] = 0.0;
286  reg[1] = 0.0;
287  }
288 
289  inline ComplexDouble(const ComplexDouble& other)
290  {
291  reg[0] = other.reg[0];
292  reg[1] = other.reg[1];
293  }
294 
295  inline ComplexDouble(double x)
296  {
297  reg[0] = x;
298  reg[1] = 0;
299  }
300 
301  inline ComplexDouble(double x, double y)
302  {
303  reg[0] = x;
304  reg[1] = y;
305  }
306 
307  inline ComplexDouble(double* ptr)
308  {
309  reg[0] = ptr[0];
310  reg[1] = ptr[1];
311  }
312 
315  inline ComplexDouble operator+(const ComplexDouble& other) const
316  {
317  return ComplexDouble( reg[0] + other.reg[0], reg[1] + other.reg[1] );
318  }
319 
320  inline ComplexDouble operator-(const ComplexDouble& other) const
321  {
322  return ComplexDouble( reg[0] - other.reg[0], reg[1] - other.reg[1] );
323  }
324 
325  inline ComplexDouble operator-(void) const
326  {
327  return ComplexDouble( -reg[0] , -reg[1] );
328  }
329 
330  inline ComplexDouble operator*(const ComplexDouble& other) const
331  {
332  return ComplexDouble( reg[0]*other.reg[0] - reg[1]*other.reg[1], reg[0]*other.reg[1] + reg[1]*other.reg[0] );
333  }
334 
335  inline ComplexDouble operator/(const ComplexDouble& other) const
336  {
337  return (*this) * other.conj() / (other.reg[0]*other.reg[0] + other.reg[1]*other.reg[1]);
338  }
339 
340  inline ComplexDouble operator*(double scalar) const
341  {
342  return ComplexDouble( reg[0]*scalar, reg[1]*scalar );
343  }
344 
345  inline ComplexDouble operator/(double scalar) const
346  {
347  return ComplexDouble( reg[0]/scalar, reg[1]/scalar );
348  }
349 
350  inline ComplexDouble operator*(unsigned int scalar) const
351  {
352  return ComplexDouble( reg[0]*(double)scalar, reg[1]*(double)scalar );
353  }
354 
355  inline void operator+=(const ComplexDouble& other)
356  {
357  reg[0] += other.reg[0];
358  reg[1] += other.reg[1];
359  }
360 
361  inline void operator-=(const ComplexDouble& other)
362  {
363  reg[0] -= other.reg[0];
364  reg[1] -= other.reg[1];
365  }
366 
367  inline void operator*=(const ComplexDouble& other)
368  {
369  double t[2];
370  t[0] = reg[0]*other.reg[0] - reg[1]*other.reg[1];
371  t[1] = reg[0]*other.reg[1] + reg[1]*other.reg[0];
372  reg[0] = t[0];
373  reg[1] = t[1];
374  }
375 
376  inline void operator*=(double scalar)
377  {
378  reg[0] *= scalar;
379  reg[1] *= scalar;
380  }
381 
382  inline void operator/=(const ComplexDouble& other)
383  {
384  ComplexDouble t = other.conj() / (other.reg[0]*other.reg[0] + other.reg[1]*other.reg[1]);
385  double r[2];
386  r[0] = reg[0]*t.reg[0] - reg[1]*t.reg[1];
387  r[1] = reg[0]*t.reg[1] + reg[1]*t.reg[0];
388  reg[0] = r[0];
389  reg[1] = r[1];
390  }
391 
395 
396  inline double length() const
397  {
398  // sqrt(real*real + imag*imag)
399  return sqrt(reg[0]*reg[0] + reg[1]*reg[1]);
400  }
401 
402  inline ComplexDouble conj() const
403  {
404  // (real, -imag)
405  return ComplexDouble( reg[0], -reg[1] );
406  }
407 
411 
412  inline ComplexDouble &operator=(const ComplexDouble& other)
413  {
414  reg[0] = other.reg[0];
415  reg[1] = other.reg[1];
416  return *this;
417  }
418 
420  inline ComplexDouble &operator=(double* ptr)
421  {
422  reg[0] = ptr[0];
423  reg[1] = ptr[1];
424  return *this;
425  }
426 
430 
432  inline void load(const double* ptr)
433  {
434  reg[0] = ptr[0];
435  reg[1] = ptr[1];
436  }
437 
439  inline void load_unaligned(const double* ptr)
440  {
441  reg[0] = ptr[0];
442  reg[1] = ptr[1];
443  }
444 
446  inline void store(double* ptr) const
447  {
448  ptr[0] = reg[0];
449  ptr[1] = reg[1];
450  }
451 
453  inline void store_unaligned(double* ptr) const
454  {
455  ptr[0] = reg[0];
456  ptr[1] = reg[1];
457  }
458 
460 };
461 
462 #endif
463 }
464 
465 }
Class to generate instrinsics for complex number arithmetic functions.
void operator*=(double scalar)
ComplexDouble operator+(const ComplexDouble &other) const
void load(const double *ptr)
load from 16byte aligned ptr
Definition of utility functions for FME layout.
ComplexDouble(double x, double y)
ComplexDouble operator/(double scalar) const
ComplexDouble & operator=(double *ptr)
load from 16byte aligned ptr
ComplexDouble operator*(double scalar) const
ComplexDouble operator*(unsigned int scalar) const
void load_unaligned(const double *ptr)
load from unaligned ptr
ComplexDouble(const ComplexDouble &other)
void operator/=(const ComplexDouble &other)
void operator*=(const ComplexDouble &other)
ComplexDouble & operator=(const ComplexDouble &other)
ComplexDouble operator/(const ComplexDouble &other) const
void store(double *ptr) const
store to 16byte aligned ptr
ComplexDouble operator*(const ComplexDouble &other) const
ComplexDouble conj() const
void operator-=(const ComplexDouble &other)
void operator+=(const ComplexDouble &other)
ComplexDouble operator-(const ComplexDouble &other) const
The namespace for all OGDF objects.
ComplexDouble operator-(void) const
void store_unaligned(double *ptr) const
store to unaligned ptr