diff options
author | David Robillard <d@drobilla.net> | 2018-08-05 10:52:27 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-08-07 20:01:19 +0200 |
commit | 2cf58f80c05c8f7b7919f2f6c0bb0d5ed132a148 (patch) | |
tree | a157a8755d50c150b2a3bca032929565c8160816 | |
parent | 5472d2524a319602371d1918b5645efbfef474d3 (diff) | |
download | chilbert-2cf58f80c05c8f7b7919f2f6c0bb0d5ed132a148.tar.gz chilbert-2cf58f80c05c8f7b7919f2f6c0bb0d5ed132a148.tar.bz2 chilbert-2cf58f80c05c8f7b7919f2f6c0bb0d5ed132a148.zip |
Use builtin FFS instructions
-rw-r--r-- | Hilbert/FixBitVec.hpp | 18 | ||||
-rw-r--r-- | 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 <Hilbert/Operations.hpp> #include <inttypes.h> #include <cassert> @@ -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<T> index, const bool value) field.set(index, value); } +/// Return 1 + the index of the least significant 1-bit of `field`, or zero +template <typename T> +int ffs(const T field); + +template <> +int +ffs<unsigned long>(const unsigned long field) +{ + return __builtin_ffsl(field); +} + +template <> +int +ffs<unsigned long long>(const unsigned long long field) +{ + return __builtin_ffsll(field); +} + #endif |