FIXED.H 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/FIXED.H 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : FIXED.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 06/19/96 *
  30. * *
  31. * Last Update : June 19, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #ifndef FIXED_H
  37. #define FIXED_H
  38. /*
  39. ** The "bool" integral type was defined by the C++ committee in
  40. ** November of '94. Until the compiler supports this, use the following
  41. ** definition.
  42. */
  43. #ifndef __BORLANDC__
  44. #ifndef TRUE_FALSE_DEFINED
  45. #define TRUE_FALSE_DEFINED
  46. enum {false=0,true=1};
  47. typedef int bool;
  48. #endif
  49. #endif
  50. //#pragma warning 604 9
  51. //#pragma warning 595 9
  52. /*
  53. ** This is a very simple fixed point class that functions like a regular integral type. However
  54. ** it is under certain restrictions. The whole part must not exceed 255. The fractional part is
  55. ** limited to an accuracy of 1/256. It cannot represent or properly handle negative values. It
  56. ** really isn't all that fast (if an FPU is guaranteed to be present than using "float" might be
  57. ** more efficient). It doesn't detect overflow or underflow in mathematical or bit-shift operations.
  58. **
  59. ** Take careful note that the normal mathematical operators return integers and not fixed point
  60. ** values if either of the components is an integer. This is the normal C auto-upcasting rule
  61. ** as it would apply presuming that integers are considered to be of higher precision than
  62. ** fixed point numbers. This allows the result of these operators to generate values with greater
  63. ** magnitude than is normally possible if the result were coerced into a fixed point number.
  64. ** If the result should be fixed point, then ensure that both parameters are fixed point.
  65. **
  66. ** Note that although integers are used as the parameters in the mathematical operators, this
  67. ** does not imply that negative parameters are supported. The use of integers is as a convenience
  68. ** to the programmer -- constant integers are presumed signed. If unsigned parameters were
  69. ** specified, then the compiler would have ambiguous conversion situation in the case of constant
  70. ** integers (e.g. 1, 10, 32, etc). This is most important for the constructor when dealing with the
  71. ** "0" parameter case. In that situation the compiler might interpret the "0" as a null pointer rather
  72. ** than an unsigned integer. There should be no adverse consequences of using signed integer parameters
  73. ** since the precision/magnitude of these integers far exceeds the fixed point component counterparts.
  74. **
  75. ** Note that when integer values are returns from the arithmetic operators, the value is rounded
  76. ** to the nearest whole integer value. This differs from normal integer math that always rounds down.
  77. */
  78. class fixed
  79. {
  80. public:
  81. // The default constructor must not touch the data members in any way.
  82. fixed(void) {}
  83. // Convenient constructor if numerator and denominator components are known.
  84. fixed(int numerator, int denominator);
  85. // Conversion constructor to get fixed point from integer.
  86. fixed(int value) {Data.Composite.Fraction = 0;Data.Composite.Whole = (unsigned char)value;}
  87. // Constructor if ASCII image of number is known.
  88. fixed(char const * ascii);
  89. // Convert to integer when implicitly required.
  90. operator unsigned (void) const {return(((unsigned)Data.Raw+(256/2)) / 256);}
  91. /*
  92. ** The standard operators as they apply to in-place operation.
  93. */
  94. fixed & operator *= (fixed const & rvalue) {Data.Raw = (unsigned short)(((int)Data.Raw * rvalue.Data.Raw) / 256);return(*this);}
  95. fixed & operator *= (int rvalue) {Data.Raw = (unsigned short)(Data.Raw * rvalue);return(*this);}
  96. fixed & operator /= (fixed const & rvalue) {if (rvalue.Data.Raw != 0 && rvalue.Data.Raw != 256) Data.Raw = (unsigned short)(((int)Data.Raw * 256) / rvalue);return(*this);}
  97. fixed & operator /= (int rvalue) {if (rvalue) Data.Raw = (unsigned short)((unsigned)Data.Raw / rvalue);return(*this);}
  98. fixed & operator += (fixed const & rvalue) {Data.Raw += rvalue.Data.Raw;return(*this);}
  99. fixed & operator -= (fixed const & rvalue) {Data.Raw -= rvalue.Data.Raw;return(*this);}
  100. /*
  101. ** The standard "My Dear Aunt Sally" operators. The integer versions of multiply
  102. ** and divide are more efficient than using the fixed point counterparts.
  103. */
  104. // const fixed operator * (fixed const & rvalue) const {return(fixed(*this) *= rvalue);}
  105. const fixed operator * (fixed const & rvalue) const {fixed temp = *this;temp.Data.Raw = (unsigned short)(((int)temp.Data.Raw * (int)rvalue.Data.Raw) / 256);return(temp);}
  106. const int operator * (int rvalue) const {return ((((unsigned)Data.Raw * rvalue) + (256/2)) / 256);}
  107. // const fixed operator / (fixed const & rvalue) const {return(fixed(*this) /= rvalue);}
  108. const fixed operator / (fixed const & rvalue) const {fixed temp = *this;if (rvalue.Data.Raw != 0 && rvalue.Data.Raw != 256) temp.Data.Raw = (unsigned short)(((int)temp.Data.Raw * 256) / rvalue.Data.Raw);return(temp);}
  109. const int operator / (int rvalue) const {if (rvalue) return(((unsigned)Data.Raw+(256/2)) / ((unsigned)rvalue*256));return(*this);}
  110. // const fixed operator + (fixed const & rvalue) const {return(fixed(*this) += rvalue);}
  111. const fixed operator + (fixed const & rvalue) const {fixed temp = *this;temp += rvalue;return(temp);}
  112. const int operator + (int rvalue) const {return((((unsigned)Data.Raw+(256/2))/256) + rvalue);}
  113. // const fixed operator - (fixed const & rvalue) const {return(fixed(*this) -= rvalue);}
  114. const fixed operator - (fixed const & rvalue) const {fixed temp = *this;temp -= rvalue;return(temp);}
  115. const int operator - (int rvalue) const {return((((unsigned)Data.Raw+(256/2))/256) - rvalue);}
  116. /*
  117. ** The Shift operators are more efficient than using multiplies or divides by power-of-2 numbers.
  118. */
  119. fixed & operator >>= (unsigned rvalue) {Data.Raw >>= rvalue;return(*this);}
  120. fixed & operator <<= (unsigned rvalue) {Data.Raw <<= rvalue;return(*this);}
  121. const fixed operator >> (unsigned rvalue) const {fixed temp = *this;temp >>= rvalue;return(temp);}
  122. const fixed operator << (unsigned rvalue) const {fixed temp = *this;temp <<= rvalue;return(temp);}
  123. /*
  124. ** The full set of comparison operators.
  125. */
  126. bool operator == (fixed const & rvalue) const {return(Data.Raw == rvalue.Data.Raw);}
  127. bool operator != (fixed const & rvalue) const {return(Data.Raw != rvalue.Data.Raw);}
  128. bool operator < (fixed const & rvalue) const {return(Data.Raw < rvalue.Data.Raw);}
  129. bool operator > (fixed const & rvalue) const {return(Data.Raw > rvalue.Data.Raw);}
  130. bool operator <= (fixed const & rvalue) const {return(Data.Raw <= rvalue.Data.Raw);}
  131. bool operator >= (fixed const & rvalue) const {return(Data.Raw >= rvalue.Data.Raw);}
  132. bool operator ! (void) const {return(Data.Raw == 0);}
  133. /*
  134. ** Comparison to integers requires consideration of fractional component.
  135. */
  136. bool operator < (int rvalue) const {return(Data.Raw < (rvalue*256));}
  137. bool operator > (int rvalue) const {return(Data.Raw > (rvalue*256));}
  138. bool operator <= (int rvalue) const {return(Data.Raw <= (rvalue*256));}
  139. bool operator >= (int rvalue) const {return(Data.Raw >= (rvalue*256));}
  140. bool operator == (int rvalue) const {return(Data.Raw == (rvalue*256));}
  141. bool operator != (int rvalue) const {return(Data.Raw != (rvalue*256));}
  142. /*
  143. ** Friend functions to handle the alternate positioning of fixed and integer parameters.
  144. */
  145. friend const int operator * (int lvalue, fixed const & rvalue) {return(rvalue * lvalue);}
  146. friend const int operator / (int lvalue, fixed const & rvalue) {if (rvalue.Data.Raw == 0 || rvalue.Data.Raw == 256) return (lvalue); return(((unsigned)(lvalue * 256)+(256/2)) / rvalue.Data.Raw);}
  147. friend const int operator + (int lvalue, fixed const & rvalue) {return(rvalue + lvalue);}
  148. friend const int operator - (int lvalue, fixed const & rvalue) {return((((lvalue*256) - rvalue.Data.Raw) + (256/2)) / 256);}
  149. friend bool operator < (unsigned lvalue, fixed const & rvalue) {return((lvalue*256) < rvalue.Data.Raw);}
  150. friend bool operator > (unsigned lvalue, fixed const & rvalue) {return((lvalue*256) > rvalue.Data.Raw);}
  151. friend bool operator <= (unsigned lvalue, fixed const & rvalue) {return((lvalue*256) <= rvalue.Data.Raw);}
  152. friend bool operator >= (unsigned lvalue, fixed const & rvalue) {return((lvalue*256) >= rvalue.Data.Raw);}
  153. friend bool operator == (unsigned lvalue, fixed const & rvalue) {return((lvalue*256) == rvalue.Data.Raw);}
  154. friend bool operator != (unsigned lvalue, fixed const & rvalue) {return((lvalue*256) != rvalue.Data.Raw);}
  155. friend int operator *= (int & lvalue, fixed const & rvalue) {lvalue = lvalue * rvalue;return(lvalue);}
  156. friend int operator /= (int & lvalue, fixed const & rvalue) {lvalue = lvalue / rvalue;return(lvalue);}
  157. friend int operator += (int & lvalue, fixed const & rvalue) {lvalue = lvalue + rvalue;return(lvalue);}
  158. friend int operator -= (int & lvalue, fixed const & rvalue) {lvalue = lvalue - rvalue;return(lvalue);}
  159. /*
  160. ** Helper functions to handle simple and common operations on fixed point numbers.
  161. */
  162. void Round_Up(void) {Data.Raw += (unsigned short)(256-1);Data.Composite.Fraction = 0;}
  163. void Round_Down(void) {Data.Composite.Fraction = 0;}
  164. void Round(void) {if (Data.Composite.Fraction >= 256/2) Round_Up();Round_Down();}
  165. void Saturate(unsigned capvalue) {if (Data.Raw > (capvalue*256)) Data.Raw = (unsigned short)(capvalue*256);}
  166. void Saturate(fixed const & capvalue) {if (*this > capvalue) *this = capvalue;}
  167. void Sub_Saturate(unsigned capvalue) {if (Data.Raw >= (capvalue*256)) Data.Raw = (unsigned short)((capvalue*256)-1);}
  168. void Sub_Saturate(fixed const & capvalue) {if (*this >= capvalue) Data.Raw = (unsigned short)(capvalue.Data.Raw-1);}
  169. void Inverse(void) {*this = fixed(1) / *this;}
  170. /*
  171. ** Friend helper functions that work in the typical C fashion of passing the object to
  172. ** be processed as a parameter to the function.
  173. */
  174. friend const fixed Round_Up(fixed const & value) {fixed temp = value; temp.Round_Up();return(temp);}
  175. friend const fixed Round_Down(fixed const & value) {fixed temp = value; temp.Round_Down();return(temp);}
  176. friend const fixed Round(fixed const & value) {fixed temp = value; temp.Round();return(temp);}
  177. friend const fixed Saturate(fixed const & value, unsigned capvalue) {fixed temp = value;temp.Saturate(capvalue);return(temp);}
  178. friend const fixed Saturate(fixed const & value, fixed const & capvalue) {fixed temp = value;temp.Saturate(capvalue);return(temp);}
  179. friend const fixed Sub_Saturate(fixed const & value, unsigned capvalue) {fixed temp = value;temp.Sub_Saturate(capvalue);return(temp);}
  180. friend const fixed Sub_Saturate(fixed const & value, fixed const & capvalue) {fixed temp = value;temp.Sub_Saturate(capvalue);return(temp);}
  181. friend const fixed Inverse(fixed const & value) {fixed temp = value;temp.Inverse();return(temp);}
  182. /*
  183. ** Conversion of the fixed point number into an ASCII string.
  184. */
  185. int To_ASCII(char * buffer, int maxlen=-1) const;
  186. char const * As_ASCII(void) const;
  187. /*
  188. ** Helper constants that provide some convenient fixed point values.
  189. */
  190. static const fixed _1_2;
  191. static const fixed _1_3;
  192. static const fixed _1_4;
  193. static const fixed _3_4;
  194. static const fixed _2_3;
  195. private:
  196. union {
  197. struct {
  198. #ifdef BIG_ENDIAN
  199. unsigned char Whole;
  200. unsigned char Fraction;
  201. #else
  202. unsigned char Fraction;
  203. unsigned char Whole;
  204. #endif
  205. } Composite;
  206. unsigned short Raw;
  207. } Data;
  208. };
  209. #endif