Skip to content

Instantly share code, notes, and snippets.

@mstump
Created July 9, 2013 15:22
Show Gist options
  • Save mstump/5958250 to your computer and use it in GitHub Desktop.
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.
#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