bignumint.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* -*-C-*-
  2. $Id: bignmint.h,v 1.4 1993/10/27 23:57:07 gjr Exp $
  3. Copyright (c) 1989-1992 Massachusetts Institute of Technology
  4. This material was developed by the Scheme project at the Massachusetts
  5. Institute of Technology, Department of Electrical Engineering and
  6. Computer Science. Permission to copy and modify this software, to
  7. redistribute either the original software or a modified version, and
  8. to use this software for any purpose is granted, subject to the
  9. following restrictions and understandings.
  10. 1. Any copy made of this software must include this copyright notice
  11. in full.
  12. 2. Users of this software agree to make their best efforts (a) to
  13. return to the MIT Scheme project any improvements or extensions that
  14. they make, so that these may be included in future releases; and (b)
  15. to inform MIT of noteworthy uses of this software.
  16. 3. All materials developed as a consequence of the use of this
  17. software shall duly acknowledge such use, in accordance with the usual
  18. standards of acknowledging credit in academic research.
  19. 4. MIT has made no warrantee or representation that the operation of
  20. this software will be error-free, and MIT is under no obligation to
  21. provide any services, by way of maintenance, update, or otherwise.
  22. 5. In conjunction with products arising from the use of this material,
  23. there shall be no use of the name of the Massachusetts Institute of
  24. Technology nor of any adaptation thereof in any advertising,
  25. promotional, or sales literature without prior written consent from
  26. MIT in each case. */
  27. /* Internal Interface to Bignum Code */
  28. #undef BIGNUM_ZERO_P
  29. #undef BIGNUM_NEGATIVE_P
  30. /* The memory model is based on the following definitions, and on the
  31. definition of the type `bignum_type'. The only other special
  32. definition is `CHAR_BIT', which is defined in the Ansi C header
  33. file "limits.h". */
  34. typedef long bignum_digit_type;
  35. typedef long bignum_length_type;
  36. /* BIGNUM_ALLOCATE allocates a (length + 1)-element array of
  37. `bignum_digit_type'; deallocation is the responsibility of the
  38. user (in Scheme, the garbage collector handles this). */
  39. #define BIGNUM_ALLOCATE_TAGGED(length_in_digits) \
  40. ((long *) s48_allocate_bignum((length_in_digits + 1) * \
  41. sizeof(bignum_digit_type)))
  42. #define BIGNUM_ALLOCATE(length_in_digits) \
  43. (S48_ADDRESS_AFTER_HEADER(((char *) \
  44. BIGNUM_ALLOCATE_TAGGED((length_in_digits))), \
  45. long))
  46. extern char * s48_allocate_bignum(long size);
  47. /* BIGNUM_TO_POINTER casts a bignum object to a digit array pointer. */
  48. #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type *) (bignum))
  49. /* BIGNUM_REDUCE_LENGTH allows the memory system to reclaim some
  50. space when a bignum's length is reduced from its original value. */
  51. #define BIGNUM_REDUCE_LENGTH(target, source, length) \
  52. target = (long *) s48_shorten_bignum((char *) source, length)
  53. extern char * s48_shorten_bignum(char*, long);
  54. /* BIGNUM_DEALLOCATE is called when disposing of bignums which are
  55. created as intermediate temporaries; Scheme doesn't need this. */
  56. #define BIGNUM_DEALLOCATE(bignum)
  57. /* If BIGNUM_FORCE_NEW_RESULTS is defined, all bignum-valued operations
  58. return freshly-allocated results. This is useful for some kinds of
  59. memory deallocation strategies. */
  60. /* #define BIGNUM_FORCE_NEW_RESULTS */
  61. /* BIGNUM_EXCEPTION is invoked to handle assertion violations. */
  62. #define BIGNUM_EXCEPTION abort
  63. #define BIGNUM_DIGIT_LENGTH (((sizeof (bignum_digit_type)) * CHAR_BIT) - 2)
  64. #define BIGNUM_HALF_DIGIT_LENGTH (BIGNUM_DIGIT_LENGTH / 2)
  65. #define BIGNUM_RADIX (((unsigned long) 1) << BIGNUM_DIGIT_LENGTH)
  66. #define BIGNUM_RADIX_ROOT (((unsigned long) 1) << BIGNUM_HALF_DIGIT_LENGTH)
  67. #define BIGNUM_DIGIT_MASK (BIGNUM_RADIX - 1)
  68. #define BIGNUM_HALF_DIGIT_MASK (BIGNUM_RADIX_ROOT - 1)
  69. #define BIGNUM_START_PTR(bignum) \
  70. ((BIGNUM_TO_POINTER (bignum)) + 1)
  71. #define BIGNUM_SET_HEADER(bignum, length, negative_p) \
  72. (* (BIGNUM_TO_POINTER (bignum))) = \
  73. ((length) | ((negative_p) ? BIGNUM_RADIX : 0))
  74. #define BIGNUM_LENGTH(bignum) \
  75. ((* (BIGNUM_TO_POINTER (bignum))) & ((bignum_length_type) BIGNUM_DIGIT_MASK))
  76. #define BIGNUM_NEGATIVE_P(bignum) \
  77. (((* (BIGNUM_TO_POINTER (bignum))) & BIGNUM_RADIX) != 0)
  78. #define BIGNUM_ZERO_P(bignum) \
  79. ((BIGNUM_LENGTH (bignum)) == 0)
  80. #define BIGNUM_REF(bignum, index) \
  81. (* ((BIGNUM_START_PTR (bignum)) + (index)))
  82. #ifdef BIGNUM_FORCE_NEW_RESULTS
  83. #define BIGNUM_MAYBE_COPY bignum_copy
  84. #else
  85. #define BIGNUM_MAYBE_COPY(bignum) bignum
  86. #endif
  87. /* These definitions are here to facilitate caching of the constants
  88. 0, 1, and -1. */
  89. #define BIGNUM_ZERO() S48_ADDRESS_AFTER_HEADER(s48_bignum_zero, long)
  90. #define BIGNUM_ONE(neg_p) \
  91. (neg_p ? S48_ADDRESS_AFTER_HEADER(s48_bignum_neg_one, long) : \
  92. S48_ADDRESS_AFTER_HEADER(s48_bignum_pos_one, long))
  93. #define HD_LOW(digit) ((digit) & BIGNUM_HALF_DIGIT_MASK)
  94. #define HD_HIGH(digit) ((digit) >> BIGNUM_HALF_DIGIT_LENGTH)
  95. #define HD_CONS(high, low) (((high) << BIGNUM_HALF_DIGIT_LENGTH) | (low))
  96. #define BIGNUM_BITS_TO_DIGITS(n) \
  97. (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
  98. #define BIGNUM_DIGITS_FOR_LONG \
  99. (BIGNUM_BITS_TO_DIGITS ((sizeof (long)) * CHAR_BIT))
  100. #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
  101. #define BIGNUM_ASSERT(expression) \
  102. { \
  103. if (! (expression)) \
  104. BIGNUM_EXCEPTION (); \
  105. }
  106. #endif /* not BIGNUM_DISABLE_ASSERTION_CHECKS */