00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 class StringBase;
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 #ifndef INC_FIXED16
00130 #define INC_FIXED16
00131
00132
00133 const INT32 FIXED16_ONE = 0x10000L;
00134 const INT32 FIXED16_HALF= 0x08000L;
00135
00136 #if !defined(USE_NATIVE_XLONG)
00137 class CCAPI xlong;
00138 #endif
00139
00140 class CCAPI String;
00141
00142 class CCAPI fixed16
00143 {
00144 public:
00145 INT32 all;
00146
00147
00148
00149 public:
00150
00151 #define F16SHIFT 16 // marks binary point position
00152
00153
00154 #define SHORT_FIXED(x) ( ((INT32)(x)) << F16SHIFT )
00155
00156 inline fixed16() {};
00157 inline fixed16(const fixed16&);
00158 inline fixed16(INT32);
00159 inline fixed16(short);
00160 inline fixed16(double);
00161
00162 fixed16& Sin();
00163 fixed16& Cos();
00164
00165 inline fixed16 trunc() const;
00166 inline fixed16 round() const;
00167 inline fixed16 abs() const;
00168 inline fixed16 sgn() const;
00169
00170 void ToString(StringBase* out) const;
00171
00172
00173
00174 inline friend fixed16 Div32By32(const INT32 a, const INT32 b);
00175 inline friend fixed16 CCAPI operator+ (const fixed16&, const fixed16&);
00176 inline friend fixed16 CCAPI operator- (const fixed16&, const fixed16&);
00177 inline friend fixed16 CCAPI operator- (const fixed16&);
00178 inline friend fixed16 CCAPI operator* (const fixed16&, const fixed16&);
00179 inline friend fixed16 CCAPI operator/ (const fixed16&, const fixed16&);
00180 inline friend fixed16 CCAPI operator>> (const fixed16&, UINT32);
00181 inline friend fixed16 CCAPI operator<< (const fixed16&, UINT32);
00182
00183
00184
00185 inline friend INT32 CCAPI operator== (const fixed16&, const fixed16&);
00186 inline friend INT32 CCAPI operator== (const fixed16&, const short);
00187 inline friend INT32 CCAPI operator!= (const fixed16&, const fixed16&);
00188 inline friend INT32 CCAPI operator!= (const fixed16&, const short);
00189 inline friend INT32 CCAPI operator> (const fixed16&, const fixed16&);
00190 inline friend INT32 CCAPI operator> (const fixed16&, const short);
00191 inline friend INT32 CCAPI operator< (const fixed16&, const fixed16&);
00192 inline friend INT32 CCAPI operator< (const fixed16&, const short);
00193 inline friend INT32 CCAPI operator>= (const fixed16&, const fixed16&);
00194 inline friend INT32 CCAPI operator>= (const fixed16&, const short);
00195 inline friend INT32 CCAPI operator<= (const fixed16&, const fixed16&);
00196 inline friend INT32 CCAPI operator<= (const fixed16&, const short);
00197
00198
00199
00200 inline fixed16& operator= (const fixed16&);
00201 inline fixed16& operator= (const INT32);
00202 inline fixed16& operator= (const short);
00203 inline fixed16& operator= (const double);
00204 inline fixed16& operator+= (const fixed16&);
00205 inline fixed16& operator+= (const short);
00206 inline fixed16& operator-= (const fixed16&);
00207 inline fixed16& operator-= (const short);
00208 inline fixed16& operator*= (const fixed16&);
00209 inline fixed16& operator*= (const short);
00210 inline fixed16& operator/= (const fixed16&);
00211 inline fixed16& operator/= (const short);
00212 inline fixed16& operator<<= (const UINT32);
00213 inline fixed16& operator>>= (const UINT32);
00214
00215
00216
00217 inline fixed16& operator++ ();
00218 inline fixed16 operator++ (INT32);
00219 inline fixed16& operator-- ();
00220 inline fixed16 operator-- (INT32);
00221
00222
00223
00224 inline INT32 MakeInt() const;
00225 inline short MakeShort() const;
00226 inline double MakeDouble() const;
00227 inline INT32 MakeLong() const;
00228 inline xlong MakeXlong() const;
00229
00230 inline static fixed16 FromRawLong(INT32 d);
00231 inline static fixed16 FromShifted16(INT32 d);
00232 inline INT32 GetRawLong() const;
00233 inline INT32 GetShifted16() const;
00234 };
00235
00236
00237
00238 #define F16ASSIGN( it ) it
00239 #define F16ASSIGNTHIS *this
00240
00241 #include "fixed.h"
00242
00243
00244
00245
00246
00247
00248
00249
00250 #define FIXED16_DBL(d) fixed16::FromRawLong((INT32)((d)*65536))
00251
00252 inline fixed16::fixed16(const fixed16& initValue)
00253 {
00254 all = initValue.all;
00255 }
00256
00257 inline fixed16::fixed16(INT32 initValue)
00258 {
00259 all = SHORT_FIXED( initValue );
00260 }
00261
00262 inline fixed16::fixed16(short initValue)
00263 {
00264 all = SHORT_FIXED( initValue );
00265 }
00266
00267 inline fixed16::fixed16(double initValue)
00268 {
00269 *this = DoubleToFixed16(initValue);
00270 }
00271
00272 inline fixed16 fixed16::FromRawLong(INT32 d)
00273 {
00274 fixed16 temp;
00275 temp.all = d;
00276 return temp;
00277 }
00278
00279 inline fixed16 fixed16::FromShifted16(INT32 d)
00280 {
00281 fixed16 temp;
00282 temp.all = d;
00283 return temp;
00284 }
00285
00286 inline INT32 fixed16::MakeInt() const
00287 {
00288 return (INT32) (all >> F16SHIFT);
00289 }
00290
00291 inline short fixed16::MakeShort() const
00292 {
00293 return (short) (all >> F16SHIFT);
00294 }
00295
00296 inline INT32 fixed16::MakeLong() const
00297 {
00298 return all >> F16SHIFT;
00299 }
00300
00301 inline xlong fixed16::MakeXlong() const
00302 {
00303 return xlong( (short) (all >> F16SHIFT) );
00304 }
00305
00306
00307 inline fixed16& fixed16::operator= (const fixed16& operand)
00308 {
00309 this->all = operand.all;
00310
00311 return *this;
00312 }
00313
00314 inline double fixed16::MakeDouble() const
00315 {
00316 return Fixed16ToDouble(*this);
00317 }
00318
00319 inline fixed16& fixed16::operator= (const short operand)
00320 {
00321 this->all = SHORT_FIXED( operand );
00322 return *this;
00323 }
00324
00325 inline INT32 fixed16::GetRawLong() const
00326 {
00327 return this->all;
00328 }
00329
00330 inline INT32 fixed16::GetShifted16() const
00331 {
00332 return this->all;
00333 }
00334
00335 inline fixed16 fixed16::trunc() const
00336 {
00337 return FromRawLong( all & 0xFFFF0000L );
00338 }
00339
00340 inline fixed16 fixed16::round() const
00341 {
00342 return FromRawLong( ( all + FIXED16_HALF ) & 0xFFFF0000L );
00343 }
00344
00345 inline fixed16 fixed16::abs() const
00346 {
00347 return FromRawLong( all<0 ? -all : all );
00348 }
00349
00350 inline fixed16 fixed16::sgn() const
00351 {
00352 return FromRawLong( all<0 ? -FIXED16_ONE : FIXED16_ONE );
00353 }
00354
00355 inline fixed16 operator+ (const fixed16& operand1, const fixed16& operand2)
00356 {
00357 fixed16 result;
00358 result.all = operand1.all + operand2.all;
00359 return result;
00360 }
00361
00362 inline fixed16 operator- (const fixed16& operand1, const fixed16& operand2)
00363 {
00364 fixed16 result;
00365 result.all = operand1.all - operand2.all;
00366 return result;
00367 }
00368
00369 inline fixed16 operator- (const fixed16& operand)
00370 {
00371 fixed16 temp;
00372 temp.all = -operand.all;
00373 return temp;
00374 }
00375
00376 inline fixed16 operator* (const fixed16& operand1, const fixed16& operand2)
00377 {
00378 fixed16 result;
00379 F16ASSIGN(result) = Fixed16Mul( operand1, operand2 );
00380 return result;
00381 }
00382
00383 inline fixed16 operator/ (const fixed16& operand1, const fixed16& operand2)
00384 {
00385 fixed16 result;
00386 F16ASSIGN(result) = Fixed16Div( operand1, operand2 );
00387 return result;
00388 }
00389
00390 inline fixed16 operator>> (const fixed16& operand1, UINT32 operand2)
00391 {
00392 fixed16 result;
00393 result.all = operand1.all >> operand2;
00394 return result;
00395 }
00396
00397 inline fixed16 operator<< (const fixed16& operand1, UINT32 operand2)
00398 {
00399 fixed16 result;
00400
00401 result.all = operand1.all << operand2;
00402
00403 return result;
00404 }
00405
00406 inline INT32 operator== (const fixed16& operand1, const fixed16& operand2)
00407 {
00408 return operand1.all == operand2.all;
00409 }
00410
00411 inline INT32 operator== (const fixed16& operand1, const short operand2)
00412 {
00413 return operand1.all == SHORT_FIXED( operand2 );
00414 }
00415
00416 inline INT32 operator!= (const fixed16& operand1, const fixed16& operand2)
00417 {
00418 return operand1.all != operand2.all;
00419 }
00420
00421 inline INT32 operator!= (const fixed16& operand1, const short operand2)
00422 {
00423 return operand1.all != SHORT_FIXED( operand2 );
00424 }
00425
00426 inline INT32 operator> (const fixed16& operand1, const fixed16& operand2)
00427 {
00428 return operand1.all > operand2.all;
00429 }
00430
00431 inline INT32 operator> (const fixed16& operand1, const short operand2)
00432 {
00433 return operand1.all > SHORT_FIXED(operand2);
00434 }
00435
00436 inline INT32 operator< (const fixed16& operand1, const fixed16& operand2)
00437 {
00438 return operand1.all < operand2.all;
00439 }
00440
00441 inline INT32 operator< (const fixed16& operand1, const short operand2)
00442 {
00443 return operand1.all < SHORT_FIXED( operand2 );
00444 }
00445
00446 inline INT32 operator>= (const fixed16& operand1, const fixed16& operand2)
00447 {
00448 return operand1.all >= operand2.all;
00449 }
00450
00451 inline INT32 operator>= (const fixed16& operand1, const short operand2)
00452 {
00453 return operand1.all >= SHORT_FIXED( operand2 );
00454 }
00455
00456 inline INT32 operator<= (const fixed16& operand1, const fixed16& operand2)
00457 {
00458 return operand1.all <= operand2.all;
00459 }
00460
00461 inline INT32 operator<= (const fixed16& operand1, const short operand2)
00462 {
00463 return operand1.all <= SHORT_FIXED( operand2 );
00464 }
00465
00466 inline fixed16& fixed16::operator= (const INT32 operand)
00467 {
00468 this->all = SHORT_FIXED( operand );
00469 return *this;
00470 }
00471
00472 inline fixed16& fixed16::operator= (const double operand)
00473 {
00474 F16ASSIGNTHIS = DoubleToFixed16(operand);
00475 return *this;
00476 }
00477
00478 inline fixed16& fixed16::operator+= (const fixed16& operand)
00479 {
00480 this->all += operand.all;
00481 return *this;
00482 }
00483
00484 inline fixed16& fixed16::operator+= (const short operand)
00485 {
00486 this->all += SHORT_FIXED( operand );
00487 return *this;
00488 }
00489
00490 inline fixed16& fixed16::operator-= (const fixed16& operand)
00491 {
00492 this->all -= operand.all;
00493 return *this;
00494 }
00495
00496 inline fixed16& fixed16::operator-= (const short operand)
00497 {
00498 this->all -= SHORT_FIXED( operand );
00499 return *this;
00500 }
00501
00502 inline fixed16& fixed16::operator*= (const fixed16& operand)
00503 {
00504 F16ASSIGNTHIS = Fixed16Mul(*this, operand);
00505
00506 return *this;
00507 }
00508
00509 inline fixed16& fixed16::operator*= (const short operand)
00510 {
00511 fixed16 temp;
00512
00513 temp.all = SHORT_FIXED( operand );
00514
00515 F16ASSIGNTHIS = Fixed16Mul(*this, temp);
00516
00517 return *this;
00518 }
00519
00520 inline fixed16& fixed16::operator/= (const fixed16& operand)
00521 {
00522 F16ASSIGNTHIS = Fixed16Div(*this, operand);
00523
00524 return *this;
00525 }
00526
00527 inline fixed16& fixed16::operator/= (const short operand)
00528 {
00529 fixed16 temp;
00530
00531 temp.all = SHORT_FIXED( operand );
00532
00533 F16ASSIGNTHIS = Fixed16Div(*this, temp);
00534
00535 return *this;
00536 }
00537
00538 inline fixed16& fixed16::operator<<= (const UINT32 operand)
00539 {
00540 this->all <<= operand;
00541 return *this;
00542 }
00543
00544 inline fixed16& fixed16::operator>>= (const UINT32 operand)
00545 {
00546 this->all >>= operand;
00547 return *this;
00548 }
00549
00550 inline fixed16& fixed16::operator++ ()
00551 {
00552 this->all += FIXED16_ONE;
00553 return *this;
00554 }
00555
00556 inline fixed16 fixed16::operator++ (INT32 dummy)
00557 {
00558 fixed16 result = *this;
00559
00560 this->all += FIXED16_ONE;
00561
00562 return result;
00563 }
00564
00565 inline fixed16& fixed16::operator-- ()
00566 {
00567 this->all -= FIXED16_ONE;
00568 return *this;
00569 }
00570
00571 inline fixed16 fixed16::operator-- (INT32 dummy)
00572 {
00573 fixed16 result = *this;
00574
00575 this->all -= FIXED16_ONE;
00576
00577 return result;
00578 }
00579
00580 #endif