bits.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_C_
  15. #define _BITS_C_
  16. #include "basics.h"
  17. INLINE_BITS\
  18. (unsigned64)
  19. LSMASKED64 (unsigned64 word,
  20. int start,
  21. int stop)
  22. {
  23. word &= LSMASK64 (start, stop);
  24. return word;
  25. }
  26. INLINE_BITS\
  27. (unsigned64)
  28. LSEXTRACTED64 (unsigned64 val,
  29. int start,
  30. int stop)
  31. {
  32. val <<= (64 - 1 - start); /* drop high bits */
  33. val >>= (64 - 1 - start) + (stop); /* drop low bits */
  34. return val;
  35. }
  36. INLINE_BITS\
  37. (unsigned32)
  38. MASKED32(unsigned32 word,
  39. unsigned start,
  40. unsigned stop)
  41. {
  42. return (word & MASK32(start, stop));
  43. }
  44. INLINE_BITS\
  45. (unsigned64)
  46. MASKED64(unsigned64 word,
  47. unsigned start,
  48. unsigned stop)
  49. {
  50. return (word & MASK64(start, stop));
  51. }
  52. INLINE_BITS\
  53. (unsigned_word)
  54. MASKED(unsigned_word word,
  55. unsigned start,
  56. unsigned stop)
  57. {
  58. return ((word) & MASK(start, stop));
  59. }
  60. INLINE_BITS\
  61. (unsigned_word)
  62. EXTRACTED(unsigned_word word,
  63. unsigned start,
  64. unsigned stop)
  65. {
  66. ASSERT(start <= stop);
  67. #if (WITH_TARGET_WORD_BITSIZE == 64)
  68. return _EXTRACTEDn(64, word, start, stop);
  69. #else
  70. if (stop < 32)
  71. return 0;
  72. else
  73. return ((word >> (63 - stop))
  74. & MASK(start+(63-stop), 63));
  75. #endif
  76. }
  77. INLINE_BITS\
  78. (unsigned_word)
  79. INSERTED(unsigned_word word,
  80. unsigned start,
  81. unsigned stop)
  82. {
  83. ASSERT(start <= stop);
  84. #if (WITH_TARGET_WORD_BITSIZE == 64)
  85. return _INSERTEDn(64, word, start, stop);
  86. #else
  87. if (stop < 32)
  88. return 0;
  89. else
  90. return ((word & MASK(start+(63-stop), 63))
  91. << (63 - stop));
  92. #endif
  93. }
  94. INLINE_BITS\
  95. (unsigned32)
  96. ROTL32(unsigned32 val,
  97. long shift)
  98. {
  99. ASSERT(shift >= 0 && shift <= 32);
  100. return _ROTLn(32, val, shift);
  101. }
  102. INLINE_BITS\
  103. (unsigned64)
  104. ROTL64(unsigned64 val,
  105. long shift)
  106. {
  107. ASSERT(shift >= 0 && shift <= 64);
  108. return _ROTLn(64, val, shift);
  109. }
  110. #endif /* _BITS_C_ */