home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / fix24.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  8.2 KB  |  346 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. //
  26. // Fix24.cc : fixed precision class support functions
  27. //
  28.  
  29. #include <Fix24.h>
  30.  
  31. const double
  32.   Fix24_fs = 2147483648.,
  33.   Fix24_mult = Fix24_fs,
  34.   Fix24_div = 1./Fix24_fs,
  35.   Fix24_max = 1. - .5/Fix24_fs,
  36.   Fix24_min = -1.;
  37.       
  38. const unsigned long
  39.   Fix24_msb = 0x80000000L,
  40.   Fix24_lsb = 0x00000100L,
  41.   Fix24_m_max = 0x7fffff00L,
  42.   Fix24_m_min = 0x80000000L;
  43.           
  44. const double
  45.   Fix48_fs = 4611686018427387904.,
  46.   Fix48_max = 1. - .5/Fix48_fs,
  47.   Fix48_min = -1.,
  48.   Fix48_div_u = 1./Fix24_fs,
  49.   Fix48_div_l = 1./Fix48_fs;
  50.    
  51. const twolongs
  52.   Fix48_msb = { 0x80000000L, 0L },
  53.   Fix48_lsb = { 0L, 0x00000100L },
  54.   Fix48_m_max = { 0x7fffff00L, 0xffffff00L },
  55.   Fix48_m_min = { 0x80000000L, 0L };
  56.           
  57. // basic operators too large to be inline
  58.  
  59. long Fix24::assign(double d) 
  60.   if (d == 1.0)
  61.     return Fix24_m_max;
  62.   else if (d > Fix24_max)
  63.   {
  64.     long i = Fix24_m_max;
  65.     range_error(i);
  66.     return i;
  67.   }
  68.   else if (d < Fix24_min)
  69.   {
  70.     long i = Fix24_m_min;
  71.     range_error(i);
  72.     return i;
  73.   }
  74.   else {
  75.     d *= Fix24_mult;
  76.     return (long)(d + ((d >= 0)? 0.5 : -0.5));
  77.   }
  78. }
  79.  
  80. twolongs Fix48::assign(double d) 
  81.   if (d == 1.0)
  82.     return Fix48_m_max;
  83.   else if (d > Fix48_max)
  84.   {
  85.     twolongs i = Fix48_m_max;
  86.     range_error(i);
  87.     return i;
  88.   }
  89.   else if (d < Fix48_min)
  90.   {
  91.     twolongs i = Fix48_m_min;
  92.     range_error(i);
  93.     return i;
  94.   }
  95.   else {
  96.     twolongs i;
  97.     i.u = (long)(d *= Fix24_mult);
  98.     i.l = (long)((d - (double)i.u) * Fix24_mult);
  99.     return i;
  100.   }
  101. }
  102.  
  103.  
  104. Fix48 operator * (Fix24& a, Fix24& b)
  105. {
  106. // break a and b into lo and hi parts, and do a multiple-precision
  107. // multiply, with rounding
  108.  
  109.   int apos = (a.m >= 0);
  110.   unsigned long ua = (apos)? a.m : - a.m;
  111.   ua <<= 1; // ua is biased so result will be 47 bit mantissa, not 46:
  112.   unsigned long hi_a = (ua >> 16) & ((1 << 16) - 1);
  113.   unsigned long lo_a = ua & ((1 << 16) - 1);
  114.  
  115.   int bpos = (b.m >= 0);
  116.   unsigned long ub = (bpos)? b.m : -b.m;
  117.   unsigned long hi_b = (ub >> 16) & ((1 << 16) - 1);
  118.   unsigned long lo_b = ub & ((1 << 16) - 1);
  119.  
  120.   unsigned long 
  121.     hi_r = hi_a * hi_b,
  122.     mi_r = hi_a * lo_b + lo_a * hi_b,
  123.     lo_r = lo_a * lo_b,
  124.     rl = ((hi_r << 16) & 0x00ffffffL) + (mi_r & 0x00ffffffL) + (lo_r >> 16);
  125.   twolongs r = {
  126.     (hi_r & 0xffffff00L) + ((mi_r >> 16) & 0x0000ff00L)
  127.       + ((rl >> 16) & 0x0000ff00L),
  128.     rl << 8
  129.   };
  130.  
  131.   if ( apos != bpos ) {
  132.     unsigned long l = r.l;
  133.     r.l = -r.l;
  134.     r.u = ~r.u + ((l ^ r.l) & Fix24_msb ? 0 : Fix24_lsb);
  135.   }
  136.   return r;
  137. }
  138.  
  139. Fix24 operator / (Fix24& a, Fix24& b)
  140. {
  141.   long q;
  142.   int apos = (a.m >= 0);
  143.   unsigned long la = (apos)? a.m : -a.m;
  144.   int bpos = (b.m >= 0);
  145.   unsigned long lb = (bpos)? b.m: -b.m;
  146.   if (la >= lb)
  147.   {
  148.     q = (apos == bpos)? Fix24_m_max: Fix24_m_min;
  149.     a.range_error(q);
  150.   }
  151.   else                        // standard shift-based division alg
  152.   {
  153.     q = 0;
  154.     long r = la;
  155.     for (int i = 31; i > 0; i--)
  156.     {
  157.       q <<= 1;
  158.       r <<= 1;
  159.       if (r >= 0)
  160.         r -= lb;
  161.       else
  162.         r += lb;
  163.       if (r >= 0)
  164.         q |= 1;
  165.     }
  166.     if (r < 0) r += lb;
  167.     if (r >= lb / 2) ++q;
  168.     if (apos != bpos) q = -q;
  169.   }
  170.   return q;
  171. }
  172.  
  173. Fix48 operator + (Fix48&  f, Fix48&  g)
  174. {
  175.   long lo_r = (f.m.l >> 8) + (g.m.l >> 8);
  176.   twolongs r = {
  177.     f.m.u + g.m.u + (lo_r & 0x01000000L ? 0x00000100L : 0),
  178.     lo_r << 8
  179.   };
  180.   if ( (f.m.u ^ r.u) & (g.m.u ^ r.u) & Fix24_msb )
  181.     f.overflow(r);
  182.   return r;
  183. }
  184.  
  185. Fix48 operator - (Fix48&  f, Fix48&  g)
  186. {
  187.   long lo_r = (f.m.l >> 8) - (g.m.l >> 8);
  188.   twolongs r = {
  189.     f.m.u - g.m.u - (lo_r & 0x01000000L ? 0x00000100L: 0),
  190.     lo_r << 8
  191.   };
  192.   if ( (f.m.u ^ r.u) & (-g.m.u ^ r.u) & Fix24_msb )
  193.     f.overflow(r);
  194.   return r;
  195. }
  196.  
  197. Fix48 operator * (Fix48& a, int b)
  198. {
  199.   twolongs r;
  200.   int bpos = (b >= 0);
  201.   unsigned ub = (bpos)? b : -b;
  202.   if ( ub >= 65536L ) {
  203.     r = (bpos)? Fix48_m_max : Fix48_m_min;
  204.     a.range_error(r);
  205.   }
  206.   else {
  207.     unsigned long 
  208.       lo_r = (a.m.l & 0xffff) * ub,
  209.       mi_r = ((a.m.l >> 16) & 0xffff) * ub,
  210.       hi_r = a.m.u * ub;
  211.     r.l = lo_r + (mi_r << 16);
  212.     r.u = hi_r + ((mi_r >> 8) & 0x00ffff00L);
  213.     if ( !bpos ) {
  214.       unsigned long l = r.l;
  215.       r.l = -r.l;
  216.       r.u = ~r.u + ((l ^ r.l) & Fix24_msb ? 0 : Fix24_lsb);
  217.     }
  218.   }
  219.   return r;
  220. }
  221.  
  222. Fix48 operator << (Fix48& a, int b)
  223. {
  224.   twolongs r = { 0L, 0L };
  225.   if ( b >= 0 )
  226.     if ( b < 24 ) {
  227.       r.u = (a.m.u << b) + ((a.m.l >> (24 - b)) & 0xffffff00L);
  228.       r.l = a.m.l << b;
  229.     }
  230.     else if ( b < 48 ) {
  231.       r.u = a.m.l << (b - 24);
  232.     }
  233.   return r;
  234. }
  235.  
  236. Fix48 operator >> (Fix48& a, int b)
  237. {
  238.   twolongs r = { 0L, 0L };
  239.   if ( b >= 0 )
  240.     if ( b < 24 ) {
  241.       r.l = (a.m.u << (24 - b)) + ((a.m.l >> b) & 0xffffff00L);
  242.       r.u = (a.m.u >> b) & 0xffffff00L;
  243.     }
  244.     else if ( b < 48 ) {
  245.       r.l = (a.m.u >> (b - 24)) & 0xffffff00L;
  246.       r.u = (a.m.u >> 24) & 0xffffff00L;
  247.     }
  248.     else {
  249.       r.l = (a.m.u >> 24) & 0xffffff00L;
  250.       r.u = r.l;
  251.     }
  252.   return r;
  253. }
  254.  
  255. // error handling
  256.  
  257. void Fix24::overflow(long& i)
  258. {
  259.   (*Fix24_overflow_handler)(i);
  260. }
  261.  
  262. void Fix48::overflow(twolongs& i)
  263. {
  264.   (*Fix48_overflow_handler)(i);
  265. }
  266.  
  267. void Fix24::range_error(long& i)
  268. {
  269.   (*Fix24_range_error_handler)(i);
  270. }
  271.  
  272. void Fix48::range_error(twolongs& i)
  273. {
  274.   (*Fix48_range_error_handler)(i);
  275. }
  276.  
  277. // data definitions
  278.  
  279. Fix24_peh Fix24_overflow_handler = Fix24_overflow_saturate;
  280. Fix48_peh Fix48_overflow_handler = Fix48_overflow_saturate;
  281.  
  282. Fix24_peh Fix24_range_error_handler = Fix24_warning;
  283. Fix48_peh Fix48_range_error_handler = Fix48_warning;
  284.  
  285. //function definitions
  286.  
  287. Fix24_peh set_Fix24_overflow_handler(Fix24_peh new_handler) {
  288.   Fix24_peh old_handler = Fix24_overflow_handler;
  289.   Fix24_overflow_handler = new_handler;
  290.   return old_handler;
  291. }
  292.  
  293. Fix48_peh set_Fix48_overflow_handler(Fix48_peh new_handler) {
  294.   Fix48_peh old_handler = Fix48_overflow_handler;
  295.   Fix48_overflow_handler = new_handler;
  296.   return old_handler;
  297. }
  298.  
  299. void set_overflow_handler(Fix24_peh handler24, Fix48_peh handler48) {
  300.   set_Fix24_overflow_handler(handler24);
  301.   set_Fix48_overflow_handler(handler48);
  302. }
  303.  
  304. Fix24_peh set_Fix24_range_error_handler(Fix24_peh new_handler) {
  305.   Fix24_peh old_handler = Fix24_range_error_handler;
  306.   Fix24_range_error_handler = new_handler;
  307.   return old_handler;
  308. }
  309.  
  310. Fix48_peh set_Fix48_range_error_handler(Fix48_peh new_handler) {
  311.   Fix48_peh old_handler = Fix48_range_error_handler;
  312.   Fix48_range_error_handler = new_handler;
  313.   return old_handler;
  314. }
  315.  
  316. void set_range_error_handler(Fix24_peh handler24, Fix48_peh handler48) {
  317.   set_Fix24_range_error_handler(handler24);
  318.   set_Fix48_range_error_handler(handler48);
  319. }
  320.  
  321. void Fix24_overflow_saturate(long& i)
  322.   { i = (i > 0 ? Fix24_m_min : Fix24_m_max); }
  323. void Fix24_ignore(long&) {}
  324. void Fix24_warning(long&)
  325.   { cerr << "warning: Fix24 result out of range\n"; }
  326. void Fix24_overflow_warning_saturate(long& i)
  327.   { cerr << "warning: Fix24 result out of range\n"; 
  328.    Fix24_overflow_saturate(i); }
  329. void Fix24_abort(long&)
  330.   { cerr << "error: Fix24 result out of range\n"; abort(); }
  331.  
  332. void Fix48_ignore(twolongs&) {}
  333. void Fix48_overflow_saturate(twolongs& i)
  334.   { i = (i.u > 0 ? Fix48_m_min : Fix48_m_max); }
  335. void Fix48_warning(twolongs&)
  336.   { cerr << "warning: Fix48 result out of range\n"; }
  337. void Fix48_overflow_warning_saturate(twolongs& i)
  338.   { cerr << "warning: Fix48 result out of range\n"; 
  339.    Fix48_overflow_saturate(i); }
  340. void Fix48_abort(twolongs&)
  341.   { cerr << "error: Fix48 result out of range\n"; abort(); }
  342.  
  343.  
  344.