-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use intrinsic for genLog2() #69333
Merged
Merged
Use intrinsic for genLog2() #69333
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d832ea3
Use intrinsic for genLog2()
kunalspathak 74088d2
Use correct macro
kunalspathak 7433acb
Fix non-msvc build
kunalspathak 9e4399c
fix 32-bit build
kunalspathak 400dd55
just do for TARGET_64BIT
kunalspathak 3522579
Review comments
kunalspathak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,56 +97,49 @@ inline T genFindLowestBit(T value) | |
return (value & (0 - value)); | ||
} | ||
|
||
/*****************************************************************************/ | ||
/***************************************************************************** | ||
* | ||
* Return the highest bit that is set (that is, a mask that includes just the highest bit). | ||
* TODO-ARM64-Throughput: we should convert these to use the _BitScanReverse() / _BitScanReverse64() | ||
* compiler intrinsics, but our CRT header file intrin.h doesn't define these for ARM64 yet. | ||
*/ | ||
|
||
//------------------------------------------------------------------------ | ||
// genFindHighestBit: Return the highest bit that is set (that is, a mask that includes just the | ||
// highest bit). | ||
// | ||
// Return Value: | ||
// The highest position (0 is LSB) of bit that is set in the 'value'. | ||
// | ||
// Note: | ||
// It performs the "LeadingZeroCount " operation using intrinsics and then mask out everything | ||
// but the highest bit. | ||
inline unsigned int genFindHighestBit(unsigned int mask) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. LGTM but this is effectively using It might be worthwhile to centralize these somewhere (maybe as an "up for grabs" work item) |
||
{ | ||
assert(mask != 0); | ||
unsigned int bit = 1U << ((sizeof(unsigned int) * 8) - 1); // start looking at the top | ||
while ((bit & mask) == 0) | ||
{ | ||
bit >>= 1; | ||
} | ||
return bit; | ||
} | ||
|
||
inline unsigned __int64 genFindHighestBit(unsigned __int64 mask) | ||
{ | ||
assert(mask != 0); | ||
unsigned __int64 bit = 1ULL << ((sizeof(unsigned __int64) * 8) - 1); // start looking at the top | ||
while ((bit & mask) == 0) | ||
{ | ||
bit >>= 1; | ||
} | ||
return bit; | ||
} | ||
|
||
#if 0 | ||
// TODO-ARM64-Cleanup: These should probably be the implementation, when intrin.h is updated for ARM64 | ||
inline | ||
unsigned int genFindHighestBit(unsigned int mask) | ||
{ | ||
assert(mask != 0); | ||
#if defined(_MSC_VER) | ||
unsigned long index; | ||
#else | ||
unsigned int index; | ||
_BitScanReverse(&index, mask); | ||
#endif | ||
BitScanReverse(&index, mask); | ||
return 1L << index; | ||
} | ||
|
||
inline | ||
unsigned __int64 genFindHighestBit(unsigned __int64 mask) | ||
//------------------------------------------------------------------------ | ||
// genFindHighestBit: Return the highest bit that is set (that is, a mask that includes just the | ||
// highest bit). | ||
// | ||
// Return Value: | ||
// The highest position (0 is LSB) of bit that is set in the 'value'. | ||
// | ||
// Note: | ||
// It performs the "LeadingZeroCount " operation using intrinsics and then mask out everything | ||
// but the highest bit. | ||
inline unsigned __int64 genFindHighestBit(unsigned __int64 mask) | ||
{ | ||
assert(mask != 0); | ||
#if defined(_MSC_VER) | ||
unsigned long index; | ||
#else | ||
unsigned int index; | ||
_BitScanReverse64(&index, mask); | ||
#endif | ||
BitScanReverse64(&index, mask); | ||
return 1LL << index; | ||
} | ||
#endif // 0 | ||
|
||
/***************************************************************************** | ||
* | ||
|
@@ -222,8 +215,11 @@ inline unsigned uhi32(unsigned __int64 value) | |
|
||
inline unsigned genLog2(unsigned __int64 value) | ||
{ | ||
unsigned lo32 = ulo32(value); | ||
unsigned hi32 = uhi32(value); | ||
#ifdef HOST_64BIT | ||
return BitPosition(value); | ||
#else // HOST_32BIT | ||
unsigned lo32 = ulo32(value); | ||
unsigned hi32 = uhi32(value); | ||
|
||
if (lo32 != 0) | ||
{ | ||
|
@@ -234,6 +230,7 @@ inline unsigned genLog2(unsigned __int64 value) | |
{ | ||
return genLog2(hi32) + 32; | ||
} | ||
#endif | ||
} | ||
|
||
/***************************************************************************** | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM, but its worth calling out that this is "effectively" doing
TrailingZeroCount
, but without handling0
.It might be worth centralizing these "primitive bit operation" methods somewhere and ensuring they are generally reusable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened tracking issue #69401