/
basic-math.cs
362 lines (294 loc) · 8.53 KB
/
basic-math.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System;
using System.Reflection;
/*
* Regression tests for the mono JIT.
*
* Each test needs to be of the form:
*
* public static int test_<result>_<name> ();
*
* where <result> is an integer (the value that needs to be returned by
* the method to make it pass.
* <name> is a user-displayed name used to identify the test.
*
* The tests can be driven in two ways:
* *) running the program directly: Main() uses reflection to find and invoke
* the test methods (this is useful mostly to check that the tests are correct)
* *) with the --regression switch of the jit (this is the preferred way since
* all the tests will be run with optimizations on and off)
*
* The reflection logic could be moved to a .dll since we need at least another
* regression test file written in IL code to have better control on how
* the IL code looks.
*/
#if __MOBILE__
class MathTests
#else
class Tests
#endif
{
#if !__MOBILE__
public static int Main (string[] args) {
return TestDriver.RunTests (typeof (Tests), args);
}
#endif
public static int test_0_sin_precision () {
double d1 = Math.Sin (1);
double d2 = Math.Sin (1) - d1;
return (d2 == 0) ? 0 : 1;
}
public static int test_0_cos_precision () {
double d1 = Math.Cos (1);
double d2 = Math.Cos (1) - d1;
return (d2 == 0) ? 0 : 1;
}
public static int test_0_tan_precision () {
double d1 = Math.Tan (1);
double d2 = Math.Tan (1) - d1;
return (d2 == 0) ? 0 : 1;
}
public static int test_0_atan_precision () {
double d1 = Math.Atan (double.NegativeInfinity);
double d2 = Math.Atan (double.NegativeInfinity) - d1;
return (d2 == 0) ? 0 : 1;
}
public static int test_0_sqrt_precision () {
double d1 = Math.Sqrt (2);
double d2 = Math.Sqrt (2) - d1;
return (d2 == 0) ? 0 : 1;
}
public static int test_2_sqrt () {
return (int) Math.Sqrt (4);
}
public static int test_0_sqrt_precision_and_not_spill () {
double expected = 0;
double[] operands = new double[3];
double[] temporaries = new double[3];
for (int i = 0; i < 3; i++) {
operands [i] = (i+1) * (i+1) * (i+1);
if (i == 0) {
expected = operands [0];
} else {
temporaries [i] = operands [i] / expected;
temporaries [i] = Math.Sqrt (temporaries [i]);
expected = temporaries [i];
}
//Console.Write( "{0}: {1}\n", i, temporaries [i] );
}
expected = temporaries [2];
double result = Math.Sqrt (operands [2] / Math.Sqrt (operands [1] / operands [0]));
//Console.Write( "result: {0,20:G}\n", result );
return (result == expected) ? 0 : 1;
}
public static int test_0_sqrt_precision_and_spill () {
double expected = 0;
double[] operands = new double[9];
double[] temporaries = new double[9];
for (int i = 0; i < 9; i++) {
operands [i] = (i+1) * (i+1) * (i+1);
if (i == 0) {
expected = operands [0];
} else {
temporaries [i] = operands [i] / expected;
temporaries [i] = Math.Sqrt (temporaries [i]);
expected = temporaries [i];
}
//Console.Write( "{0}: {1}\n", i, temporaries [i] );
}
expected = temporaries [8];
double result = Math.Sqrt (operands [8] / Math.Sqrt (operands [7] / Math.Sqrt (operands [6] / Math.Sqrt (operands [5] / Math.Sqrt (operands [4] / Math.Sqrt (operands [3] / Math.Sqrt (operands [2] / Math.Sqrt (operands [1] / operands [0]))))))));
//Console.Write( "result: {0,20:G}\n", result );
return (result == expected) ? 0 : 1;
}
public static int test_0_div_precision_and_spill () {
double expected = 0;
double[] operands = new double[9];
double[] temporaries = new double[9];
for (int i = 0; i < 9; i++) {
operands [i] = (i+1) * (i+1);
if (i == 0) {
expected = operands [0];
} else {
temporaries [i] = operands [i] / expected;
expected = temporaries [i];
}
//Console.Write( "{0}: {1}\n", i, temporaries [i] );
}
expected = temporaries [8];
double result = (operands [8] / (operands [7] / (operands [6] / (operands [5] / (operands [4] / (operands [3] / (operands [2] / (operands [1] / operands [0]))))))));
//Console.Write( "result: {0,20:G}\n", result );
return (result == expected) ? 0 : 1;
}
public static int test_0_sqrt_nan () {
return Double.IsNaN (Math.Sqrt (Double.NaN)) ? 0 : 1;
}
public static int test_0_sin_nan () {
return Double.IsNaN (Math.Sin (Double.NaN)) ? 0 : 1;
}
public static int test_0_cos_nan () {
return Double.IsNaN (Math.Cos (Double.NaN)) ? 0 : 1;
}
public static int test_0_tan_nan () {
return Double.IsNaN (Math.Tan (Double.NaN)) ? 0 : 1;
}
public static int test_0_atan_nan () {
return Double.IsNaN (Math.Atan (Double.NaN)) ? 0 : 1;
}
public static int test_0_min () {
if (Math.Min (5, 6) != 5)
return 1;
if (Math.Min (6, 5) != 5)
return 2;
if (Math.Min (-100, -101) != -101)
return 3;
if (Math.Min ((long)5, (long)6) != 5)
return 4;
if (Math.Min ((long)6, (long)5) != 5)
return 5;
if (Math.Min ((long)-100, (long)-101) != -101)
return 6;
// this will trip if Min is accidentally using unsigned/logical comparison
if (Math.Min((long)-100000000000L, (long)0L) != (long)-100000000000L)
return 7;
return 0;
}
public static int test_0_max () {
if (Math.Max (5, 6) != 6)
return 1;
if (Math.Max (6, 5) != 6)
return 2;
if (Math.Max (-100, -101) != -100)
return 3;
if (Math.Max ((long)5, (long)6) != 6)
return 4;
if (Math.Max ((long)6, (long)5) != 6)
return 5;
if (Math.Max ((long)-100, (long)-101) != -100)
return 6;
// this will trip if Max is accidentally using unsigned/logical comparison
if (Math.Max((long)-100000000000L, (long)0L) != (long)0L)
return 7;
return 0;
}
public static int test_0_min_un () {
uint a = (uint)int.MaxValue + 10;
for (uint b = 7; b <= 10; ++b) {
if (Math.Min (a, b) != b)
return (int)b;
if (Math.Min (b, a) != b)
return (int)b;
}
if (Math.Min ((ulong)5, (ulong)6) != 5)
return 4;
if (Math.Min ((ulong)6, (ulong)5) != 5)
return 5;
ulong la = (ulong)long.MaxValue + 10;
for (ulong b = 7; b <= 10; ++b) {
if (Math.Min (la, b) != b)
return (int)b;
if (Math.Min (b, la) != b)
return (int)b;
}
return 0;
}
public static int test_0_max_un () {
uint a = (uint)int.MaxValue + 10;
for (uint b = 7; b <= 10; ++b) {
if (Math.Max (a, b) != a)
return (int)b;
if (Math.Max (b, a) != a)
return (int)b;
}
if (Math.Max ((ulong)5, (ulong)6) != 6)
return 4;
if (Math.Max ((ulong)6, (ulong)5) != 6)
return 5;
ulong la = (ulong)long.MaxValue + 10;
for (ulong b = 7; b <= 10; ++b) {
if (Math.Max (la, b) != la)
return (int)b;
if (Math.Max (b, la) != la)
return (int)b;
}
return 0;
}
public static int test_0_abs () {
double d = -5.0;
if (Math.Abs (d) != 5.0)
return 1;
return 0;
}
public static int test_0_float_abs () {
float f = -1.0f;
if (Math.Abs (f) != 1.0f)
return 1;
return 0;
}
public static int test_0_round () {
if (Math.Round (5.0) != 5.0)
return 1;
if (Math.Round (5.000000000000001) != 5.0)
return 2;
if (Math.Round (5.499999999999999) != 5.0)
return 3;
if (Math.Round (5.5) != 6.0)
return 4;
if (Math.Round (5.999999999999999) != 6.0)
return 5;
if (Math.Round (Double.Epsilon) != 0)
return 6;
if (!Double.IsNaN (Math.Round (Double.NaN)))
return 7;
if (!Double.IsPositiveInfinity (Math.Round (Double.PositiveInfinity)))
return 8;
if (!Double.IsNegativeInfinity (Math.Round (Double.NegativeInfinity)))
return 9;
if (Math.Round (Double.MinValue) != Double.MinValue)
return 10;
if (Math.Round (Double.MaxValue) != Double.MaxValue)
return 11;
return 0;
}
public static int test_0_mathf_sin () {
float f = MathF.Sin (3.14159f);
return f < 0.01f ? 0 : 1;
}
public static int test_0_mathf_cos () {
float f = MathF.Cos (3.14159f);
return f - -1f < 0.01f ? 0 : 1;
}
public static int test_0_mathf_abs () {
float f;
f = MathF.Abs (2.25f) - 2.25f;
if (f > 0.01f || f < -0.01f)
return 1;
f = MathF.Abs (-2.25f) - 2.25f;
if (f > 0.01f || f < -0.01f)
return 2;
return 0;
}
public static int test_0_mathf_sqrt () {
float f;
f = MathF.Sqrt (16.0f) - 4.0f;
if (f > 0.01f || f < -0.01f)
return 1;
return 0;
}
public static int test_0_mathf_max () {
float f;
f = MathF.Max (1.0f, 2.0f) - 2.0f;
if (f > 0.01f || f < -0.01f)
return 1;
f = MathF.Max (2.0f, 1.0f) - 2.0f;
if (f > 0.01f || f < -0.01f)
return 2;
return 0;
}
public static int test_0_mathf_pow () {
float f;
f = MathF.Pow (2.0f, 4.0f) - 16.0f;
if (f > 0.01f || f < -0.01f)
return 1;
return 0;
}
}