prbit.h 5.2 KB

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