1 /***************************************************************************
2 * Copyright (C) 2010 PCSX4ALL Team *
3 * Copyright (C) 2010 Unai *
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. *
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. *
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 ***************************************************************************/
24 #include "arm_features.h"
28 #ifdef GPU_TABLE_10_BITS
36 #define fixed_ZERO ((fixed)0)
37 #define fixed_ONE ((fixed)1<<FIXED_BITS)
38 #define fixed_TWO ((fixed)2<<FIXED_BITS)
39 #define fixed_HALF ((fixed)((1<<FIXED_BITS)>>1))
41 // big precision inverse table.
42 s32 s_invTable[(1<<TABLE_BITS)];
44 INLINE fixed i2x(const int _x) { return ((_x)<<FIXED_BITS); }
45 INLINE fixed x2i(const fixed _x) { return ((_x)>>FIXED_BITS); }
48 INLINE u32 Log2(u32 _a)
50 u32 c = 0; // result of log2(v) will go here
51 if (_a & 0xFFFF0000) { _a >>= 16; c |= 16; }
52 if (_a & 0xFF00) { _a >>= 8; c |= 8; }
53 if (_a & 0xF0) { _a >>= 4; c |= 4; }
54 if (_a & 0xC) { _a >>= 2; c |= 2; }
55 if (_a & 0x2) { _a >>= 1; c |= 1; }
61 INLINE u32 Log2(u32 x) { u32 res; asm("clz %0,%1" : "=r" (res) : "r" (x)); return 32-res; }
63 INLINE u32 Log2(u32 x) { u32 i = 0; for ( ; x > 0; ++i, x >>= 1); return i - 1; }
66 #ifdef GPU_TABLE_10_BITS
67 INLINE void xInv (const fixed _b, s32& iFactor_, s32& iShift_)
69 u32 uD = (_b<0) ? -_b : _b ;
71 uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0;
73 iFactor_ = s_invTable[uDen];
74 iFactor_ = (_b<0) ? -iFactor_ :iFactor_;
78 INLINE void xInv (const fixed _b, s32& iFactor_, s32& iShift_)
80 u32 uD = (_b<0) ? -_b : _b;
84 uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0;
85 u32 uDen = (uD>>uLog)-1;
86 iFactor_ = s_invTable[uDen];
87 iFactor_ = (_b<0) ? -iFactor_ :iFactor_;
98 INLINE fixed xInvMulx (const fixed _a, const s32 _iFact, const s32 _iShift)
102 asm ("smull %Q0, %R0, %1, %2" : "=&r" (res) : "r"(_a) , "r"(_iFact));
103 return fixed(res>>_iShift);
105 return fixed( ((s64)(_a)*(s64)(_iFact))>>(_iShift) );
109 INLINE fixed xLoDivx (const fixed _a, const fixed _b)
112 xInv(_b, iFact, iShift);
113 return xInvMulx(_a, iFact, iShift);
116 ///////////////////////////////////////////////////////////////////////////
118 INLINE T Min2 (const T _a, const T _b) { return (_a<_b)?_a:_b; }
121 INLINE T Min3 (const T _a, const T _b, const T _c) { return Min2(Min2(_a,_b),_c); }
123 ///////////////////////////////////////////////////////////////////////////
125 INLINE T Max2 (const T _a, const T _b) { return (_a>_b)?_a:_b; }
128 INLINE T Max3 (const T _a, const T _b, const T _c) { return Max2(Max2(_a,_b),_c); }
130 ///////////////////////////////////////////////////////////////////////////