Created
July 9, 2013 15:22
-
-
Save mstump/5958250 to your computer and use it in GitHub Desktop.
Cross platform implementation of ffsll. Find the index of the first set bit of a 64 bit unsigned int. It uses the intrinsics available via clang, gcc and mvcc.
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
#include <stdint.h> | |
#include <iostream> | |
#include <boost/lexical_cast.hpp> | |
#ifndef ffsll | |
#ifdef _WIN32 | |
inline char | |
ffsll(uint64_t x) | |
{ | |
char index = 0x00; | |
_BitScanForward64(&index, x); | |
return index; | |
} | |
#endif | |
#define ffsll(x) __builtin_ffsll(x) | |
#endif | |
int | |
main() | |
{ | |
std::cout << boost::lexical_cast<std::string>(((0 & -0) * 0x07EDD5E59A4E28C2ULL) >> 58) << std::endl; | |
std::cout << boost::lexical_cast<std::string>(ffsll(0)) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment