uLib  User mode C/C++ extended API library for Win32 programmers.
Fixed.hpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // Project: uLib
3 // Module: Basic support for the FIXED type used in GDI.
4 // Author: Copyright (c) Love Nystrom
5 // License: NNOSL (BSD decendant)
6 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
20 
21 #ifdef __cplusplus
22 #ifndef __Fixed_hpp_incl__
23 #define __Fixed_hpp_incl__
24 
25 #include <uLib/Common.h>
26 
29 
30 struct Fixed : FIXED
31 {
32  // WORD fract;
33  // short value;
34 
36  #define _LT LITERAL( long, *this ) // Long (this)
37  #define _LL(x) LITERAL( long, x ) // Literal long
38  #define _F1 (1 << 16) // Fixed integer 1
39 
41  Fixed( void ) { _LT = 0; }
42  Fixed( const float val ) { *this = val; } // Call opr = (float)
43  Fixed( const Fixed& val ) { *this = val; } // Call opr = (Fixed)
44  //Fixed( const short val ) { value = val; fract = 0; } // Annoying ambiguity..
45 
46  operator float() const { return float(_LT) / _F1; } // Note: Limited precision!
47 
48  Fixed& operator = ( const short val ) { value = val; fract = 0; return *this; }
49  Fixed& operator = ( const Fixed& val ) { _LT = _LL( val ); return *this; }
50  Fixed& operator = ( const FIXED& val ) { _LT = _LL( val ); return *this; }
51  Fixed& operator = ( const float val ) { _LT = long( val * _F1 ); return *this; }
52 
53  Fixed& operator += ( const short rhs ) { value += rhs; return *this; }
54  Fixed& operator -= ( const short rhs ) { value -= rhs; return *this; }
55  Fixed& operator *= ( const short rhs ) { _LT *= rhs; return *this; }
56  Fixed& operator /= ( const short rhs ) { _LT /= rhs; return *this; }
57  Fixed& operator %= ( const short rhs ) { _LT %= long(rhs) << 16; return *this; }
58 
59  Fixed& operator += ( const Fixed& rhs ) { _LT += _LL( rhs ); return *this; }
60  Fixed& operator -= ( const Fixed& rhs ) { _LT -= _LL( rhs ); return *this; }
61  Fixed& operator *= ( const Fixed& rhs ) { _LT = long( (int64(_LT) * _LL( rhs )) >> 16 ); return *this; }
62  Fixed& operator /= ( const Fixed& rhs ) { _LT = long( (int64(_LT) << 16) / _LL( rhs )); return *this; }
63  Fixed& operator %= ( const Fixed& rhs ) { _LT %= _LL( rhs ); return *this; }
64 
65  Fixed operator + ( const Fixed& rhs ) const { Fixed ret = *this; ret += rhs; return ret; }
66  Fixed operator - ( const Fixed& rhs ) const { Fixed ret = *this; ret -= rhs; return ret; }
67  Fixed operator * ( const Fixed& rhs ) const { Fixed ret = *this; ret *= rhs; return ret; }
68  Fixed operator / ( const Fixed& rhs ) const { Fixed ret = *this; ret /= rhs; return ret; }
69  Fixed operator % ( const Fixed& rhs ) const { Fixed ret = *this; ret %= rhs; return ret; }
70 
71  Fixed operator + ( const short rhs ) const { Fixed ret = *this; ret += rhs; return ret; }
72  Fixed operator - ( const short rhs ) const { Fixed ret = *this; ret -= rhs; return ret; }
73  Fixed operator * ( const short rhs ) const { Fixed ret = *this; ret *= rhs; return ret; }
74  Fixed operator / ( const short rhs ) const { Fixed ret = *this; ret /= rhs; return ret; }
75  Fixed operator % ( const short rhs ) const { Fixed ret = *this; ret %= rhs; return ret; }
76 
78  #undef _F1
79  #undef _LL
80  #undef _LT
81 };
83 
85 #endif//ndef __Fixed_hpp_incl__
86 #endif//def __cplusplus
87 // EOF
Fixed & operator/=(const short rhs)
Definition: Fixed.hpp:56
Fixed & operator=(const short val)
Definition: Fixed.hpp:48
Fixed operator %(const Fixed &rhs) const
Definition: Fixed.hpp:69
Fixed & operator *=(const short rhs)
Definition: Fixed.hpp:55
Fixed & operator+=(const short rhs)
Definition: Fixed.hpp:53
signed __int64 int64
Definition: uLib/Types.h:50
Fixed operator -(const Fixed &rhs) const
Definition: Fixed.hpp:66
Fixed operator+(const Fixed &rhs) const
Definition: Fixed.hpp:65
Fixed(void)
Definition: Fixed.hpp:41
Fixed(const float val)
Definition: Fixed.hpp:42
Fixed & operator %=(const short rhs)
Definition: Fixed.hpp:57
Fixed operator *(const Fixed &rhs) const
Definition: Fixed.hpp:67
Common include; Added types, small "ubiquitous" utilities, et c.
Fixed operator/(const Fixed &rhs) const
Definition: Fixed.hpp:68
Definition: Fixed.hpp:30
Fixed(const Fixed &val)
Definition: Fixed.hpp:43
Fixed & operator -=(const short rhs)
Definition: Fixed.hpp:54