Fixed.h
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:7k
源码类别:
DirextX编程
开发平台:
Visual C++
- // Based on SciTech MGL Public License Version 1.0. "High Speed Fixed/Floating Point Library".
- // The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
- // The Initial Developer of the Original Code is SciTech Software, Inc.
- // Copyright (C) 1999 DXGuide. All Rights Reserved.
- // File: Fixed.h
- #ifndef _FIXED__H
- #define _FIXED__H
- #if _MSC_VER >= 1000
- #pragma once
- #endif // _MSC_VER >= 1000
- //#define _GENFIXEDTABLE
- //#define _TESTFIXED
- #define _USE_386ASM
- #define _FASTCALL
- typedef long Fixed; // 16.16 format
- extern "C"
- {
- #ifdef _FASTCALL
- Fixed __fastcall FXMul(Fixed fA, Fixed fB);
- Fixed __fastcall FXSquare(Fixed f);
- Fixed __fastcall FXMulDiv(Fixed fA, Fixed fB, Fixed fC);
- Fixed __fastcall FXDiv(Fixed fA, Fixed fB);
- Fixed __fastcall FXOneOver(Fixed f);
- Fixed __fastcall FXSqrt(Fixed f);
- Fixed __fastcall FXSin(Fixed fAngle);
- Fixed __fastcall FXCos(Fixed fAngle);
- Fixed __fastcall FXTan(Fixed fAngle);
- Fixed __fastcall FXLog10(Fixed f);
- Fixed __fastcall FXLog(Fixed f);
- #else // !_FASTCALL
- Fixed __cdecl FXMul(Fixed fA, Fixed fB);
- Fixed __cdecl FXSquare(Fixed f);
- Fixed __cdecl FXMulDiv(Fixed fA, Fixed fB, Fixed fC);
- Fixed __cdecl FXDiv(Fixed fA, Fixed fB);
- Fixed __cdecl FXOneOver(Fixed f);
- Fixed __cdecl FXSqrt(Fixed f);
- Fixed __cdecl FXSin(Fixed fAngle);
- Fixed __cdecl FXCos(Fixed fAngle);
- Fixed __cdecl FXTan(Fixed fAngle);
- Fixed __cdecl FXLog10(Fixed f);
- Fixed __cdecl FXLog(Fixed f);
- #endif // _FASTCALL
- };
- #ifndef _USE_386ASM
- #define FXSquare(f) FXMul(f, f)
- #define FXMulDiv(fA, fB, fC) FXMul(fA, FXDiv(fB, fC))
- #define FXOneOver(f) FXDiv(FXDbl2Real(1), f)
- #define FXCos(fAngle) FXSin(FXDbl2Real(90) + fAngle)
- #define FXLog(f) FXMul(FXLog10(f), FXDbl2Real(2.302585093))
- #endif // _USE_386ASM
- // The ceil(), floor() and round() operations can be quickly implemented using fast adds and masks.
- #define FXCeil(f) ((Fixed)(((f) + 0xFFFFL) & 0xFFFF0000L))
- #define FXFloor(f) ((Fixed)((f) & 0xFFFF0000L))
- #define FXRound(f) ((Fixed)(((f) + 0x8000L) & 0xFFFF0000L))
- // Conversion operations.
- #define FXInt2Real(i) ((Fixed)(i) << 16)
- #define FXDbl2Real(d) ((Fixed)((d) * 65536.0 + 0.5))
- #define FXRnd2Int(f) ((int)(((f) + 0x8000L) >> 16))
- #define FXReal2Dbl(f) ((double)((f) / 65536.0))
- // The following defines a C++ wrapper class for 16.16 fixed
- // point routines. Using this class is convenient (can be
- // used just like any normal C++ data type)
- #include <iostream.h>
- class CFixed
- {
- public:
- CFixed()
- {
- };
- // Constructors for the fixed point data type
- CFixed(double d)
- {
- m_fixed = FXDbl2Real(d);
- };
- CFixed(int i)
- {
- m_fixed = FXInt2Real(i);
- };
- CFixed(Fixed l)
- {
- m_fixed = l;
- };
- public:
- // Standard arithmetic operators for CFixed
- friend CFixed operator+(const CFixed& f, const CFixed& g)
- {
- return CFixed(f.m_fixed + g.m_fixed);
- }
- friend CFixed operator+(const CFixed& f, int i)
- {
- return CFixed(f.m_fixed + FXInt2Real(i));
- }
- friend CFixed operator+(int i, const CFixed& f)
- {
- return CFixed(FXInt2Real(i) + f.m_fixed);
- }
- friend CFixed operator-(const CFixed& f, const CFixed& g)
- {
- return CFixed(f.m_fixed - g.m_fixed);
- }
- friend CFixed operator-(const CFixed& f, int i)
- {
- return CFixed(f.m_fixed - FXInt2Real(i));
- }
- friend CFixed operator-(int i, const CFixed& f)
- {
- return CFixed(FXInt2Real(i) - f.m_fixed);
- }
- friend CFixed operator*(const CFixed& f, const CFixed& g)
- {
- return CFixed(FXMul(f.m_fixed, g.m_fixed));
- }
- friend CFixed operator*(const CFixed& f, int i)
- {
- return CFixed(f.m_fixed * i);
- }
- friend CFixed operator*(int i, const CFixed& f)
- {
- return CFixed(f.m_fixed * i);
- }
- friend CFixed operator/(const CFixed& f, const CFixed& g)
- {
- return CFixed(FXDiv(f.m_fixed, g.m_fixed));
- }
- friend CFixed operator/(const CFixed& f, int i)
- {
- return CFixed(FXDiv(f.m_fixed, FXInt2Real(i)));
- }
- friend CFixed operator/(int i, const CFixed& f)
- {
- return CFixed(FXDiv(FXInt2Real(i), f));
- }
- // Fixed point manipulation routines
- friend CFixed ceil(const CFixed& f)
- {
- return CFixed(FXCeil(f.m_fixed));
- };
- friend CFixed floor(const CFixed& f)
- {
- return CFixed(FXFloor(f.m_fixed));
- };
- friend CFixed round(const CFixed& f)
- {
- return CFixed(FXRound(f.m_fixed));
- };
- // Routines to compute a fixed point trig functions, with all angles in degrees
- friend CFixed sin(const CFixed& a)
- {
- return CFixed(FXSin(a.m_fixed));
- };
- friend CFixed cos(const CFixed& a)
- {
- return CFixed(FXCos(a.m_fixed));
- };
- friend CFixed tan(const CFixed& a)
- {
- return CFixed(FXTan(a.m_fixed));
- };
- // Routine to compute the square root for fixed point
- friend CFixed sqrt(const CFixed& f)
- {
- return CFixed(FXSqrt(f));
- };
- // Misc routines
- friend CFixed log(const CFixed& f)
- {
- return CFixed(FXLog(f.m_fixed));
- }
- friend CFixed log10(const CFixed& f)
- {
- return CFixed(FXLog10(f.m_fixed));
- }
- // Friend function to dump a fixed point value to a stream
- friend ostream& operator<<(ostream& o, const CFixed& f)
- {
- return o << double(f);
- }
- public:
- CFixed& operator+=(const CFixed& f)
- {
- m_fixed += f.m_fixed;
- return *this;
- }
- CFixed& operator+=(int i)
- {
- m_fixed += FXInt2Real(i);
- return *this;
- }
- CFixed& operator-=(const CFixed& f)
- {
- m_fixed -= f.m_fixed;
- return *this;
- }
- CFixed& operator-=(int i)
- {
- m_fixed -= FXInt2Real(i);
- return *this;
- }
- CFixed& operator*=(const CFixed& f)
- {
- m_fixed = FXMul(m_fixed, f.m_fixed);
- return *this;
- }
- CFixed& operator*=(int i)
- {
- m_fixed *= i;
- return *this;
- }
- CFixed& operator/=(const CFixed& f)
- {
- m_fixed = FXDiv(m_fixed, f.m_fixed);
- return *this;
- }
- CFixed& operator/=(int i)
- {
- m_fixed = FXDiv(m_fixed, FXInt2Real(i));
- return *this;
- }
- // Unary arithmetic operators for CFixed point
- CFixed operator-() const
- {
- return CFixed(-m_fixed);
- };
- // Relational operators for CFixed point
- int operator<(const CFixed& f) const
- {
- return m_fixed < f.m_fixed;
- };
- int operator<=(const CFixed& f) const
- {
- return m_fixed <= f.m_fixed;
- };
- int operator==(const CFixed& f) const
- {
- return m_fixed == f.m_fixed;
- };
- int operator!=(const CFixed& f) const
- {
- return m_fixed != f.m_fixed;
- };
- int operator>=(const CFixed& f) const
- {
- return m_fixed >= f.m_fixed;
- };
- int operator>(const CFixed& f) const
- {
- return m_fixed > f.m_fixed;
- };
- // Conversion routines
- operator int() const
- {
- return (int)(m_fixed >> 16);
- };
- operator Fixed() const
- {
- return m_fixed;
- };
- operator float() const
- {
- return (float)(m_fixed / 65536.0);
- };
- operator double() const
- {
- return FXReal2Dbl(m_fixed);
- };
- protected:
- Fixed m_fixed;
- };
- #endif // _FIXED__H