[utils] Small header sanitization. networking.h now include config.h
[mono-project.git] / mono / metadata / sysmath.c
blobc5943b44cc908340346131c61e31c353e1a2b190
1 /*
2 * sysmath.c: these are based on bob smith's csharp routines
4 * Author:
5 * Mono Project (http://www.mono-project.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 */
10 #define __USE_ISOC99
11 #include <math.h>
12 #include <mono/metadata/sysmath.h>
13 #include <mono/metadata/exception.h>
15 #ifndef NAN
16 # if G_BYTE_ORDER == G_BIG_ENDIAN
17 # define __nan_bytes { 0x7f, 0xc0, 0, 0 }
18 # endif
19 # if G_BYTE_ORDER == G_LITTLE_ENDIAN
20 # define __nan_bytes { 0, 0, 0xc0, 0x7f }
21 # endif
23 static union { unsigned char __c[4]; float __d; } __nan_union = { __nan_bytes };
24 # define NAN (__nan_union.__d)
25 #endif
27 #ifndef HUGE_VAL
28 #define __huge_val_t union { unsigned char __c[8]; double __d; }
29 # if G_BYTE_ORDER == G_BIG_ENDIAN
30 # define __HUGE_VAL_bytes { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 }
31 # endif
32 # if G_BYTE_ORDER == G_LITTLE_ENDIAN
33 # define __HUGE_VAL_bytes { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f }
34 # endif
35 static __huge_val_t __huge_val = { __HUGE_VAL_bytes };
36 # define HUGE_VAL (__huge_val.__d)
37 #endif
40 gdouble ves_icall_System_Math_Floor (gdouble x) {
41 MONO_ARCH_SAVE_REGS;
42 return floor(x);
45 gdouble ves_icall_System_Math_Round (gdouble x) {
46 double int_part, dec_part;
47 MONO_ARCH_SAVE_REGS;
48 int_part = floor(x);
49 dec_part = x - int_part;
50 if (((dec_part == 0.5) &&
51 ((2.0 * ((int_part / 2.0) - floor(int_part / 2.0))) != 0.0)) ||
52 (dec_part > 0.5)) {
53 int_part++;
55 return int_part;
58 gdouble ves_icall_System_Math_Round2 (gdouble value, gint32 digits, gboolean away_from_zero) {
59 #if !defined (HAVE_ROUND) || !defined (HAVE_RINT)
60 double int_part, dec_part;
61 #endif
62 double p;
64 MONO_ARCH_SAVE_REGS;
65 if (value == HUGE_VAL)
66 return HUGE_VAL;
67 if (value == -HUGE_VAL)
68 return -HUGE_VAL;
69 p = pow(10, digits);
70 #if defined (HAVE_ROUND) && defined (HAVE_RINT)
71 if (away_from_zero)
72 return round (value * p) / p;
73 else
74 return rint (value * p) / p;
75 #else
76 dec_part = modf (value, &int_part);
77 dec_part *= 1000000000000000ULL;
78 if (away_from_zero && dec_part > 0)
79 dec_part = ceil (dec_part);
80 else
81 dec_part = floor (dec_part);
82 dec_part /= (1000000000000000ULL / p);
83 if (away_from_zero) {
84 if (dec_part > 0)
85 dec_part = floor (dec_part + 0.5);
86 else
87 dec_part = ceil (dec_part - 0.5);
88 } else
89 dec_part = ves_icall_System_Math_Round (dec_part);
90 dec_part /= p;
91 return ves_icall_System_Math_Round ((int_part + dec_part) * p) / p;
92 #endif
95 gdouble
96 ves_icall_System_Math_Sin (gdouble x)
98 MONO_ARCH_SAVE_REGS;
100 return sin (x);
103 gdouble
104 ves_icall_System_Math_Cos (gdouble x)
106 MONO_ARCH_SAVE_REGS;
108 return cos (x);
111 gdouble
112 ves_icall_System_Math_Tan (gdouble x)
114 MONO_ARCH_SAVE_REGS;
116 return tan (x);
119 gdouble
120 ves_icall_System_Math_Sinh (gdouble x)
122 MONO_ARCH_SAVE_REGS;
124 return sinh (x);
127 gdouble
128 ves_icall_System_Math_Cosh (gdouble x)
130 MONO_ARCH_SAVE_REGS;
132 return cosh (x);
135 gdouble
136 ves_icall_System_Math_Tanh (gdouble x)
138 MONO_ARCH_SAVE_REGS;
140 return tanh (x);
143 gdouble
144 ves_icall_System_Math_Acos (gdouble x)
146 MONO_ARCH_SAVE_REGS;
148 if (x < -1 || x > 1)
149 return NAN;
151 return acos (x);
154 gdouble
155 ves_icall_System_Math_Asin (gdouble x)
157 MONO_ARCH_SAVE_REGS;
159 if (x < -1 || x > 1)
160 return NAN;
162 return asin (x);
165 gdouble
166 ves_icall_System_Math_Atan (gdouble x)
168 MONO_ARCH_SAVE_REGS;
170 return atan (x);
173 gdouble
174 ves_icall_System_Math_Atan2 (gdouble y, gdouble x)
176 double result;
177 MONO_ARCH_SAVE_REGS;
179 if ((y == HUGE_VAL && x == HUGE_VAL) ||
180 (y == HUGE_VAL && x == -HUGE_VAL) ||
181 (y == -HUGE_VAL && x == HUGE_VAL) ||
182 (y == -HUGE_VAL && x == -HUGE_VAL)) {
183 return NAN;
185 result = atan2 (y, x);
186 return (result == -0)? 0: result;
189 gdouble
190 ves_icall_System_Math_Exp (gdouble x)
192 MONO_ARCH_SAVE_REGS;
194 return exp (x);
197 gdouble
198 ves_icall_System_Math_Log (gdouble x)
200 MONO_ARCH_SAVE_REGS;
202 if (x == 0)
203 return -HUGE_VAL;
204 else if (x < 0)
205 return NAN;
207 return log (x);
210 gdouble
211 ves_icall_System_Math_Log10 (gdouble x)
213 MONO_ARCH_SAVE_REGS;
215 if (x == 0)
216 return -HUGE_VAL;
217 else if (x < 0)
218 return NAN;
220 return log10 (x);
223 gdouble
224 ves_icall_System_Math_Pow (gdouble x, gdouble y)
226 double result;
227 MONO_ARCH_SAVE_REGS;
229 if (isnan(x) || isnan(y)) {
230 return NAN;
233 if ((x == 1 || x == -1) && (y == HUGE_VAL || y == -HUGE_VAL)) {
234 return NAN;
237 /* This code is for return the same results as MS.NET for certain
238 * limit values */
239 if (x < -9007199254740991.0) {
240 if (y > 9007199254740991.0)
241 return HUGE_VAL;
242 if (y < -9007199254740991.0)
243 return 0;
246 result = pow (x, y);
248 /* This code is for return the same results as MS.NET for certain
249 * limit values */
250 if (isnan(result) &&
251 (x == -1.0) &&
252 ((y > 9007199254740991.0) || (y < -9007199254740991.0))) {
253 return 1;
256 return (result == -0)? 0: result;
259 gdouble
260 ves_icall_System_Math_Sqrt (gdouble x)
262 MONO_ARCH_SAVE_REGS;
264 if (x < 0)
265 return NAN;
267 return sqrt (x);