diff options
author | Pat Gavlin <pagavlin@microsoft.com> | 2016-04-11 10:24:23 -0700 |
---|---|---|
committer | Pat Gavlin <pagavlin@microsoft.com> | 2016-04-14 13:58:29 -0700 |
commit | d0b6a85c895b4102ff849ffc0d276b6af8c63bb0 (patch) | |
tree | dd792afec1f09ecd4cc816d1068b61c5b302f659 /src | |
parent | 08e1b2f05ba15efff366c4fd70ff475359022bb3 (diff) | |
download | coreclr-d0b6a85c895b4102ff849ffc0d276b6af8c63bb0.tar.gz coreclr-d0b6a85c895b4102ff849ffc0d276b6af8c63bb0.tar.bz2 coreclr-d0b6a85c895b4102ff849ffc0d276b6af8c63bb0.zip |
Move `BitPosition` into its own file.
This will allow the GC info encoder and the JIT to use this
function without pulling in the entire utilcode header.
Diffstat (limited to 'src')
-rw-r--r-- | src/gcinfo/gcinfoencoder.cpp | 1 | ||||
-rw-r--r-- | src/inc/bitposition.h | 55 | ||||
-rw-r--r-- | src/inc/utilcode.h | 48 |
3 files changed, 57 insertions, 47 deletions
diff --git a/src/gcinfo/gcinfoencoder.cpp b/src/gcinfo/gcinfoencoder.cpp index 9fb37d37ef..b76290bd56 100644 --- a/src/gcinfo/gcinfoencoder.cpp +++ b/src/gcinfo/gcinfoencoder.cpp @@ -25,6 +25,7 @@ #ifndef STANDALONE_BUILD #include "log.h" #include "simplerhash.h" +#include "bitposition.h" #endif #ifdef MEASURE_GCINFO diff --git a/src/inc/bitposition.h b/src/inc/bitposition.h new file mode 100644 index 0000000000..0f3831fce9 --- /dev/null +++ b/src/inc/bitposition.h @@ -0,0 +1,55 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#ifndef _BITPOSITION_H_ +#define _BITPOSITION_H_ + +//------------------------------------------------------------------------ +// BitPosition: Return the position of the single bit that is set in 'value'. +// +// Return Value: +// The position (0 is LSB) of bit that is set in 'value' +// +// Notes: +// 'value' must have exactly one bit set. +// The algorithm is as follows: +// - PRIME is a prime bigger than sizeof(unsigned int), which is not of the +// form 2^n-1. +// - Taking the modulo of 'value' with this will produce a unique hash for all +// powers of 2 (which is what "value" is). +// - Entries in hashTable[] which are -1 should never be used. There +// should be PRIME-8*sizeof(value) entries which are -1 . +// +inline +unsigned BitPosition(unsigned value) +{ + _ASSERTE((value != 0) && ((value & (value-1)) == 0)); +#ifndef _TARGET_AMD64_ + const unsigned PRIME = 37; + + static const char hashTable[PRIME] = + { + -1, 0, 1, 26, 2, 23, 27, -1, 3, 16, + 24, 30, 28, 11, -1, 13, 4, 7, 17, -1, + 25, 22, 31, 15, 29, 10, 12, 6, -1, 21, + 14, 9, 5, 20, 8, 19, 18 + }; + + _ASSERTE(PRIME >= 8*sizeof(value)); + _ASSERTE(sizeof(hashTable) == PRIME); + + + unsigned hash = value % PRIME; + unsigned index = hashTable[hash]; + _ASSERTE(index != (unsigned char)-1); +#else + // not enabled for x86 because BSF is extremely slow on Atom + // (15 clock cycles vs 1-3 on any other Intel CPU post-P4) + DWORD index; + BitScanForward(&index, value); +#endif + return index; +} + +#endif diff --git a/src/inc/utilcode.h b/src/inc/utilcode.h index b9985a8b25..bb7c8bad81 100644 --- a/src/inc/utilcode.h +++ b/src/inc/utilcode.h @@ -1041,53 +1041,7 @@ inline int CountBits(int iNum) return (iBits); } -//------------------------------------------------------------------------ -// BitPosition: Return the position of the single bit that is set in 'value'. -// -// Return Value: -// The position (0 is LSB) of bit that is set in 'value' -// -// Notes: -// 'value' must have exactly one bit set. -// The algorithm is as follows: -// - PRIME is a prime bigger than sizeof(unsigned int), which is not of the -// form 2^n-1. -// - Taking the modulo of 'value' with this will produce a unique hash for all -// powers of 2 (which is what "value" is). -// - Entries in hashTable[] which are -1 should never be used. There -// should be PRIME-8*sizeof(value) entries which are -1 . -// -inline -unsigned BitPosition(unsigned value) -{ - _ASSERTE((value != 0) && ((value & (value-1)) == 0)); -#ifndef _TARGET_AMD64_ - const unsigned PRIME = 37; - - static const char hashTable[PRIME] = - { - -1, 0, 1, 26, 2, 23, 27, -1, 3, 16, - 24, 30, 28, 11, -1, 13, 4, 7, 17, -1, - 25, 22, 31, 15, 29, 10, 12, 6, -1, 21, - 14, 9, 5, 20, 8, 19, 18 - }; - - _ASSERTE(PRIME >= 8*sizeof(value)); - _ASSERTE(sizeof(hashTable) == PRIME); - - - unsigned hash = value % PRIME; - unsigned index = hashTable[hash]; - _ASSERTE(index != (unsigned char)-1); -#else - // not enabled for x86 because BSF is extremely slow on Atom - // (15 clock cycles vs 1-3 on any other Intel CPU post-P4) - DWORD index; - BitScanForward(&index, value); -#endif - return index; -} - +#include "bitposition.h" // Used to remove trailing zeros from Decimal types. // NOTE: Assumes hi32 bits are empty (used for conversions from Cy->Dec) |