integer.scm 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Operations on integers.
  3. ; These expect their arguments to be integers.
  4. (define integer-add bignum-add)
  5. (define integer-subtract bignum-subtract)
  6. (define integer-multiply bignum-multiply)
  7. (define integer-remainder bignum-remainder)
  8. (define integer-quotient bignum-quotient)
  9. (define integer-divide bignum-divide)
  10. (define integer-abs bignum-abs)
  11. ; Fixnums and bignums are disjoint.
  12. (define (integer= x y)
  13. (cond ((and (fixnum? x)
  14. (fixnum? y))
  15. (fixnum= x y))
  16. ((and (bignum? x)
  17. (bignum? y))
  18. (bignum= x y))
  19. (else
  20. #f)))
  21. ; Positive bignums are greater than all fixnums, negative bignums are less
  22. ; than all fixnums.
  23. (define (integer< x y)
  24. (cond ((fixnum? x)
  25. (if (fixnum? y)
  26. (fixnum< x y)
  27. (bignum-positive? y)))
  28. ((fixnum? y)
  29. (not (bignum-positive? x)))
  30. (else
  31. (bignum< x y))))
  32. (define (integer<= x y)
  33. (not (integer< y x)))
  34. (define (integer> x y)
  35. (integer< y x))
  36. (define (integer>= x y)
  37. (not (integer< x y)))
  38. (define integer-arithmetic-shift bignum-arithmetic-shift)
  39. (define integer-bitwise-not bignum-bitwise-not)
  40. (define integer-bit-count bignum-bit-count)
  41. (define integer-bitwise-and bignum-bitwise-and)
  42. (define integer-bitwise-ior bignum-bitwise-ior)
  43. (define integer-bitwise-xor bignum-bitwise-xor)
  44. (define (enter-integer x key)
  45. (if (or (too-big-for-fixnum? x)
  46. (too-small-for-fixnum? x))
  47. (long->bignum x key)
  48. (enter-fixnum x)))
  49. (define (enter-unsigned-integer x key)
  50. (if (unsigned-too-big-for-fixnum? x)
  51. (unsigned-long->bignum x key)
  52. (enter-fixnum (unsigned->integer x))))
  53. (define long-as-integer-size (bignum-bits-to-size 32))