home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / fix24.h < prev    next >
C/C++ Source or Header  |  1993-07-23  |  12KB  |  576 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 GNU CC.
  8.  
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the GNU CC General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. GNU CC, but only under the conditions described in the
  18. GNU CC General Public License.   A copy of this license is
  19. supposed to have been given to you along with GNU CC so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies.  
  23. */
  24.  
  25. #ifndef _Fix24_h
  26. #pragma once
  27. #define _Fix24_h 1
  28.  
  29. #include <stream.h>
  30. #include <std.h>
  31.  
  32. // extra type definitions 
  33.  
  34. typedef struct {
  35.   long                 u;
  36.   unsigned long        l;
  37. } twolongs;
  38.  
  39. // constant definitions
  40.  
  41. extern const double
  42.   Fix24_fs,
  43.   Fix24_mult,
  44.   Fix24_div,
  45.   Fix24_max,
  46.   Fix24_min;
  47.  
  48. extern const unsigned long
  49.   Fix24_msb,
  50.   Fix24_lsb,
  51.   Fix24_m_max,
  52.   Fix24_m_min;
  53.  
  54. extern const double
  55.   Fix48_fs,
  56.   Fix48_max,
  57.   Fix48_min,
  58.   Fix48_div_u,    
  59.   Fix48_div_l;
  60.  
  61. extern const twolongs
  62.   Fix48_msb,
  63.   Fix48_lsb,
  64.   Fix48_m_max,
  65.   Fix48_m_min;
  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.   double operator       double();
  80.                         Fix24(long i);
  81.                         Fix24(int i);
  82.  
  83.  
  84. public:
  85.                         Fix24();
  86.                         Fix24(Fix24&  f);
  87.                         Fix24(double d);
  88.                         Fix24(Fix48& f);
  89.  
  90.                         ~Fix24();
  91.  
  92.   Fix24&                operator=(Fix24&  f);
  93.   Fix24&                operator=(double d);
  94.   Fix24&                operator=(Fix48& f);
  95.  
  96.   friend long&          mantissa(Fix24&  f);
  97.   friend double         value(Fix24&  f);
  98.  
  99.   Fix24                 operator +  ();
  100.   Fix24                 operator -  ();
  101.  
  102.   friend Fix24          operator +  (Fix24&  f, Fix24&  g);
  103.   friend Fix24          operator -  (Fix24&  f, Fix24&  g);
  104.   friend Fix48          operator *  (Fix24&  f, Fix24&  g);
  105.   friend Fix24          operator *  (Fix24&  f, int     g);
  106.   friend Fix24          operator *  (int     g, Fix24&  f);
  107.   friend Fix24          operator /  (Fix24&  f, Fix24&  g);
  108.   friend Fix24          operator << (Fix24&  f, int b);
  109.   friend Fix24          operator >> (Fix24&  f, int b);
  110.  
  111.   Fix24&                operator += (Fix24&  f);
  112.   Fix24&                operator -= (Fix24&  f);
  113.   Fix24&                operator *= (Fix24&  f);
  114.   Fix24&                operator *= (int     b);
  115.   Fix24&                operator /= (Fix24&  f);
  116.  
  117.   Fix24&                operator <<=(int b);
  118.   Fix24&                operator >>=(int b);
  119.  
  120.   friend int            operator == (Fix24&  f, Fix24&  g);
  121.   friend int            operator != (Fix24&  f, Fix24&  g);
  122.   friend int            operator >= (Fix24&  f, Fix24&  g);
  123.   friend int            operator <= (Fix24&  f, Fix24&  g);
  124.   friend int            operator >  (Fix24&  f, Fix24&  g);
  125.   friend int            operator <  (Fix24&  f, Fix24&  g);
  126.  
  127.   friend istream&       operator >> (istream& s, Fix24&  f);
  128.   friend ostream&       operator << (ostream& s, Fix24&  f);
  129.  
  130.   void                  overflow(long&);
  131.   void                  range_error(long&);
  132. };
  133.  
  134.  
  135. //
  136. // Fix48 class: 48-bit Fixed point data type
  137. //
  138. //    consists of a 48-bit mantissa (sign bit & 47 data bits).
  139. //
  140.  
  141. class Fix48 
  142.   friend class         Fix24;
  143.  
  144.   twolongs             m;
  145.  
  146.   twolongs             assign(double d);
  147.   double operator      double();
  148.                        Fix48(twolongs i);
  149.  
  150. public:
  151.                        Fix48();
  152.                        Fix48(Fix48& f);
  153.                        Fix48(Fix24&  f);
  154.                        Fix48(double d);
  155.                        ~Fix48();
  156.  
  157.   Fix48&               operator =  (Fix48& f);
  158.   Fix48&               operator =  (Fix24&  f);
  159.   Fix48&               operator =  (double d);
  160.  
  161.   friend twolongs&     mantissa(Fix48& f);
  162.   friend double        value(Fix48& f);
  163.  
  164.   Fix48                operator +  ();
  165.   Fix48                operator -  ();
  166.  
  167.   friend Fix48         operator +  (Fix48& f, Fix48& g);
  168.   friend Fix48         operator -  (Fix48& f, Fix48& g);
  169.   friend Fix48         operator *  (Fix48& f, int    g);
  170.   friend Fix48         operator *  (int    g, Fix48& f);
  171.   friend Fix48         operator << (Fix48& f, int b);
  172.   friend Fix48         operator >> (Fix48& f, int b);
  173.  
  174.   Fix48&               operator += (Fix48& f);
  175.   Fix48&               operator -= (Fix48& f);
  176.   Fix48&               operator *= (int    b);
  177.   Fix48&               operator <<=(int b);
  178.   Fix48&               operator >>=(int b);
  179.  
  180.   friend int           operator == (Fix48& f, Fix48& g);
  181.   friend int           operator != (Fix48& f, Fix48& g);
  182.   friend int           operator >= (Fix48& f, Fix48& g);
  183.   friend int           operator <= (Fix48& f, Fix48& g);
  184.   friend int           operator >  (Fix48& f, Fix48& g);
  185.   friend int           operator <  (Fix48& f, Fix48& g);
  186.  
  187.   friend istream&      operator >> (istream& s, Fix48& f);
  188.   friend ostream&      operator << (ostream& s, Fix48& f);
  189.  
  190.   void                 overflow(twolongs& i);
  191.   void                 range_error(twolongs& i);
  192. };
  193.  
  194. inline Fix24::~Fix24() {}
  195.  
  196. inline Fix24::Fix24(long i)        
  197.   m = i; 
  198. }
  199.  
  200. inline Fix24::Fix24(int i)        
  201.   m = i; 
  202. }
  203.  
  204. inline double Fix24::operator double() 
  205.   return  Fix24_div * m; 
  206. }
  207.  
  208. inline Fix24::Fix24()                 
  209.   m = 0; 
  210. }
  211.  
  212. inline Fix24::Fix24(Fix24&  f)        
  213.   m = f.m; 
  214. }
  215.  
  216. inline Fix24::Fix24(double d)        
  217. {
  218.   m = assign(d);
  219. }
  220.  
  221. inline Fix24::Fix24(Fix48& f)        
  222.   m = f.m.u;
  223. }
  224.  
  225. inline Fix24&  Fix24::operator=(Fix24&  f)    
  226.   m = f.m; 
  227.   return *this; 
  228. }
  229.  
  230. inline Fix24&  Fix24::operator=(double d) 
  231.   m = assign(d); 
  232.   return *this; 
  233. }
  234.  
  235. inline Fix24&  Fix24::operator=(Fix48& f)
  236.   m = f.m.u;
  237.   return *this; 
  238. }
  239.  
  240. inline long& mantissa(Fix24&  f)    
  241.   return f.m; 
  242. }
  243.  
  244. inline double value(Fix24&  f)        
  245.   return double(f); 
  246. }
  247.  
  248. inline Fix24 Fix24::operator+()         
  249.   return m; 
  250. }
  251.  
  252. inline Fix24 Fix24::operator-()         
  253.   return -m; 
  254. }
  255.  
  256. inline Fix24 operator+(Fix24&  f, Fix24&  g) 
  257. {
  258.   long sum = f.m + g.m;
  259.   if ( (f.m ^ sum) & (g.m ^ sum) & Fix24_msb )
  260.     f.overflow(sum);
  261.   return sum;
  262. }
  263.  
  264. inline Fix24 operator-(Fix24&  f, Fix24&  g) 
  265. {
  266.   long sum = f.m - g.m;
  267.   if ( (f.m ^ sum) & (-g.m ^ sum) & Fix24_msb )
  268.     f.overflow(sum);
  269.   return sum;
  270. }
  271.  
  272. inline Fix24 operator*(Fix24& a, int b)     
  273.   return a.m * b; 
  274. }
  275.  
  276. inline Fix24 operator*(int b, Fix24& a)     
  277.   return a * b; 
  278. }
  279.  
  280. inline Fix24 operator<<(Fix24& a, int b)     
  281.   return a.m << b; 
  282. }
  283.  
  284. inline Fix24 operator>>(Fix24& a, int b)     
  285.   return (a.m >> b) & 0xffffff00L; 
  286. }
  287.  
  288. inline  Fix24&  Fix24:: operator+=(Fix24&  f)
  289.   return *this = *this + f; 
  290. }
  291.  
  292. inline Fix24&  Fix24:: operator-=(Fix24&  f)     
  293.   return *this = *this - f; 
  294. }
  295.  
  296. inline Fix24& Fix24::operator*=(Fix24& f)     
  297.   return *this = *this * f; 
  298. }
  299.  
  300. inline Fix24&  Fix24:: operator/=(Fix24&  f)     
  301.   return *this = *this / f; 
  302. }
  303.  
  304. inline Fix24&  Fix24:: operator<<=(int b)    
  305.   return *this = *this << b;
  306. }
  307.  
  308. inline Fix24&  Fix24:: operator>>=(int b)    
  309.   return *this = *this >> b;
  310. }
  311.  
  312. inline Fix24& Fix24::operator*=(int b)
  313.   return *this = *this * b; 
  314. }
  315.  
  316. inline int operator==(Fix24&  f, Fix24&  g)    
  317.   return f.m == g.m;
  318. }
  319.  
  320. inline int operator!=(Fix24&  f, Fix24&  g)    
  321.   return f.m != g.m;
  322. }
  323.  
  324. inline int operator>=(Fix24&  f, Fix24&  g)    
  325.   return f.m >= g.m;
  326. }
  327.  
  328. inline int operator<=(Fix24&  f, Fix24&  g)    
  329.   return f.m <= g.m;
  330. }
  331.  
  332. inline int operator>(Fix24&  f, Fix24&  g)    
  333.   return f.m > g.m;
  334. }
  335.  
  336. inline int operator<(Fix24&  f, Fix24&  g)    
  337.   return f.m < g.m;
  338. }
  339.  
  340. inline istream&  operator>>(istream& s, Fix24&  f)
  341.   double d;
  342.   s >> d; 
  343.   f = d; 
  344.   return s; 
  345. }
  346.  
  347. inline ostream&  operator<<(ostream& s, Fix24&  f)
  348.   return s << double(f);
  349. }
  350.  
  351. inline Fix48::~Fix48() {}
  352.  
  353. inline Fix48::Fix48(twolongs i)        
  354.   m = i;
  355. }
  356.  
  357. inline double Fix48:: operator double()        
  358.   return Fix48_div_u * m.u + Fix48_div_l * m.l;
  359. }
  360.  
  361. inline Fix48::Fix48()                
  362.   m.u = 0;
  363.   m.l = 0;
  364. }
  365.  
  366. inline Fix48::Fix48(Fix48& f)        
  367.   m = f.m;
  368. }
  369.  
  370. inline Fix48::Fix48(Fix24&  f)    
  371.   m.u = f.m;
  372.   m.l = 0;
  373. }
  374.  
  375. inline Fix48::Fix48(double d)        
  376.   m = assign(d);
  377. }
  378.  
  379. inline Fix48& Fix48::operator=(Fix48& f)    
  380.   m = f.m;
  381.   return *this; 
  382. }
  383.  
  384. inline Fix48& Fix48::operator=(Fix24&  f)    
  385.   m.u = f.m;
  386.   m.l = 0;
  387.   return *this;
  388. }
  389.  
  390. inline Fix48& Fix48::operator=(double d)    
  391.   m = assign(d);
  392.   return *this; 
  393. }
  394.  
  395. inline twolongs& mantissa(Fix48& f)    
  396.   return f.m;
  397. }
  398.  
  399. inline double value(Fix48& f)        
  400.   return double(f);
  401. }
  402.  
  403. inline Fix48 Fix48::operator+()         
  404.   return m;
  405. }
  406.  
  407. inline Fix48 Fix48::operator-()         
  408.   twolongs n;
  409.   n.l = -m.l;
  410.   n.u = ~m.u + ((n.l ^ m.l) & Fix24_msb ? 0 : Fix24_lsb);
  411.   return Fix48(n);
  412. }
  413.  
  414. inline Fix48 operator*(int b, Fix48& a)     
  415.   return a * b; 
  416. }
  417.  
  418. inline Fix48& Fix48::operator+=(Fix48& f)     
  419.   return *this = *this + f;
  420. }
  421.  
  422. inline Fix48& Fix48::operator-=(Fix48& f)     
  423.   return *this = *this - f;
  424. }
  425.  
  426. inline Fix48& Fix48::operator*=(int b)    
  427.   return *this = *this * b;
  428. }
  429.  
  430. inline Fix48& Fix48::operator<<=(int b)    
  431.   return *this = *this << b;
  432. }
  433.  
  434. inline Fix48& Fix48::operator>>=(int b)    
  435.   return *this = *this >> b;
  436. }
  437.  
  438. inline int operator==(Fix48& f, Fix48& g)    
  439.   return f.m.u == g.m.u && f.m.l == g.m.l;
  440. }
  441.  
  442. inline int operator!=(Fix48& f, Fix48& g)    
  443.   return f.m.u != g.m.u || f.m.l != g.m.l;
  444. }
  445.  
  446. inline int operator>=(Fix48& f, Fix48& g)    
  447.   return f.m.u >= g.m.u || (f.m.u == g.m.u && f.m.l >= g.m.l);
  448. }
  449.  
  450. inline int operator<=(Fix48& f, Fix48& g)    
  451.   return f.m.u <= g.m.u || (f.m.u == g.m.u && f.m.l <= g.m.l);
  452. }
  453.  
  454. inline int operator>(Fix48& f, Fix48& g)    
  455.   return f.m.u > g.m.u || (f.m.u == g.m.u && f.m.l > g.m.l);
  456. }
  457.  
  458. inline int operator<(Fix48& f, Fix48& g)    
  459.   return f.m.u < g.m.u || (f.m.u == g.m.u && f.m.l < g.m.l);
  460. }
  461.  
  462. inline istream& operator>>(istream& s, Fix48& f)
  463.   double d;
  464.   s >> d; 
  465.   f = d; 
  466.   return s; 
  467. }
  468.  
  469. inline ostream& operator<<(ostream& s, Fix48& f)
  470.   return s << double(f);
  471. }
  472.  
  473.  
  474. // active error handler declarations
  475.  
  476. typedef void (*Fix24_peh)(long&);
  477. typedef void (*Fix48_peh)(twolongs&);
  478.  
  479. extern Fix24_peh Fix24_overflow_handler;
  480. extern Fix48_peh Fix48_overflow_handler;
  481.  
  482. extern Fix24_peh Fix24_range_error_handler;
  483. extern Fix48_peh Fix48_range_error_handler;
  484.  
  485.  
  486. // error handler declarations
  487.  
  488. overload set_overflow_handler;
  489. extern Fix24_peh set_Fix24_overflow_handler(Fix24_peh);
  490. extern Fix48_peh set_Fix48_overflow_handler(Fix48_peh);
  491. extern void set_overflow_handler(Fix24_peh, Fix48_peh);
  492.  
  493. overload set_range_error_handler;
  494. extern Fix24_peh set_Fix24_range_error_handler(Fix24_peh);
  495. extern Fix48_peh set_Fix48_range_error_handler(Fix48_peh);
  496. extern void set_range_error_handler(Fix24_peh, Fix48_peh);
  497.  
  498. extern void
  499.   Fix24_ignore(long&),
  500.   Fix24_overflow_saturate(long&),
  501.   Fix24_overflow_warning_saturate(long&),
  502.   Fix24_warning(long&),
  503.   Fix24_abort(long&);
  504.  
  505. extern void
  506.   Fix48_ignore(twolongs&),
  507.   Fix48_overflow_saturate(twolongs&),
  508.   Fix48_overflow_warning_saturate(twolongs&),
  509.   Fix48_warning(twolongs&),
  510.   Fix48_abort(twolongs&);
  511.  
  512.  
  513. #endif
  514.  
  515.