home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / fix16.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  5.9 KB  |  242 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. // Fix.cc : fixed precision class support functions
  27. //
  28.  
  29. #include <Fix16.h>
  30.  
  31. // basic operators too large to be inline
  32.  
  33. short Fix16::assign(double d) 
  34.   if (d == 1.0)
  35.     return Fix16_m_max;
  36.   else if (d > Fix16_max)
  37.   {
  38.     short i = Fix16_m_max;
  39.     range_error(i);
  40.     return i;
  41.   }
  42.   else if (d < Fix16_min)
  43.   {
  44.     short i = Fix16_m_min;
  45.     range_error(i);
  46.     return i;
  47.   }
  48.   else 
  49.     return round(Fix16_mult * d);
  50. }
  51.  
  52. long Fix32::assign(double d) 
  53.   if (d == 1.0)
  54.     return Fix32_m_max;
  55.   else if (d > Fix32_max)
  56.   {
  57.     long i = Fix32_m_max;
  58.     range_error(i);
  59.     return i;
  60.   }
  61.   else if (d < Fix32_min)
  62.   {
  63.     long i = Fix32_m_min;
  64.     range_error(i);
  65.     return i;
  66.   }
  67.   else 
  68.     return round(Fix32_mult * d);
  69. }
  70.  
  71.  
  72. Fix32 operator * (Fix32& a, Fix32& b)
  73. {
  74. // break a and b into lo and hi parts, and do a multiple-precision
  75. // multiply, with rounding
  76.  
  77.   int apos = (a.m >= 0);
  78.   unsigned long ua = (apos)? a.m : - a.m;
  79.   ua <<= 1; // ua is biased so result will be 31 bit mantissa, not 30:
  80.   unsigned long hi_a = (ua >> 16) & ((1 << 16) - 1);
  81.   unsigned long lo_a = ua & ((1 << 16) - 1);
  82.  
  83.   int bpos = (b.m >= 0);
  84.   unsigned long ub = (bpos)? b.m : -b.m;
  85.   unsigned long hi_b = (ub >> 16) & ((1 << 16) - 1);
  86.   unsigned long lo_b = ub & ((1 << 16) - 1);
  87.  
  88.   unsigned long r = lo_a * lo_b + (1 << 15);
  89.   r = (r >> 16) + hi_a * lo_b + lo_a * hi_b + (1 << 15);
  90.   r = (r >> 16) + hi_a * hi_b;
  91.   long p = (apos != bpos)? -r : r;
  92.   return Fix32(p);
  93. }
  94.  
  95. Fix16 operator / (Fix16& a, Fix16& b)
  96. {
  97.   short q;
  98.   int apos = (a.m >= 0);
  99.   long la = (apos)? a.m : -a.m;
  100.   long scaled_a = la << 15;
  101.   int bpos = (b.m >= 0);
  102.   short sb = (bpos)? b.m: -b.m;
  103.   if (la >= sb)
  104.   {
  105.     q = (apos == bpos)? Fix16_m_max: Fix16_m_min;
  106.     a.range_error(q);
  107.   }
  108.   else
  109.   {
  110.     q = scaled_a / sb;
  111.     if ((scaled_a % sb) >= (sb / 2)) ++q;
  112.     if (apos != bpos) q = -q;
  113.   }
  114.   return Fix16(q);
  115. }
  116.  
  117. Fix32 operator / (Fix32& a, Fix32& b)
  118. {
  119.   long q;
  120.   int apos = (a.m >= 0);
  121.   unsigned long la = (apos)? a.m : -a.m;
  122.   int bpos = (b.m >= 0);
  123.   unsigned long lb = (bpos)? b.m: -b.m;
  124.   if (la >= lb)
  125.   {
  126.     q = (apos == bpos)? Fix32_m_max: Fix32_m_min;
  127.     a.range_error(q);
  128.   }
  129.   else                        // standard shift-based division alg
  130.   {
  131.     q = 0;
  132.     long r = la;
  133.     for (int i = 31; i > 0; i--)
  134.     {
  135.       q <<= 1;
  136.       r <<= 1;
  137.       if (r >= 0)
  138.         r -= lb;
  139.       else
  140.         r += lb;
  141.       if (r >= 0)
  142.         q |= 1;
  143.     }
  144.     if (r < 0) r += lb;
  145.     if (r >= lb / 2) ++q;
  146.     if (apos != bpos) q = -q;
  147.   }
  148.   return Fix32(q);
  149. }
  150.  
  151. // error handling
  152.  
  153. void Fix16::overflow(short& i)
  154. {
  155.   (*Fix16_overflow_handler)(i);
  156. }
  157.  
  158. void Fix32::overflow(long& i)
  159. {
  160.   (*Fix32_overflow_handler)(i);
  161. }
  162.  
  163. void Fix16::range_error(short& i)
  164. {
  165.   (*Fix16_range_error_handler)(i);
  166. }
  167.  
  168. void Fix32::range_error(long& i)
  169. {
  170.   (*Fix32_range_error_handler)(i);
  171. }
  172.  
  173. // data definitions
  174.  
  175. Fix16_peh Fix16_overflow_handler = Fix16_overflow_saturate;
  176. Fix32_peh Fix32_overflow_handler = Fix32_overflow_saturate;
  177.  
  178. Fix16_peh Fix16_range_error_handler = Fix16_warning;
  179. Fix32_peh Fix32_range_error_handler = Fix32_warning;
  180.  
  181. //function definitions
  182.  
  183. Fix16_peh set_Fix16_overflow_handler(Fix16_peh new_handler) {
  184.   Fix16_peh old_handler = Fix16_overflow_handler;
  185.   Fix16_overflow_handler = new_handler;
  186.   return old_handler;
  187. }
  188.  
  189. Fix32_peh set_Fix32_overflow_handler(Fix32_peh new_handler) {
  190.   Fix32_peh old_handler = Fix32_overflow_handler;
  191.   Fix32_overflow_handler = new_handler;
  192.   return old_handler;
  193. }
  194.  
  195. void set_overflow_handler(Fix16_peh handler16, Fix32_peh handler32) {
  196.   set_Fix16_overflow_handler(handler16);
  197.   set_Fix32_overflow_handler(handler32);
  198. }
  199.  
  200. Fix16_peh set_Fix16_range_error_handler(Fix16_peh new_handler) {
  201.   Fix16_peh old_handler = Fix16_range_error_handler;
  202.   Fix16_range_error_handler = new_handler;
  203.   return old_handler;
  204. }
  205.  
  206. Fix32_peh set_Fix32_range_error_handler(Fix32_peh new_handler) {
  207.   Fix32_peh old_handler = Fix32_range_error_handler;
  208.   Fix32_range_error_handler = new_handler;
  209.   return old_handler;
  210. }
  211.  
  212. void set_range_error_handler(Fix16_peh handler16, Fix32_peh handler32) {
  213.   set_Fix16_range_error_handler(handler16);
  214.   set_Fix32_range_error_handler(handler32);
  215. }
  216.  
  217. void Fix16_overflow_saturate(short& i)
  218.   { i = (i > 0 ? Fix16_m_min : Fix16_m_max); }
  219. void Fix16_ignore(short&) {}
  220. void Fix16_warning(short&)
  221.   { cerr << "warning: Fix16 result out of range\n"; }
  222. void Fix16_overflow_warning_saturate(short& i)
  223.   { cerr << "warning: Fix16 result out of range\n"; 
  224.    Fix16_overflow_saturate(i); }
  225. void Fix16_abort(short&)
  226.   { cerr << "error: Fix16 result out of range\n"; abort(); }
  227.  
  228. void Fix32_ignore(long&) {}
  229. void Fix32_overflow_saturate(long& i)
  230.   { i = (i > 0 ? Fix32_m_min : Fix32_m_max); }
  231. void Fix32_warning(long&)
  232.   { cerr << "warning: Fix32 result out of range\n"; }
  233. void Fix32_overflow_warning_saturate(long& i)
  234.   { cerr << "warning: Fix32 result out of range\n"; 
  235.    Fix32_overflow_saturate(i); }
  236. void Fix32_abort(long&)
  237.   { cerr << "error: Fix32 result out of range\n"; abort(); }
  238.  
  239.  
  240.