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