home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 13.0 KB | 478 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWFxMath.h
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWFXMATH_H
- #define FWFXMATH_H
-
- #ifndef FWENVDEF_H
- #include "FWEnvDef.h"
- #endif
-
- #ifndef FWSTDDEF_H
- #include "FWStdDef.h"
- #endif
-
- #ifndef _ODMATH_
- #include <ODMath.h>
- #endif
-
- //========================================================================================
- // Definitions and utilities
-
- // These have to be done as macros because Metrowerks C++ 1.2.2 has difficulties
- // with inlines that call other inlines
-
- #define FW_PrivIntToFixed(i) (ODFixed)((long) (i) << 16)
- #define FW_PrivDoubleToFixed(d) (ODFixed)((FW_Double) (d) * 65536.0)
-
- #define FW_PrivFixedToInt(f) (int)((ODFixed)((f) + 0x00008000) >> 16)
- #define FW_PrivFixedToTruncatedInt(f) (int)((ODFixed)((f) + 0x00000010) >> 16)
-
- #define FW_PrivFixedToDouble(f) (FW_Double)((ODFixed)(f) / 65536.0)
-
- //========================================================================================
- // SOM fixed math types
- //========================================================================================
-
- struct FW_Fixed
- {
- ODFixed fRep;
- };
-
- struct FW_Wide
- {
- ODWide fRep;
- };
-
- //========================================================================================
- // Forward class declaration
- //========================================================================================
-
- class FW_CWritableStream;
- class FW_CReadableStream;
-
- //========================================================================================
- // Operators
- //========================================================================================
-
- // ----- Stream I/O operators -----
- // No operators for wide numbers: those are intended to be used only as temporaries
-
- FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_Fixed& fx);
- FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_Fixed& fx);
-
- // ----- Operators: FW_Fixed -----
-
- FW_Boolean operator == (FW_Fixed f1, FW_Fixed f2);
- FW_Boolean operator != (FW_Fixed f1, FW_Fixed f2);
-
- FW_Boolean operator > (FW_Fixed f1, FW_Fixed f2);
- FW_Boolean operator < (FW_Fixed f1, FW_Fixed f2);
- FW_Boolean operator >= (FW_Fixed f1, FW_Fixed f2);
- FW_Boolean operator <= (FW_Fixed f1, FW_Fixed f2);
-
- FW_Fixed operator + (FW_Fixed f1, FW_Fixed f2);
- FW_Fixed operator - (FW_Fixed f1, FW_Fixed f2);
- FW_Fixed operator * (FW_Fixed f1, FW_Fixed f2);
- FW_Fixed operator / (FW_Fixed f1, FW_Fixed f2);
- FW_Fixed operator % (FW_Fixed f1, FW_Fixed f2);
-
- FW_Fixed& operator += (FW_Fixed& f1, FW_Fixed f2);
- FW_Fixed& operator -= (FW_Fixed& f1, FW_Fixed f2);
- FW_Fixed& operator *= (FW_Fixed& f1, FW_Fixed f2);
- FW_Fixed& operator /= (FW_Fixed& f1, FW_Fixed f2);
- FW_Fixed& operator %= (FW_Fixed& f1, FW_Fixed f2);
-
- FW_Fixed operator - (FW_Fixed f);
-
- FW_Fixed& operator ++ (FW_Fixed& f); // prefix
- void operator ++ (FW_Fixed& f, int i); // postfix
- FW_Fixed& operator -- (FW_Fixed& f); // prefix
- void operator -- (FW_Fixed& f, int i); // postfix
-
- // ----- Special cases -----
-
- FW_Fixed FW_Half(FW_Fixed f);
- FW_Fixed FW_MultipliedByInt(FW_Fixed f, short i);
- FW_Fixed FW_DividedByInt(FW_Fixed f, int i);
-
- FW_Fixed FW_RoundedToInt(FW_Fixed f);
- FW_Fixed FW_TruncatedToInt(FW_Fixed f);
-
- // ----- Transcendental functions -----
-
- FW_Fixed FW_Sin(FW_Fixed f);
- FW_Fixed FW_Cos(FW_Fixed f);
- FW_Fixed FW_Sqrt(FW_Fixed f);
-
- // ----- Conversions -----
-
- #define FW_FixedToInt(f) FW_PrivFixedToInt((f).fRep)
- #define FW_FixedToTruncatedInt(f) FW_PrivFixedToTruncatedInt((f).fRep)
- #define FW_FixedToDouble(f) FW_PrivFixedToDouble((f).fRep)
- #define FW_FixedToODFixed(f) ((f).fRep)
-
- FW_Fixed FW_ODFixedToFixed(ODFixed f);
- FW_Fixed FW_IntToFixed(int i);
- FW_Fixed FW_DoubleToFixed(FW_Double d);
-
- //========================================================================================
- // Conversions
- //========================================================================================
-
- inline FW_Fixed FW_ODFixedToFixed(ODFixed f)
- {
- FW_Fixed fx;
- fx.fRep = f;
- return fx;
- }
-
- inline FW_Fixed FW_IntToFixed(int i)
- {
- return FW_ODFixedToFixed(FW_PrivIntToFixed(i));
- }
-
- inline FW_Fixed FW_DoubleToFixed(FW_Double d)
- {
- return FW_ODFixedToFixed(FW_PrivDoubleToFixed(d));
- }
-
- //========================================================================================
- // Special math cases
- //========================================================================================
-
- inline FW_Fixed FW_Half(FW_Fixed f)
- {
- return FW_ODFixedToFixed(f.fRep / 2);
- }
-
- inline FW_Fixed FW_MultipliedByInt(FW_Fixed f, short i)
- {
- return FW_ODFixedToFixed(f.fRep * i);
- }
-
- inline FW_Fixed FW_DividedByInt(FW_Fixed f, int i)
- {
- return FW_ODFixedToFixed(f.fRep / i);
- }
-
- inline FW_Fixed FW_RoundedToInt(FW_Fixed f)
- {
- return FW_ODFixedToFixed((f.fRep + 0x00008000) & 0xFFFF0000ul);
- }
-
- inline FW_Fixed FW_TruncatedToInt(FW_Fixed f)
- {
- return FW_ODFixedToFixed(f.fRep & 0xFFFF0000ul);
- }
-
- //========================================================================================
- // Increment/decrement
- //========================================================================================
-
- inline FW_Fixed& operator++(FW_Fixed& f)
- {
- f.fRep += FW_PrivIntToFixed(1);
- return f;
- }
-
- inline void operator++(FW_Fixed& f, int /* i */)
- {
- f.fRep += FW_PrivIntToFixed(1);
- }
-
- inline FW_Fixed& operator--(FW_Fixed& f)
- {
- f.fRep -= FW_PrivIntToFixed(1);
- return f;
- }
-
- inline void operator--(FW_Fixed& f, int /* i */)
- {
- f.fRep -= FW_PrivIntToFixed(1);
- }
-
- //========================================================================================
- // Math
- //========================================================================================
-
- inline FW_Fixed operator-(FW_Fixed f)
- {
- FW_Fixed r;
- r.fRep = -f.fRep;
- return r;
- }
-
- #ifndef FW_DEBUG
- // These inlines are not used during debugging. When FW_DEBUG, a set of functions which
- // checks for overflow is used instead.
-
- inline FW_Fixed operator+(FW_Fixed f1, FW_Fixed f2)
- {
- return FW_ODFixedToFixed(f1.fRep + f2.fRep);
- }
-
- inline FW_Fixed operator-(FW_Fixed f1, FW_Fixed f2)
- {
- return FW_ODFixedToFixed(f1.fRep - f2.fRep);
- }
-
- inline FW_Fixed operator*(FW_Fixed f1, FW_Fixed f2)
- {
- return FW_ODFixedToFixed(ODFixedMultiply(f1.fRep, f2.fRep));
- }
-
- inline FW_Fixed operator/(FW_Fixed f1, FW_Fixed f2)
- {
- return FW_ODFixedToFixed(ODFixedDivide(f1.fRep, f2.fRep));
- }
-
- inline FW_Fixed operator%(FW_Fixed f1, FW_Fixed f2)
- {
- return f1 - FW_TruncatedToInt(f1 / f2) * f2;
- }
-
- #endif
-
- //========================================================================================
- // Math with assignment
- //========================================================================================
-
- inline FW_Fixed& operator += (FW_Fixed& f1, FW_Fixed f2)
- {
- f1.fRep += f2.fRep;
- return f1;
- }
-
- inline FW_Fixed& operator -= (FW_Fixed& f1, FW_Fixed f2)
- {
- f1.fRep -= f2.fRep;
- return f1;
- }
-
- inline FW_Fixed& operator *= (FW_Fixed& f1, FW_Fixed f2)
- {
- f1.fRep = ODFixedMultiply(f1.fRep, f2.fRep);
- return f1;
- }
-
- inline FW_Fixed& operator /= (FW_Fixed& f1, FW_Fixed f2)
- {
- f1.fRep = ODFixedDivide(f1.fRep, f2.fRep);
- return f1;
- }
-
- inline FW_Fixed& operator %= (FW_Fixed& f1, FW_Fixed f2)
- {
- f1 = f1 - FW_TruncatedToInt(f1 / f2) * f2;
- return f1;
- }
-
- //========================================================================================
- // Comparisons
- //========================================================================================
-
- inline FW_Boolean operator>(FW_Fixed f1, FW_Fixed f2)
- {
- return f1.fRep > f2.fRep;
- }
-
- inline FW_Boolean operator==(FW_Fixed f1, FW_Fixed f2)
- {
- return f1.fRep == f2.fRep;
- }
-
- inline FW_Boolean operator<(FW_Fixed f1, FW_Fixed f2)
- {
- return f1.fRep < f2.fRep;
- }
-
- inline FW_Boolean operator>=(FW_Fixed f1, FW_Fixed f2)
- {
- return f1.fRep >= f2.fRep;
- }
-
- inline FW_Boolean operator!=(FW_Fixed f1, FW_Fixed f2)
- {
- return f1.fRep != f2.fRep;
- }
-
- inline FW_Boolean operator<=(FW_Fixed f1, FW_Fixed f2)
- {
- return f1.fRep <= f2.fRep;
- }
-
- //========================================================================================
- // FW_Wide
- //========================================================================================
-
- // ----- Conversions -----
-
- FW_Wide FW_IntToWide(int i);
- FW_Wide FW_FixedToWide(FW_Fixed f);
- FW_Wide FW_ODFixedToWide(ODFixed f);
- FW_Wide FW_ODWideToWide(const ODWide& w);
-
- int FW_WideToInt(const FW_Wide& w);
- ODFixed FW_WideToODFixed(const FW_Wide& w);
- inline FW_Fixed FW_WideToFixed(const FW_Wide& w);
-
- // ----- Comparisons -----
-
- FW_Boolean operator == (const FW_Wide& w1, const FW_Wide& w2);
- FW_Boolean operator != (const FW_Wide& w1, const FW_Wide& w2);
-
- FW_Boolean operator > (const FW_Wide& w1, const FW_Wide& w2);
- FW_Boolean operator < (const FW_Wide& w1, const FW_Wide& w2);
- FW_Boolean operator >= (const FW_Wide& w1, const FW_Wide& w2);
- FW_Boolean operator <= (const FW_Wide& w1, const FW_Wide& w2);
-
- // ----- Math -----
-
- FW_Wide operator + (const FW_Wide& w1, const FW_Wide& w2);
- FW_Wide operator - (const FW_Wide& w1, const FW_Wide& w2);
-
- FW_Wide& operator += (FW_Wide& w1, const FW_Wide& w2);
- FW_Wide& operator -= (FW_Wide& w1, const FW_Wide& w2);
-
- #ifdef FW_DEBUG
- FW_Wide& operator += (FW_Wide& w1, const FW_Fixed& f2);
- FW_Wide& operator -= (FW_Wide& w1, const FW_Fixed& f2);
- #endif
-
- FW_Wide FW_WideMultiply (FW_Fixed f1, FW_Fixed f2);
- FW_Fixed operator / (const FW_Wide& w1, FW_Fixed f2);
-
- FW_Fixed FW_Sqrt(const FW_Wide& w);
-
- //========================================================================================
- // Conversions
- //========================================================================================
-
- inline FW_Wide FW_ODWideToWide(const ODWide& w)
- {
- FW_Wide sw;
- sw.fRep = w;
- return sw;
- }
-
- //========================================================================================
- // Math
- //========================================================================================
-
- inline FW_Wide& operator += (FW_Wide& w1, const FW_Wide& w2)
- {
- ODWideAdd(&w1.fRep, &w2.fRep);
- return w1;
- }
-
- inline FW_Wide& operator -= (FW_Wide& w1, const FW_Wide& w2)
- {
- ODWideSubtract(&w1.fRep, &w2.fRep);
- return w1;
- }
-
- //========================================================================================
- // Comparison
- //========================================================================================
-
- inline FW_Boolean operator == (const FW_Wide& w1, const FW_Wide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) == 0;
- }
-
- inline FW_Boolean operator != (const FW_Wide& w1, const FW_Wide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) != 0;
- }
-
- inline FW_Boolean operator > (const FW_Wide& w1, const FW_Wide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) > 0;
- }
-
- inline FW_Boolean operator < (const FW_Wide& w1, const FW_Wide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) < 0;
- }
-
- inline FW_Boolean operator >= (const FW_Wide& w1, const FW_Wide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) >= 0;
- }
-
- inline FW_Boolean operator <= (const FW_Wide& w1, const FW_Wide& w2)
- {
- return ODWideCompare(&w1.fRep, &w2.fRep) <= 0;
- }
-
- //========================================================================================
- // Min/Max/Abs utilities
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_Fixed
- //----------------------------------------------------------------------------------------
-
- inline FW_Fixed FW_Minimum(FW_Fixed f1, FW_Fixed f2)
- {
- return f1.fRep < f2.fRep ? f1 : f2;
- }
-
- inline FW_Fixed FW_Maximum(FW_Fixed f1, FW_Fixed f2)
- {
- return f1.fRep > f2.fRep ? f1 : f2;
- }
-
- inline FW_Fixed FW_Absolute(FW_Fixed f)
- {
- return FW_ODFixedToFixed(f.fRep > 0 ? f.fRep : -f.fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_Wide
- //----------------------------------------------------------------------------------------
-
- inline FW_Wide FW_Minimum(const FW_Wide& w1, const FW_Wide& w2)
- {
- return w1 < w2 ? w1 : w2;
- }
-
- inline FW_Wide FW_Maximum(const FW_Wide& w1, const FW_Wide& w2)
- {
- return w1 > w2 ? w1 : w2;
- }
-
- inline FW_Fixed FW_WideToFixed(const FW_Wide& w1)
- {
- return FW_ODFixedToFixed(FW_WideToODFixed(w1));
- }
-
- //========================================================================================
- // Global constants
- //========================================================================================
-
- extern const FW_Fixed FW_kFixed0; // 0
- extern const FW_Fixed FW_kFixedPos1; // +1
- extern const FW_Fixed FW_kFixedNeg1; // -1
- extern const FW_Fixed FW_kFixedPos2; // +2
- extern const FW_Fixed FW_kFixedNeg2; // -2
- extern const FW_Fixed FW_kFixed72; // 72
- extern const FW_Fixed FW_kFixedPI; // pi
-
- extern const FW_Wide FW_kWide0; // 0
- extern const FW_Wide FW_kWidePos1; // +1
- extern const FW_Wide FW_kWideNeg1; // -1
- extern const FW_Wide FW_kWidePos2; // +2
- extern const FW_Wide FW_kWideNeg2; // -2
- extern const FW_Wide FW_kWide72; // 72
- extern const FW_Wide FW_kWidePI; // pi
-
- #endif
-