From 2cf58f80c05c8f7b7919f2f6c0bb0d5ed132a148 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 5 Aug 2018 10:52:27 +0200 Subject: Use builtin FFS instructions --- Hilbert/FixBitVec.hpp | 18 ++---------------- Hilbert/Operations.hpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Hilbert/FixBitVec.hpp b/Hilbert/FixBitVec.hpp index 89f7bf6..58a7f38 100644 --- a/Hilbert/FixBitVec.hpp +++ b/Hilbert/FixBitVec.hpp @@ -20,6 +20,7 @@ #ifndef _FIXBITVEC_HPP_ #define _FIXBITVEC_HPP_ +#include #include #include @@ -341,22 +342,7 @@ public: int fsb() const { - FBV_UINT i = m_uiRack; - int c = 0; - -#if FBV_BITS == 64 - if ( i == FBV0 ) return 0; - if ( (i&FBVN1S(32)) == FBV0 ) { i>>=32; c^=32; } -#elif FBV_BITS == 32 - if ( i == FBV0 ) return 0; -#endif - if ( (i&FBVN1S(16)) == FBV0 ) { i>>=16; c^=16; } - if ( (i&FBVN1S( 8)) == FBV0 ) { i>>= 8; c^= 8; } - if ( (i&FBVN1S( 4)) == FBV0 ) { i>>= 4; c^= 4; } - if ( (i&FBVN1S( 2)) == FBV0 ) { i>>= 2; c^= 2; } - if ( (i&FBVN1S( 1)) == FBV0 ) { i>>= 1; c^= 1; } - - return ++c; + return ffs(m_uiRack); } diff --git a/Hilbert/Operations.hpp b/Hilbert/Operations.hpp index 1c76dc2..cff38a6 100644 --- a/Hilbert/Operations.hpp +++ b/Hilbert/Operations.hpp @@ -67,4 +67,22 @@ setBit(T& field, const BitsetIndex index, const bool value) field.set(index, value); } +/// Return 1 + the index of the least significant 1-bit of `field`, or zero +template +int ffs(const T field); + +template <> +int +ffs(const unsigned long field) +{ + return __builtin_ffsl(field); +} + +template <> +int +ffs(const unsigned long long field) +{ + return __builtin_ffsll(field); +} + #endif -- cgit v1.2.1