prbit.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef prbit_h___
  6. #define prbit_h___
  7. #include "prtypes.h"
  8. PR_BEGIN_EXTERN_C
  9. /*
  10. ** Replace compare/jump/add/shift sequence with compiler built-in/intrinsic
  11. ** functions.
  12. */
  13. #if defined(_WIN32) && (_MSC_VER >= 1300) && \
  14. (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_ARM))
  15. # include <intrin.h>
  16. # pragma intrinsic(_BitScanForward,_BitScanReverse)
  17. __forceinline static int __prBitScanForward32(unsigned int val)
  18. {
  19. unsigned long idx;
  20. _BitScanForward(&idx, (unsigned long)val);
  21. return( (int)idx );
  22. }
  23. __forceinline static int __prBitScanReverse32(unsigned int val)
  24. {
  25. unsigned long idx;
  26. _BitScanReverse(&idx, (unsigned long)val);
  27. return( (int)(31-idx) );
  28. }
  29. # define pr_bitscan_ctz32(val) __prBitScanForward32(val)
  30. # define pr_bitscan_clz32(val) __prBitScanReverse32(val)
  31. # define PR_HAVE_BUILTIN_BITSCAN32
  32. #elif ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) && \
  33. (defined(__i386__) || defined(__x86_64__) || defined(__arm__))
  34. # define pr_bitscan_ctz32(val) __builtin_ctz(val)
  35. # define pr_bitscan_clz32(val) __builtin_clz(val)
  36. # define PR_HAVE_BUILTIN_BITSCAN32
  37. #endif /* MSVC || GCC */
  38. /*
  39. ** A prbitmap_t is a long integer that can be used for bitmaps
  40. */
  41. typedef unsigned long prbitmap_t;
  42. #define PR_TEST_BIT(_map,_bit) \
  43. ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] & (1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  44. #define PR_SET_BIT(_map,_bit) \
  45. ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] |= (1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  46. #define PR_CLEAR_BIT(_map,_bit) \
  47. ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] &= ~(1L << ((_bit) & (PR_BITS_PER_LONG-1))))
  48. /*
  49. ** Compute the log of the least power of 2 greater than or equal to n
  50. */
  51. NSPR_API(PRIntn) PR_CeilingLog2(PRUint32 i);
  52. /*
  53. ** Compute the log of the greatest power of 2 less than or equal to n
  54. */
  55. NSPR_API(PRIntn) PR_FloorLog2(PRUint32 i);
  56. /*
  57. ** Macro version of PR_CeilingLog2: Compute the log of the least power of
  58. ** 2 greater than or equal to _n. The result is returned in _log2.
  59. */
  60. #ifdef PR_HAVE_BUILTIN_BITSCAN32
  61. #define PR_CEILING_LOG2(_log2,_n) \
  62. PR_BEGIN_MACRO \
  63. PRUint32 j_ = (PRUint32)(_n); \
  64. (_log2) = (j_ <= 1 ? 0 : 32 - pr_bitscan_clz32(j_ - 1)); \
  65. PR_END_MACRO
  66. #else
  67. #define PR_CEILING_LOG2(_log2,_n) \
  68. PR_BEGIN_MACRO \
  69. PRUint32 j_ = (PRUint32)(_n); \
  70. (_log2) = 0; \
  71. if ((j_) & ((j_)-1)) \
  72. (_log2) += 1; \
  73. if ((j_) >> 16) \
  74. (_log2) += 16, (j_) >>= 16; \
  75. if ((j_) >> 8) \
  76. (_log2) += 8, (j_) >>= 8; \
  77. if ((j_) >> 4) \
  78. (_log2) += 4, (j_) >>= 4; \
  79. if ((j_) >> 2) \
  80. (_log2) += 2, (j_) >>= 2; \
  81. if ((j_) >> 1) \
  82. (_log2) += 1; \
  83. PR_END_MACRO
  84. #endif /* PR_HAVE_BUILTIN_BITSCAN32 */
  85. /*
  86. ** Macro version of PR_FloorLog2: Compute the log of the greatest power of
  87. ** 2 less than or equal to _n. The result is returned in _log2.
  88. **
  89. ** This is equivalent to finding the highest set bit in the word.
  90. */
  91. #ifdef PR_HAVE_BUILTIN_BITSCAN32
  92. #define PR_FLOOR_LOG2(_log2,_n) \
  93. PR_BEGIN_MACRO \
  94. PRUint32 j_ = (PRUint32)(_n); \
  95. (_log2) = 31 - pr_bitscan_clz32((j_) | 1); \
  96. PR_END_MACRO
  97. #else
  98. #define PR_FLOOR_LOG2(_log2,_n) \
  99. PR_BEGIN_MACRO \
  100. PRUint32 j_ = (PRUint32)(_n); \
  101. (_log2) = 0; \
  102. if ((j_) >> 16) \
  103. (_log2) += 16, (j_) >>= 16; \
  104. if ((j_) >> 8) \
  105. (_log2) += 8, (j_) >>= 8; \
  106. if ((j_) >> 4) \
  107. (_log2) += 4, (j_) >>= 4; \
  108. if ((j_) >> 2) \
  109. (_log2) += 2, (j_) >>= 2; \
  110. if ((j_) >> 1) \
  111. (_log2) += 1; \
  112. PR_END_MACRO
  113. #endif /* PR_HAVE_BUILTIN_BITSCAN32 */
  114. /*
  115. ** Macros for rotate left and right. The argument 'a' must be an unsigned
  116. ** 32-bit integer type such as PRUint32.
  117. **
  118. ** There is no rotate operation in the C Language, so the construct
  119. ** (a << 4) | (a >> 28) is frequently used instead. Most compilers convert
  120. ** this to a rotate instruction, but MSVC doesn't without a little help.
  121. ** To get MSVC to generate a rotate instruction, we have to use the _rotl
  122. ** or _rotr intrinsic and use a pragma to make it inline.
  123. **
  124. ** Note: MSVC in VS2005 will do an inline rotate instruction on the above
  125. ** construct.
  126. */
  127. #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || \
  128. defined(_M_X64) || defined(_M_ARM))
  129. #include <stdlib.h>
  130. #pragma intrinsic(_rotl, _rotr)
  131. #define PR_ROTATE_LEFT32(a, bits) _rotl(a, bits)
  132. #define PR_ROTATE_RIGHT32(a, bits) _rotr(a, bits)
  133. #else
  134. #define PR_ROTATE_LEFT32(a, bits) (((a) << (bits)) | ((a) >> (32 - (bits))))
  135. #define PR_ROTATE_RIGHT32(a, bits) (((a) >> (bits)) | ((a) << (32 - (bits))))
  136. #endif
  137. PR_END_EXTERN_C
  138. #endif /* prbit_h___ */