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_divul3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  1.9 KB  |  65 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. /*
  22.     ulong low,divisor,h,q;
  23.     if divisor!= 0 and if (hiremainder:low)/divisor (ie q)
  24.         is expressible in 32 bits,
  25.     then
  26.     (h = hiremainder, q = divll(low,divisor),
  27.       h:low == q * divisor + hiremainder && 0<= hiremainder && hiremainder < divisor)
  28.     is TRUE.  
  29.     [the arithmetic is ordinary arithmetic among unsigned 64 bit integers]
  30.     A sufficient criteria for (hiremainder:low)/divisor
  31.          to be expressible in 32 bits,
  32.     is bfffo(divisor)-bfffo(hiremainder) <= 0
  33.     
  34. */   
  35.  
  36. #include "include.h"
  37. #include "arith.h"
  38.  
  39. #define WORD_SIZE 32
  40. /* SHIFT1BIT: shift h and l left by 1 as 64 bits.  We don't care what
  41.   is coming into the bottom word  */
  42.  
  43. #define shift1bit(h,l) \
  44.   l = (h = h << 1, ( l & (1<<(WORD_SIZE -1)) ? h +=1 : 0), l<<1)
  45.  
  46. divul3(x,y,hi)
  47.      ulong x,y,*hi;
  48. {ulong q =0;
  49.  ulong h = *hi,l=x,hibit;
  50.  int count = WORD_SIZE;
  51. /* if (y<=h) printf("error: the quotient will be more than 32 bits"); */
  52. #ifdef QUICK_DIV
  53.  QUICK_DIV(x,y,h,hi)
  54. #endif 
  55.   do { q = q << 1;
  56.      hibit = h & (1 << (WORD_SIZE -1));
  57.      shift1bit(h,l);
  58.      if (hibit || (y <= h))
  59.        { q += 1; h -= y;}
  60.      } while(--count > 0);
  61.  *hi = h;
  62.  return q;
  63. }
  64.  
  65.