86aad47b |
1 | /*************************************************************************** |
2 | * Copyright (C) 2010 PCSX4ALL Team * |
3 | * Copyright (C) 2010 Unai * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #ifndef FIXED_H |
22 | #define FIXED_H |
23 | |
24 | typedef s32 fixed; |
25 | |
26 | #ifdef GPU_TABLE_10_BITS |
27 | #define TABLE_BITS 10 |
28 | #else |
29 | #define TABLE_BITS 16 |
30 | #endif |
31 | |
32 | #define FIXED_BITS 16 |
33 | |
34 | #define fixed_ZERO ((fixed)0) |
35 | #define fixed_ONE ((fixed)1<<FIXED_BITS) |
36 | #define fixed_TWO ((fixed)2<<FIXED_BITS) |
37 | #define fixed_HALF ((fixed)((1<<FIXED_BITS)>>1)) |
38 | |
39 | // big precision inverse table. |
40 | s32 s_invTable[(1<<TABLE_BITS)]; |
41 | |
42 | INLINE fixed i2x(const int _x) { return ((_x)<<FIXED_BITS); } |
43 | INLINE fixed x2i(const fixed _x) { return ((_x)>>FIXED_BITS); } |
44 | |
45 | /* |
46 | INLINE u32 Log2(u32 _a) |
47 | { |
48 | u32 c = 0; // result of log2(v) will go here |
49 | if (_a & 0xFFFF0000) { _a >>= 16; c |= 16; } |
50 | if (_a & 0xFF00) { _a >>= 8; c |= 8; } |
51 | if (_a & 0xF0) { _a >>= 4; c |= 4; } |
52 | if (_a & 0xC) { _a >>= 2; c |= 2; } |
53 | if (_a & 0x2) { _a >>= 1; c |= 1; } |
54 | return c; |
55 | } |
56 | */ |
57 | |
58 | #ifdef __arm__ |
59 | INLINE u32 Log2(u32 x) { u32 res; asm("clz %0,%1" : "=r" (res) : "r" (x)); return 32-res; } |
60 | #else |
61 | INLINE u32 Log2(u32 x) { u32 i = 0; for ( ; x > 0; ++i, x >>= 1); return i - 1; } |
62 | #endif |
63 | |
64 | #ifdef GPU_TABLE_10_BITS |
65 | INLINE void xInv (const fixed _b, s32& iFactor_, s32& iShift_) |
66 | { |
67 | u32 uD = (_b<0) ? -_b : _b ; |
68 | u32 uLog = Log2(uD); |
69 | uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0; |
70 | u32 uDen = uD>>uLog; |
71 | iFactor_ = s_invTable[uDen]; |
72 | iFactor_ = (_b<0) ? -iFactor_ :iFactor_; |
73 | iShift_ = 15+uLog; |
74 | } |
75 | #else |
76 | INLINE void xInv (const fixed _b, s32& iFactor_, s32& iShift_) |
77 | { |
78 | u32 uD = (_b<0) ? -_b : _b; |
79 | if(uD>1) |
80 | { |
81 | u32 uLog = Log2(uD); |
82 | uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0; |
83 | u32 uDen = (uD>>uLog)-1; |
84 | iFactor_ = s_invTable[uDen]; |
85 | iFactor_ = (_b<0) ? -iFactor_ :iFactor_; |
86 | iShift_ = 15+uLog; |
87 | } |
88 | else |
89 | { |
90 | iFactor_=_b; |
91 | iShift_ = 0; |
92 | } |
93 | } |
94 | #endif |
95 | |
96 | INLINE fixed xInvMulx (const fixed _a, const s32 _iFact, const s32 _iShift) |
97 | { |
98 | #ifdef __arm__ |
99 | s64 res; |
100 | asm ("smull %Q0, %R0, %1, %2" : "=&r" (res) : "r"(_a) , "r"(_iFact)); |
101 | return fixed(res>>_iShift); |
102 | #else |
103 | return fixed( ((s64)(_a)*(s64)(_iFact))>>(_iShift) ); |
104 | #endif |
105 | } |
106 | |
107 | INLINE fixed xLoDivx (const fixed _a, const fixed _b) |
108 | { |
109 | s32 iFact, iShift; |
110 | xInv(_b, iFact, iShift); |
111 | return xInvMulx(_a, iFact, iShift); |
112 | } |
113 | |
114 | /////////////////////////////////////////////////////////////////////////// |
115 | template<typename T> |
116 | INLINE T Min2 (const T _a, const T _b) { return (_a<_b)?_a:_b; } |
117 | |
118 | template<typename T> |
119 | INLINE T Min3 (const T _a, const T _b, const T _c) { return Min2(Min2(_a,_b),_c); } |
120 | |
121 | /////////////////////////////////////////////////////////////////////////// |
122 | template<typename T> |
123 | INLINE T Max2 (const T _a, const T _b) { return (_a>_b)?_a:_b; } |
124 | |
125 | template<typename T> |
126 | INLINE T Max3 (const T _a, const T _b, const T _c) { return Max2(Max2(_a,_b),_c); } |
127 | |
128 | /////////////////////////////////////////////////////////////////////////// |
129 | #endif //FIXED_H |