home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWODMisc / FWFxMath.h < prev    next >
Encoding:
Text File  |  1996-09-17  |  13.0 KB  |  478 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFxMath.h
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWFXMATH_H
  11. #define FWFXMATH_H
  12.  
  13. #ifndef FWENVDEF_H
  14. #include "FWEnvDef.h"
  15. #endif
  16.  
  17. #ifndef FWSTDDEF_H
  18. #include "FWStdDef.h"
  19. #endif
  20.  
  21. #ifndef _ODMATH_
  22. #include <ODMath.h>
  23. #endif
  24.  
  25. //========================================================================================
  26. // Definitions and utilities
  27.  
  28. // These have to be done as macros because Metrowerks C++ 1.2.2 has difficulties
  29. //    with inlines that call other inlines
  30.  
  31. #define FW_PrivIntToFixed(i)            (ODFixed)((long) (i) << 16)
  32. #define FW_PrivDoubleToFixed(d)            (ODFixed)((FW_Double) (d) * 65536.0)
  33.  
  34. #define FW_PrivFixedToInt(f)            (int)((ODFixed)((f) + 0x00008000) >> 16)
  35. #define FW_PrivFixedToTruncatedInt(f)    (int)((ODFixed)((f) + 0x00000010) >> 16)
  36.  
  37. #define FW_PrivFixedToDouble(f)            (FW_Double)((ODFixed)(f) / 65536.0)
  38.  
  39. //========================================================================================
  40. // SOM fixed math types
  41. //========================================================================================
  42.  
  43. struct FW_Fixed
  44. {
  45.     ODFixed    fRep;
  46. };
  47.  
  48. struct FW_Wide
  49. {
  50.     ODWide    fRep;
  51. };
  52.  
  53. //========================================================================================
  54. // Forward class declaration
  55. //========================================================================================
  56.  
  57. class FW_CWritableStream;
  58. class FW_CReadableStream;
  59.  
  60. //========================================================================================
  61. //    Operators
  62. //========================================================================================
  63.  
  64. // ----- Stream I/O operators -----
  65. // No operators for wide numbers: those are intended to be used only as temporaries
  66.  
  67. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_Fixed& fx);
  68. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_Fixed& fx);
  69.  
  70. // ----- Operators: FW_Fixed -----
  71.  
  72. FW_Boolean            operator ==    (FW_Fixed f1, FW_Fixed f2);
  73. FW_Boolean            operator !=    (FW_Fixed f1, FW_Fixed f2);
  74.  
  75. FW_Boolean            operator >    (FW_Fixed f1, FW_Fixed f2);
  76. FW_Boolean            operator <    (FW_Fixed f1, FW_Fixed f2);
  77. FW_Boolean            operator >=    (FW_Fixed f1, FW_Fixed f2);
  78. FW_Boolean            operator <=    (FW_Fixed f1, FW_Fixed f2);
  79.  
  80. FW_Fixed            operator +    (FW_Fixed f1, FW_Fixed f2);
  81. FW_Fixed            operator -    (FW_Fixed f1, FW_Fixed f2);
  82. FW_Fixed            operator *    (FW_Fixed f1, FW_Fixed f2);
  83. FW_Fixed            operator /    (FW_Fixed f1, FW_Fixed f2);
  84. FW_Fixed            operator %    (FW_Fixed f1, FW_Fixed f2);
  85.  
  86. FW_Fixed&            operator +=    (FW_Fixed& f1, FW_Fixed f2);
  87. FW_Fixed&            operator -=    (FW_Fixed& f1, FW_Fixed f2);
  88. FW_Fixed&            operator *=    (FW_Fixed& f1, FW_Fixed f2);
  89. FW_Fixed&            operator /=    (FW_Fixed& f1, FW_Fixed f2);
  90. FW_Fixed&            operator %=    (FW_Fixed& f1, FW_Fixed f2);
  91.  
  92. FW_Fixed            operator -  (FW_Fixed f);
  93.  
  94. FW_Fixed&            operator ++    (FW_Fixed& f);                // prefix
  95. void                operator ++    (FW_Fixed& f, int i);        // postfix
  96. FW_Fixed&            operator --    (FW_Fixed& f);                // prefix
  97. void                 operator --    (FW_Fixed& f, int i);        // postfix
  98.  
  99. // ----- Special cases -----
  100.  
  101. FW_Fixed            FW_Half(FW_Fixed f);
  102. FW_Fixed            FW_MultipliedByInt(FW_Fixed f, short i);
  103. FW_Fixed            FW_DividedByInt(FW_Fixed f, int i);
  104.  
  105. FW_Fixed            FW_RoundedToInt(FW_Fixed f);
  106. FW_Fixed            FW_TruncatedToInt(FW_Fixed f);
  107.  
  108. // ----- Transcendental functions -----
  109.  
  110. FW_Fixed            FW_Sin(FW_Fixed f);
  111. FW_Fixed            FW_Cos(FW_Fixed f);
  112. FW_Fixed            FW_Sqrt(FW_Fixed f);
  113.  
  114. // ----- Conversions -----
  115.  
  116. #define FW_FixedToInt(f)            FW_PrivFixedToInt((f).fRep)
  117. #define FW_FixedToTruncatedInt(f)    FW_PrivFixedToTruncatedInt((f).fRep)
  118. #define FW_FixedToDouble(f)            FW_PrivFixedToDouble((f).fRep)
  119. #define FW_FixedToODFixed(f)        ((f).fRep)
  120.  
  121. FW_Fixed            FW_ODFixedToFixed(ODFixed f);
  122. FW_Fixed            FW_IntToFixed(int i);
  123. FW_Fixed            FW_DoubleToFixed(FW_Double d);
  124.  
  125. //========================================================================================
  126. // Conversions
  127. //========================================================================================
  128.  
  129. inline FW_Fixed FW_ODFixedToFixed(ODFixed f)
  130. {
  131.     FW_Fixed fx;
  132.     fx.fRep = f;
  133.     return fx;
  134. }
  135.  
  136. inline FW_Fixed FW_IntToFixed(int i)
  137. {
  138.     return FW_ODFixedToFixed(FW_PrivIntToFixed(i));
  139. }
  140.  
  141. inline FW_Fixed FW_DoubleToFixed(FW_Double d)
  142. {
  143.     return FW_ODFixedToFixed(FW_PrivDoubleToFixed(d));
  144. }
  145.  
  146. //========================================================================================
  147. // Special math cases
  148. //========================================================================================
  149.  
  150. inline FW_Fixed FW_Half(FW_Fixed f)
  151. {
  152.     return FW_ODFixedToFixed(f.fRep / 2);
  153. }
  154.  
  155. inline FW_Fixed FW_MultipliedByInt(FW_Fixed f, short i)
  156. {
  157.     return FW_ODFixedToFixed(f.fRep * i);
  158. }
  159.  
  160. inline FW_Fixed FW_DividedByInt(FW_Fixed f, int i)
  161. {
  162.     return FW_ODFixedToFixed(f.fRep / i);
  163. }
  164.  
  165. inline FW_Fixed FW_RoundedToInt(FW_Fixed f)
  166. {
  167.     return FW_ODFixedToFixed((f.fRep + 0x00008000) & 0xFFFF0000ul);
  168. }
  169.  
  170. inline FW_Fixed FW_TruncatedToInt(FW_Fixed f)
  171. {
  172.     return FW_ODFixedToFixed(f.fRep & 0xFFFF0000ul);
  173. }
  174.  
  175. //========================================================================================
  176. // Increment/decrement
  177. //========================================================================================
  178.  
  179. inline FW_Fixed& operator++(FW_Fixed& f)
  180. {
  181.     f.fRep += FW_PrivIntToFixed(1);
  182.     return f;
  183. }
  184.  
  185. inline void operator++(FW_Fixed& f, int /* i */)
  186. {
  187.     f.fRep += FW_PrivIntToFixed(1);
  188. }
  189.  
  190. inline FW_Fixed& operator--(FW_Fixed& f)
  191. {
  192.     f.fRep -= FW_PrivIntToFixed(1);
  193.     return f;
  194. }
  195.  
  196. inline void operator--(FW_Fixed& f, int /* i */)
  197. {
  198.     f.fRep -= FW_PrivIntToFixed(1);
  199. }
  200.  
  201. //========================================================================================
  202. // Math
  203. //========================================================================================
  204.  
  205. inline FW_Fixed operator-(FW_Fixed f)
  206. {
  207.     FW_Fixed r;
  208.     r.fRep = -f.fRep;
  209.     return r;
  210. }
  211.  
  212. #ifndef FW_DEBUG
  213. // These inlines are not used during debugging.  When FW_DEBUG, a set of functions which 
  214. // checks for overflow is used instead.
  215.  
  216. inline FW_Fixed operator+(FW_Fixed f1, FW_Fixed f2)
  217. {
  218.     return FW_ODFixedToFixed(f1.fRep + f2.fRep);
  219. }
  220.  
  221. inline FW_Fixed operator-(FW_Fixed f1, FW_Fixed f2)
  222. {
  223.     return FW_ODFixedToFixed(f1.fRep - f2.fRep);
  224. }
  225.  
  226. inline FW_Fixed operator*(FW_Fixed f1, FW_Fixed f2)
  227. {
  228.     return FW_ODFixedToFixed(ODFixedMultiply(f1.fRep, f2.fRep));
  229. }
  230.  
  231. inline FW_Fixed operator/(FW_Fixed f1, FW_Fixed f2)
  232. {
  233.     return FW_ODFixedToFixed(ODFixedDivide(f1.fRep, f2.fRep));
  234. }
  235.  
  236. inline FW_Fixed operator%(FW_Fixed f1, FW_Fixed f2)
  237. {
  238.     return f1 - FW_TruncatedToInt(f1 / f2) * f2;
  239. }
  240.  
  241. #endif
  242.  
  243. //========================================================================================
  244. // Math with assignment
  245. //========================================================================================
  246.  
  247. inline FW_Fixed& operator += (FW_Fixed& f1, FW_Fixed f2)
  248. {
  249.     f1.fRep += f2.fRep;
  250.     return f1;
  251. }
  252.  
  253. inline FW_Fixed& operator -= (FW_Fixed& f1, FW_Fixed f2)
  254. {
  255.     f1.fRep -= f2.fRep;
  256.     return f1;
  257. }
  258.  
  259. inline FW_Fixed& operator *= (FW_Fixed& f1, FW_Fixed f2)
  260. {
  261.     f1.fRep = ODFixedMultiply(f1.fRep, f2.fRep);
  262.     return f1;
  263. }
  264.  
  265. inline FW_Fixed& operator /= (FW_Fixed& f1, FW_Fixed f2)
  266. {
  267.     f1.fRep = ODFixedDivide(f1.fRep, f2.fRep);
  268.     return f1;
  269. }
  270.  
  271. inline FW_Fixed& operator %= (FW_Fixed& f1, FW_Fixed f2)
  272. {
  273.     f1 = f1 - FW_TruncatedToInt(f1 / f2) * f2;
  274.     return f1;
  275. }
  276.  
  277. //========================================================================================
  278. // Comparisons
  279. //========================================================================================
  280.  
  281. inline FW_Boolean operator>(FW_Fixed f1, FW_Fixed f2)
  282. {
  283.     return f1.fRep > f2.fRep;
  284. }
  285.  
  286. inline FW_Boolean operator==(FW_Fixed f1, FW_Fixed f2)
  287. {
  288.     return f1.fRep == f2.fRep;
  289. }
  290.  
  291. inline FW_Boolean operator<(FW_Fixed f1, FW_Fixed f2)
  292. {
  293.     return f1.fRep < f2.fRep;
  294. }
  295.  
  296. inline FW_Boolean operator>=(FW_Fixed f1, FW_Fixed f2)
  297. {
  298.     return f1.fRep >= f2.fRep;
  299. }
  300.  
  301. inline FW_Boolean operator!=(FW_Fixed f1, FW_Fixed f2)
  302. {
  303.     return f1.fRep != f2.fRep;
  304. }
  305.  
  306. inline FW_Boolean operator<=(FW_Fixed f1, FW_Fixed f2)
  307. {
  308.     return f1.fRep <= f2.fRep;
  309. }
  310.  
  311. //========================================================================================
  312. // FW_Wide
  313. //========================================================================================
  314.  
  315. // ----- Conversions -----
  316.  
  317. FW_Wide                FW_IntToWide(int i);
  318. FW_Wide                FW_FixedToWide(FW_Fixed f);
  319. FW_Wide                FW_ODFixedToWide(ODFixed f);
  320. FW_Wide                FW_ODWideToWide(const ODWide& w);
  321.  
  322. int                    FW_WideToInt(const FW_Wide& w);
  323. ODFixed                FW_WideToODFixed(const FW_Wide& w);
  324. inline FW_Fixed        FW_WideToFixed(const FW_Wide& w);
  325.  
  326. // ----- Comparisons -----
  327.  
  328. FW_Boolean            operator ==    (const FW_Wide& w1, const FW_Wide& w2);
  329. FW_Boolean            operator !=    (const FW_Wide& w1, const FW_Wide& w2);
  330.  
  331. FW_Boolean            operator >    (const FW_Wide& w1, const FW_Wide& w2);
  332. FW_Boolean            operator <    (const FW_Wide& w1, const FW_Wide& w2);
  333. FW_Boolean            operator >=    (const FW_Wide& w1, const FW_Wide& w2);
  334. FW_Boolean            operator <=    (const FW_Wide& w1, const FW_Wide& w2);
  335.  
  336. // ----- Math -----
  337.  
  338. FW_Wide                 operator +    (const FW_Wide& w1, const FW_Wide& w2);
  339. FW_Wide                 operator -    (const FW_Wide& w1, const FW_Wide& w2);
  340.  
  341. FW_Wide&            operator +=    (FW_Wide& w1, const FW_Wide& w2);
  342. FW_Wide&            operator -=    (FW_Wide& w1, const FW_Wide& w2);
  343.  
  344. #ifdef FW_DEBUG
  345. FW_Wide&            operator +=    (FW_Wide& w1, const FW_Fixed& f2);
  346. FW_Wide&            operator -=    (FW_Wide& w1, const FW_Fixed& f2);
  347. #endif
  348.  
  349. FW_Wide                 FW_WideMultiply    (FW_Fixed f1, FW_Fixed f2);
  350. FW_Fixed            operator /  (const FW_Wide& w1, FW_Fixed f2);
  351.  
  352. FW_Fixed            FW_Sqrt(const FW_Wide& w);
  353.  
  354. //========================================================================================
  355. // Conversions
  356. //========================================================================================
  357.  
  358. inline FW_Wide FW_ODWideToWide(const ODWide& w)
  359. {
  360.     FW_Wide sw;
  361.     sw.fRep = w;
  362.     return sw;    
  363. }
  364.  
  365. //========================================================================================
  366. // Math
  367. //========================================================================================
  368.  
  369. inline FW_Wide& operator += (FW_Wide& w1, const FW_Wide& w2)
  370. {
  371.     ODWideAdd(&w1.fRep, &w2.fRep);
  372.     return w1;
  373. }
  374.  
  375. inline FW_Wide& operator -= (FW_Wide& w1, const FW_Wide& w2)
  376. {
  377.     ODWideSubtract(&w1.fRep, &w2.fRep);
  378.     return w1;
  379. }
  380.  
  381. //========================================================================================
  382. // Comparison
  383. //========================================================================================
  384.  
  385. inline FW_Boolean operator ==    (const FW_Wide& w1, const FW_Wide& w2)
  386. {
  387.     return ODWideCompare(&w1.fRep, &w2.fRep) == 0;
  388. }
  389.  
  390. inline FW_Boolean operator !=    (const FW_Wide& w1, const FW_Wide& w2)
  391. {
  392.     return ODWideCompare(&w1.fRep, &w2.fRep) != 0;
  393. }
  394.  
  395. inline FW_Boolean operator >    (const FW_Wide& w1, const FW_Wide& w2)
  396. {
  397.     return ODWideCompare(&w1.fRep, &w2.fRep) > 0;
  398. }
  399.  
  400. inline FW_Boolean operator <    (const FW_Wide& w1, const FW_Wide& w2)
  401. {
  402.     return ODWideCompare(&w1.fRep, &w2.fRep) < 0;
  403. }
  404.  
  405. inline FW_Boolean operator >=    (const FW_Wide& w1, const FW_Wide& w2)
  406. {
  407.     return ODWideCompare(&w1.fRep, &w2.fRep) >= 0;
  408. }
  409.  
  410. inline FW_Boolean operator <=    (const FW_Wide& w1, const FW_Wide& w2)
  411. {
  412.     return ODWideCompare(&w1.fRep, &w2.fRep) <= 0;
  413. }
  414.  
  415. //========================================================================================
  416. // Min/Max/Abs utilities
  417. //========================================================================================
  418.  
  419. //----------------------------------------------------------------------------------------
  420. //    FW_Fixed
  421. //----------------------------------------------------------------------------------------
  422.  
  423. inline FW_Fixed FW_Minimum(FW_Fixed f1, FW_Fixed f2)
  424. {
  425.     return f1.fRep < f2.fRep ? f1 : f2;
  426. }
  427.  
  428. inline FW_Fixed FW_Maximum(FW_Fixed f1, FW_Fixed f2)
  429. {
  430.     return f1.fRep > f2.fRep ? f1 : f2;
  431. }
  432.  
  433. inline FW_Fixed FW_Absolute(FW_Fixed f)
  434. {
  435.     return FW_ODFixedToFixed(f.fRep > 0 ? f.fRep : -f.fRep);
  436. }
  437.  
  438. //----------------------------------------------------------------------------------------
  439. //    FW_Wide
  440. //----------------------------------------------------------------------------------------
  441.  
  442. inline FW_Wide FW_Minimum(const FW_Wide& w1, const FW_Wide& w2)
  443. {
  444.     return w1 < w2 ? w1 : w2;
  445. }
  446.  
  447. inline FW_Wide FW_Maximum(const FW_Wide& w1, const FW_Wide& w2)
  448. {
  449.     return w1 > w2 ? w1 : w2;
  450. }
  451.  
  452. inline FW_Fixed FW_WideToFixed(const FW_Wide& w1)
  453. {
  454.     return FW_ODFixedToFixed(FW_WideToODFixed(w1));
  455. }
  456.  
  457. //========================================================================================
  458. // Global constants
  459. //========================================================================================
  460.  
  461. extern const FW_Fixed FW_kFixed0;        // 0
  462. extern const FW_Fixed FW_kFixedPos1;    // +1
  463. extern const FW_Fixed FW_kFixedNeg1;    // -1
  464. extern const FW_Fixed FW_kFixedPos2;    // +2
  465. extern const FW_Fixed FW_kFixedNeg2;    // -2
  466. extern const FW_Fixed FW_kFixed72;        // 72
  467. extern const FW_Fixed FW_kFixedPI;        // pi
  468.  
  469. extern const FW_Wide FW_kWide0;            // 0
  470. extern const FW_Wide FW_kWidePos1;        // +1
  471. extern const FW_Wide FW_kWideNeg1;        // -1
  472. extern const FW_Wide FW_kWidePos2;        // +2
  473. extern const FW_Wide FW_kWideNeg2;        // -2
  474. extern const FW_Wide FW_kWide72;        // 72
  475. extern const FW_Wide FW_kWidePI;        // pi
  476.  
  477. #endif
  478.