home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libg++ / src / Fix24.h < prev    next >
C/C++ Source or Header  |  1994-04-25  |  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, 675 Mass Ave, Cambridge, MA 02139, 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.   long                 u;
  33.   unsigned long           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 unsigned long
  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.   long                  m;
  77.  
  78.   long                  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 long&          mantissa(Fix24&  f);
  97.   friend const long&    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(long&) const;
  132.   void                  range_error(long&) 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)(long&);
  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(long&),
  228.   Fix24_overflow_saturate(long&),
  229.   Fix24_overflow_warning_saturate(long&),
  230.   Fix24_warning(long&),
  231.   Fix24_abort(long&);
  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 Fix24::Fix24(long i)        
  244.   m = i; 
  245. }
  246.  
  247. inline Fix24::Fix24(int i)        
  248.   m = i; 
  249. }
  250.  
  251. inline Fix24::operator double() const
  252.   return  Fix24_div * m; 
  253. }
  254.  
  255. inline Fix24::Fix24()                 
  256.   m = 0; 
  257. }
  258.  
  259. inline Fix24::Fix24(const Fix24&  f)        
  260.   m = f.m; 
  261. }
  262.  
  263. inline Fix24::Fix24(double d)        
  264. {
  265.   m = assign(d);
  266. }
  267.  
  268. inline Fix24::F