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 / bitstr.h < prev    next >
C/C++ Source or Header  |  2000-12-05  |  5KB  |  159 lines

  1. /* -*-C-*-
  2.  
  3. $Id: bitstr.h,v 1.10 2000/12/05 21:23:43 cph Exp $
  4.  
  5. Copyright (c) 1987-2000 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. /* Bit string macros.  "Little indian" version. */
  23.  
  24. #define BIT_STRING_LENGTH_OFFSET    1
  25. #define BIT_STRING_FIRST_WORD        2
  26.  
  27. #define BIT_STRING_LENGTH_TO_GC_LENGTH(bits)                \
  28.   (((bits) + (OBJECT_LENGTH - 1)) / OBJECT_LENGTH)
  29.  
  30. #define LOW_MASK(nbits) ((1L << (nbits)) - 1)
  31. #define ANY_MASK(nbits, offset) ((LOW_MASK (nbits)) << (offset))
  32.  
  33. #define BIT_STRING_LENGTH(bit_string)                    \
  34.   ((long) (FAST_MEMORY_REF ((bit_string), BIT_STRING_LENGTH_OFFSET)))
  35.  
  36. #define BIT_STRING_MSW(bit_string)                    \
  37.   (BIT_STRING_WORD (BIT_STRING_HIGH_PTR (bit_string)))
  38.  
  39. #define BIT_STRING_LSW(bit_string)                    \
  40.   (BIT_STRING_WORD                            \
  41.    (MEMORY_LOC                                \
  42.     ((bit_string), (BIT_STRING_INDEX_TO_WORD ((bit_string), 0)))))
  43.  
  44. /* Byte order dependencies. */
  45.  
  46. #ifndef WORDS_BIGENDIAN
  47.  
  48. /*
  49.  
  50. Memory layout of bit strings:
  51.  
  52. +-------+-------+-------+-------+
  53. |  NMV    |  GC size (longwords)    | 0
  54. +-------+-------+-------+-------+
  55. |       Size in bits        | 1
  56. +-------+-------+-------+-------+
  57. |                 LSB| 2
  58. +-------+-------+-------+-------+
  59. |                | 3
  60. +-------+-------+-------+-------+
  61. .                . .
  62. .                . .
  63. .                . .
  64. +-------+-------+-------+-------+
  65. |MSB                    | N
  66. +-------+-------+-------+-------+
  67.  
  68. The last data word (marked as word "N" above) is where any excess
  69. bits are kept.
  70.  
  71. The "size in bits" is a C "long" integer.
  72.  
  73. */
  74.  
  75. #define BIT_STRING_HIGH_PTR(bit_string)                    \
  76.   (MEMORY_LOC ((bit_string), ((VECTOR_LENGTH (bit_string)) + 1)))
  77.  
  78. #define BIT_STRING_LOW_PTR(bit_string)                    \
  79.   (MEMORY_LOC ((bit_string), BIT_STRING_FIRST_WORD))
  80.  
  81. #define BIT_STRING_WORD(ptr)        (*((ptr) - 1))
  82. #define DEC_BIT_STRING_PTR(ptr)        (--(ptr))
  83. #define INC_BIT_STRING_PTR(ptr)        ((ptr)++)
  84.  
  85. /* This is off by one so BIT_STRING_WORD will get the correct word. */
  86.  
  87. #define BIT_STRING_INDEX_TO_WORD(bit_string, index)            \
  88.   ((BIT_STRING_FIRST_WORD + 1) + ((index) / OBJECT_LENGTH))
  89.  
  90. #define BIT_STRING_INDEX_PAIR_TO_INDEX(string, word, bit)        \
  91.   ((((word) - (BIT_STRING_FIRST_WORD + 1)) * OBJECT_LENGTH) + (bit))
  92.  
  93. #define READ_BITS_PTR(object, offset, end)                \
  94.   (MEMORY_LOC                                \
  95.    ((object), (BIT_STRING_LENGTH_TO_GC_LENGTH (((offset) + (end)) - 1))))
  96.  
  97. #define COMPUTE_READ_BITS_OFFSET(offset, end)                \
  98. {                                    \
  99.   offset = ((offset + end) % OBJECT_LENGTH);                \
  100.   if (offset != 0)                            \
  101.     offset = (OBJECT_LENGTH - offset);                    \
  102. }
  103.  
  104. #else /* WORDS_BIGENDIAN */
  105.  
  106. /*
  107.  
  108. Memory layout of bit strings:
  109.  
  110. +-------+-------+-------+-------+
  111. |  NMV    |  GC size (longwords)    | 0
  112. +-------+-------+-------+-------+
  113. |       Size in bits        | 1
  114. +-------+-------+-------+-------+
  115. |MSB                | 2
  116. +-------+-------+-------+-------+
  117. |                | 3
  118. +-------+-------+-------+-------+
  119. .                . .
  120. .                . .
  121. .                . .
  122. +-------+-------+-------+-------+
  123. |                 LSB| N
  124. +-------+-------+-------+-------+
  125.  
  126. The first data word (marked as word "2" above) is where any excess
  127. bits are kept.
  128.  
  129. The "size in bits" is a C "long" integer.
  130. */
  131.  
  132. #define BIT_STRING_HIGH_PTR(bit_string)                    \
  133.   (MEMORY_LOC ((bit_string), BIT_STRING_FIRST_WORD))
  134.  
  135. #define BIT_STRING_LOW_PTR(bit_string)                    \
  136.   (MEMORY_LOC ((bit_string), ((VECTOR_LENGTH (bit_string)) + 1)))
  137.  
  138. #define BIT_STRING_WORD(ptr)        (*(ptr))
  139. #define DEC_BIT_STRING_PTR(ptr)        ((ptr)++)
  140. #define INC_BIT_STRING_PTR(ptr)        (--(ptr))
  141.  
  142. /* This is especially clever.  To understand it, note that the index
  143.    of the last pointer of a vector is also the GC length of the
  144.    vector, so that all we need do is subtract the zero-based word
  145.    index from the GC length. */
  146. #define BIT_STRING_INDEX_TO_WORD(bit_string, index)            \
  147.   ((VECTOR_LENGTH (bit_string)) - ((index) / OBJECT_LENGTH))
  148.  
  149. #define BIT_STRING_INDEX_PAIR_TO_INDEX(string, word, bit)        \
  150.   ((((VECTOR_LENGTH (string)) - (word)) * OBJECT_LENGTH) + (bit))
  151.  
  152. #define READ_BITS_PTR(object, offset, end)                \
  153.   (MEMORY_LOC ((object), ((offset) / OBJECT_LENGTH)))
  154.  
  155. #define COMPUTE_READ_BITS_OFFSET(offset, end)                \
  156.   (offset) = ((offset) % OBJECT_LENGTH);
  157.  
  158. #endif /* WORDS_BIGENDIAN */
  159.