1 
2 /* Compiler implementation of the D programming language
3  * Copyright (C) 1999-2019 by The D Language Foundation, All Rights Reserved
4  * written by Walter Bright
5  * http://www.digitalmars.com
6  * Distributed under the Boost Software License, Version 1.0.
7  * http://www.boost.org/LICENSE_1_0.txt
8  * https://github.com/dlang/dmd/blob/master/src/dmd/complex_t.h
9  */
10 
11 #pragma once
12 
13 #include "root/ctfloat.h"
14 
15 /* Roll our own complex type for compilers that don't support complex
16  */
17 
18 struct complex_t
19 {
20     real_t re;
21     real_t im;
22 
complex_tcomplex_t23     complex_t(real_t re) : re(re), im(ldouble(0)) {}
complex_tcomplex_t24     complex_t(real_t re, real_t im) : re(re), im(im) {}
25 
26     complex_t operator + (complex_t y) { return complex_t(re + y.re, im + y.im); }
27     complex_t operator - (complex_t y) { return complex_t(re - y.re, im - y.im); }
28     complex_t operator - () { return complex_t(-re, -im); }
29     complex_t operator * (complex_t y) { return complex_t(re * y.re - im * y.im, im * y.re + re * y.im); }
30 
31     complex_t operator / (complex_t y)
32     {
33         if (CTFloat::fabs(y.re) < CTFloat::fabs(y.im))
34         {
35             real_t r = y.re / y.im;
36             real_t den = y.im + r * y.re;
37             return complex_t((re * r + im) / den,
38                              (im * r - re) / den);
39         }
40         else
41         {
42             real_t r = y.im / y.re;
43             real_t den = y.re + r * y.im;
44             return complex_t((re + r * im) / den,
45                              (im - r * re) / den);
46         }
47     }
48 
49     operator bool () { return re || im; }
50 
51     int operator == (complex_t y) { return re == y.re && im == y.im; }
52     int operator != (complex_t y) { return re != y.re || im != y.im; }
53 
54 private:
complex_tcomplex_t55     complex_t() : re(ldouble(0)), im(ldouble(0)) {}
56 };
57 
58 inline complex_t operator * (real_t x, complex_t y) { return complex_t(x) * y; }
59 inline complex_t operator * (complex_t x, real_t y) { return x * complex_t(y); }
60 inline complex_t operator / (complex_t x, real_t y) { return x / complex_t(y); }
61 
62 
creall(complex_t x)63 inline real_t creall(complex_t x)
64 {
65     return x.re;
66 }
67 
cimagl(complex_t x)68 inline real_t cimagl(complex_t x)
69 {
70     return x.im;
71 }
72