aboutsummaryrefslogtreecommitdiff
path: root/plugins/gpu_unai/gpu_fixedpoint.h
blob: 03f07d2108bf8d3d84a43591b73d5fef5e91bf6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/***************************************************************************
 *   Copyright (C) 2010 PCSX4ALL Team                                      *
 *   Copyright (C) 2010 Unai                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA.           *
 ***************************************************************************/

#ifndef FIXED_H
#define FIXED_H

typedef s32 fixed;

#ifdef GPU_TABLE_10_BITS
#define TABLE_BITS 10
#else
#define TABLE_BITS 16
#endif

#define FIXED_BITS 16

#define fixed_ZERO ((fixed)0)
#define fixed_ONE  ((fixed)1<<FIXED_BITS)
#define fixed_TWO  ((fixed)2<<FIXED_BITS)
#define fixed_HALF ((fixed)((1<<FIXED_BITS)>>1))

//  big precision inverse table.
s32 s_invTable[(1<<TABLE_BITS)];

INLINE  fixed i2x(const int   _x) { return  ((_x)<<FIXED_BITS); }
INLINE  fixed x2i(const fixed _x) { return  ((_x)>>FIXED_BITS); }

/*
INLINE u32 Log2(u32 _a)
{
  u32 c = 0; // result of log2(v) will go here
  if (_a & 0xFFFF0000) { _a >>= 16; c |= 16;  }
  if (_a & 0xFF00) { _a >>= 8; c |= 8;  }
  if (_a & 0xF0) { _a >>= 4; c |= 4;  }
  if (_a & 0xC) { _a >>= 2; c |= 2;  }
  if (_a & 0x2) { _a >>= 1; c |= 1;  }
  return c;
}
*/

#ifdef __arm__
INLINE u32 Log2(u32 x) { u32 res; asm("clz %0,%1" : "=r" (res) : "r" (x)); return 32-res; }
#else
INLINE u32 Log2(u32 x) { u32 i = 0; for ( ; x > 0; ++i, x >>= 1); return i - 1; }
#endif

#ifdef GPU_TABLE_10_BITS
INLINE  void  xInv (const fixed _b, s32& iFactor_, s32& iShift_)
{
    u32 uD   = (_b<0) ? -_b : _b ;
    u32 uLog = Log2(uD);
    uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0;
    u32 uDen = uD>>uLog;
    iFactor_ = s_invTable[uDen];
    iFactor_ = (_b<0) ? -iFactor_ :iFactor_;
    iShift_  = 15+uLog;
}
#else
INLINE  void  xInv (const fixed _b, s32& iFactor_, s32& iShift_)
{
  u32 uD = (_b<0) ? -_b : _b;
  if(uD>1)
  {
	u32 uLog = Log2(uD);
    uLog = uLog>(TABLE_BITS-1) ? uLog-(TABLE_BITS-1) : 0;
    u32 uDen = (uD>>uLog)-1;
    iFactor_ = s_invTable[uDen];
    iFactor_ = (_b<0) ? -iFactor_ :iFactor_;
    iShift_  = 15+uLog;
  }
  else
  {
    iFactor_=_b;
    iShift_ = 0;
  }
}
#endif

INLINE  fixed xInvMulx  (const fixed _a, const s32 _iFact, const s32 _iShift)
{
	#ifdef __arm__
		s64 res;
		asm ("smull %Q0, %R0, %1, %2" : "=&r" (res) : "r"(_a) , "r"(_iFact));
		return fixed(res>>_iShift);
	#else
		return fixed( ((s64)(_a)*(s64)(_iFact))>>(_iShift) );
	#endif
}

INLINE  fixed xLoDivx   (const fixed _a, const fixed _b)
{
  s32 iFact, iShift;
  xInv(_b, iFact, iShift);
  return xInvMulx(_a, iFact, iShift);
}

///////////////////////////////////////////////////////////////////////////
template<typename T>
INLINE  T Min2 (const T _a, const T _b)             { return (_a<_b)?_a:_b; }

template<typename T>
INLINE  T Min3 (const T _a, const T _b, const T _c) { return  Min2(Min2(_a,_b),_c); }

///////////////////////////////////////////////////////////////////////////
template<typename T>
INLINE  T Max2 (const T _a, const T _b)             { return  (_a>_b)?_a:_b; }

template<typename T>
INLINE  T Max3 (const T _a, const T _b, const T _c) { return  Max2(Max2(_a,_b),_c); }

///////////////////////////////////////////////////////////////////////////
#endif  //FIXED_H