Merge pull request #205 from m3rlinez/master
[mono.git] / mono / metadata / sysmath.c
1 /*
2  * sysmath.c: these are based on bob smith's csharp routines 
3  *
4  * Author:
5  *      Mono Project (http://www.mono-project.com)
6  *
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>
14
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
22
23 static union { unsigned char __c[4]; float __d; } __nan_union = { __nan_bytes };
24 # define NAN    (__nan_union.__d)
25 #endif
26
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
38
39
40 gdouble ves_icall_System_Math_Floor (gdouble x) {
41         MONO_ARCH_SAVE_REGS;
42         return floor(x);
43 }
44
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++;
54         }
55         return int_part;
56 }
57
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;
63
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
93 }
94
95 gdouble 
96 ves_icall_System_Math_Sin (gdouble x)
97 {
98         MONO_ARCH_SAVE_REGS;
99
100         return sin (x);
101 }
102
103 gdouble 
104 ves_icall_System_Math_Cos (gdouble x)
105 {
106         MONO_ARCH_SAVE_REGS;
107
108         return cos (x);
109 }
110
111 gdouble 
112 ves_icall_System_Math_Tan (gdouble x)
113 {
114         MONO_ARCH_SAVE_REGS;
115
116         return tan (x);
117 }
118
119 gdouble 
120 ves_icall_System_Math_Sinh (gdouble x)
121 {
122         MONO_ARCH_SAVE_REGS;
123
124         return sinh (x);
125 }
126
127 gdouble 
128 ves_icall_System_Math_Cosh (gdouble x)
129 {
130         MONO_ARCH_SAVE_REGS;
131
132         return cosh (x);
133 }
134
135 gdouble 
136 ves_icall_System_Math_Tanh (gdouble x)
137 {
138         MONO_ARCH_SAVE_REGS;
139
140         return tanh (x);
141 }
142
143 gdouble 
144 ves_icall_System_Math_Acos (gdouble x)
145 {
146         MONO_ARCH_SAVE_REGS;
147
148         if (x < -1 || x > 1)
149                 return NAN;
150
151         return acos (x);
152 }
153
154 gdouble 
155 ves_icall_System_Math_Asin (gdouble x)
156 {
157         MONO_ARCH_SAVE_REGS;
158
159         if (x < -1 || x > 1)
160                 return NAN;
161
162         return asin (x);
163 }
164
165 gdouble 
166 ves_icall_System_Math_Atan (gdouble x)
167 {
168         MONO_ARCH_SAVE_REGS;
169
170         return atan (x);
171 }
172
173 gdouble 
174 ves_icall_System_Math_Atan2 (gdouble y, gdouble x)
175 {
176         double result;
177         MONO_ARCH_SAVE_REGS;
178
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;
184         }
185         result = atan2 (y, x);
186         return (result == -0)? 0: result;
187 }
188
189 gdouble 
190 ves_icall_System_Math_Exp (gdouble x)
191 {
192         MONO_ARCH_SAVE_REGS;
193
194         return exp (x);
195 }
196
197 gdouble 
198 ves_icall_System_Math_Log (gdouble x)
199 {
200         MONO_ARCH_SAVE_REGS;
201
202         if (x == 0)
203                 return -HUGE_VAL;
204         else if (x < 0)
205                 return NAN;
206
207         return log (x);
208 }
209
210 gdouble 
211 ves_icall_System_Math_Log10 (gdouble x)
212 {
213         MONO_ARCH_SAVE_REGS;
214
215         if (x == 0)
216                 return -HUGE_VAL;
217         else if (x < 0)
218                 return NAN;
219
220         return log10 (x);
221 }
222
223 gdouble 
224 ves_icall_System_Math_Pow (gdouble x, gdouble y)
225 {
226         double result;
227         MONO_ARCH_SAVE_REGS;
228
229         if (isnan(x) || isnan(y)) {
230                 return NAN;
231         }
232
233         if ((x == 1 || x == -1) && (y == HUGE_VAL || y == -HUGE_VAL)) {
234                 return NAN;
235         }
236
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;
244         }
245
246         result = pow (x, y);
247
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;
254         }
255
256         return (result == -0)? 0: result;
257 }
258
259 gdouble 
260 ves_icall_System_Math_Sqrt (gdouble x)
261 {
262         MONO_ARCH_SAVE_REGS;
263
264         if (x < 0)
265                 return NAN;
266
267         return sqrt (x);
268 }