/[pcsx2_0.9.7]/trunk/common/include/Utilities/FixedPointTypes.inl
ViewVC logotype

Contents of /trunk/common/include/Utilities/FixedPointTypes.inl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (9 years, 11 months ago) by william
File size: 7295 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #pragma once
17
18 #include "FixedPointTypes.h"
19 #include <cmath> // for pow!
20
21 template< int Precision >
22 FixedInt<Precision>::FixedInt()
23 {
24 Raw = 0;
25 }
26
27 template< int Precision >
28 FixedInt<Precision>::FixedInt( int signedval )
29 {
30 Raw = signedval * Precision;
31 }
32
33 template< int Precision >
34 FixedInt<Precision>::FixedInt( double doubval )
35 {
36 Raw = (int)(doubval * (double)Precision);
37 }
38
39 template< int Precision >
40 FixedInt<Precision>::FixedInt( float floval )
41 {
42 Raw = (int)(floval * (float)Precision);
43 }
44
45 template< int Precision >
46 FixedInt<Precision> FixedInt<Precision>::operator+( const FixedInt<Precision>& right ) const
47 {
48 return FixedInt<Precision>().SetRaw( Raw + right.Raw );
49 }
50
51 template< int Precision >
52 FixedInt<Precision> FixedInt<Precision>::operator-( const FixedInt<Precision>& right ) const
53 {
54 return FixedInt<Precision>().SetRaw( Raw + right.Raw );
55 }
56
57 template< int Precision >
58 FixedInt<Precision>& FixedInt<Precision>::operator+=( const FixedInt<Precision>& right )
59 {
60 return SetRaw( Raw + right.Raw );
61 }
62
63 template< int Precision >
64 FixedInt<Precision>& FixedInt<Precision>::operator-=( const FixedInt<Precision>& right )
65 {
66 return SetRaw( Raw + right.Raw );
67 }
68
69 template< int Precision >
70 FixedInt<Precision>& FixedInt<Precision>::ConfineTo( const FixedInt<Precision>& low, const FixedInt<Precision>& high )
71 {
72 return SetRaw( std::min( std::max( Raw, low.Raw ), high.Raw ) );
73 }
74
75 // Uses 64 bit internally to avoid overflows. For more precise/optimized 32 bit math
76 // you'll need to use the Raw values directly.
77 template< int Precision >
78 FixedInt<Precision> FixedInt<Precision>::operator*( const FixedInt<Precision>& right ) const
79 {
80 s64 mulres = (s64)Raw * right.Raw;
81 return FixedInt<Precision>().SetRaw( (s32)(mulres / Precision) );
82 }
83
84 // Uses 64 bit internally to avoid overflows. For more precise/optimized 32 bit math
85 // you'll need to use the Raw values directly.
86 template< int Precision >
87 FixedInt<Precision> FixedInt<Precision>::operator/( const FixedInt<Precision>& right ) const
88 {
89 s64 divres = Raw * Precision;
90 return FixedInt<Precision>().SetRaw( (s32)(divres / right.Raw) );
91 }
92
93 // Uses 64 bit internally to avoid overflows. For more precise/optimized 32 bit math
94 // you'll need to use the Raw values directly.
95 template< int Precision >
96 FixedInt<Precision>& FixedInt<Precision>::operator*=( const FixedInt<Precision>& right )
97 {
98 s64 mulres = (s64)Raw * right.Raw;
99 return SetRaw( (s32)(mulres / Precision) );
100 }
101
102 // Uses 64 bit internally to avoid overflows. For more precise/optimized 32 bit math
103 // you'll need to use the Raw values directly.
104 template< int Precision >
105 FixedInt<Precision>& FixedInt<Precision>::operator/=( const FixedInt<Precision>& right )
106 {
107 s64 divres = Raw * Precision;
108 return SetRaw( (s32)(divres / right.Raw) );
109 }
110
111 // returns TRUE if the value overflows the legal integer range of this container.
112 template< int Precision >
113 bool FixedInt<Precision>::OverflowCheck( int signedval )
114 {
115 return ( signedval >= (INT_MAX / Precision) );
116 }
117
118 // returns TRUE if the value overflows the legal integer range of this container.
119 template< int Precision >
120 bool FixedInt<Precision>::OverflowCheck( double signedval )
121 {
122 return ( signedval >= (INT_MAX / Precision) );
123 }
124
125 template< int Precision > int FixedInt<Precision>::GetWhole() const { return Raw / Precision; }
126 template< int Precision > int FixedInt<Precision>::GetFraction() const { return Raw % Precision; }
127
128 template< int Precision >
129 FixedInt<Precision>& FixedInt<Precision>::SetRaw( s32 rawsrc )
130 {
131 Raw = rawsrc;
132 return *this;
133 }
134
135 template< int Precision >
136 FixedInt<Precision>& FixedInt<Precision>::Round()
137 {
138 Raw = ToIntRounded();
139 return *this;
140 }
141
142 template< int Precision >
143 FixedInt<Precision>& FixedInt<Precision>::SetWhole( s32 wholepart )
144 {
145 pxAssert( wholepart < (INT_MAX / Precision) );
146 Raw = GetFraction() + (wholepart * Precision);
147 return *this;
148 }
149
150 template< int Precision >
151 FixedInt<Precision>& FixedInt<Precision>::SetFraction( u32 fracpart )
152 {
153 Raw = (GetWhole() * Precision) + fracpart;
154 return *this;
155 }
156
157 template< int Precision >
158 wxString FixedInt<Precision>::ToString() const
159 {
160 return wxsFormat( L"%d.%d", GetWhole(), (GetFraction() * 100) / Precision );
161 }
162
163 template< int Precision >
164 wxString FixedInt<Precision>::ToString( int fracDigits ) const
165 {
166 if( fracDigits == 0 ) return wxsFormat( L"%d", GetWhole() );
167
168 pxAssert( fracDigits <= 7 ); // higher numbers would just cause overflows and bad mojo.
169 int mulby = (int)pow( 10.0, fracDigits );
170 return wxsFormat( L"%d.%d", GetWhole(), (GetFraction() * mulby) / Precision );
171 }
172
173 template< int Precision >
174 double FixedInt<Precision>::ToDouble() const
175 {
176 return ((double)Raw / (double)Precision);
177 }
178
179 template< int Precision >
180 float FixedInt<Precision>::ToFloat() const
181 {
182 return ((float)Raw / (float)Precision);
183 }
184
185 template< int Precision >
186 int FixedInt<Precision>::ToIntTruncated() const
187 {
188 return Raw / Precision;
189 }
190
191 template< int Precision >
192 int FixedInt<Precision>::ToIntRounded() const
193 {
194 return (Raw + (Precision/2)) / Precision;
195 }
196
197 template< int Precision >
198 bool FixedInt<Precision>::TryFromString( FixedInt<Precision>& dest, const wxString& parseFrom )
199 {
200 long whole=0, frac=0;
201 const wxString beforeFirst( parseFrom.BeforeFirst( L'.' ) );
202 const wxString afterFirst( parseFrom.AfterFirst( L'.' ).Mid(0, 5) );
203 bool success = true;
204
205 if( !beforeFirst.IsEmpty() )
206 success = success && beforeFirst.ToLong( &whole );
207
208 if( !afterFirst.IsEmpty() )
209 success = success && afterFirst.ToLong( &frac );
210
211 if( !success ) return false;
212
213 dest.SetWhole( whole );
214
215 if( afterFirst.Length() != 0 && frac != 0 )
216 {
217 int fracPower = (int)pow( 10.0, (int)afterFirst.Length() );
218 dest.SetFraction( (frac * Precision) / fracPower );
219 }
220 return true;
221 }
222
223 template< int Precision >
224 FixedInt<Precision> FixedInt<Precision>::FromString( const wxString& parseFrom, const FixedInt<Precision>& defval )
225 {
226 FixedInt<Precision> dest;
227 if( !TryFromString( dest, parseFrom ) ) return defval;
228 return dest;
229 }
230
231 // This version of FromString throws a ParseError exception if the conversion fails.
232 template< int Precision >
233 FixedInt<Precision> FixedInt<Precision>::FromString( const wxString parseFrom )
234 {
235 FixedInt<Precision> dest;
236 if( !TryFromString( dest, parseFrom ) ) throw Exception::ParseError()
237 .SetDiagMsg(wxsFormat(L"Parse error on FixedInt<%d>::FromString", Precision));
238
239 return dest;
240 }

  ViewVC Help
Powered by ViewVC 1.1.22