First set of licensing changes
[mono.git] / mono / utils / mono-complex.h
1 /*
2 * mono-complex.h: C99 Complex math cross-platform support code
3 *
4 * Author:
5 *       Joao Matos (joao.matos@xamarin.com)
6 *
7 * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9 */
10
11 #include <config.h>
12 #include <glib.h>
13
14 #if !defined (HAVE_COMPLEX_H)
15 #include <../../support/libm/complex.h>
16 #else
17 #include <complex.h>
18 #endif
19
20 #define _USE_MATH_DEFINES // needed by MSVC to define math constants
21 #include <math.h>
22
23 #ifdef _MSC_VER
24
25 #define double_complex _C_double_complex
26
27 static inline
28 double_complex mono_double_complex_make(gdouble re, gdouble im)
29 {
30         return _Cbuild (re, im);
31 }
32
33 static inline
34 double_complex mono_double_complex_scalar_div(double_complex c, gdouble s)
35 {
36         return mono_double_complex_make(creal(c) / s, cimag(c) / s);
37 }
38
39 static inline
40 double_complex mono_double_complex_scalar_mul(double_complex c, gdouble s)
41 {
42         return mono_double_complex_make(creal(c) * s, cimag(c) * s);
43 }
44
45 static inline
46 double_complex mono_double_complex_div(double_complex left, double_complex right)
47 {
48         double denom = creal(right) * creal(right) + cimag(right) * cimag(right);
49
50         return mono_double_complex_make(
51                 (creal(left) * creal(right) + cimag(left) * cimag(right)) / denom,
52                 (-creal(left) * cimag(right) + cimag(left) * creal(right)) / denom);
53 }
54
55 static inline
56 double_complex mono_double_complex_sub(double_complex left, double_complex right)
57 {
58         return mono_double_complex_make(creal(left) - creal(right), cimag(left)
59                 - cimag(right));
60 }
61
62 #else
63
64 #define double_complex double complex
65
66 static inline
67 double_complex mono_double_complex_make(gdouble re, gdouble im)
68 {
69         return re + im * I;
70 }
71
72 static inline
73 double_complex mono_double_complex_scalar_div(double_complex c, gdouble s)
74 {
75         return c / s;
76 }
77
78 static inline
79 double_complex mono_double_complex_scalar_mul(double_complex c, gdouble s)
80 {
81         return c * s;
82 }
83
84 static inline
85 double_complex mono_double_complex_div(double_complex left, double_complex right)
86 {
87         return left / right;
88 }
89
90 static inline
91 double_complex mono_double_complex_sub(double_complex left, double_complex right)
92 {
93         return left - right;
94 }
95
96 #endif