mirror of
https://github.com/MoleskiCoder/EightBit.git
synced 2025-01-02 18:29:41 +00:00
Add a non-intrinsic version of findFirstSet to the EightBit library.
Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
parent
def1c58e9d
commit
9d34d7ec3d
@ -1,5 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <bitset>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
# include <intrin.h>
|
# include <intrin.h>
|
||||||
#endif
|
#endif
|
||||||
@ -9,9 +12,9 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace EightBit {
|
namespace EightBit {
|
||||||
[[nodiscard]] int countBits(uint8_t value) noexcept ;
|
[[nodiscard]] int countBits(uint8_t value) noexcept;
|
||||||
[[nodiscard]] bool oddParity(uint8_t value) noexcept ;
|
[[nodiscard]] bool oddParity(uint8_t value) noexcept;
|
||||||
[[nodiscard]] int findFirstSet(int value) noexcept ;
|
[[nodiscard]] int findFirstSet(unsigned long value) noexcept;
|
||||||
constexpr void assume(int expression);
|
constexpr void assume(int expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +42,7 @@ inline bool EightBit::oddParity(const uint8_t value) noexcept {
|
|||||||
return countBits(value) % 2;
|
return countBits(value) % 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int EightBit::findFirstSet(const int value) noexcept {
|
inline int EightBit::findFirstSet(const unsigned long value) noexcept {
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
unsigned long index;
|
unsigned long index;
|
||||||
if (_BitScanForward(&index, value))
|
if (_BitScanForward(&index, value))
|
||||||
@ -48,7 +51,11 @@ inline int EightBit::findFirstSet(const int value) noexcept {
|
|||||||
#elif defined(__GNUG__)
|
#elif defined(__GNUG__)
|
||||||
return __builtin_ffs(value);
|
return __builtin_ffs(value);
|
||||||
#else
|
#else
|
||||||
# error Find first set not implemented
|
std::bitset<sizeof(unsigned long) * 8> bits(value);
|
||||||
|
for (size_t i = bits.size() - 1; i >= 0; --i)
|
||||||
|
if (bits.test(i))
|
||||||
|
return i + 1;
|
||||||
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user