Mercurial > mm7
annotate Math.h @ 1307:d346e11b550b
m
author | Ritor1 |
---|---|
date | Mon, 24 Jun 2013 18:59:11 +0600 |
parents | 0aeac0b9ca30 |
children | f0551cd4cbc9 |
rev | line source |
---|---|
0 | 1 #pragma once |
1131
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
2 #include <cassert> |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
3 #include <limits> |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
4 #include <float.h> |
1262 | 5 #include <cmath> |
6 #include <cstdlib> | |
7 #include <ciso646> | |
0 | 8 |
9 | |
10 /* 186 */ | |
11 #pragma pack(push, 1) | |
12 struct stru193_math | |
13 { | |
14 stru193_math(); | |
323 | 15 int Cos(int angle); |
0 | 16 unsigned int Atan2(int x, int y); |
323 | 17 int Sin(int angle); |
0 | 18 |
19 int pTanTable[520]; | |
20 int pCosTable[520]; | |
21 int pInvCosTable[520]; | |
1214
0d6c7ff3cddd
simplified stru193_math constructor and class definition
Grumpy7
parents:
1131
diff
changeset
|
22 static const unsigned int uIntegerPi = 1024; |
0d6c7ff3cddd
simplified stru193_math constructor and class definition
Grumpy7
parents:
1131
diff
changeset
|
23 static const unsigned int uIntegerHalfPi = 512; |
0d6c7ff3cddd
simplified stru193_math constructor and class definition
Grumpy7
parents:
1131
diff
changeset
|
24 static const unsigned int uIntegerDoublePi = 2048; |
0d6c7ff3cddd
simplified stru193_math constructor and class definition
Grumpy7
parents:
1131
diff
changeset
|
25 static const unsigned int uDoublePiMask = 2047; |
0d6c7ff3cddd
simplified stru193_math constructor and class definition
Grumpy7
parents:
1131
diff
changeset
|
26 static const unsigned int uPiMask = 1023; |
0d6c7ff3cddd
simplified stru193_math constructor and class definition
Grumpy7
parents:
1131
diff
changeset
|
27 static const unsigned int uHalfPiMask = 511; |
0 | 28 }; |
29 #pragma pack(pop) | |
30 | |
31 | |
1051 | 32 int fixpoint_sub0(int, int); |
33 int fixpoint_div(int, int); | |
34 int fixpoint_mul(int, int); | |
35 int fixpoint_from_float(float value); | |
36 | |
1130 | 37 #ifndef ROUNDING_EPSILON |
38 #define ROUNDING_EPSILON 0.0000001 | |
39 #endif | |
0 | 40 |
1130 | 41 |
1131
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
42 template <typename FloatType> |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
43 int bankersRounding( |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
44 const FloatType& value |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
45 ) { |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
46 assert("Method unsupported for this type" && false); |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
47 return value; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
48 } |
1130 | 49 |
1131
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
50 template<> static int bankersRounding<float>(const float& inValue) |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
51 { |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
52 union Cast |
1130 | 53 { |
1131
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
54 double d; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
55 long l; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
56 }; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
57 volatile Cast c; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
58 c.d = inValue + 6755399441055744.0; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
59 return c.l; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
60 } |
1130 | 61 |
1131
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
62 #pragma push_macro("max") |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
63 #undef max |
1130 | 64 |
1131
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
65 template<> static int bankersRounding<double>(const double& inValue) |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
66 { |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
67 double maxValue = std::numeric_limits<int>::max(); |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
68 assert(maxValue - 6755399441055744.0 >= inValue); |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
69 union Cast |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
70 { |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
71 double d; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
72 long l; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
73 }; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
74 volatile Cast c; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
75 c.d = inValue + 6755399441055744.0; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
76 return c.l; |
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
77 } |
1130 | 78 |
79 | |
1131
71ba92960bc5
banker's rounding template made as fast as before and a bit more safe
Grumpy7
parents:
1130
diff
changeset
|
80 #pragma pop_macro("max") |
1130 | 81 |
0 | 82 extern struct stru193_math *stru_5C6E00; |