Created
June 1, 2016 12:20
-
-
Save bzoz/b6ddba0b94ea04b072cba57771f20839 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <intrin.h> | |
#include <cstdint> | |
#include <iostream> | |
#pragma intrinsic(_BitScanReverse64) | |
#pragma intrinsic(_BitScanReverse) | |
using namespace std; | |
unsigned long old_version(int64_t value) | |
{ | |
unsigned long leading_zero = 0; | |
_BitScanReverse64(&leading_zero, value); | |
return leading_zero; | |
} | |
unsigned long new_version(int64_t value) | |
{ | |
unsigned long leading_zero = 0; | |
uint32_t high = value >> 32; | |
if (_BitScanReverse(&leading_zero, high) != 0) | |
{ | |
leading_zero += 32; | |
} | |
else | |
{ | |
uint32_t low = value & 0x00000000FFFFFFFF; | |
_BitScanReverse(&leading_zero, low); | |
} | |
return leading_zero; | |
} | |
int main() | |
{ | |
cout << "Bit\tOld\tNew\tSame?\n"; | |
for (int i = 0; i <= 64; ++i) | |
{ | |
int64_t value = 1; | |
value <<= i; | |
auto old_lz = old_version(value); | |
auto new_lz = new_version(value); | |
cout << i << "\t" << old_lz << "\t" << new_lz << "\t" << (old_lz == new_lz) << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment