364321b7e5a86a52d0939f86cf471604b8b63112
[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 typedef s32 fixed;
25
26 //senquack - The gpu_drhell poly routines I adapted use 22.10 fixed point,
27 //           while original Unai used 16.16: (see README_senquack.txt)
28 //#define FIXED_BITS 16
29 #define FIXED_BITS 10
30
31 #define fixed_ZERO ((fixed)0)
32 #define fixed_ONE  ((fixed)1<<FIXED_BITS)
33 #define fixed_TWO  ((fixed)2<<FIXED_BITS)
34 #define fixed_HALF ((fixed)((1<<FIXED_BITS)>>1))
35
36 #define fixed_LOMASK ((fixed)((1<<FIXED_BITS)-1))
37 #define fixed_HIMASK ((fixed)(~fixed_LOMASK))
38
39 // int<->fixed conversions:
40 #define i2x(x) ((x)<<FIXED_BITS)
41 #define x2i(x) ((x)>>FIXED_BITS)
42
43 INLINE fixed FixedCeil(const fixed x)
44 {
45         return (x + (fixed_ONE - 1)) & fixed_HIMASK;
46 }
47
48 INLINE s32 FixedCeilToInt(const fixed x)
49 {
50         return (x + (fixed_ONE - 1)) >> FIXED_BITS;
51 }
52
53 //senquack - float<->fixed conversions:
54 #define f2x(x) ((s32)((x) * (float)(1<<FIXED_BITS)))
55 #define x2f(x) ((float)(x) / (float)(1<<FIXED_BITS))
56
57 //senquack - floating point reciprocal:
58 //NOTE: These assume x is always != 0 !!!
59 #ifdef GPU_UNAI_USE_FLOATMATH
60 #if defined(_MIPS_ARCH_MIPS32R2) || (__mips == 64)
61 INLINE float FloatInv(const float x)
62 {
63         float res;
64         asm("recip.s %0,%1" : "=f" (res) : "f" (x));
65         return res;
66 }
67 #else
68 INLINE float FloatInv(const float x)
69 {
70         return (1.0f / x);
71 }
72 #endif
73 #endif
74
75 ///////////////////////////////////////////////////////////////////////////
76 // --- BEGIN INVERSE APPROXIMATION SECTION ---
77 ///////////////////////////////////////////////////////////////////////////
78 #if defined(GPU_UNAI_USE_INT_DIV_MULTINV) || (!defined(GPU_UNAI_NO_OLD) && !defined(GPU_UNAI_USE_FLOATMATH))
79
80 //  big precision inverse table.
81 #define TABLE_BITS 16
82 s32 s_invTable[(1<<TABLE_BITS)];
83 #endif
84
85 #ifdef GPU_UNAI_USE_INT_DIV_MULTINV
86 //senquack - MIPS32 happens to have same instruction/format:
87 #if defined(__arm__) || (__mips == 32)
88 INLINE u32 Log2(u32 x) { u32 res; asm("clz %0,%1" : "=r" (res) : "r" (x)); return 32-res; }
89 #else
90 INLINE u32 Log2(u32 x) { u32 i = 0; for ( ; x > 0; ++i, x >>= 1); return i - 1; }
91 #endif
92
93 INLINE  void  xInv (const fixed _b, s32& iFactor_, s32& iShift_)
94 {
95   u32 uD = (_b<0) ? -_b : _b;
96   if(uD>1)
97   {
98         u32 uLog = Log2(uD);
99     uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0;
100     u32 uDen = (uD>>uLog);
101     iFactor_ = s_invTable[uDen];
102     iFactor_ = (_b<0) ? -iFactor_ :iFactor_;
103     //senquack - Adapted to 22.10 fixed point (originally 16.16):
104     //iShift_  = 15+uLog;
105     iShift_  = 21+uLog;
106   }
107   else
108   {
109     iFactor_=_b;
110     iShift_ = 0;
111   }
112 }
113
114 INLINE  fixed xInvMulx  (const fixed _a, const s32 _iFact, const s32 _iShift)
115 {
116         #ifdef __arm__
117                 s64 res;
118                 asm ("smull %Q0, %R0, %1, %2" : "=&r" (res) : "r"(_a) , "r"(_iFact));
119                 return fixed(res>>_iShift);
120         #else
121                 return fixed( ((s64)(_a)*(s64)(_iFact))>>(_iShift) );
122         #endif
123 }
124
125 INLINE  fixed xLoDivx   (const fixed _a, const fixed _b)
126 {
127   s32 iFact, iShift;
128   xInv(_b, iFact, iShift);
129   return xInvMulx(_a, iFact, iShift);
130 }
131 #endif // GPU_UNAI_USE_INT_DIV_MULTINV
132 ///////////////////////////////////////////////////////////////////////////
133 // --- END INVERSE APPROXIMATION SECTION ---
134 ///////////////////////////////////////////////////////////////////////////
135
136 #endif  //FIXED_H