add some missing license headers
[pcsx_rearmed.git] / plugins / gpu_unai / gpu_fixedpoint.h
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 #include "arm_features.h"
25
26 typedef s32 fixed;
27
28 #ifdef GPU_TABLE_10_BITS
29 #define TABLE_BITS 10
30 #else
31 #define TABLE_BITS 16
32 #endif
33
34 #define FIXED_BITS 16
35
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))
40
41 //  big precision inverse table.
42 s32 s_invTable[(1<<TABLE_BITS)];
43
44 INLINE  fixed i2x(const int   _x) { return  ((_x)<<FIXED_BITS); }
45 INLINE  fixed x2i(const fixed _x) { return  ((_x)>>FIXED_BITS); }
46
47 /*
48 INLINE u32 Log2(u32 _a)
49 {
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;  }
56   return c;
57 }
58 */
59
60 #ifdef HAVE_ARMV5
61 INLINE u32 Log2(u32 x) { u32 res; asm("clz %0,%1" : "=r" (res) : "r" (x)); return 32-res; }
62 #else
63 INLINE u32 Log2(u32 x) { u32 i = 0; for ( ; x > 0; ++i, x >>= 1); return i - 1; }
64 #endif
65
66 #ifdef GPU_TABLE_10_BITS
67 INLINE  void  xInv (const fixed _b, s32& iFactor_, s32& iShift_)
68 {
69     u32 uD   = (_b<0) ? -_b : _b ;
70     u32 uLog = Log2(uD);
71     uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0;
72     u32 uDen = uD>>uLog;
73     iFactor_ = s_invTable[uDen];
74     iFactor_ = (_b<0) ? -iFactor_ :iFactor_;
75     iShift_  = 15+uLog;
76 }
77 #else
78 INLINE  void  xInv (const fixed _b, s32& iFactor_, s32& iShift_)
79 {
80   u32 uD = (_b<0) ? -_b : _b;
81   if(uD>1)
82   {
83         u32 uLog = Log2(uD);
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_;
88     iShift_  = 15+uLog;
89   }
90   else
91   {
92     iFactor_=_b;
93     iShift_ = 0;
94   }
95 }
96 #endif
97
98 INLINE  fixed xInvMulx  (const fixed _a, const s32 _iFact, const s32 _iShift)
99 {
100         #ifdef __arm__
101                 s64 res;
102                 asm ("smull %Q0, %R0, %1, %2" : "=&r" (res) : "r"(_a) , "r"(_iFact));
103                 return fixed(res>>_iShift);
104         #else
105                 return fixed( ((s64)(_a)*(s64)(_iFact))>>(_iShift) );
106         #endif
107 }
108
109 INLINE  fixed xLoDivx   (const fixed _a, const fixed _b)
110 {
111   s32 iFact, iShift;
112   xInv(_b, iFact, iShift);
113   return xInvMulx(_a, iFact, iShift);
114 }
115
116 ///////////////////////////////////////////////////////////////////////////
117 template<typename T>
118 INLINE  T Min2 (const T _a, const T _b)             { return (_a<_b)?_a:_b; }
119
120 template<typename T>
121 INLINE  T Min3 (const T _a, const T _b, const T _c) { return  Min2(Min2(_a,_b),_c); }
122
123 ///////////////////////////////////////////////////////////////////////////
124 template<typename T>
125 INLINE  T Max2 (const T _a, const T _b)             { return  (_a>_b)?_a:_b; }
126
127 template<typename T>
128 INLINE  T Max3 (const T _a, const T _b, const T _c) { return  Max2(Max2(_a,_b),_c); }
129
130 ///////////////////////////////////////////////////////////////////////////
131 #endif  //FIXED_H