home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / gcl-1.000 / gcl-1 / gcl-1.0 / mp / mp_sl3todivul3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  2.4 KB  |  111 lines

  1.  
  2. /*          Copyright (C) 1994 W. Schelter
  3.  
  4. This file is part of GNU Common Lisp, herein referred to as GCL
  5.  
  6. GCL is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GCL is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU library general public
  17. license along with GCL; see the file COPYING.  If not, write to the
  18. Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #define ulong unsigned long
  22. #define shift1BitRight(h,l) \
  23.   (l = l >> 1 , (h & 1 ? l = l | (1 << (WORD_SIZE -1))   : 0),  h = h >> 1)
  24.  
  25. #define shift2BitRight(h,l) \
  26.   (l = l >> 2 , (h & 3 ? l = l | ((h & 3) << (WORD_SIZE -2))   : 0),  h = h >> 2)
  27.  
  28. #define addll(x,y) \
  29.        (Xtx=(x),Xty=(y), Xtres = Xtx+Xty, \
  30.          (Xtres < Xtx ? overflow = 1 :0), Xtres)
  31.  
  32. /* the following defines divul3 in terms of divsl3.
  33.  */   
  34.    
  35.  
  36. #define WORD_SIZE 32
  37. divul3(x, y, hi)
  38.     ulong           x, y, *hi;
  39. {
  40.     ulong           q = 0,Xtx,Xty,Xtres,addy,overflow;
  41.     ulong           h = *hi, l = x, hibit;
  42.     ulong           dd;
  43.     /* if (y<=h) printf("error: the quotient will be more than 32 bits"); */
  44.     
  45.     if ((int) y > 0) {
  46.         dd = y >> 1;
  47.         if (dd <= h) {
  48.             unsigned int    ll = l;
  49.             shift1BitRight(h, ll);
  50.             q = divsl3(ll, y, &h);
  51.             h = h + h + (l & 1);
  52.             q = q + q;
  53.             if (h >= y) {
  54.                 q++;
  55.                 h -= y;
  56.             }
  57.             *hi = h;
  58.             return q;
  59.         } else {
  60.             return divsl3(x, y, hi);
  61.         }
  62.     }
  63.     /* negative */
  64.     {
  65.         ulong           ll;
  66.         ulong           rem;
  67.         ll = l;
  68.         shift2BitRight(h, ll);
  69.         dd = y >> 1;
  70.         q = divsl3(ll, dd, &h);
  71.         rem = h + h;
  72.         overflow = 0;
  73.         rem = addll(rem, rem);
  74.         rem += l & 3;
  75.         q = q + q;
  76.         addy = 0;
  77.         if (y & 1) {
  78.           if (overflow==0 && rem < q)
  79.             { addy = 1;
  80.               rem = addll(rem, y);
  81.               if (overflow==0 && rem < q)
  82.             { addy = 2;
  83.               rem +=y;
  84.             }
  85.             }
  86.              if (q > rem ) overflow = 0;
  87.           rem -= q;
  88.         }
  89.         if (addy > 0)
  90.           { q -= addy; }
  91.         else
  92.           { if (overflow || (rem >= y))
  93.               { rem -= y;
  94.             q++;
  95.               }
  96.           }
  97.             
  98.         *hi = rem;
  99.         return q;
  100.     }
  101. }
  102.  
  103.  
  104. /*
  105.  ;;- Local variables:
  106.  ;;- mode:c
  107.  ;;- version-control:t
  108.  ;;- End: 
  109.  
  110.  */
  111.