| 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 | #ifdef GPU_UNAI_USE_INT_DIV_MULTINV |
| 79 | |
| 80 | // big precision inverse table. |
| 81 | #define TABLE_BITS 16 |
| 82 | s32 s_invTable[(1<<TABLE_BITS)]; |
| 83 | |
| 84 | //senquack - MIPS32 happens to have same instruction/format: |
| 85 | #if defined(__arm__) || (__mips == 32) |
| 86 | INLINE u32 Log2(u32 x) { u32 res; asm("clz %0,%1" : "=r" (res) : "r" (x)); return 32-res; } |
| 87 | #else |
| 88 | INLINE u32 Log2(u32 x) { u32 i = 0; for ( ; x > 0; ++i, x >>= 1); return i - 1; } |
| 89 | #endif |
| 90 | |
| 91 | INLINE void xInv (const fixed _b, s32& iFactor_, s32& iShift_) |
| 92 | { |
| 93 | u32 uD = (_b<0) ? -_b : _b; |
| 94 | if(uD>1) |
| 95 | { |
| 96 | u32 uLog = Log2(uD); |
| 97 | uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0; |
| 98 | u32 uDen = (uD>>uLog); |
| 99 | iFactor_ = s_invTable[uDen]; |
| 100 | iFactor_ = (_b<0) ? -iFactor_ :iFactor_; |
| 101 | //senquack - Adapted to 22.10 fixed point (originally 16.16): |
| 102 | //iShift_ = 15+uLog; |
| 103 | iShift_ = 21+uLog; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | iFactor_=_b; |
| 108 | iShift_ = 0; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | INLINE fixed xInvMulx (const fixed _a, const s32 _iFact, const s32 _iShift) |
| 113 | { |
| 114 | #ifdef __arm__ |
| 115 | s64 res; |
| 116 | asm ("smull %Q0, %R0, %1, %2" : "=&r" (res) : "r"(_a) , "r"(_iFact)); |
| 117 | return fixed(res>>_iShift); |
| 118 | #else |
| 119 | return fixed( ((s64)(_a)*(s64)(_iFact))>>(_iShift) ); |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | INLINE fixed xLoDivx (const fixed _a, const fixed _b) |
| 124 | { |
| 125 | s32 iFact, iShift; |
| 126 | xInv(_b, iFact, iShift); |
| 127 | return xInvMulx(_a, iFact, iShift); |
| 128 | } |
| 129 | #endif // GPU_UNAI_USE_INT_DIV_MULTINV |
| 130 | /////////////////////////////////////////////////////////////////////////// |
| 131 | // --- END INVERSE APPROXIMATION SECTION --- |
| 132 | /////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | #endif //FIXED_H |