nsprpub/pr/include/prbit.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial