home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / lib / g++-include / fix16.h < prev    next >
C/C++ Source or Header  |  1994-12-22  |  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, 675 Mass Ave, Cambridge, MA 02139, 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 short Fix16::round(double d)
  242.   return short( (d >= 0)? d + 0.5 : d - 0.5); 
  243. }
  244.  
  245. inline Fix16::Fix16(short i)        
  246.   m = i; 
  247. }
  248.  
  249. inline Fix16::Fix16(int i)        
  250.   m = i; 
  251. }
  252.  
  253. inline Fix16::operator double()