bignumint.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /* Internal Interface to Bignum Code */
  30. #undef BIGNUM_ZERO_P
  31. #undef BIGNUM_NEGATIVE_P
  32. /* The memory model is based on the following definitions, and on the
  33. definition of the type `bignum_type'. The only other special
  34. definition is `CHAR_BIT', which is defined in the Ansi C header
  35. file "limits.h". */
  36. typedef long bignum_digit_type;
  37. typedef long bignum_length_type;
  38. /* BIGNUM_ALLOCATE allocates a (length + 1)-element array of
  39. `bignum_digit_type'; deallocation is the responsibility of the
  40. user (in Scheme, the garbage collector handles this). */
  41. #define BIGNUM_ALLOCATE_TAGGED(length_in_digits) \
  42. ((long *) s48_allocate_bignum((length_in_digits + 1) * \
  43. sizeof(bignum_digit_type)))
  44. #define BIGNUM_ALLOCATE(length_in_digits) \
  45. (S48_ADDRESS_AFTER_HEADER(((char *) \
  46. BIGNUM_ALLOCATE_TAGGED((length_in_digits))), \
  47. long))
  48. extern char * s48_allocate_bignum(long size);
  49. /* BIGNUM_TO_POINTER casts a bignum object to a digit array pointer. */
  50. #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type *) (bignum))
  51. /* BIGNUM_REDUCE_LENGTH allows the memory system to reclaim some
  52. space when a bignum's length is reduced from its original value. */
  53. #define BIGNUM_REDUCE_LENGTH(target, source, length) \
  54. target = (long *) s48_shorten_bignum((char *) source, length)
  55. extern char * s48_shorten_bignum(char*, long);
  56. /* BIGNUM_DEALLOCATE is called when disposing of bignums which are
  57. created as intermediate temporaries; Scheme doesn't need this. */
  58. #define BIGNUM_DEALLOCATE(bignum)
  59. /* If BIGNUM_FORCE_NEW_RESULTS is defined, all bignum-valued operations
  60. return freshly-allocated results. This is useful for some kinds of
  61. memory deallocation strategies. */
  62. /* #define BIGNUM_FORCE_NEW_RESULTS */
  63. /* BIGNUM_EXCEPTION is invoked to handle assertion violations. */
  64. #define BIGNUM_EXCEPTION abort
  65. #define BIGNUM_DIGIT_LENGTH (((sizeof (bignum_digit_type)) * CHAR_BIT) - 2)
  66. #define BIGNUM_HALF_DIGIT_LENGTH (BIGNUM_DIGIT_LENGTH / 2)
  67. #define BIGNUM_RADIX (((unsigned long) 1) << BIGNUM_DIGIT_LENGTH)
  68. #define BIGNUM_RADIX_ROOT (((unsigned long) 1) << BIGNUM_HALF_DIGIT_LENGTH)
  69. #define BIGNUM_DIGIT_MASK (BIGNUM_RADIX - 1)
  70. #define BIGNUM_HALF_DIGIT_MASK (BIGNUM_RADIX_ROOT - 1)
  71. #define BIGNUM_START_PTR(bignum) \
  72. ((BIGNUM_TO_POINTER (bignum)) + 1)
  73. #define BIGNUM_SET_HEADER(bignum, length, negative_p) \
  74. (* (BIGNUM_TO_POINTER (bignum))) = \
  75. ((length) | ((negative_p) ? BIGNUM_RADIX : 0))
  76. #define BIGNUM_LENGTH(bignum) \
  77. ((* (BIGNUM_TO_POINTER (bignum))) & ((bignum_length_type) BIGNUM_DIGIT_MASK))
  78. #define BIGNUM_NEGATIVE_P(bignum) \
  79. (((* (BIGNUM_TO_POINTER (bignum))) & BIGNUM_RADIX) != 0)
  80. #define BIGNUM_ZERO_P(bignum) \
  81. ((BIGNUM_LENGTH (bignum)) == 0)
  82. #define BIGNUM_REF(bignum, index) \
  83. (* ((BIGNUM_START_PTR (bignum)) + (index)))
  84. #ifdef BIGNUM_FORCE_NEW_RESULTS
  85. #define BIGNUM_MAYBE_COPY bignum_copy
  86. #else
  87. #define BIGNUM_MAYBE_COPY(bignum) bignum
  88. #endif
  89. /* These definitions are here to facilitate caching of the constants
  90. 0, 1, and -1. */
  91. #define BIGNUM_ZERO() S48_ADDRESS_AFTER_HEADER(s48_bignum_zero, long)
  92. #define BIGNUM_ONE(neg_p) \
  93. (neg_p ? S48_ADDRESS_AFTER_HEADER(s48_bignum_neg_one, long) : \
  94. S48_ADDRESS_AFTER_HEADER(s48_bignum_pos_one, long))
  95. #define HD_LOW(digit) ((digit) & BIGNUM_HALF_DIGIT_MASK)
  96. #define HD_HIGH(digit) ((digit) >> BIGNUM_HALF_DIGIT_LENGTH)
  97. #define HD_CONS(high, low) (((high) << BIGNUM_HALF_DIGIT_LENGTH) | (low))
  98. #define BIGNUM_BITS_TO_DIGITS(n) \
  99. (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
  100. #define BIGNUM_DIGITS_FOR_LONG \
  101. (BIGNUM_BITS_TO_DIGITS ((sizeof (long)) * CHAR_BIT))
  102. #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
  103. #define BIGNUM_ASSERT(expression) \
  104. { \
  105. if (! (expression)) \
  106. BIGNUM_EXCEPTION (); \
  107. }
  108. #endif /* not BIGNUM_DISABLE_ASSERTION_CHECKS */