123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810 |
- //****************************************************************************
- //****************************************************************************
- //
- //
- // FIXED point class ... a replacement for floats in Actua Soccer
- //
- // Format: Fixed (1,19,12)
- //
- // NOTES :
- // There is very little range checking at the moment so ...
- //
- // BE VERY CAREFUL
- //
- //
- //
- // Author : P.Rankin
- // Version: 1.00.01.08.95
- //
- //****************************************************************************
- //****************************************************************************
- #ifndef ABS
- #define ABS(a) (( (a) >= 0 ) ? (a):-(a))
- #endif
- #ifndef f2L
- #define f2L(f) ((fixed)((long)((f)*4096)))
- #endif
- #define ItoF(f) ((long) (f)*4096)
- #define FRACTION 12
- #define HALF_FRACTION 6
- extern "C" int mul_64bit(int,int);
- extern "C" int div_64bit(int,int);
- class fixed {
- public:
- long f;
- operator char();
- operator unsigned char();
- operator short();
- operator int();
- operator long();
- fixed();
- fixed( int );
- fixed( long );
- fixed( fixed const & );
- // fixed &operator = ( double );
- fixed &operator = ( fixed const & );
- // fixed &operator = ( float );
- fixed &operator = ( long );
- fixed &operator = ( int );
- fixed &operator = ( short );
- fixed &operator += ( fixed const & );
- fixed &operator += ( long );
- fixed &operator += ( int );
- fixed &operator += ( short );
- fixed &operator -= ( fixed const & );
- fixed &operator -= ( long );
- fixed &operator -= ( int );
- fixed &operator -= ( short );
- fixed &operator *= ( fixed const & );
- fixed &operator *= ( long );
- fixed &operator *= ( int );
- fixed &operator *= ( short );
- fixed &operator /= ( fixed const & );
- fixed &operator /= ( int );
- fixed &operator /= ( short );
- fixed &operator >>= ( int );
- fixed &operator <<= ( int );
- fixed operator + () const;
- fixed operator - () const;
- };
- fixed operator * ( fixed const &, fixed const &);
- fixed operator * ( fixed const &, int);
- fixed operator * ( fixed const &, short);
- fixed operator * ( fixed const &, long);
- fixed operator * ( int , fixed const & );
- fixed operator * ( short , fixed const & );
- fixed operator * ( long , fixed const &);
- fixed operator + ( fixed const &, fixed const &);
- fixed operator + ( fixed const &, long);
- fixed operator + ( fixed const &, int);
- fixed operator + ( fixed const &, short);
- fixed operator + ( long __f1 , fixed const &__f0 );
- fixed operator + ( int , fixed const & );
- fixed operator + ( short , fixed const & );
- fixed operator - ( fixed const &, fixed const &);
- fixed operator - ( fixed const &, long);
- fixed operator - ( fixed const &, int);
- fixed operator - ( fixed const &, short);
- fixed operator - ( long ,fixed const &);
- fixed operator - ( int ,fixed const &);
- //fixed operator - ( short ,fixed const &);
- fixed operator / ( fixed const &, fixed const &);
- fixed operator / ( fixed const &, long);
- fixed operator / ( fixed const &, int);
- fixed operator / ( fixed const &, short);
- fixed operator / ( long , fixed const & );
- fixed operator / ( int , fixed const & );
- int operator == ( fixed const &, fixed const & );
- int operator == ( fixed const &, int );
- int operator == ( fixed const &, short );
- int operator == ( int ,fixed const & );
- int operator == ( short ,fixed const & );
- int operator != ( fixed const &, fixed const & );
- int operator != ( fixed const &, int );
- int operator != ( fixed const &, short );
- int operator != ( int ,fixed const & );
- int operator != ( short ,fixed const & );
- int operator < ( fixed const &, fixed const & );
- int operator < ( fixed const &, long );
- int operator < ( fixed const &, int );
- int operator < ( fixed const &, short );
- int operator < ( long , fixed const & );
- int operator < ( int ,fixed const & );
- int operator < ( short ,fixed const & );
- int operator > ( fixed const &, fixed const & );
- int operator > ( fixed const &, long );
- int operator > ( fixed const &, int );
- int operator > ( fixed const &, short );
- int operator > ( long , fixed const & );
- int operator > ( int ,fixed const & );
- int operator > ( short ,fixed const & );
- int operator <= ( fixed const &, fixed const & );
- int operator <= ( fixed const &, long );
- int operator <= ( fixed const &, int );
- int operator <= ( fixed const &, short );
- int operator <= ( int ,fixed const & );
- int operator <= ( short ,fixed const & );
- int operator >= ( fixed const &, fixed const & );
- int operator >= ( fixed const &, long );
- int operator >= ( fixed const &, int );
- int operator >= ( fixed const &, short );
- int operator >= ( int ,fixed const & );
- int operator >= ( short ,fixed const & );
- //****************************************************************************
- //
- // Constructors
- //
- //****************************************************************************
- inline fixed::fixed() {
- }
- inline fixed::fixed( int __f0 ) {
- f = __f0 << FRACTION;
- }
- inline fixed::fixed( long __f0 ) {
- f = __f0 ;
- }
- inline fixed::fixed( fixed const &__f0 ) {
- f = __f0.f;
- }
- //****************************************************************************
- //
- // Casts
- //
- //****************************************************************************
- inline fixed:: operator char() {
- return( f>>FRACTION );
- }
- inline fixed:: operator unsigned char() {
- return( f>>FRACTION );
- }
- inline fixed:: operator short() {
- return( f>>FRACTION );
- }
- inline fixed:: operator int() {
- return( f>>FRACTION );
- }
- inline fixed:: operator long() {
- return( f );
- }
- //****************************************************************************
- //
- // =
- //
- //****************************************************************************
- inline fixed &fixed:: operator =(fixed const &__d) {
- f = (long) ( __d.f );
- return(*this);
- }
- inline fixed &fixed:: operator =(int __d) {
- f = ( ItoF(__d) );
- return(*this);
- }
- inline fixed &fixed:: operator =(long __d) {
- f = ( (__d) );
- return(*this);
- }
- inline fixed &fixed:: operator =(short __d) {
- f = ( ItoF(__d) );
- return(*this);
- }
- //****************************************************************************
- //
- // *
- //
- //****************************************************************************
- inline fixed operator * ( fixed const &__f0, fixed const &__f1 ) {
- return((long)mul_64bit(__f0.f,__f1.f));
- }
- inline fixed operator * ( fixed const &__f0, long __f1 ) {
- return((long)mul_64bit(__f0.f,__f1));
- }
- inline fixed operator * ( fixed const &__f0, int __f1 ) {
- return( ((long)(__f0.f)) * __f1 );
- }
- inline fixed operator * ( fixed const &__f0, short __f1 ) {
- return( ((long)(__f0.f)) * __f1 );
- }
- inline fixed operator * ( int __f1 , fixed const &__f0) {
- return( ((long)(__f0.f)) * __f1 );
- }
- inline fixed operator * ( short __f1 , fixed const &__f0) {
- return( ((long)(__f0.f)) * __f1 );
- }
- inline fixed operator * ( long __f1 , fixed const &__f0) {
- return((long)mul_64bit(__f1,__f0.f));
- }
- //****************************************************************************
- //
- // +
- //
- //****************************************************************************
- inline fixed operator + ( fixed const &__f0, fixed const &__f1 ) {
- return( __f0.f + __f1.f );
- }
- inline fixed operator + ( fixed const &__f0, long __f1 ) {
- return( __f0.f + ((long)__f1) );
- }
- inline fixed operator + ( fixed const &__f0, int __f1 ) {
- return( __f0.f + (((long)__f1)<<FRACTION) );
- }
- inline fixed operator + ( fixed const &__f0, short __f1 ) {
- return( __f0.f + (((long)__f1)<<FRACTION) );
- }
- inline fixed operator + ( long __f1 , fixed const &__f0 ) {
- return( __f0.f + ((long)__f1) );
- }
- inline fixed operator + ( int __f1 , fixed const &__f0 ) {
- return( __f0.f + (((long)__f1)<<FRACTION) );
- }
- inline fixed operator + ( short __f1 , fixed const &__f0 ) {
- return( __f0.f + (((long)__f1)<<FRACTION) );
- }
- //****************************************************************************
- //
- // -
- //
- //****************************************************************************
- inline fixed operator - ( fixed const &__f0, fixed const &__f1 ) {
- return( __f0.f - __f1.f );
- }
- inline fixed operator - ( fixed const &__f0, long __f1 ) {
- return( __f0.f - (((long)__f1)) );
- }
- inline fixed operator - ( fixed const &__f0, int __f1 ) {
- return( __f0.f - (((long)__f1)<<FRACTION) );
- }
- inline fixed operator - ( fixed const &__f0, short __f1 ) {
- return( __f0.f - (((long)__f1)<<FRACTION) );
- }
- inline fixed operator - ( long __f1 , fixed const &__f0 ) {
- return( ( (((long)__f1)) - __f0.f ) );
- }
- inline fixed operator - ( int __f1 , fixed const &__f0 ) {
- return( ( (((long)__f1)<<FRACTION) - __f0.f ) );
- }
- /*
- inline fixed operator - ( short __f1 , fixed const &__f0 ) {
- return( (((long)__f1)<<FRACTION) - __f0.f ) );
- }
- */
- //****************************************************************************
- //
- // /
- //
- //****************************************************************************
- inline fixed operator / ( fixed const &__f0, fixed const &__f1 ) {
- if(__f1.f!=0)
- return((long)div_64bit(__f0.f,__f1.f));
- else
- return(__f0);
- #if 0
- if(( __f0.f <= 0x3ffff )&&(__f0.f >= -0x3ffff))
- {
- if(__f1.f!=0)
- return ( ( (__f0.f << FRACTION) / __f1.f ) );
- else
- return ( __f0.f );
- }
- else
- {
- if(ABS(__f1.f) < (long)64 )
- return ( __f0.f );
- else
- return ( ( __f0.f / (__f1.f>>HALF_FRACTION) ) <<HALF_FRACTION );
- }
- #endif
- }
- inline fixed operator / ( fixed const &__f0, long __f1 ) {
- if(( __f0.f < 0x3ffff )&&(__f0.f > -0x3ffff))
- return ( ( (__f0.f << FRACTION) / __f1 ) );
- else
- return ( ( __f0.f / (__f1>>HALF_FRACTION) ) <<HALF_FRACTION );
- }
- inline fixed operator / ( fixed const &__f0, int __f1 ) {
- return ( ( __f0.f / __f1 ) );
- }
- inline fixed operator / ( fixed const &__f0, short __f1 ) {
- return ( ( __f0.f / __f1 ) );
- }
- inline fixed operator / ( long __f0 , fixed const &__f1 ) {
- if(__f1.f!=0)
- return((long)div_64bit(__f0,__f1.f));
- else
- return(__f0);
- #if 0
- if(( __f0 < 0x3ffff )&&(__f0 > -0x3ffff))
- return ( ( (__f0 << FRACTION) / __f1.f ) );
- else
- return ( ( __f0 / (__f1.f>>HALF_FRACTION) ) <<HALF_FRACTION );
- #endif
- }
- inline fixed operator / ( int __f0 , fixed const &__f1 ) {
- if(__f1.f!=0)
- return((long)div_64bit(__f0 << FRACTION,__f1.f));
- else
- return(__f0);
- #if 0
- if(( __f0 <= 0x7f )&&(__f0 >= -0x7f))
- {
- if( __f1.f != 0 )
- return ( ( (__f0 << (2*FRACTION)) / __f1.f ) );
- else
- return ( __f0 );
- }
- else
- {
- if(ABS(__f1.f)< (long)64 )
- return ( __f0 );
- else
- return ( ( (__f0 << FRACTION) / (__f1.f>>HALF_FRACTION) ) <<HALF_FRACTION );
- }
- #endif
- }
- //****************************************************************************
- //
- // +=
- //
- //****************************************************************************
- inline fixed &fixed::operator += ( fixed const &__cv ) {
- f += __cv.f;
- return ( *this );
- }
- inline fixed &fixed::operator += ( long __cv ) {
- f += ( __cv );
- return ( *this );
- }
- inline fixed &fixed::operator += ( int __cv ) {
- f += ( __cv<<FRACTION );
- return ( *this );
- }
- inline fixed &fixed::operator += ( short __cv ) {
- f += ( __cv<<FRACTION );
- return ( *this );
- }
- //****************************************************************************
- //
- // -=
- //
- //****************************************************************************
- inline fixed &fixed::operator -= ( fixed const &__cv ) {
- f -= __cv.f;
- return ( *this );
- }
- inline fixed &fixed::operator -= ( long __cv ) {
- f += ( __cv );
- return ( *this );
- }
- inline fixed &fixed::operator -= ( int __cv ) {
- f -= ( __cv<<FRACTION );
- return ( *this );
- }
- inline fixed &fixed::operator -= ( short __cv ) {
- f -= ( __cv<<FRACTION );
- return ( *this );
- }
- //****************************************************************************
- //
- // *=
- //
- //****************************************************************************
- inline fixed &fixed::operator *= ( fixed const &__cv ) {
- f = (f >> HALF_FRACTION ) * (__cv.f >> HALF_FRACTION);
- return ( *this );
- }
- inline fixed &fixed::operator *= ( long __cv ) {
- f = (f >> HALF_FRACTION ) * (__cv >> HALF_FRACTION);
- return ( *this );
- }
- inline fixed &fixed::operator *= ( int __cv ) {
- f *= ( __cv );
- return ( *this );
- }
- inline fixed &fixed::operator *= ( short __cv ) {
- f *= ( __cv );
- return ( *this );
- }
- //****************************************************************************
- //
- // /=
- //
- //****************************************************************************
- inline fixed &fixed::operator /= ( fixed const &__cv ) {
- if(ABS(__cv.f)<64)
- return (*this);
- else
- f = ( ( f / (__cv.f>>HALF_FRACTION) ) <<HALF_FRACTION );
- return( *this );
- }
- inline fixed &fixed::operator /= ( int __cv ) {
- if(f!=0)
- f /= ( __cv );
- return ( *this );
- }
- inline fixed &fixed::operator /= ( short __cv ) {
- if(f!=0)
- f /= ( __cv );
- return ( *this );
- }
- //****************************************************************************
- //
- // << >> <<= >>=
- //
- //****************************************************************************
- #if 0
- inline int operator << ( int __i ) {
- // f = f << __i;
- return ( f<<__i );
- }
- #endif
- inline fixed &fixed::operator <<= ( int __i ) {
- f <<= __i;
- return ( *this );
- }
- #if 0
- inline int operator >> ( int __i ) {
- // f = f >> __i;
- return (f >>__i );
- }
- #endif
- inline fixed &fixed::operator >>= ( int __i ) {
- f >>= __i;
- return ( *this );
- }
- inline fixed fixed::operator + () const {
- return( *this );
- }
- inline fixed fixed::operator - () const {
- return( -f );
- }
- //****************************************************************************
- //
- // Conditionals
- //
- // ==
- //
- //****************************************************************************
- inline int operator == ( fixed const &__f0, fixed const &__f1 ) {
- return ( __f0.f == __f1.f );
- }
- inline int operator == ( fixed const &__f0, int __f1 ) {
- return ( __f0.f == (__f1<<FRACTION) );
- }
- inline int operator == ( fixed const &__f0, short __f1 ) {
- return ( __f0.f == (__f1<<FRACTION) );
- }
- inline int operator == ( int __f1 , fixed const &__f0 ) {
- return ( __f0.f == (__f1<<FRACTION) );
- }
- inline int operator == ( short __f1 , fixed const &__f0 ) {
- return ( __f0.f == (__f1<<FRACTION) );
- }
- //****************************************************************************
- //
- // !=
- //
- //****************************************************************************
- inline int operator != ( fixed const &__f0, fixed const &__f1 ) {
- return ( __f0.f != __f1.f );
- }
- inline int operator != ( fixed const &__f0, int __f1 ) {
- return ( __f0.f != (__f1<<FRACTION) );
- }
- inline int operator != ( fixed const &__f0, short __f1 ) {
- return ( __f0.f != (__f1<<FRACTION) );
- }
- inline int operator != ( int __f1 , fixed const &__f0 ) {
- return ( __f0.f != (__f1<<FRACTION) );
- }
- inline int operator != ( short __f1 , fixed const &__f0 ) {
- return ( __f0.f != (__f1<<FRACTION) );
- }
- //****************************************************************************
- //
- // <
- //
- //****************************************************************************
- inline int operator < ( fixed const &__f0, fixed const &__f1 ) {
- return ( __f0.f < __f1.f );
- }
- inline int operator < ( fixed const &__f0, long __f1 ) {
- return ( __f0.f < __f1 );
- }
- inline int operator < ( fixed const &__f0, int __f1 ) {
- return ( (__f0.f) < (__f1<<FRACTION) );
- }
- inline int operator < ( fixed const &__f0, short __f1 ) {
- return ( (__f0.f) < (__f1<<FRACTION) );
- }
- inline int operator < ( long __f1 , fixed const &__f0 ) {
- return ( __f1 < __f0.f );
- }
- inline int operator < ( int __f1 , fixed const &__f0 ) {
- return ( (__f1<<FRACTION) < (__f0.f) );
- }
- inline int operator < ( short __f1 , fixed const &__f0 ) {
- return ( (__f1<<FRACTION) < (__f0.f) );
- }
- //****************************************************************************
- //
- // >
- //
- //****************************************************************************
- inline int operator > ( fixed const &__f0, fixed const &__f1 ) {
- return ( __f0.f > __f1.f );
- }
- inline int operator > ( fixed const &__f0, long __f1 ) {
- return ( __f0.f > __f1 );
- }
- inline int operator > ( fixed const &__f0, int __f1 ) {
- return ( (__f0.f) > (__f1<<FRACTION) );
- }
- inline int operator > ( fixed const &__f0, short __f1 ) {
- return ( (__f0.f) > (__f1<<FRACTION) );
- }
- inline int operator > ( long __f1 , fixed const &__f0 ) {
- return ( __f1 > __f0.f );
- }
- inline int operator > ( int __f1 , fixed const &__f0 ) {
- return ( (__f1<<FRACTION) > (__f0.f) );
- }
- inline int operator > ( short __f1 , fixed const &__f0 ) {
- return ( (__f1<<FRACTION) > (__f0.f) );
- }
- //****************************************************************************
- //
- // <=
- //
- //****************************************************************************
- inline int operator <= ( fixed const &__f0, fixed const &__f1 ) {
- return ( __f0.f <= __f1.f );
- }
- inline int operator <= ( fixed const &__f0, long __f1 ) {
- return ( __f0.f <= __f1 );
- }
- inline int operator <= ( fixed const &__f0, int __f1 ) {
- return ( (__f0.f) <= (__f1<<FRACTION) );
- }
- inline int operator <= ( fixed const &__f0, short __f1 ) {
- return ( (__f0.f) <= (__f1<<FRACTION) );
- }
- inline int operator <= ( int __f1 , fixed const &__f0 ) {
- return ( (__f1<<FRACTION) <= (__f0.f) );
- }
- inline int operator <= ( short __f1 , fixed const &__f0 ) {
- return ( (__f1<<FRACTION) <= (__f0.f) );
- }
- //****************************************************************************
- //
- // >=
- //
- //****************************************************************************
- inline int operator >= ( fixed const &__f0, fixed const &__f1 ) {
- return ( __f0.f >= __f1.f );
- }
- inline int operator >= ( fixed const &__f0, long __f1 ) {
- return ( __f0.f >= __f1 );
- }
- inline int operator >= ( fixed const &__f0, int __f1 ) {
- return ( (__f0.f) >= (__f1<<FRACTION) );
- }
- inline int operator >= ( fixed const &__f0, short __f1 ) {
- return ( (__f0.f) >= (__f1<<FRACTION) );
- }
- inline int operator >= ( int __f1 , fixed const &__f0 ) {
- return ( (__f1<<FRACTION) >= (__f0.f) );
- }
- inline int operator >= ( short __f1 , fixed const &__f0 ) {
- return ( (__f1<<FRACTION) >= (__f0.f) );
- }
- #if 0
- inline Complex conj( Complex const &__cv ) {
- return Complex( __cv.real(), -__cv.imag() );
- }
- inline double Complex::real() const {
- return( __r );
- }
- inline double real( Complex const &__cv ) {
- return( __cv.real() );
- }
- inline double norm( Complex const &__cv ) {
- return( __cv.real() * __cv.real() + __cv.imag() * __cv.imag() );
- }
- double abs ( Complex const & ); // magnitude of vector
- Complex acos ( Complex const & ); // arccosine
- Complex acosh( Complex const & ); // hyperbolic arccosine
- double arg ( Complex const & ); // angle of vector
- Complex asin ( Complex const & ); // arcsin
- Complex asinh( Complex const & ); // hyperbolic arcsin
- Complex atan ( Complex const & ); // arctangent
- Complex atanh( Complex const & ); // hyperbolic arctangent
- Complex conj ( Complex const & ); // conjugate
- Complex cos ( Complex const & ); // cosine
- Complex cosh ( Complex const & ); // hyperbolic cosine
- Complex exp ( Complex const & ); // e raised to a power
- double imag ( Complex const & ); // imaginary part
- Complex log ( Complex const & ); // log base e
- Complex log10( Complex const & ); // log base 10
- double norm ( Complex const & ); // square of magnitude
- Complex polar( double __mag,
- double __angle = 0 ); // polar to Complex
- Complex pow ( Complex const &__base, // Complex ** Complex
- Complex const &__power );
- Complex pow ( Complex const &__base, // Complex ** double
- double __power );
- Complex pow ( double __base, // double ** Complex
- Complex const &__power );
- Complex pow ( Complex const &__base, // Complex ** int
- int __power );
- double real ( Complex const & ); // real part
- Complex sin ( Complex const & ); // sin
- Complex sinh ( Complex const & ); // hyperbolic sin
- Complex sqrt ( Complex const & ); // square root
- Complex tan ( Complex const & ); // tan
- Complex tanh ( Complex const & ); // hyperbolic tangent
- #endif
|