home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / Fix16.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  14KB  |  649 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 _Fix16_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _Fix16_h 1
  25.  
  26. #include <stream.h>
  27. #include <std.h>
  28.  
  29. // constant definitions
  30.  
  31. #define Fix16_fs     ((double)((unsigned)(1 << 15)))
  32.  
  33. #define Fix16_msb    (1 << 15)
  34. #define Fix16_m_max    ((1 << 15) - 1)
  35. #define Fix16_m_min    ((short)(1 << 15))
  36.  
  37. #define Fix16_mult    Fix16_fs
  38. #define Fix16_div    (1./Fix16_fs)
  39. #define Fix16_max    (1. - .5/Fix16_fs)
  40. #define Fix16_min    (-1.)
  41.  
  42.  
  43. #define Fix32_fs     ((double)((_G_uint32_t)(1 << 31)))
  44.  
  45. #define Fix32_msb    ((_G_uint32_t)(1 << 31))
  46. #define Fix32_m_max    ((_G_int32_t)((1 << 31) - 1))
  47. #define Fix32_m_min    ((_G_int32_t)(1 << 31))
  48.  
  49. #define Fix32_mult    Fix32_fs
  50. #define Fix32_div    (1./Fix32_fs)
  51. #define Fix32_max    (1. - .5/Fix32_fs)
  52. #define Fix32_min    (-1.)
  53.  
  54.  
  55. //
  56. // Fix16    class: 16-bit Fixed point data type
  57. //
  58. //    consists of a 16-bit mantissa (sign bit & 15 data bits).
  59. //
  60.  
  61. class Fix16 
  62.   friend class          Fix32;
  63.  
  64.   short                 m;
  65.  
  66.   short                 round(double d);
  67.   short                 assign(double d);
  68.                         Fix16(short i);
  69.                         Fix16(int i);
  70.  
  71.          operator       double() const;
  72.  
  73.  
  74. public:
  75.                         Fix16();
  76.                         Fix16(const Fix16&  f);
  77.                         Fix16(double d);
  78.                         Fix16(const Fix32& f);
  79.  
  80.                         ~Fix16();
  81.  
  82.   Fix16&                operator=(const Fix16&  f);
  83.   Fix16&                operator=(double d);
  84.   Fix16&                operator=(const Fix32& f);
  85.  
  86.   friend short&         mantissa(Fix16&  f);
  87.   friend const short&   mantissa(const Fix16&  f);
  88.   friend double         value(const Fix16&  f);
  89.  
  90.   Fix16                 operator +  () const;
  91.   Fix16                 operator -  () const;
  92.  
  93.   friend Fix16          operator +  (const Fix16&  f, const Fix16&  g);
  94.   friend Fix16          operator -  (const Fix16&  f, const Fix16&  g);
  95.   friend Fix32          operator *  (const Fix16&  f, const Fix16&  g);
  96.   friend Fix16          operator /  (const Fix16&  f, const Fix16&  g);
  97.   friend Fix16          operator << (const Fix16&  f, int b);
  98.   friend Fix16          operator >> (const Fix16&  f, int b);
  99.  
  100.   Fix16&                operator += (const Fix16&  f);
  101.   Fix16&                operator -= (const Fix16&  f);
  102.   Fix16&                operator *= (const Fix16& );
  103.   Fix16&                operator /= (const Fix16&  f);
  104.   
  105.   Fix16&                operator <<=(int b);
  106.   Fix16&                operator >>=(int b);
  107.  
  108.   friend int            operator == (const Fix16&  f, const Fix16&  g);
  109.   friend int            operator != (const Fix16&  f, const Fix16&  g);
  110.   friend int            operator >= (const Fix16&  f, const Fix16&  g);
  111.   friend int            operator <= (const Fix16&  f, const Fix16&  g);
  112.   friend int            operator >  (const Fix16&  f, const Fix16&  g);
  113.   friend int            operator <  (const Fix16&  f, const Fix16&  g);
  114.  
  115.   friend istream&       operator >> (istream& s, Fix16&  f);
  116.   friend ostream&       operator << (ostream& s, const Fix16&  f);
  117.  
  118.   void                  overflow(short&) const;
  119.   void                  range_error(short&) const;
  120.  
  121.   friend Fix16          operator *  (const Fix16&  f, int g);
  122.   friend Fix16          operator *  (int g, const Fix16&  f);
  123.   Fix16&                operator *= (int g);
  124. };
  125.  
  126.  
  127. //
  128. // Fix32 class: 32-bit Fixed point data type
  129. //
  130. //    consists of a 32-bit mantissa (sign bit & 31 data bits).
  131. //
  132.  
  133. class Fix32 
  134.   friend class         Fix16;
  135.  
  136.   _G_int32_t           m;
  137.  
  138.   _G_int32_t           round(double d);
  139.   _G_int32_t           assign(double d);
  140.  
  141.                        Fix32(_G_int32_t i);
  142.                        operator double() const;
  143.  
  144.  
  145. public:
  146.                        Fix32();
  147.                        Fix32(const Fix32& f);
  148.                        Fix32(const Fix16&  f);
  149.                        Fix32(double d);
  150.                        ~Fix32();
  151.  
  152.   Fix32&               operator =  (const Fix32& f);
  153.   Fix32&               operator =  (const Fix16&  f);
  154.   Fix32&               operator =  (double d);
  155.  
  156.   friend _G_int32_t&         mantissa(Fix32& f);
  157.   friend const _G_int32_t&   mantissa(const Fix32& f);
  158.   friend double        value(const Fix32& f);
  159.  
  160.   Fix32                operator +  () const;
  161.   Fix32                operator -  () const;
  162.  
  163.   friend Fix32         operator +  (const Fix32& f, const Fix32& g);
  164.   friend Fix32         operator -  (const Fix32& f, const Fix32& g);
  165.   friend Fix32         operator *  (const Fix32& f, const Fix32& g);
  166.   friend Fix32         operator /  (const Fix32& f, const Fix32& g);
  167.   friend Fix32         operator << (const Fix32& f, int b);
  168.   friend Fix32         operator >> (const Fix32& f, int b);
  169.  
  170.   friend Fix32         operator *  (const Fix16&  f, const Fix16&  g);
  171.  
  172.   Fix32&               operator += (const Fix32& f);
  173.   Fix32&               operator -= (const Fix32& f);
  174.   Fix32&               operator *= (const Fix32& f);
  175.   Fix32&               operator /= (const Fix32& f);
  176.   Fix32&               operator <<=(int b);
  177.   Fix32&               operator >>=(int b);
  178.  
  179.   friend int           operator == (const Fix32& f, const Fix32& g);
  180.   friend int           operator != (const Fix32& f, const Fix32& g);
  181.   friend int           operator >= (const Fix32& f, const Fix32& g);
  182.   friend int           operator <= (const Fix32& f, const Fix32& g);
  183.   friend int           operator >  (const Fix32& f, const Fix32& g);
  184.   friend int           operator <  (const Fix32& f, const Fix32& g);
  185.  
  186.   friend istream&      operator >> (istream& s, Fix32& f);
  187.   friend ostream&      operator << (ostream& s, const Fix32& f);
  188.  
  189.   void                 overflow(_G_int32_t& i) const;
  190.   void                 range_error(_G_int32_t& i) const;
  191.  
  192.   friend Fix32          operator *  (const Fix32&  f, int g);
  193.   friend Fix32          operator *  (int g, const Fix32&  f);
  194.   Fix32&                operator *= (int g);
  195. };
  196.  
  197. // active error handler declarations
  198.  
  199. typedef void (*Fix16_peh)(short&);
  200. typedef void (*Fix32_peh)(_G_int32_t&);
  201.  
  202. extern Fix16_peh Fix16_overflow_handler;
  203. extern Fix32_peh Fix32_overflow_handler;
  204.  
  205. extern Fix16_peh Fix16_range_error_handler;
  206. extern Fix32_peh Fix32_range_error_handler;
  207.  
  208. #if defined(SHORT_NAMES) || defined(VMS)
  209. #define    set_overflow_handler    sohndl
  210. #define set_range_error_handler    srnghdl
  211. #endif
  212.  
  213.  
  214. // error handler declarations
  215.  
  216. extern Fix16_peh set_Fix16_overflow_handler(Fix16_peh);
  217. extern Fix32_peh set_Fix32_overflow_handler(Fix32_peh);
  218. extern void set_overflow_handler(Fix16_peh, Fix32_peh);
  219.  
  220. extern Fix16_peh set_Fix16_range_error_handler(Fix16_peh);
  221. extern Fix32_peh set_Fix32_range_error_handler(Fix32_peh);
  222. extern void set_range_error_handler(Fix16_peh, Fix32_peh);
  223.  
  224. extern void
  225.   Fix16_ignore(short&),
  226.   Fix16_overflow_saturate(short&),
  227.   Fix16_overflow_warning_saturate(short&),
  228.   Fix16_warning(short&),
  229.   Fix16_abort(short&);
  230.  
  231. extern void
  232.   Fix32_ignore(_G_int32_t&),
  233.   Fix32_overflow_saturate(_G_int32_t&),
  234.   Fix32_overflow_warning_saturate(_G_int32_t&),
  235.   Fix32_warning(_G_int32_t&),
  236.   Fix32_abort(_G_int32_t&);
  237.  
  238.  
  239. inline Fix16::~Fix16() {}
  240.  
  241. inline Fix32::~Fix32() {}
  242.  
  243. inline short Fix16::round(double d)
  244.   return short( (d >= 0)? d + 0.5 : d - 0.5); 
  245. }
  246.  
  247. inline Fix16::Fix16(short i)        
  248.   m = i; 
  249. }
  250.  
  251. inline Fix16::Fix16(int i)        
  252.   m = i; 
  253. }
  254.  
  255. inline Fix16::operator double() const 
  256.   return  Fix16_div * m; 
  257. }
  258.  
  259. inline Fix16::Fix16()                 
  260.   m = 0; 
  261. }
  262.  
  263. inline Fix16::Fix16(const Fix16&  f)        
  264.   m = f.m; 
  265. }
  266.  
  267. inline Fix16::Fix16(double d)        
  268. {
  269.   m = assign(d);
  270. }
  271.  
  272.  
  273. inline Fix16&  Fix16::operator=(const Fix16&  f)    
  274.   m = f.m; 
  275.   return *this; 
  276. }
  277.  
  278. inline Fix16&  Fix16::operator=(double d) 
  279.   m = assign(d); 
  280.   return *this; 
  281. }
  282.  
  283.  
  284. inline Fix32::Fix32()                
  285.   m = 0;
  286. }
  287.  
  288. inline Fix32::Fix32(_G_int32_t i)        
  289.   m = i;
  290. }
  291.  
  292. inline Fix32:: operator double() const        
  293.   return Fix32_div * m;
  294. }
  295.  
  296.  
  297. inline Fix32::Fix32(const Fix32& f)        
  298.   m = f.m;
  299. }
  300.  
  301. inline Fix32::Fix32(const Fix16&  f)    
  302.   m = _G_int32_t(f.m) << 16;
  303. }
  304.  
  305. inline Fix32::Fix32(double d)        
  306.   m = assign(d);
  307. }
  308.  
  309. inline Fix16::Fix16(const Fix32& f)        
  310.   m = f.m >> 16; 
  311. }
  312.  
  313.  
  314. inline Fix16&  Fix16::operator=(const Fix32& f)
  315.   m = f.m >> 16; 
  316.   return *this; 
  317. }
  318.  
  319. inline Fix32& Fix32::operator=(const Fix32& f)    
  320.   m = f.m;
  321.   return *this; 
  322. }
  323.  
  324. inline Fix32& Fix32::operator=(const Fix16&  f)    
  325.   m = _G_int32_t(f.m) << 16;
  326.   return *this;
  327. }
  328.  
  329. inline Fix32& Fix32::operator=(double d)    
  330.   m = assign(d);
  331.   return *this; 
  332. }
  333.  
  334. inline short& mantissa(Fix16&  f)    
  335.   return f.m; 
  336. }
  337.  
  338. inline const short& mantissa(const Fix16&  f)    
  339.   return f.m; 
  340. }
  341.  
  342. inline double value(const Fix16&  f)        
  343.   return double(f); 
  344. }
  345.  
  346. inline Fix16 Fix16::operator+() const        
  347.   return m; 
  348. }
  349.  
  350. inline Fix16 Fix16::operator-() const    
  351.   return -m; 
  352. }
  353.  
  354. inline Fix16 operator+(const Fix16&  f, const Fix16&  g) 
  355. {
  356.   short sum = f.m + g.m;
  357.   if ( (f.m ^ sum) & (g.m ^ sum) & Fix16_msb )
  358.     f.overflow(sum);
  359.   return sum;
  360. }
  361.  
  362. inline Fix16 operator-(const Fix16&  f, const Fix16&  g) 
  363. {
  364.   short sum = f.m - g.m;
  365.   if ( (f.m ^ sum) & (-g.m ^ sum) & Fix16_msb )
  366.     f.overflow(sum);
  367.   return sum;
  368. }
  369.  
  370. inline Fix32 operator*(const Fix16&  f, const Fix16&  g)
  371.   return Fix32( _G_int32_t( _G_int32_t(f.m) * _G_int32_t(g.m) << 1)); 
  372. }
  373.  
  374. inline Fix16 operator<<(const Fix16& a, int b)     
  375.   return a.m << b; 
  376. }
  377.  
  378. inline Fix16 operator>>(const Fix16& a, int b)     
  379.   return a.m >> b; 
  380. }
  381.  
  382. inline  Fix16&  Fix16:: operator+=(const Fix16&  f)
  383.   return *this = *this + f; 
  384. }
  385.  
  386. inline Fix16&  Fix16:: operator-=(const Fix16&  f)     
  387.   return *this = *this - f; 
  388. }
  389.  
  390. inline Fix16& Fix16::operator*=(const Fix16& f)     
  391.   return *this = *this * f; 
  392. }
  393.  
  394. inline Fix16&  Fix16:: operator/=(const Fix16&  f)     
  395.   return *this = *this / f; 
  396. }
  397.  
  398. inline Fix16&  Fix16:: operator<<=(int b)    
  399.   return *this = *this << b;
  400. }
  401.  
  402. inline Fix16&  Fix16:: operator>>=(int b)    
  403.   return *this = *this >> b;
  404. }
  405.  
  406. inline int operator==(const Fix16&  f, const Fix16&  g)    
  407.   return f.m == g.m;
  408. }
  409.  
  410. inline int operator!=(const Fix16&  f, const Fix16&  g)    
  411.   return f.m != g.m;
  412. }
  413.  
  414. inline int operator>=(const Fix16&  f, const Fix16&  g)    
  415.   return f.m >= g.m;
  416. }
  417.  
  418. inline int operator<=(const Fix16&  f, const Fix16&  g)    
  419.   return f.m <= g.m;
  420. }
  421.  
  422. inline int operator>(const Fix16&  f, const Fix16&  g)    
  423.   return f.m > g.m;
  424. }
  425.  
  426. inline int operator<(const Fix16&  f, const Fix16&  g)    
  427.   return f.m < g.m;
  428. }
  429.  
  430. inline istream&  operator>>(istream& s, Fix16&  f)
  431.   double d;
  432.   s >> d; 
  433.   f = d; 
  434.   return s; 
  435. }
  436.  
  437. inline ostream&  operator<<(ostream& s, const Fix16&  f)
  438.   return s << double(f);
  439. }
  440.  
  441.  
  442. inline Fix16 operator*(const Fix16&  f, int g)
  443. {
  444.   return Fix16(short(f.m * g));
  445. }
  446.  
  447. inline Fix16 operator*(int g, const Fix16&  f)
  448. {
  449.   return f * g;
  450. }
  451.  
  452.  
  453. inline Fix16& Fix16::operator*=(int g)
  454. {
  455.   return *this = *this * g;
  456. }
  457.  
  458. inline _G_int32_t Fix32::round(double d)
  459.   return _G_int32_t( (d >= 0)? d + 0.5 : d - 0.5);
  460. }
  461.  
  462. inline _G_int32_t& mantissa(Fix32& f)    
  463.   return f.m;
  464. }
  465.  
  466. inline const _G_int32_t& mantissa(const Fix32& f)    
  467.   return f.m;
  468. }
  469.  
  470. inline double value(const Fix32& f)        
  471.   return double(f);
  472. }
  473.  
  474. inline Fix32 Fix32::operator+() const
  475.   return m;
  476. }
  477.  
  478. inline Fix32 Fix32::operator-() const        
  479.   return -m;
  480. }
  481.  
  482. inline Fix32 operator+(const Fix32& f, const Fix32& g) 
  483. {
  484.   _G_int32_t sum = f.m + g.m;
  485.   if ( (f.m ^ sum) & (g.m ^ sum) & Fix32_msb )
  486.     f.overflow(sum);
  487.   return sum;
  488. }
  489.  
  490. inline Fix32 operator-(const Fix32& f, const Fix32& g) 
  491. {
  492.   _G_int32_t sum = f.m - g.m;
  493.   if ( (f.m ^ sum) & (-g.m ^ sum) & Fix32_msb )
  494.     f.overflow(sum);
  495.   return sum;
  496. }
  497.  
  498. inline Fix32 operator<<(const Fix32& a, int b)     
  499.   return a.m << b;
  500. }
  501.  
  502. inline Fix32 operator>>(const Fix32& a, int b)     
  503.   return a.m >> b;
  504. }
  505.  
  506. inline Fix32& Fix32::operator+=(const Fix32& f)     
  507.   return *this = *this + f;
  508. }
  509.  
  510. inline Fix32& Fix32::operator-=(const Fix32& f)     
  511.   return *this = *this - f;
  512. }
  513.  
  514. inline Fix32& Fix32::operator*=(const Fix32& f)     
  515.   return *this = *this * f;
  516. }
  517.  
  518. inline Fix32& Fix32::operator/=(const Fix32& f)     
  519.   return *this = *this / f;
  520. }
  521.  
  522.  
  523. inline Fix32& Fix32::operator<<=(int b)    
  524.   return *this = *this << b;
  525. }
  526.  
  527. inline Fix32& Fix32::operator>>=(int b)    
  528.   return *this = *this >> b;
  529. }
  530.  
  531. inline int operator==(const Fix32& f, const Fix32& g)    
  532.   return f.m == g.m;
  533. }
  534.  
  535. inline int operator!=(const Fix32& f, const Fix32& g)    
  536.   return f.m != g.m;
  537. }
  538.  
  539. inline int operator>=(const Fix32& f, const Fix32& g)    
  540.   return f.m >= g.m;
  541. }
  542.  
  543. inline int operator<=(const Fix32& f, const Fix32& g)    
  544.   return f.m <= g.m;
  545. }
  546.  
  547. inline int operator>(const Fix32& f, const Fix32& g)    
  548.   return f.m > g.m;
  549. }
  550.  
  551. inline int operator<(const Fix32& f, const Fix32& g)    
  552.   return f.m < g.m;
  553. }
  554.  
  555. inline istream& operator>>(istream& s, Fix32& f)
  556.   double d;
  557.   s >> d; 
  558.   f = d; 
  559.   return s; 
  560. }
  561.  
  562. inline ostream& operator<<(ostream& s, const Fix32& f)
  563.   return s << double(f);
  564. }
  565.  
  566. inline Fix32 operator*(const Fix32&  f, int g)
  567. {
  568.   return Fix32(_G_int32_t(f.m * g));
  569. }
  570.  
  571. inline Fix32 operator*(int g, const Fix32&  f)
  572. {
  573.   return f * g;
  574. }
  575.  
  576.  
  577.  
  578. inline Fix32& Fix32::operator*=(int g)
  579. {
  580.   return *this = *this * g;
  581. }
  582.  
  583. #endif
  584.