2 * sysmath.c: these are based on bob smith's csharp routines
5 * Mono Project (http://www.mono-project.com)
6 * Ludovic Henry (ludovic@xamarin.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10 * Copyright 2015 Xamarin, Inc (https://www.xamarin.com)
14 // Copyright (c) Microsoft. All rights reserved.
15 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 // - src/classlibnative/float/floatnative.cpp
19 // - src/pal/src/cruntime/floatnative.cpp
21 // Ported from C++ to C and adjusted to Mono runtime
26 #include <mono/metadata/sysmath.h>
28 #include "number-ms.h"
29 #include "utils/mono-compiler.h"
31 static const MonoDouble_double NaN = { .s = { .sign = 0x0, .exp = 0x7FF, .mantHi = 0x80000, .mantLo = 0x0 } };
34 static const MonoDouble_double PInfinity = { .s = { .sign = 0x0, .exp = 0x7FF, .mantHi = 0x0, .mantLo = 0x0 } };
37 static const MonoDouble_double MInfinity = { .s = { .sign = 0x1, .exp = 0x7FF, .mantHi = 0x0, .mantLo = 0x0 } };
40 static const MonoDouble_double POne = { .s = { .sign = 0x0, .exp = 0x3FF, .mantHi = 0x0, .mantLo = 0x0 } };
43 static const MonoDouble_double MOne = { .s = { .sign = 0x1, .exp = 0x3FF, .mantHi = 0x0, .mantLo = 0x0 } };
45 static MONO_ALWAYS_INLINE gboolean
46 isplusinfinity (gdouble d)
48 return d == PInfinity.d;
51 static MONO_ALWAYS_INLINE gboolean
52 isminusinfinity (gdouble d)
54 return d == MInfinity.d;
57 static MONO_ALWAYS_INLINE gboolean
58 isinfinity (gdouble d)
60 return isplusinfinity (d) || isminusinfinity (d);
63 static MONO_ALWAYS_INLINE gboolean
69 static MONO_ALWAYS_INLINE gboolean
70 isminusone (gdouble d)
76 ves_icall_System_Math_Floor (gdouble x)
82 ves_icall_System_Math_Round (gdouble x)
84 gdouble tmp, floor_tmp;
86 /* If the number has no fractional part do nothing This shortcut is necessary
87 * to workaround precision loss in borderline cases on some platforms */
88 if (x == (gdouble)(gint64) x)
92 floor_tmp = floor (tmp);
94 if (floor_tmp == tmp) {
95 if (fmod (tmp, 2.0) != 0)
99 return copysign (floor_tmp, x);
103 ves_icall_System_Math_Sin (gdouble x)
109 ves_icall_System_Math_Cos (gdouble x)
115 ves_icall_System_Math_Tan (gdouble x)
121 ves_icall_System_Math_Sinh (gdouble x)
127 ves_icall_System_Math_Cosh (gdouble x)
133 ves_icall_System_Math_Tanh (gdouble x)
139 ves_icall_System_Math_Acos (gdouble x)
148 ves_icall_System_Math_Asin (gdouble x)
157 ves_icall_System_Math_Atan (gdouble x)
163 ves_icall_System_Math_Atan2 (gdouble y, gdouble x)
167 if (isinfinity (x) && isinfinity (y))
170 result = atan2 (y, x);
171 return result == -0.0 ? 0.0: result;
175 ves_icall_System_Math_Exp (gdouble x)
178 return x < 0 ? 0.0 : x;
184 ves_icall_System_Math_Log (gdouble x)
195 ves_icall_System_Math_Log10 (gdouble x)
206 ves_icall_System_Math_Pow (gdouble x, gdouble y)
215 if (isinfinity (y)) {
222 /* following are cases from PAL_pow which abstract the implementation of pow for posix and win32 platforms
223 * (https://github.com/dotnet/coreclr/blob/master/src/pal/src/cruntime/finite.cpp#L331) */
225 if (isplusinfinity (y) && !isnan (x)) {
226 if (isplusone (x) || isminusone (x))
228 else if (x > MOne.d && x < POne.d)
231 result = PInfinity.d;
232 } else if (isminusinfinity (y) && !isnan (x)) {
233 if (isplusone (x) || isminusone (x))
235 if (x > MOne.d && x < POne.d)
236 result = PInfinity.d;
239 } else if (x == 0.0 && y < 0.0) {
240 result = PInfinity.d;
241 } else if (y == 0.0 && isnan (x)) {
242 /* Windows returns NaN for pow(NaN, 0), but POSIX specifies
243 * a return value of 1 for that case. We need to return
244 * the same result as Windows. */
250 if (result == PInfinity.d && x < 0.0 && isfinite (x) && ceil (y / 2) != floor (y / 2))
251 result = MInfinity.d;
254 * The even/odd test in the if (this one and the one above) used to be ((long long) y % 2 == 0)
255 * on SPARC (long long) y for large y (>2**63) is always 0x7fffffff7fffffff, which
256 * is an odd number, so the test ((long long) y % 2 == 0) will always fail for
257 * large y. Since large double numbers are always even (e.g., the representation of
258 * 1E20+1 is the same as that of 1E20, the last .+1. is too insignificant to be part
259 * of the representation), this test will always return the wrong result for large y.
261 * The (ceil(y/2) == floor(y/2)) test is slower, but more robust.
263 if (result == MInfinity.d && x < 0.0 && isfinite (x) && ceil (y / 2) == floor (y / 2))
264 result = PInfinity.d;
266 return result == -0.0 ? 0 : result;
270 ves_icall_System_Math_Sqrt (gdouble x)
279 ves_icall_System_Math_Abs_double (gdouble v)
285 ves_icall_System_Math_Abs_single (gfloat v)
291 ves_icall_System_Math_Ceiling (gdouble v)
297 ves_icall_System_Math_SplitFractionDouble (gdouble *v)