diff options
author | Jiyoung Yun <jy910.yun@samsung.com> | 2016-11-23 19:09:09 +0900 |
---|---|---|
committer | Jiyoung Yun <jy910.yun@samsung.com> | 2016-11-23 19:09:09 +0900 |
commit | 4b4aad7217d3292650e77eec2cf4c198ea9c3b4b (patch) | |
tree | 98110734c91668dfdbb126fcc0e15ddbd93738ca /src/inc/complex.h | |
parent | fa45f57ed55137c75ac870356a1b8f76c84b229c (diff) | |
download | coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.gz coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.bz2 coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.zip |
Imported Upstream version 1.1.0upstream/1.1.0
Diffstat (limited to 'src/inc/complex.h')
-rw-r--r-- | src/inc/complex.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/inc/complex.h b/src/inc/complex.h new file mode 100644 index 0000000000..159c81d04b --- /dev/null +++ b/src/inc/complex.h @@ -0,0 +1,80 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +// +// complex.h +// + +// +// Defines a basic complex number data type. We cannot use the standard C++ library's +// complex implementation, because the CLR links to the wrong CRT. +// + +#ifndef _COMPLEX_H_ +#define _COMPLEX_H_ + +#include <math.h> + +// +// Default compilation mode is /fp:precise, which disables fp intrinsics. This causes us to pull in FP stuff (sqrt,etc.) from +// The CRT, and increases our download size. We don't need the extra precision this gets us, so let's switch to +// the intrinsic versions. +// +#ifdef _MSC_VER +#pragma float_control(precise, off, push) +#endif + + +class Complex +{ +public: + double r; + double i; + + Complex() : r(0), i(0) {} + Complex(double real) : r(real), i(0) {} + Complex(double real, double imag) : r(real), i(imag) {} + Complex(const Complex& other) : r(other.r), i(other.i) {} +}; + +inline Complex operator+(Complex left, Complex right) +{ + LIMITED_METHOD_CONTRACT; + return Complex(left.r + right.r, left.i + right.i); +} + +inline Complex operator-(Complex left, Complex right) +{ + LIMITED_METHOD_CONTRACT; + return Complex(left.r - right.r, left.i - right.i); +} + +inline Complex operator*(Complex left, Complex right) +{ + LIMITED_METHOD_CONTRACT; + return Complex( + left.r * right.r - left.i * right.i, + left.r * right.i + left.i * right.r); +} + +inline Complex operator/(Complex left, Complex right) +{ + LIMITED_METHOD_CONTRACT; + double denom = right.r * right.r + right.i * right.i; + return Complex( + (left.r * right.r + left.i * right.i) / denom, + (-left.r * right.i + left.i * right.r) / denom); +} + +inline double abs(Complex c) +{ + LIMITED_METHOD_CONTRACT; + return sqrt(c.r * c.r + c.i * c.i); +} + +#ifdef _MSC_VER +#pragma float_control(pop) +#endif + + +#endif //_COMPLEX_H_ |