bits.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _BITS_H_
  15. #define _BITS_H_
  16. /* bit manipulation routines:
  17. Bit numbering: The bits are numbered according to the PowerPC
  18. convention - the left most (or most significant) is bit 0 while the
  19. right most (least significant) is bit 1.
  20. Size convention: Each macro is in three forms - <MACRO>32 which
  21. operates in 32bit quantity (bits are numbered 0..31); <MACRO>64
  22. which operates using 64bit quantites (and bits are numbered 0..64);
  23. and <MACRO> which operates using the bit size of the target
  24. architecture (bits are still numbered 0..63), with 32bit
  25. architectures ignoring the first 32bits having bit 32 as the most
  26. significant.
  27. BIT*(POS): Quantity with just 1 bit set.
  28. MASK*(FIRST, LAST): Create a constant bit mask of the specified
  29. size with bits [FIRST .. LAST] set.
  30. MASKED*(VALUE, FIRST, LAST): Masks out all but bits [FIRST
  31. .. LAST].
  32. LSMASKED*(VALUE, FIRST, LAST): Like MASKED - LS bit is zero.
  33. EXTRACTED*(VALUE, FIRST, LAST): Masks out bits [FIRST .. LAST] but
  34. also right shifts the masked value so that bit LAST becomes the
  35. least significant (right most).
  36. LSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - LS bit is
  37. zero.
  38. SHUFFLED**(VALUE, OLD, NEW): Mask then move a single bit from OLD
  39. new NEW.
  40. MOVED**(VALUE, OLD_FIRST, OLD_LAST, NEW_FIRST, NEW_LAST): Moves
  41. things around so that bits OLD_FIRST..OLD_LAST are masked then
  42. moved to NEW_FIRST..NEW_LAST.
  43. INSERTED*(VALUE, FIRST, LAST): Takes VALUE and `inserts' the (LAST
  44. - FIRST + 1) least significant bits into bit positions [ FIRST
  45. .. LAST ]. This is almost the complement to EXTRACTED.
  46. IEA_MASKED(SHOULD_MASK, ADDR): Convert the address to the targets
  47. natural size. If in 32bit mode, discard the high 32bits.
  48. EXTENDED(VALUE): Convert VALUE (32bits of it) to the targets
  49. natural size. If in 64bit mode, sign extend the value.
  50. ALIGN_*(VALUE): Round upwards the value so that it is aligned.
  51. FLOOR_*(VALUE): Truncate the value so that it is aligned.
  52. ROTL*(VALUE, NR_BITS): Return the value rotated by NR_BITS
  53. */
  54. #define _MAKE_SHIFT(WIDTH, pos) ((WIDTH) - 1 - (pos))
  55. #if (WITH_TARGET_WORD_MSB == 0)
  56. #define _LSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
  57. #else
  58. #define _LSB_POS(WIDTH, SHIFT) (SHIFT)
  59. #endif
  60. /* MakeBit */
  61. #define _BITn(WIDTH, pos) (((uint##WIDTH##_t)(1)) \
  62. << _MAKE_SHIFT(WIDTH, pos))
  63. #define BIT4(POS) (1 << _MAKE_SHIFT(4, POS))
  64. #define BIT5(POS) (1 << _MAKE_SHIFT(5, POS))
  65. #define BIT8(POS) (1 << _MAKE_SHIFT(8, POS))
  66. #define BIT10(POS) (1 << _MAKE_SHIFT(10, POS))
  67. #define BIT32(POS) _BITn(32, POS)
  68. #define BIT64(POS) _BITn(64, POS)
  69. #if (WITH_TARGET_WORD_BITSIZE == 64)
  70. #define BIT(POS) BIT64(POS)
  71. #else
  72. #define BIT(POS) (((POS) < 32) ? 0 : _BITn(32, (POS)-32))
  73. #endif
  74. /* multi bit mask */
  75. #define _MASKn(WIDTH, START, STOP) \
  76. (((((uint##WIDTH##_t)0) - 1) \
  77. >> (WIDTH - ((STOP) - (START) + 1))) \
  78. << (WIDTH - 1 - (STOP)))
  79. #define MASK32(START, STOP) _MASKn(32, START, STOP)
  80. #define MASK64(START, STOP) _MASKn(64, START, STOP)
  81. /* Multi-bit mask on least significant bits */
  82. #define _LSMASKn(WIDTH, FIRST, LAST) _MASKn (WIDTH, \
  83. _LSB_POS (WIDTH, FIRST), \
  84. _LSB_POS (WIDTH, LAST))
  85. #define LSMASK64(FIRST, LAST) _LSMASKn (64, (FIRST), (LAST))
  86. #if (WITH_TARGET_WORD_BITSIZE == 64)
  87. #define MASK(START, STOP) \
  88. (((START) <= (STOP)) \
  89. ? _MASKn(64, START, STOP) \
  90. : (_MASKn(64, 0, STOP) \
  91. | _MASKn(64, START, 63)))
  92. #else
  93. #define MASK(START, STOP) \
  94. (((START) <= (STOP)) \
  95. ? (((STOP) < 32) \
  96. ? 0 \
  97. : _MASKn(32, \
  98. (START) < 32 ? 0 : (START) - 32, \
  99. (STOP)-32)) \
  100. : (_MASKn(32, \
  101. (START) < 32 ? 0 : (START) - 32, \
  102. 31) \
  103. | (((STOP) < 32) \
  104. ? 0 \
  105. : _MASKn(32, \
  106. 0, \
  107. (STOP) - 32))))
  108. #endif
  109. /* mask the required bits, leaving them in place */
  110. INLINE_BITS\
  111. (uint32_t) MASKED32
  112. (uint32_t word,
  113. unsigned start,
  114. unsigned stop);
  115. INLINE_BITS\
  116. (uint64_t) MASKED64
  117. (uint64_t word,
  118. unsigned start,
  119. unsigned stop);
  120. INLINE_BITS\
  121. (unsigned_word) MASKED
  122. (unsigned_word word,
  123. unsigned start,
  124. unsigned stop);
  125. INLINE_BITS\
  126. (uint64_t) LSMASKED64
  127. (uint64_t word,
  128. int first,
  129. int last);
  130. /* extract the required bits aligning them with the lsb */
  131. #define _EXTRACTEDn(WIDTH, WORD, START, STOP) \
  132. ((((uint##WIDTH##_t)(WORD)) >> (WIDTH - (STOP) - 1)) \
  133. & _MASKn(WIDTH, WIDTH-1+(START)-(STOP), WIDTH-1))
  134. /* #define EXTRACTED10(WORD, START, STOP) _EXTRACTEDn(10, WORD, START, STOP) */
  135. #define EXTRACTED32(WORD, START, STOP) _EXTRACTEDn(32, WORD, START, STOP)
  136. #define EXTRACTED64(WORD, START, STOP) _EXTRACTEDn(64, WORD, START, STOP)
  137. INLINE_BITS\
  138. (unsigned_word) EXTRACTED
  139. (unsigned_word val,
  140. unsigned start,
  141. unsigned stop);
  142. INLINE_BITS\
  143. (uint64_t) LSEXTRACTED64
  144. (uint64_t val,
  145. int start,
  146. int stop);
  147. /* move a single bit around */
  148. /* NB: the wierdness (N>O?N-O:0) is to stop a warning from GCC */
  149. #define _SHUFFLEDn(N, WORD, OLD, NEW) \
  150. ((OLD) < (NEW) \
  151. ? (((uint##N##_t)(WORD) \
  152. >> (((NEW) > (OLD)) ? ((NEW) - (OLD)) : 0)) \
  153. & MASK32((NEW), (NEW))) \
  154. : (((uint##N##_t)(WORD) \
  155. << (((OLD) > (NEW)) ? ((OLD) - (NEW)) : 0)) \
  156. & MASK32((NEW), (NEW))))
  157. #define SHUFFLED32(WORD, OLD, NEW) _SHUFFLEDn(32, WORD, OLD, NEW)
  158. #define SHUFFLED64(WORD, OLD, NEW) _SHUFFLEDn(64, WORD, OLD, NEW)
  159. #define SHUFFLED(WORD, OLD, NEW) _SHUFFLEDn(_word, WORD, OLD, NEW)
  160. /* move a group of bits around */
  161. #define _INSERTEDn(N, WORD, START, STOP) \
  162. (((uint##N##_t)(WORD) << _MAKE_SHIFT(N, STOP)) & _MASKn(N, START, STOP))
  163. #define INSERTED32(WORD, START, STOP) _INSERTEDn(32, WORD, START, STOP)
  164. #define INSERTED64(WORD, START, STOP) _INSERTEDn(64, WORD, START, STOP)
  165. INLINE_BITS\
  166. (unsigned_word) INSERTED
  167. (unsigned_word val,
  168. unsigned start,
  169. unsigned stop);
  170. /* depending on MODE return a 64bit or 32bit (sign extended) value */
  171. #if (WITH_TARGET_WORD_BITSIZE == 64)
  172. #define EXTENDED(X) ((int64_t)(int32_t)(X))
  173. #else
  174. #define EXTENDED(X) (X)
  175. #endif
  176. /* memory alignment macro's */
  177. #define _ALIGNa(A,X) (((X) + ((A) - 1)) & ~((A) - 1))
  178. #define _FLOORa(A,X) ((X) & ~((A) - 1))
  179. #define ALIGN_8(X) _ALIGNa(8, X)
  180. #define ALIGN_16(X) _ALIGNa(16, X)
  181. #define ALIGN_PAGE(X) _ALIGNa(0x1000, X)
  182. #define FLOOR_PAGE(X) ((X) & ~(0x1000 - 1))
  183. /* bit bliting macro's */
  184. #define BLIT32(V, POS, BIT) \
  185. do { \
  186. if (BIT) \
  187. V |= BIT32(POS); \
  188. else \
  189. V &= ~BIT32(POS); \
  190. } while (0)
  191. #define MBLIT32(V, LO, HI, VAL) \
  192. do { \
  193. (V) = (((V) & ~MASK32((LO), (HI))) \
  194. | INSERTED32(VAL, LO, HI)); \
  195. } while (0)
  196. /* some rotate functions to make things easier
  197. NOTE: These are functions not macro's as the latter tickles bugs in
  198. gcc-2.6.3 */
  199. #define _ROTLn(N, VAL, SHIFT) \
  200. (((VAL) << (SHIFT)) | ((VAL) >> ((N)-(SHIFT))))
  201. INLINE_BITS\
  202. (uint32_t) ROTL32
  203. (uint32_t val,
  204. long shift);
  205. INLINE_BITS\
  206. (uint64_t) ROTL64
  207. (uint64_t val,
  208. long shift);
  209. #if (BITS_INLINE & INCLUDE_MODULE)
  210. #include "bits.c"
  211. #endif
  212. #endif /* _BITS_H_ */