-
Notifications
You must be signed in to change notification settings - Fork 277
Implement __builtin_ctz{,l,ll} via count_trailing_zeros_exprt #5881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <assert.h> | ||
#include <limits.h> | ||
|
||
#ifndef __GNUC__ | ||
int __builtin_ctz(unsigned int); | ||
int __builtin_ctzl(unsigned long); | ||
int __builtin_ctzll(unsigned long long); | ||
#endif | ||
|
||
#ifdef _MSC_VER | ||
# define _Static_assert(x, m) static_assert(x, m) | ||
#endif | ||
|
||
unsigned __VERIFIER_nondet_unsigned(); | ||
|
||
// http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup | ||
static const int multiply_de_bruijn_bit_position[32] = { | ||
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, | ||
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9}; | ||
|
||
int main() | ||
{ | ||
_Static_assert(__builtin_ctz(1) == 0, ""); | ||
_Static_assert(__builtin_ctz(UINT_MAX) == 0, ""); | ||
_Static_assert( | ||
__builtin_ctz(1U << (sizeof(unsigned) * 8 - 1)) == sizeof(unsigned) * 8 - 1, | ||
""); | ||
_Static_assert( | ||
__builtin_ctzl(1UL << (sizeof(unsigned) * 8 - 1)) == | ||
sizeof(unsigned) * 8 - 1, | ||
""); | ||
_Static_assert( | ||
__builtin_ctzll(1ULL << (sizeof(unsigned) * 8 - 1)) == | ||
sizeof(unsigned) * 8 - 1, | ||
""); | ||
|
||
unsigned u = __VERIFIER_nondet_unsigned(); | ||
__CPROVER_assume(u != 0); | ||
__CPROVER_assume(sizeof(u) == 4); | ||
__CPROVER_assume(u > INT_MIN); | ||
assert( | ||
multiply_de_bruijn_bit_position | ||
[((unsigned)((u & -u) * 0x077CB531U)) >> 27] == __builtin_ctz(u)); | ||
|
||
// a failing assertion should be generated as __builtin_ctz is undefined for 0 | ||
__builtin_ctz(0U); | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CORE | ||
main.c | ||
--bounds-check | ||
^\[main.bit_count.\d+\] line 46 count trailing zeros is undefined for value zero in __builtin_ctz\(0u\): FAILURE$ | ||
^\*\* 1 of \d+ failed | ||
^VERIFICATION FAILED$ | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
-- | ||
^warning: ignoring |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,6 +172,28 @@ simplify_exprt::simplify_clz(const count_leading_zeros_exprt &expr) | |
return from_integer(n_leading_zeros, expr.type()); | ||
} | ||
|
||
simplify_exprt::resultt<> | ||
simplify_exprt::simplify_ctz(const count_trailing_zeros_exprt &expr) | ||
{ | ||
const auto const_bits_opt = expr2bits( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to use the lowering in the simplifier to avoid the duplicate implementation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could, but it's probably more expensive. Whether that's a practical concern, however, is a different question. I suppose ctz will be fairly rare. |
||
expr.op(), byte_extract_id() == ID_byte_extract_little_endian, ns); | ||
|
||
if(!const_bits_opt.has_value()) | ||
return unchanged(expr); | ||
|
||
// expr2bits generates a bit string starting with the least-significant bit | ||
std::size_t n_trailing_zeros = const_bits_opt->find('1'); | ||
if(n_trailing_zeros == std::string::npos) | ||
{ | ||
if(!expr.zero_permitted()) | ||
return unchanged(expr); | ||
|
||
n_trailing_zeros = const_bits_opt->size(); | ||
} | ||
|
||
return from_integer(n_trailing_zeros, expr.type()); | ||
} | ||
|
||
/// Simplify String.endsWith function when arguments are constant | ||
/// \param expr: the expression to simplify | ||
/// \param ns: namespace | ||
|
@@ -2421,6 +2443,10 @@ simplify_exprt::resultt<> simplify_exprt::simplify_node(exprt node) | |
{ | ||
r = simplify_clz(to_count_leading_zeros_expr(expr)); | ||
} | ||
else if(expr.id() == ID_count_trailing_zeros) | ||
{ | ||
r = simplify_ctz(to_count_trailing_zeros_expr(expr)); | ||
} | ||
else if(expr.id() == ID_function_application) | ||
{ | ||
r = simplify_function_application(to_function_application_expr(expr)); | ||
|
Uh oh!
There was an error while loading. Please reload this page.