| OLD | NEW |
| 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 /* ***** BEGIN LICENSE BLOCK ***** | 2 /* ***** BEGIN LICENSE BLOCK ***** |
| 3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 4 * | 4 * |
| 5 * The contents of this file are subject to the Mozilla Public License Version | 5 * The contents of this file are subject to the Mozilla Public License Version |
| 6 * 1.1 (the "License"); you may not use this file except in compliance with | 6 * 1.1 (the "License"); you may not use this file except in compliance with |
| 7 * the License. You may obtain a copy of the License at | 7 * the License. You may obtain a copy of the License at |
| 8 * http://www.mozilla.org/MPL/ | 8 * http://www.mozilla.org/MPL/ |
| 9 * | 9 * |
| 10 * Software distributed under the License is distributed on an "AS IS" basis, | 10 * Software distributed under the License is distributed on an "AS IS" basis, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 * the terms of any one of the MPL, the GPL or the LGPL. | 34 * the terms of any one of the MPL, the GPL or the LGPL. |
| 35 * | 35 * |
| 36 * ***** END LICENSE BLOCK ***** */ | 36 * ***** END LICENSE BLOCK ***** */ |
| 37 | 37 |
| 38 #ifndef prbit_h___ | 38 #ifndef prbit_h___ |
| 39 #define prbit_h___ | 39 #define prbit_h___ |
| 40 | 40 |
| 41 #include "prtypes.h" | 41 #include "prtypes.h" |
| 42 PR_BEGIN_EXTERN_C | 42 PR_BEGIN_EXTERN_C |
| 43 | 43 |
| 44 /* replace compare/jump/add/shift sequence with x86 BSF/BSR instruction */ |
| 45 #if defined(_WIN32) && (_MSC_VER >= 1300) && (defined(_M_IX86) || defined(_M_AMD
64)) |
| 46 unsigned char _BitScanForward(unsigned long * Index, unsigned long Mask); |
| 47 unsigned char _BitScanReverse(unsigned long * Index, unsigned long Mask); |
| 48 # pragma intrinsic(_BitScanForward,_BitScanReverse) |
| 49 __forceinline static int __prBitScanForward32(unsigned int val) |
| 50 { |
| 51 unsigned long idx; |
| 52 _BitScanForward(&idx, (unsigned long)val); |
| 53 return( (int)idx ); |
| 54 } |
| 55 __forceinline static int __prBitScanReverse32(unsigned int val) |
| 56 { |
| 57 unsigned long idx; |
| 58 _BitScanReverse(&idx, (unsigned long)val); |
| 59 return( (int)(31-idx) ); |
| 60 } |
| 61 # define pr_bitscan_ctz32(val) __prBitScanForward32(val) |
| 62 # define pr_bitscan_clz32(val) __prBitScanReverse32(val) |
| 63 # define PR_HAVE_BUILTIN_BITSCAN32 |
| 64 #elif ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) && \ |
| 65 (defined(__i386__) || defined(__x86_64__) || defined(__arm__)) |
| 66 # define pr_bitscan_ctz32(val) __builtin_ctz(val) |
| 67 # define pr_bitscan_clz32(val) __builtin_clz(val) |
| 68 # define PR_HAVE_BUILTIN_BITSCAN32 |
| 69 #endif /* MSVC || GCC */ |
| 70 |
| 44 /* | 71 /* |
| 45 ** A prbitmap_t is a long integer that can be used for bitmaps | 72 ** A prbitmap_t is a long integer that can be used for bitmaps |
| 46 */ | 73 */ |
| 47 typedef unsigned long prbitmap_t; | 74 typedef unsigned long prbitmap_t; |
| 48 | 75 |
| 49 #define PR_TEST_BIT(_map,_bit) \ | 76 #define PR_TEST_BIT(_map,_bit) \ |
| 50 ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] & (1L << ((_bit) & (PR_BITS_PER_LONG-
1)))) | 77 ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] & (1L << ((_bit) & (PR_BITS_PER_LONG-
1)))) |
| 51 #define PR_SET_BIT(_map,_bit) \ | 78 #define PR_SET_BIT(_map,_bit) \ |
| 52 ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] |= (1L << ((_bit) & (PR_BITS_PER_LONG
-1)))) | 79 ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] |= (1L << ((_bit) & (PR_BITS_PER_LONG
-1)))) |
| 53 #define PR_CLEAR_BIT(_map,_bit) \ | 80 #define PR_CLEAR_BIT(_map,_bit) \ |
| 54 ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] &= ~(1L << ((_bit) & (PR_BITS_PER_LON
G-1)))) | 81 ((_map)[(_bit)>>PR_BITS_PER_LONG_LOG2] &= ~(1L << ((_bit) & (PR_BITS_PER_LON
G-1)))) |
| 55 | 82 |
| 56 /* | 83 /* |
| 57 ** Compute the log of the least power of 2 greater than or equal to n | 84 ** Compute the log of the least power of 2 greater than or equal to n |
| 58 */ | 85 */ |
| 59 NSPR_API(PRIntn) PR_CeilingLog2(PRUint32 i); | 86 NSPR_API(PRIntn) PR_CeilingLog2(PRUint32 i); |
| 60 | 87 |
| 61 /* | 88 /* |
| 62 ** Compute the log of the greatest power of 2 less than or equal to n | 89 ** Compute the log of the greatest power of 2 less than or equal to n |
| 63 */ | 90 */ |
| 64 NSPR_API(PRIntn) PR_FloorLog2(PRUint32 i); | 91 NSPR_API(PRIntn) PR_FloorLog2(PRUint32 i); |
| 65 | 92 |
| 66 /* | 93 /* |
| 67 ** Macro version of PR_CeilingLog2: Compute the log of the least power of | 94 ** Macro version of PR_CeilingLog2: Compute the log of the least power of |
| 68 ** 2 greater than or equal to _n. The result is returned in _log2. | 95 ** 2 greater than or equal to _n. The result is returned in _log2. |
| 69 */ | 96 */ |
| 97 #ifdef PR_HAVE_BUILTIN_BITSCAN32 |
| 98 #define PR_CEILING_LOG2(_log2,_n) \ |
| 99 PR_BEGIN_MACRO \ |
| 100 PRUint32 j_ = (PRUint32)(_n); \ |
| 101 (_log2) = (j_ <= 1 ? 0 : 32 - pr_bitscan_clz32(j_ - 1)); \ |
| 102 PR_END_MACRO |
| 103 #else |
| 70 #define PR_CEILING_LOG2(_log2,_n) \ | 104 #define PR_CEILING_LOG2(_log2,_n) \ |
| 71 PR_BEGIN_MACRO \ | 105 PR_BEGIN_MACRO \ |
| 72 PRUint32 j_ = (PRUint32)(_n); \ | 106 PRUint32 j_ = (PRUint32)(_n); \ |
| 73 (_log2) = 0; \ | 107 (_log2) = 0; \ |
| 74 if ((j_) & ((j_)-1)) \ | 108 if ((j_) & ((j_)-1)) \ |
| 75 (_log2) += 1; \ | 109 (_log2) += 1; \ |
| 76 if ((j_) >> 16) \ | 110 if ((j_) >> 16) \ |
| 77 (_log2) += 16, (j_) >>= 16; \ | 111 (_log2) += 16, (j_) >>= 16; \ |
| 78 if ((j_) >> 8) \ | 112 if ((j_) >> 8) \ |
| 79 (_log2) += 8, (j_) >>= 8; \ | 113 (_log2) += 8, (j_) >>= 8; \ |
| 80 if ((j_) >> 4) \ | 114 if ((j_) >> 4) \ |
| 81 (_log2) += 4, (j_) >>= 4; \ | 115 (_log2) += 4, (j_) >>= 4; \ |
| 82 if ((j_) >> 2) \ | 116 if ((j_) >> 2) \ |
| 83 (_log2) += 2, (j_) >>= 2; \ | 117 (_log2) += 2, (j_) >>= 2; \ |
| 84 if ((j_) >> 1) \ | 118 if ((j_) >> 1) \ |
| 85 (_log2) += 1; \ | 119 (_log2) += 1; \ |
| 86 PR_END_MACRO | 120 PR_END_MACRO |
| 121 #endif /* PR_HAVE_BUILTIN_BITSCAN32 */ |
| 87 | 122 |
| 88 /* | 123 /* |
| 89 ** Macro version of PR_FloorLog2: Compute the log of the greatest power of | 124 ** Macro version of PR_FloorLog2: Compute the log of the greatest power of |
| 90 ** 2 less than or equal to _n. The result is returned in _log2. | 125 ** 2 less than or equal to _n. The result is returned in _log2. |
| 91 ** | 126 ** |
| 92 ** This is equivalent to finding the highest set bit in the word. | 127 ** This is equivalent to finding the highest set bit in the word. |
| 93 */ | 128 */ |
| 129 #ifdef PR_HAVE_BUILTIN_BITSCAN32 |
| 130 #define PR_FLOOR_LOG2(_log2,_n) \ |
| 131 PR_BEGIN_MACRO \ |
| 132 PRUint32 j_ = (PRUint32)(_n); \ |
| 133 (_log2) = 31 - pr_bitscan_clz32((j_) | 1); \ |
| 134 PR_END_MACRO |
| 135 #else |
| 94 #define PR_FLOOR_LOG2(_log2,_n) \ | 136 #define PR_FLOOR_LOG2(_log2,_n) \ |
| 95 PR_BEGIN_MACRO \ | 137 PR_BEGIN_MACRO \ |
| 96 PRUint32 j_ = (PRUint32)(_n); \ | 138 PRUint32 j_ = (PRUint32)(_n); \ |
| 97 (_log2) = 0; \ | 139 (_log2) = 0; \ |
| 98 if ((j_) >> 16) \ | 140 if ((j_) >> 16) \ |
| 99 (_log2) += 16, (j_) >>= 16; \ | 141 (_log2) += 16, (j_) >>= 16; \ |
| 100 if ((j_) >> 8) \ | 142 if ((j_) >> 8) \ |
| 101 (_log2) += 8, (j_) >>= 8; \ | 143 (_log2) += 8, (j_) >>= 8; \ |
| 102 if ((j_) >> 4) \ | 144 if ((j_) >> 4) \ |
| 103 (_log2) += 4, (j_) >>= 4; \ | 145 (_log2) += 4, (j_) >>= 4; \ |
| 104 if ((j_) >> 2) \ | 146 if ((j_) >> 2) \ |
| 105 (_log2) += 2, (j_) >>= 2; \ | 147 (_log2) += 2, (j_) >>= 2; \ |
| 106 if ((j_) >> 1) \ | 148 if ((j_) >> 1) \ |
| 107 (_log2) += 1; \ | 149 (_log2) += 1; \ |
| 108 PR_END_MACRO | 150 PR_END_MACRO |
| 151 #endif /* PR_HAVE_BUILTIN_BITSCAN32 */ |
| 109 | 152 |
| 110 /* | 153 /* |
| 111 ** Macros for rotate left and right. The argument 'a' must be an unsigned | 154 ** Macros for rotate left and right. The argument 'a' must be an unsigned |
| 112 ** 32-bit integer type such as PRUint32. | 155 ** 32-bit integer type such as PRUint32. |
| 113 ** | 156 ** |
| 114 ** There is no rotate operation in the C Language, so the construct | 157 ** There is no rotate operation in the C Language, so the construct |
| 115 ** (a << 4) | (a >> 28) is frequently used instead. Most compilers convert | 158 ** (a << 4) | (a >> 28) is frequently used instead. Most compilers convert |
| 116 ** this to a rotate instruction, but MSVC doesn't without a little help. | 159 ** this to a rotate instruction, but MSVC doesn't without a little help. |
| 117 ** To get MSVC to generate a rotate instruction, we have to use the _rotl | 160 ** To get MSVC to generate a rotate instruction, we have to use the _rotl |
| 118 ** or _rotr intrinsic and use a pragma to make it inline. | 161 ** or _rotr intrinsic and use a pragma to make it inline. |
| 119 ** | 162 ** |
| 120 ** Note: MSVC in VS2005 will do an inline rotate instruction on the above | 163 ** Note: MSVC in VS2005 will do an inline rotate instruction on the above |
| 121 ** construct. | 164 ** construct. |
| 122 */ | 165 */ |
| 123 | 166 |
| 124 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || \ | 167 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || \ |
| 125 defined(_M_X64)) | 168 defined(_M_X64)) |
| 126 #include <stdlib.h> | 169 #include <stdlib.h> |
| 127 #pragma intrinsic(_rotl, _rotr) | 170 #pragma intrinsic(_rotl, _rotr) |
| 128 #define PR_ROTATE_LEFT32(a, bits) _rotl(a, bits) | 171 #define PR_ROTATE_LEFT32(a, bits) _rotl(a, bits) |
| 129 #define PR_ROTATE_RIGHT32(a, bits) _rotr(a, bits) | 172 #define PR_ROTATE_RIGHT32(a, bits) _rotr(a, bits) |
| 130 #else | 173 #else |
| 131 #define PR_ROTATE_LEFT32(a, bits) (((a) << (bits)) | ((a) >> (32 - (bits)))) | 174 #define PR_ROTATE_LEFT32(a, bits) (((a) << (bits)) | ((a) >> (32 - (bits)))) |
| 132 #define PR_ROTATE_RIGHT32(a, bits) (((a) >> (bits)) | ((a) << (32 - (bits)))) | 175 #define PR_ROTATE_RIGHT32(a, bits) (((a) >> (bits)) | ((a) << (32 - (bits)))) |
| 133 #endif | 176 #endif |
| 134 | 177 |
| 135 PR_END_EXTERN_C | 178 PR_END_EXTERN_C |
| 136 #endif /* prbit_h___ */ | 179 #endif /* prbit_h___ */ |
| OLD | NEW |