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 ***************************************************************************/
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
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))
36 #define fixed_LOMASK ((fixed)((1<<FIXED_BITS)-1))
37 #define fixed_HIMASK ((fixed)(~fixed_LOMASK))
39 // int<->fixed conversions:
40 #define i2x(x) ((x)<<FIXED_BITS)
41 #define x2i(x) ((x)>>FIXED_BITS)
43 INLINE fixed FixedCeil(const fixed x)
45 return (x + (fixed_ONE - 1)) & fixed_HIMASK;
48 INLINE s32 FixedCeilToInt(const fixed x)
50 return (x + (fixed_ONE - 1)) >> FIXED_BITS;
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))
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)
64 asm("recip.s %0,%1" : "=f" (res) : "f" (x));
68 INLINE float FloatInv(const float x)
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))
80 // big precision inverse table.
82 s32 s_invTable[(1<<TABLE_BITS)];
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; }
90 INLINE u32 Log2(u32 x) { u32 i = 0; for ( ; x > 0; ++i, x >>= 1); return i - 1; }
93 INLINE void xInv (const fixed _b, s32& iFactor_, s32& iShift_)
95 u32 uD = (_b<0) ? -_b : _b;
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):
114 INLINE fixed xInvMulx (const fixed _a, const s32 _iFact, const s32 _iShift)
118 asm ("smull %Q0, %R0, %1, %2" : "=&r" (res) : "r"(_a) , "r"(_iFact));
119 return fixed(res>>_iShift);
121 return fixed( ((s64)(_a)*(s64)(_iFact))>>(_iShift) );
125 INLINE fixed xLoDivx (const fixed _a, const fixed _b)
128 xInv(_b, iFact, iShift);
129 return xInvMulx(_a, iFact, iShift);
131 #endif // GPU_UNAI_USE_INT_DIV_MULTINV
132 ///////////////////////////////////////////////////////////////////////////
133 // --- END INVERSE APPROXIMATION SECTION ---
134 ///////////////////////////////////////////////////////////////////////////