bitwise: add ctz, aka count trailing zeroes

This commit is contained in:
Danny Robson 2019-04-24 13:25:44 +10:00
parent cc9b9b19c1
commit 174bd62a29
2 changed files with 27 additions and 0 deletions

View File

@ -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
/// inclusive range [lo, hi].

View File

@ -87,5 +87,7 @@ main (int, char**)
test_reverse (tap);
test_bitfield (tap);
tap.expect_eq (cruft::ctz (0b11110000u), 4u, "count-trailing-zero trivial test");
return tap.status ();
}