SAF
Loading...
Searching...
No Matches
saf_utility_complex.h
Go to the documentation of this file.
1/*
2 * Copyright 2016-2018 Leo McCormack
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
29#ifndef SAF_COMPLEX_H_INCLUDED
30#define SAF_COMPLEX_H_INCLUDED
31
32#if defined(__cplusplus)
33# include <complex>
34 typedef std::complex<float> float_complex;
35 typedef std::complex<double> double_complex;
36
37 /*
38 Single-Precision Complex Operations
39 */
40 inline float_complex cmplxf(float re, float im) {return float_complex(re, im); }
41 inline float_complex ccaddf(float_complex x, float_complex y) { return x + y; }
42 inline float_complex craddf(float_complex x, float y) { return x + y; }
43 inline float_complex ccsubf(float_complex x, float_complex y) { return x - y; }
44 inline float_complex crsubf(float_complex x, float y) { return x - y; }
45 inline float_complex ccmulf(float_complex x, float_complex y) { return x * y; }
46 inline float_complex cccmulf(float_complex x, float_complex y, float_complex z) { return x * y * z; }
47 inline float_complex crmulf(float_complex x, float y) { return x * y; }
48 inline float_complex ccdivf(float_complex x, float_complex y) { return x / y; }
49 inline float_complex crdivf(float_complex x, float y) { return x / y; }
50
51 /*
52 Double-Precision Complex Operations
53 */
54 inline double_complex cmplx(double re, double im) {return double_complex(re, im);}
55 inline double_complex ccadd(double_complex x, double_complex y) { return x + y; }
56 inline double_complex cradd(double_complex x, double y) { return x + y; }
57 inline double_complex ccsub(double_complex x, double_complex y) { return x - y; }
58 inline double_complex crsub(double_complex x, double y) { return x - y; }
59 inline double_complex ccmul(double_complex x, double_complex y) { return x * y; }
60 inline double_complex cccmul(double_complex x, double_complex y, double_complex z) { return x * y * z; }
61 inline double_complex crmul(double_complex x, double y) { return x * y; }
62 inline double_complex ccdiv(double_complex x, double_complex y) { return x / y; }
63 inline double_complex crdiv(double_complex x, double y) { return x / y; }
64
65
66#elif __STDC_VERSION__ >= 199901L
67 /* for fully C99+/C++11 compliant compilers */
68
69# include <complex.h>
70 typedef float _Complex float_complex;
71 typedef double _Complex double_complex;
72
73 /*
74 Single-Precision Complex Operations
75 */
76 float_complex cmplxf(float re, float im);
77 float_complex ccaddf(float_complex x, float_complex y);
78 float_complex craddf(float_complex x, float y);
79 float_complex ccsubf(float_complex x, float_complex y);
80 float_complex crsubf(float_complex x, float y);
81 float_complex ccmulf(float_complex x, float_complex y);
82 float_complex cccmulf(float_complex x, float_complex y, float_complex z);
83 float_complex crmulf(float_complex x, float y);
84 float_complex ccdivf(float_complex x, float_complex y);
85 float_complex crdivf(float_complex x, float y);
86
87 /*
88 Double-Precision Complex Operations
89 */
90 double_complex cmplx(double re, double im);
91 double_complex ccadd(double_complex x, double_complex y);
92 double_complex cradd(double_complex x, double y);
93 double_complex ccsub(double_complex x, double_complex y);
94 double_complex crsub(double_complex x, double y);
95 double_complex ccmul(double_complex x, double_complex y);
96 double_complex cccmul(double_complex x, double_complex y, double_complex z);
97 double_complex crmul(double_complex x, double y);
98 double_complex ccdiv(double_complex x, double_complex y);
99 double_complex crdiv(double_complex x, double y);
100
101
102#elif _MSC_VER >= 1900
103 /* for Microsoft's ancient C compiler (MS VC++ 14.0 _MSC_VER = 1900, Visual
104 * Studio 2015) */
105
106# include <complex.h>
107 typedef _Fcomplex float_complex;
108 typedef _Dcomplex double_complex;
109
110 /*
111 Single-Precision Complex Operations
112 */
113 inline float_complex cmplxf(float re, float im) {
114 float_complex z;
115 z._Val[0] = re; z._Val[1] = im;
116 return z;
117 }
118
119 inline float_complex ccaddf(float_complex x, float_complex y) {
120 float_complex z;
121 z._Val[0] = x._Val[0] + y._Val[0]; z._Val[1] = x._Val[1] + y._Val[1];
122 return z;
123 }
124
125 inline float_complex craddf(float_complex x, float y) {
126 float_complex z;
127 z._Val[0] = x._Val[0] + y; z._Val[1] = x._Val[1];
128 return z;
129 }
130
131 inline float_complex ccsubf(float_complex x, float_complex y) {
132 float_complex z;
133 z._Val[0] = x._Val[0] - y._Val[0]; z._Val[1] = x._Val[1] - y._Val[1];
134 return z;
135 }
136
137 inline float_complex crsubf(float_complex x, float y) {
138 float_complex z;
139 z._Val[0] = x._Val[0] - y; z._Val[1] = x._Val[1];
140 return z;
141 }
142
143 inline float_complex ccmulf(float_complex x, float_complex y) {
144 return _FCmulcc(x, y);
145 }
146
147 inline float_complex cccmulf(float_complex x, float_complex y, float_complex z) {
148 return _FCmulcc(_FCmulcc(x, y), z);
149 }
150
151 inline float_complex crmulf(float_complex x, float y) {
152 return _FCmulcr(x, y);
153 }
154
155 inline float_complex ccdivf(float_complex x, float_complex y) {
156 float_complex z;
157 z._Val[0] = (x._Val[0] * y._Val[0] + x._Val[1] * y._Val[1]) / (y._Val[0] * y._Val[0] + y._Val[1] * y._Val[1] );
158 z._Val[1] = (x._Val[1] * y._Val[0] - x._Val[0] * y._Val[1]) / (y._Val[0] * y._Val[0] + y._Val[1] * y._Val[1] );
159 return z;
160 }
161
162 inline float_complex crdivf(float_complex x, float y) {
163 float_complex z;
164 z._Val[0] = x._Val[0] / y;
165 z._Val[1] = x._Val[1] / y;
166 return z;
167 }
168
169 /*
170 Double-Precision Complex Operations
171 */
172 inline double_complex cmplx(double re, double im) {
173 double_complex z;
174 z._Val[0] = re; z._Val[1] = im;
175 return z;
176 }
177
178 inline double_complex ccadd(double_complex x, double_complex y) {
179 double_complex z;
180 z._Val[0] = x._Val[0] + y._Val[0]; z._Val[1] = x._Val[1] + y._Val[1];
181 return z;
182 }
183
184 inline double_complex cradd(double_complex x, double y) {
185 double_complex z;
186 z._Val[0] = x._Val[0] + y; z._Val[1] = x._Val[1];
187 return z;
188 }
189
190 inline double_complex ccsub(double_complex x, double_complex y) {
191 double_complex z;
192 z._Val[0] = x._Val[0] - y._Val[0]; z._Val[1] = x._Val[1] - y._Val[1];
193 return z;
194 }
195
196 inline double_complex crsub(double_complex x, double y) {
197 double_complex z;
198 z._Val[0] = x._Val[0] - y; z._Val[1] = x._Val[1];
199 return z;
200 }
201
202 inline double_complex ccmul(double_complex x, double_complex y) {
203 return _Cmulcc(x, y);
204 }
205
206 inline double_complex cccmul(double_complex x, double_complex y, double_complex z) {
207 return _Cmulcc(_Cmulcc(x, y), z);
208 }
209
210 inline double_complex crmul(double_complex x, double y) {
211 return _Cmulcr(x, y);
212 }
213
214 inline double_complex ccdiv(double_complex x, double_complex y) {
215 double_complex z;
216 z._Val[0] = (x._Val[0] * y._Val[0] + x._Val[1] * y._Val[1]) / (y._Val[0] * y._Val[0] + y._Val[1] * y._Val[1]);
217 z._Val[1] = (x._Val[1] * y._Val[0] - x._Val[0] * y._Val[1]) / (y._Val[0] * y._Val[0] + y._Val[1] * y._Val[1]);
218 return z;
219 }
220
221 inline double_complex crdiv(double_complex x, double y) {
222 double_complex z;
223 z._Val[0] = x._Val[0] / y;
224 z._Val[1] = x._Val[1] / y;
225 return z;
226 }
227
228#else
229# error "SAF requires a compiler that supports the C99 standard, or MSVC version 1900 or newer"
230#endif
231
232
233#endif /* SAF_COMPLEX_H_INCLUDED */
234
235 /* doxygen addtogroup Utilities */