bignumint.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*-C-*-
  2. $Id: bignmint.h,v 1.4 1993/10/27 23:57:07 gjr Exp $
  3. Copyright 1989,1992,1993,2004 Massachusetts Institute of Technology
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above
  10. copyright notice, this list of conditions and the following
  11. disclaimer in the documentation and/or other materials provided
  12. with the distribution.
  13. 3. The name of the author may not be used to endorse or promote
  14. products derived from this software without specific prior
  15. written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /* License changed to modified BSD by cph on 2004-11-08. */
  29. /*
  30. * Modifications for Scheme 48 by Martin Gasbichler, Mike Sperber,
  31. * Marcus Crestani, David Frese, Taylor Campbell, Harald Glab-Phlak
  32. */
  33. /* Internal Interface to Bignum Code */
  34. #undef BIGNUM_ZERO_P
  35. #undef BIGNUM_NEGATIVE_P
  36. /* The memory model is based on the following definitions, and on the
  37. definition of the type `bignum_type'. The only other special
  38. definition is `CHAR_BIT', which is defined in the Ansi C header
  39. file "limits.h". */
  40. typedef long bignum_digit_type;
  41. typedef long bignum_length_type;
  42. /* BIGNUM_ALLOCATE allocates a (length + 1)-element array of
  43. `bignum_digit_type'; deallocation is the responsibility of the
  44. user (in Scheme, the garbage collector handles this). */
  45. #define BIGNUM_ALLOCATE_TAGGED(length_in_digits) \
  46. (s48_allocate_bignum((length_in_digits + 1) * \
  47. sizeof(bignum_digit_type)))
  48. #define BIGNUM_ALLOCATE(length_in_digits) \
  49. (S48_ADDRESS_AFTER_HEADER((BIGNUM_ALLOCATE_TAGGED((length_in_digits))), \
  50. long))
  51. extern s48_value s48_allocate_bignum(long size);
  52. /* BIGNUM_TO_POINTER casts a bignum object to a digit array pointer. */
  53. #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type *) (bignum))
  54. /* BIGNUM_REDUCE_LENGTH allows the memory system to reclaim some
  55. space when a bignum's length is reduced from its original value. */
  56. #define BIGNUM_REDUCE_LENGTH(target, source, length) \
  57. target = (long *) s48_shorten_bignum((char *) source, length)
  58. extern char * s48_shorten_bignum(char*, long);
  59. /* BIGNUM_DEALLOCATE is called when disposing of bignums which are
  60. created as intermediate temporaries; Scheme doesn't need this. */
  61. #define BIGNUM_DEALLOCATE(bignum)
  62. /* If BIGNUM_FORCE_NEW_RESULTS is defined, all bignum-valued operations
  63. return freshly-allocated results. This is useful for some kinds of
  64. memory deallocation strategies. */
  65. /* #define BIGNUM_FORCE_NEW_RESULTS */
  66. /* BIGNUM_EXCEPTION is invoked to handle assertion violations. */
  67. #define BIGNUM_EXCEPTION abort
  68. #define BIGNUM_DIGIT_LENGTH (((sizeof (bignum_digit_type)) * CHAR_BIT) - 2)
  69. #define BIGNUM_HALF_DIGIT_LENGTH (BIGNUM_DIGIT_LENGTH / 2)
  70. #define BIGNUM_RADIX (1L << BIGNUM_DIGIT_LENGTH)
  71. #define BIGNUM_RADIX_ROOT (1L << BIGNUM_HALF_DIGIT_LENGTH)
  72. #define BIGNUM_DIGIT_MASK (BIGNUM_RADIX - 1)
  73. #define BIGNUM_HALF_DIGIT_MASK (BIGNUM_RADIX_ROOT - 1)
  74. #define BIGNUM_START_PTR(bignum) \
  75. ((BIGNUM_TO_POINTER (bignum)) + 1)
  76. #define BIGNUM_SET_HEADER(bignum, length, negative_p) \
  77. (* (BIGNUM_TO_POINTER (bignum))) = \
  78. ((length) | ((negative_p) ? BIGNUM_RADIX : 0))
  79. #define BIGNUM_LENGTH(bignum) \
  80. ((* (BIGNUM_TO_POINTER (bignum))) & ((bignum_length_type) BIGNUM_DIGIT_MASK))
  81. #define BIGNUM_NEGATIVE_P(bignum) \
  82. (((* (BIGNUM_TO_POINTER (bignum))) & BIGNUM_RADIX) != 0)
  83. #define BIGNUM_ZERO_P(bignum) \
  84. ((BIGNUM_LENGTH (bignum)) == 0)
  85. #define BIGNUM_REF(bignum, index) \
  86. (* ((BIGNUM_START_PTR (bignum)) + (index)))
  87. #ifdef BIGNUM_FORCE_NEW_RESULTS
  88. #define BIGNUM_MAYBE_COPY bignum_copy
  89. #else
  90. #define BIGNUM_MAYBE_COPY(bignum) bignum
  91. #endif
  92. /* These definitions are here to facilitate caching of the constants
  93. 0, 1, and -1. */
  94. #define BIGNUM_ZERO() S48_ADDRESS_AFTER_HEADER(s48_deref(s48_bignum_zero), long)
  95. #define BIGNUM_ONE(neg_p) \
  96. (neg_p ? S48_ADDRESS_AFTER_HEADER(s48_deref(s48_bignum_neg_one), long) : \
  97. S48_ADDRESS_AFTER_HEADER(s48_deref(s48_bignum_pos_one), long))
  98. #define HD_LOW(digit) ((digit) & BIGNUM_HALF_DIGIT_MASK)
  99. #define HD_HIGH(digit) ((digit) >> BIGNUM_HALF_DIGIT_LENGTH)
  100. #define HD_CONS(high, low) (((high) << BIGNUM_HALF_DIGIT_LENGTH) | (low))
  101. #define BIGNUM_BITS_TO_DIGITS(n) \
  102. (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
  103. #define BIGNUM_DIGITS_FOR_LONG \
  104. (BIGNUM_BITS_TO_DIGITS ((sizeof (long)) * CHAR_BIT))
  105. #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
  106. #define BIGNUM_ASSERT(expression) \
  107. { \
  108. if (! (expression)) \
  109. BIGNUM_EXCEPTION (); \
  110. }
  111. #endif /* not BIGNUM_DISABLE_ASSERTION_CHECKS */