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