home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / Fix24.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  13KB  |  598 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Kurt Baudendistel (gt-eedsp!baud@gatech.edu)
  5.     adapted for libg++ by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of the GNU C++ Library.  This library is free
  8. software; you can redistribute it and/or modify it under the terms of
  9. the GNU Library General Public License as published by the Free
  10. Software Foundation; either version 2 of the License, or (at your
  11. option) any later version.  This library is distributed in the hope
  12. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  13. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. PURPOSE.  See the GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19.  
  20. #ifndef _Fix24_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _Fix24_h 1
  25.  
  26. #include <stream.h>
  27. #include <std.h>
  28.  
  29. // extra type definitions 
  30.  
  31. typedef struct {
  32.   _G_int32_t        u;
  33.   _G_uint32_t        l;
  34. } twolongs;
  35.  
  36. // constant definitions
  37.  
  38. static const int
  39.   Fix24_shift = 31;
  40.           
  41. static const double
  42.   Fix24_fs = 2147483648.,        // 2^Fix24_shift
  43.   Fix24_mult = Fix24_fs,
  44.   Fix24_div = 1./Fix24_fs,
  45.   Fix24_max = 1. - .5/Fix24_fs,
  46.   Fix24_min = -1.;
  47.       
  48. static const _G_uint32_t
  49.   Fix24_msb = 0x80000000L,
  50.   Fix24_lsb = 0x00000100L,
  51.   Fix24_m_max = 0x7fffff00L,
  52.   Fix24_m_min = 0x80000000L;
  53.  
  54. static const double
  55.   Fix48_fs = 36028797018963968.,    // 2^(24+Fix24_shift)
  56.   Fix48_max = 1. - .5/Fix48_fs,
  57.   Fix48_min = -1.,
  58.   Fix48_div_u = 1./Fix24_fs,
  59.   Fix48_div_l = 1./Fix48_fs;
  60.    
  61. static const twolongs
  62.   Fix48_msb = { 0x80000000L, 0L },
  63.   Fix48_lsb = { 0L, 0x00000100L },
  64.   Fix48_m_max = { 0x7fffff00L, 0xffffff00L },
  65.   Fix48_m_min = { 0x80000000L, 0L };
  66.           
  67. //
  68. // Fix24    class: 24-bit Fixed point data type
  69. //
  70. //    consists of a 24-bit mantissa (sign bit & 23 data bits).
  71. //
  72.  
  73. class Fix24 
  74.   friend class          Fix48;
  75.  
  76.   _G_int32_t            m;
  77.  
  78.   _G_int32_t            assign(double d);
  79.          operator       double() const;
  80.                         Fix24(long i);
  81.                         Fix24(int i);
  82.  
  83.  
  84. public:
  85.                         Fix24();
  86.                         Fix24(const Fix24&  f);
  87.                         Fix24(double d);
  88.                         Fix24(const Fix48& f);
  89.  
  90.                         ~Fix24();
  91.  
  92.   Fix24&                operator=(const Fix24&  f);
  93.   Fix24&                operator=(double d);
  94.   Fix24&                operator=(const Fix48& f);
  95.  
  96.   friend _G_int32_t&    mantissa(Fix24&  f);
  97.   friend const _G_int32_t&    mantissa(const Fix24&  f);
  98.   friend double         value(const Fix24&  f);
  99.  
  100.   Fix24                 operator +  () const;
  101.   Fix24                 operator -  () const;
  102.  
  103.   friend Fix24          operator +  (const Fix24&  f, const Fix24&  g);
  104.   friend Fix24          operator -  (const Fix24&  f, const Fix24&  g);
  105.   friend Fix48          operator *  (const Fix24&  f, const Fix24&  g);
  106.   friend Fix24          operator *  (const Fix24&  f, int     g);
  107.   friend Fix24          operator *  (int     g, const Fix24&  f);
  108.   friend Fix24          operator /  (const Fix24&  f, const Fix24&  g);
  109.   friend Fix24          operator << (const Fix24&  f, int b);
  110.   friend Fix24          operator >> (const Fix24&  f, int b);
  111.  
  112.   Fix24&                operator += (const Fix24&  f);
  113.   Fix24&                operator -= (const Fix24&  f);
  114.   Fix24&                operator *= (const Fix24&  f);
  115.   Fix24&                operator *= (int     b);
  116.   Fix24&                operator /= (const Fix24&  f);
  117.  
  118.   Fix24&                operator <<=(int b);
  119.   Fix24&                operator >>=(int b);
  120.  
  121.   friend int            operator == (const Fix24&  f, const Fix24&  g);
  122.   friend int            operator != (const Fix24&  f, const Fix24&  g);
  123.   friend int            operator >= (const Fix24&  f, const Fix24&  g);
  124.   friend int            operator <= (const Fix24&  f, const Fix24&  g);
  125.   friend int            operator >  (const Fix24&  f, const Fix24&  g);
  126.   friend int            operator <  (const Fix24&  f, const Fix24&  g);
  127.  
  128.   friend istream&       operator >> (istream& s, Fix24&  f);
  129.   friend ostream&       operator << (ostream& s, const Fix24&  f);
  130.  
  131.   void                  overflow(_G_int32_t&) const;
  132.   void                  range_error(_G_int32_t&) const;
  133. };
  134.  
  135.  
  136. //
  137. // Fix48 class: 48-bit Fixed point data type
  138. //
  139. //    consists of a 48-bit mantissa (sign bit & 47 data bits).
  140. //
  141.  
  142. class Fix48 
  143.   friend class         Fix24;
  144.  
  145.   twolongs             m;
  146.  
  147.   twolongs             assign(double d);
  148.          operator      double() const;
  149.                        Fix48(twolongs i);
  150.  
  151. public:
  152.                        Fix48();
  153.                        Fix48(const Fix48& f);
  154.                        Fix48(const Fix24&  f);
  155.                        Fix48(double d);
  156.                        ~Fix48();
  157.  
  158.   Fix48&               operator =  (const Fix48& f);
  159.   Fix48&               operator =  (const Fix24&  f);
  160.   Fix48&               operator =  (double d);
  161.  
  162.   friend twolongs&     mantissa(Fix48& f);
  163.   friend const twolongs&  mantissa(const Fix48& f);
  164.   friend double        value(const Fix48& f);
  165.  
  166.   Fix48                operator +  () const;
  167.   Fix48                operator -  () const;
  168.  
  169.   friend Fix48         operator +  (const Fix48& f, const Fix48& g);
  170.   friend Fix48         operator -  (const Fix48& f, const Fix48& g);
  171.   friend Fix48         operator *  (const Fix48& f, int    g);
  172.   friend Fix48         operator *  (int    g, const Fix48& f);
  173.   friend Fix48         operator << (const Fix48& f, int b);
  174.   friend Fix48         operator >> (const Fix48& f, int b);
  175.  
  176.   friend Fix48         operator *  (const Fix24&  f, const Fix24&  g);
  177.  
  178.   Fix48&               operator += (const Fix48& f);
  179.   Fix48&               operator -= (const Fix48& f);
  180.   Fix48&               operator *= (int    b);
  181.   Fix48&               operator <<=(int b);
  182.   Fix48&               operator >>=(int b);
  183.  
  184.   friend int           operator == (const Fix48& f, const Fix48& g);
  185.   friend int           operator != (const Fix48& f, const Fix48& g);
  186.   friend int           operator >= (const Fix48& f, const Fix48& g);
  187.   friend int           operator <= (const Fix48& f, const Fix48& g);
  188.   friend int           operator >  (const Fix48& f, const Fix48& g);
  189.   friend int           operator <  (const Fix48& f, const Fix48& g);
  190.  
  191.   friend istream&      operator >> (istream& s, Fix48& f);
  192.   friend ostream&      operator << (ostream& s, const Fix48& f);
  193.  
  194.   void                 overflow(twolongs& i) const;
  195.   void                 range_error(twolongs& i) const;
  196. };
  197.  
  198.  
  199. // active error handler declarations
  200.  
  201. typedef void (*Fix24_peh)(_G_int32_t&);
  202. typedef void (*Fix48_peh)(twolongs&);
  203.  
  204. extern Fix24_peh Fix24_overflow_handler;
  205. extern Fix48_peh Fix48_overflow_handler;
  206.  
  207. extern Fix24_peh Fix24_range_error_handler;
  208. extern Fix48_peh Fix48_range_error_handler;
  209.  
  210.  
  211. // error handler declarations
  212.  
  213. #if defined(SHORT_NAMES) || defined(VMS)
  214. #define    set_overflow_handler    sohndl
  215. #define set_range_error_handler    srnghdl
  216. #endif
  217.  
  218. extern Fix24_peh set_Fix24_overflow_handler(Fix24_peh);
  219. extern Fix48_peh set_Fix48_overflow_handler(Fix48_peh);
  220. extern void set_overflow_handler(Fix24_peh, Fix48_peh);
  221.  
  222. extern Fix24_peh set_Fix24_range_error_handler(Fix24_peh);
  223. extern Fix48_peh set_Fix48_range_error_handler(Fix48_peh);
  224. extern void set_range_error_handler(Fix24_peh, Fix48_peh);
  225.  
  226. extern void
  227.   Fix24_ignore(_G_int32_t&),
  228.   Fix24_overflow_saturate(_G_int32_t&),
  229.   Fix24_overflow_warning_saturate(_G_int32_t&),
  230.   Fix24_warning(_G_int32_t&),
  231.   Fix24_abort(_G_int32_t&);
  232.  
  233. extern void
  234.   Fix48_ignore(twolongs&),
  235.   Fix48_overflow_saturate(twolongs&),
  236.   Fix48_overflow_warning_saturate(twolongs&),
  237.   Fix48_warning(twolongs&),
  238.   Fix48_abort(twolongs&);
  239.  
  240.  
  241. inline Fix24::~Fix24() {}
  242.  
  243. inline Fix48::~Fix48() {}
  244.  
  245. inline Fix24::Fix24(long i)        
  246.   m = i; 
  247. }
  248.  
  249. inline Fix24::Fix24(int i)        
  250.   m = i; 
  251. }
  252.  
  253. inline Fix24::operator double() const
  254.   return  Fix24_div * m; 
  255. }
  256.  
  257. inline Fix24::Fix24()                 
  258.   m = 0; 
  259. }
  260.  
  261. inline Fix24::Fix24(const Fix24&  f)        
  262.   m = f.m; 
  263. }
  264.  
  265. inline Fix24::Fix24(double d)        
  266. {
  267.   m = assign(d);
  268. }
  269.  
  270. inline Fix24::Fix24(const Fix48& f)        
  271.   m = f.m.u;
  272. }
  273.  
  274. inline Fix24&  Fix24::operator=(const Fix24&  f)    
  275.   m = f.m; 
  276.   return *this; 
  277. }
  278.  
  279. inline Fix24&  Fix24::operator=(double d) 
  280.   m = assign(d); 
  281.   return *this; 
  282. }
  283.  
  284. inline Fix24&  Fix24::operator=(const Fix48& f)
  285.   m = f.m.u;
  286.   return *this; 
  287. }
  288.  
  289. inline _G_int32_t& mantissa(Fix24&  f)
  290.   return f.m; 
  291. }
  292.  
  293. inline const _G_int32_t& mantissa(const Fix24&  f)
  294.   return f.m; 
  295. }
  296.  
  297. inline double value(const Fix24&  f)
  298.   return double(f); 
  299. }
  300.  
  301. inline Fix24 Fix24::operator+() const        
  302.   return m; 
  303. }
  304.  
  305. inline Fix24 Fix24::operator-() const
  306.   return -m; 
  307. }
  308.  
  309. inline Fix24 operator+(const Fix24&  f, const Fix24&  g) 
  310. {
  311.   _G_int32_t sum = f.m + g.m;
  312.   if ( (f.m ^ sum) & (g.m ^ sum) & Fix24_msb )
  313.     f.overflow(sum);
  314.   return sum;
  315. }
  316.  
  317. inline Fix24 operator-(const Fix24&  f, const Fix24&  g) 
  318. {
  319.   _G_int32_t sum = f.m - g.m;
  320.   if ( (f.m ^ sum) & (-g.m ^ sum) & Fix24_msb )
  321.     f.overflow(sum);
  322.   return sum;
  323. }
  324.  
  325. inline Fix24 operator*(const Fix24& a, int b)     
  326.   return a.m * b; 
  327. }
  328.  
  329. inline Fix24 operator*(int b, const Fix24& a)     
  330.   return a * b; 
  331. }
  332.  
  333. inline Fix24 operator<<(const Fix24& a, int b)     
  334.   return a.m << b; 
  335. }
  336.  
  337. inline Fix24 operator>>(const Fix24& a, int b)     
  338.   return (a.m >> b) & ~0xff;
  339. }
  340.  
  341. inline  Fix24&  Fix24:: operator+=(const Fix24&  f)
  342. {
  343.   return *this = *this + f;
  344. }
  345.  
  346. inline Fix24&  Fix24:: operator-=(const Fix24&  f)     
  347.   return *this = *this - f; 
  348. }
  349.  
  350. inline Fix24& Fix24::operator*=(const Fix24& f)     
  351.   return *this = *this * f; 
  352. }
  353.  
  354. inline Fix24&  Fix24:: operator/=(const Fix24&  f)     
  355.   return *this = *this / f; 
  356. }
  357.  
  358. inline Fix24&  Fix24:: operator<<=(int b)    
  359.   return *this = *this << b;
  360. }
  361.  
  362. inline Fix24&  Fix24:: operator>>=(int b)    
  363.   return *this = *this >> b;
  364. }
  365.  
  366. inline Fix24& Fix24::operator*=(int b)
  367.   return *this = *this * b; 
  368. }
  369.  
  370. inline int operator==(const Fix24&  f, const Fix24&  g)    
  371.   return f.m == g.m;
  372. }
  373.  
  374. inline int operator!=(const Fix24&  f, const Fix24&  g)    
  375.   return f.m != g.m;
  376. }
  377.  
  378. inline int operator>=(const Fix24&  f, const Fix24&  g)    
  379.   return f.m >= g.m;
  380. }
  381.  
  382. inline int operator<=(const Fix24&  f, const Fix24&  g)    
  383.   return f.m <= g.m;
  384. }
  385.  
  386. inline int operator>(const Fix24&  f, const Fix24&  g)    
  387.   return f.m > g.m;
  388. }
  389.  
  390. inline int operator<(const Fix24&  f, const Fix24&  g)    
  391.   return f.m < g.m;
  392. }
  393.  
  394. inline istream&  operator>>(istream& s, Fix24&  f)
  395.   double d;
  396.   s >> d; 
  397.   f = d; 
  398.   return s; 
  399. }
  400.  
  401. inline ostream&  operator<<(ostream& s, const Fix24&  f)
  402.   return s << double(f);
  403. }
  404.  
  405. inline Fix48::Fix48(twolongs i)        
  406.   m = i;
  407. }
  408.  
  409. inline Fix48:: operator double() const
  410. /*
  411.  * Note: can't simply do Fix48_div_u * m.u + Fix48_div_l * m.l, because
  412.  * m.u is signed and m.l is unsigned.
  413.  */
  414.   return (m.u >= 0)? Fix48_div_u * m.u + Fix48_div_l * m.l :
  415.       (Fix48_div_u * ((_G_uint32_t)(m.u & 0xffffff00)) 
  416.       + Fix48_div_l * m.l) - 2;
  417. }
  418.  
  419. inline Fix48::Fix48()                
  420.   m.u = 0;
  421.   m.l = 0;
  422. }
  423.  
  424. inline Fix48::Fix48(const Fix48& f)        
  425.   m = f.m;
  426. }
  427.  
  428. inline Fix48::Fix48(const Fix24&  f)    
  429.   m.u = f.m;
  430.   m.l = 0;
  431. }
  432.  
  433. inline Fix48::Fix48(double d)        
  434.   m = assign(d);
  435. }
  436.  
  437. inline Fix48& Fix48::operator=(const Fix48& f)    
  438.   m = f.m;
  439.   return *this; 
  440. }
  441.  
  442. inline Fix48& Fix48::operator=(const Fix24&  f)    
  443.   m.u = f.m;
  444.   m.l = 0;
  445.   return *this;
  446. }
  447.  
  448. inline Fix48& Fix48::operator=(double d)    
  449.   m = assign(d);
  450.   return *this; 
  451. }
  452.  
  453. inline twolongs& mantissa(Fix48& f)    
  454.   return f.m;
  455. }
  456.  
  457. inline const twolongs& mantissa(const Fix48& f)    
  458.   return f.m;
  459. }
  460.  
  461. inline double value(const Fix48& f)
  462.   return double(f);
  463. }
  464.  
  465. inline Fix48 Fix48::operator+() const        
  466.   return m;
  467. }
  468.  
  469. inline Fix48 Fix48::operator-() const
  470.   twolongs n;
  471.   n.l = -m.l;
  472.   n.u = ~m.u + ((n.l ^ m.l) & Fix24_msb ? 0 : Fix24_lsb);
  473.   return Fix48(n);
  474. }
  475.  
  476. inline Fix48 operator*(int b, const Fix48& a)     
  477.   return a * b; 
  478. }
  479.  
  480. inline Fix48& Fix48::operator+=(const Fix48& f)     
  481.   return *this = *this + f;
  482. }
  483.  
  484. inline Fix48& Fix48::operator-=(const Fix48& f)     
  485.   return *this = *this - f;
  486. }
  487.  
  488. inline Fix48& Fix48::operator*=(int b)    
  489.   return *this = *this * b;
  490. }
  491.  
  492. inline Fix48& Fix48::operator<<=(int b)    
  493.   return *this = *this << b;
  494. }
  495.  
  496. inline Fix48& Fix48::operator>>=(int b)    
  497.   return *this = *this >> b;
  498. }
  499.  
  500. inline int operator==(const Fix48& f, const Fix48& g)    
  501.   return f.m.u == g.m.u && f.m.l == g.m.l;
  502. }
  503.  
  504. inline int operator!=(const Fix48& f, const Fix48& g)    
  505.   return f.m.u != g.m.u || f.m.l != g.m.l;
  506. }
  507.  
  508. inline int operator>=(const Fix48& f, const Fix48& g)    
  509.   return f.m.u >= g.m.u || (f.m.u == g.m.u && f.m.l >= g.m.l);
  510. }
  511.  
  512. inline int operator<=(const Fix48& f, const Fix48& g)    
  513.   return f.m.u <= g.m.u || (f.m.u == g.m.u && f.m.l <= g.m.l);
  514. }
  515.  
  516. inline int operator>(const Fix48& f, const Fix48& g)    
  517.   return f.m.u > g.m.u || (f.m.u == g.m.u && f.m.l > g.m.l);
  518. }
  519.  
  520. inline int operator<(const Fix48& f, const Fix48& g)    
  521.   return f.m.u < g.m.u || (f.m.u == g.m.u && f.m.l < g.m.l);
  522. }
  523.  
  524. inline istream& operator>>(istream& s, Fix48& f)
  525.   double d;
  526.   s >> d; 
  527.   f = d; 
  528.   return s; 
  529. }
  530.  
  531. inline ostream& operator<<(ostream& s, const Fix48& f)
  532.   return s << double(f);
  533. }
  534.  
  535. #endif
  536.