home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / bignmint.h < prev    next >
C/C++ Source or Header  |  1999-01-02  |  5KB  |  144 lines

  1. /* -*-C-*-
  2.  
  3. $Id: bignmint.h,v 1.6 1999/01/02 06:11:34 cph Exp $
  4.  
  5. Copyright (c) 1989-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. /* Internal Interface to Bignum Code */
  23.  
  24. #undef BIGNUM_ZERO_P
  25. #undef BIGNUM_NEGATIVE_P
  26.  
  27. /* The memory model is based on the following definitions, and on the
  28.    definition of the type `bignum_type'.  The only other special
  29.    definition is `CHAR_BIT', which is defined in the Ansi C header
  30.    file "limits.h". */
  31.  
  32. typedef long bignum_digit_type;
  33. typedef long bignum_length_type;
  34.  
  35. #ifdef MIT_SCHEME
  36.  
  37. #include "prims.h"
  38.  
  39. /* BIGNUM_ALLOCATE allocates a (length + 1)-element array of
  40.    `bignum_digit_type'; deallocation is the responsibility of the
  41.    user (in Scheme, the garbage collector handles this). */
  42. #define BIGNUM_ALLOCATE(length)                        \
  43.   (allocate_non_marked_vector                        \
  44.    (TC_BIG_FIXNUM, (BIGNUM_LENGTH_TO_GC_LENGTH (length)), 1))
  45.  
  46. /* BIGNUM_TO_POINTER casts a bignum object to a digit array pointer. */
  47. #define BIGNUM_TO_POINTER(bignum)                    \
  48.   ((bignum_digit_type *) (VECTOR_LOC ((bignum), 0)))
  49.  
  50. /* BIGNUM_REDUCE_LENGTH allows the memory system to reclaim some
  51.    space when a bignum's length is reduced from its original value. */
  52. #define BIGNUM_REDUCE_LENGTH(target, source, length)            \
  53. {                                    \
  54.   SET_VECTOR_LENGTH ((source), (BIGNUM_LENGTH_TO_GC_LENGTH (length)));    \
  55.   (target) = (source);                            \
  56. }
  57.  
  58. #define BIGNUM_LENGTH_TO_GC_LENGTH(length)                \
  59.   (BYTES_TO_WORDS (((length) + 1) * (sizeof (bignum_digit_type))))
  60.  
  61. /* BIGNUM_DEALLOCATE is called when disposing of bignums which are
  62.    created as intermediate temporaries; Scheme doesn't need this. */
  63. #define BIGNUM_DEALLOCATE(bignum)
  64.  
  65. /* If BIGNUM_FORCE_NEW_RESULTS is defined, all bignum-valued operations
  66.    return freshly-allocated results.  This is useful for some kinds of
  67.    memory deallocation strategies. */
  68. /* #define BIGNUM_FORCE_NEW_RESULTS */
  69.  
  70. /* BIGNUM_EXCEPTION is invoked to handle assertion violations. */
  71. #define BIGNUM_EXCEPTION error_external_return
  72.  
  73. #else /* not MIT_SCHEME */
  74.  
  75. #define BIGNUM_ALLOCATE bignum_malloc
  76. #define BIGNUM_TO_POINTER(bignum) ((bignum_digit_type *) (bignum))
  77. #define BIGNUM_REDUCE_LENGTH(target, source, length)            \
  78.   (target) = (bignum_realloc ((source), (length)))
  79. #define BIGNUM_DEALLOCATE free
  80. #define BIGNUM_FORCE_NEW_RESULTS
  81. #define BIGNUM_EXCEPTION abort
  82. #define fast register
  83. extern void free ();
  84. extern void abort ();
  85.  
  86. #endif /* MIT_SCHEME */
  87.  
  88. #define BIGNUM_DIGIT_LENGTH (((sizeof (bignum_digit_type)) * CHAR_BIT) - 2)
  89. #define BIGNUM_HALF_DIGIT_LENGTH (BIGNUM_DIGIT_LENGTH / 2)
  90. #define BIGNUM_RADIX (((unsigned long) 1) << BIGNUM_DIGIT_LENGTH)
  91. #define BIGNUM_RADIX_ROOT (((unsigned long) 1) << BIGNUM_HALF_DIGIT_LENGTH)
  92. #define BIGNUM_DIGIT_MASK     (BIGNUM_RADIX - 1)
  93. #define BIGNUM_HALF_DIGIT_MASK     (BIGNUM_RADIX_ROOT - 1)
  94.  
  95. #define BIGNUM_START_PTR(bignum)                    \
  96.   ((BIGNUM_TO_POINTER (bignum)) + 1)
  97.  
  98. #define BIGNUM_SET_HEADER(bignum, length, negative_p)            \
  99.   (* (BIGNUM_TO_POINTER (bignum))) =                    \
  100.     ((length) | ((negative_p) ? BIGNUM_RADIX : 0))
  101.  
  102. #define BIGNUM_LENGTH(bignum)                        \
  103.   ((* (BIGNUM_TO_POINTER (bignum))) & BIGNUM_DIGIT_MASK)
  104.  
  105. #define BIGNUM_NEGATIVE_P(bignum)                    \
  106.   (((* (BIGNUM_TO_POINTER (bignum))) & BIGNUM_RADIX) != 0)
  107.  
  108. #define BIGNUM_ZERO_P(bignum)                        \
  109.   ((BIGNUM_LENGTH (bignum)) == 0)
  110.  
  111. #define BIGNUM_REF(bignum, index)                    \
  112.   (* ((BIGNUM_START_PTR (bignum)) + (index)))
  113.  
  114. #ifdef BIGNUM_FORCE_NEW_RESULTS
  115. #define BIGNUM_MAYBE_COPY bignum_copy
  116. #else
  117. #define BIGNUM_MAYBE_COPY(bignum) bignum
  118. #endif
  119.  
  120. /* These definitions are here to facilitate caching of the constants
  121.    0, 1, and -1. */
  122. #define BIGNUM_ZERO bignum_make_zero
  123. #define BIGNUM_ONE bignum_make_one
  124.  
  125. #define HD_LOW(digit) ((digit) & BIGNUM_HALF_DIGIT_MASK)
  126. #define HD_HIGH(digit) ((digit) >> BIGNUM_HALF_DIGIT_LENGTH)
  127. #define HD_CONS(high, low) (((high) << BIGNUM_HALF_DIGIT_LENGTH) | (low))
  128.  
  129. #define BIGNUM_BITS_TO_DIGITS(n)                    \
  130.   (((n) + (BIGNUM_DIGIT_LENGTH - 1)) / BIGNUM_DIGIT_LENGTH)
  131.  
  132. #define BIGNUM_DIGITS_FOR_LONG                        \
  133.   (BIGNUM_BITS_TO_DIGITS ((sizeof (long)) * CHAR_BIT))
  134.  
  135. #ifndef BIGNUM_DISABLE_ASSERTION_CHECKS
  136.  
  137. #define BIGNUM_ASSERT(expression)                    \
  138. {                                    \
  139.   if (! (expression))                            \
  140.     BIGNUM_EXCEPTION ();                        \
  141. }
  142.  
  143. #endif /* not BIGNUM_DISABLE_ASSERTION_CHECKS */
  144.