bitwise: add ctz, aka count trailing zeroes
This commit is contained in:
parent
cc9b9b19c1
commit
174bd62a29
25
bitwise.hpp
25
bitwise.hpp
@ -107,6 +107,31 @@ namespace cruft {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
/// Count trailing zeroes
|
||||||
|
constexpr unsigned int
|
||||||
|
ctz (unsigned int x) noexcept
|
||||||
|
{
|
||||||
|
return __builtin_ctz (x);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
constexpr unsigned long
|
||||||
|
ctz (unsigned long x) noexcept
|
||||||
|
{
|
||||||
|
return __builtin_ctzl (x);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
constexpr unsigned long long
|
||||||
|
ctz (unsigned long long x) noexcept
|
||||||
|
{
|
||||||
|
return __builtin_ctzll (x);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
/// returns the integral value composed of the bits from `val' in the
|
/// returns the integral value composed of the bits from `val' in the
|
||||||
/// inclusive range [lo, hi].
|
/// inclusive range [lo, hi].
|
||||||
|
@ -87,5 +87,7 @@ main (int, char**)
|
|||||||
test_reverse (tap);
|
test_reverse (tap);
|
||||||
test_bitfield (tap);
|
test_bitfield (tap);
|
||||||
|
|
||||||
|
tap.expect_eq (cruft::ctz (0b11110000u), 4u, "count-trailing-zero trivial test");
|
||||||
|
|
||||||
return tap.status ();
|
return tap.status ();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user