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