/* Copyright (c) 2002-2012 Croteam Ltd. All rights reserved. */ #ifndef SE_INCL_FIXINT_H #define SE_INCL_FIXINT_H #ifdef PRAGMA_ONCE #pragma once #endif /* * iInt - number of bits in integer part * iFrac - number of bits in fractional part * NOTE: Since 32 bit integer holds this fix int, iInt+iFrac must be 32. */ /* * Template class for fixed integer with arbitrary sizes of integer and fractional parts */ template class FixInt { //implementation: public: SLONG slHolder; // the representation is stored here //interface: public: /* Default constructor. */ inline FixInt(void) {}; /* Copy constructor. */ inline FixInt(const FixInt &x) { slHolder = x.slHolder; }; /* Constructor from integer. */ inline FixInt(SLONG sl) : slHolder(sl<(ULONG ul) : slHolder((SLONG)(ul<(SWORD sw) : slHolder((SLONG)(sw<(UWORD uw) : slHolder((SLONG)(uw<(SBYTE sb) : slHolder((SLONG)(sb<(UBYTE ub) : slHolder((SLONG)(ub<(signed int si) : slHolder((SLONG)(si<(unsigned int ui) : slHolder((SLONG)(ui<(float f) : slHolder((SLONG)(f*(1L<(double f) : slHolder((SLONG)(f*(1L<(SLONG slNewHolder, int iDummy) : slHolder(slNewHolder) {}; /* Conversion to integer (truncatenation). */ inline operator SLONG(void) const { return slHolder>>iFrac; }; /* Conversion to float (exact). */ inline operator float(void) const { return slHolder/(float) (1L< operator-(void) const { return FixInt(-slHolder, 1); }; /* Addition. */ inline FixInt &operator+=(FixInt x) { slHolder += x.slHolder; return *this; }; inline FixInt operator+(FixInt x) const { return FixInt(slHolder+x.slHolder, 1); }; /* Substraction. */ inline FixInt &operator-=(FixInt x) { slHolder -= x.slHolder; return *this;}; inline FixInt operator-(FixInt x) const { return FixInt(slHolder-x.slHolder, 1); }; /* Multiplication. */ inline FixInt operator*(FixInt x) const { return FixInt( (SLONG)((((__int64) slHolder)*x.slHolder) >>iFrac), 1); }; inline FixInt operator*(SLONG sl) const { return FixInt(slHolder*sl, 1); }; friend inline FixInt operator*(SLONG sl, FixInt x){ return FixInt(x.slHolder*sl, 1); }; inline FixInt &operator*=(FixInt x) { return *this = *this*x; }; /* Division. */ inline FixInt operator/(FixInt x) const { return FixInt( (SLONG) ( (((__int64)slHolder)< &operator/=(FixInt x) { return *this = *this/x; }; /* Relational operators. */ inline BOOL operator< (FixInt x) const { return slHolder< x.slHolder; }; inline BOOL operator<=(FixInt x) const { return slHolder<=x.slHolder; }; inline BOOL operator> (FixInt x) const { return slHolder> x.slHolder; }; inline BOOL operator>=(FixInt x) const { return slHolder>=x.slHolder; }; inline BOOL operator==(FixInt x) const { return slHolder==x.slHolder; }; inline BOOL operator!=(FixInt x) const { return slHolder!=x.slHolder; }; /* Rounding and truncatenation functions. */ friend inline SLONG Floor(FixInt x) { return (x.slHolder)>>iFrac; }; friend inline SLONG Ceil(FixInt x) { return (x.slHolder+(0xFFFFFFFFL>>iInt))>>iFrac; }; /* Truncatenate to fixed number of decimal bits. */ friend inline FixInt Trunc(FixInt x, int iDecimalPart) { return FixInt(x.slHolder& (0xFFFFFFFF*((1<