Skip to content

GCC built-ins: implement __builtin_ffs{,l,ll} #5728

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

Merged
merged 1 commit into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions regression/cbmc-library/__builtin_ffs-01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <assert.h>
#include <limits.h>

#ifndef __GNUC__
int __builtin_ffs(int);
int __builtin_ffsl(long);
int __builtin_ffsll(long long);
#endif

int __VERIFIER_nondet_int();
long __VERIFIER_nondet_long();
long long __VERIFIER_nondet_long_long();

// 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()
{
assert(__builtin_ffs(0) == 0);
assert(__builtin_ffs(-1) == 1);
assert(__builtin_ffs(INT_MIN) == sizeof(int) * 8);
assert(__builtin_ffsl(INT_MIN) == sizeof(int) * 8);
assert(__builtin_ffsl(LONG_MIN) == sizeof(long) * 8);
assert(__builtin_ffsll(INT_MIN) == sizeof(int) * 8);
assert(__builtin_ffsll(LLONG_MIN) == sizeof(long long) * 8);
assert(__builtin_ffs(INT_MAX) == 1);

int i = __VERIFIER_nondet_int();
__CPROVER_assume(i != 0);
__CPROVER_assume(sizeof(i) == 4);
__CPROVER_assume(i > INT_MIN);
assert(
multiply_de_bruijn_bit_position
[((unsigned)((i & -i) * 0x077CB531U)) >> 27] +
1 ==
__builtin_ffs(i));

return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc-library/__builtin_ffs-01/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
51 changes: 51 additions & 0 deletions src/ansi-c/library/gcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,57 @@ inline int __builtin_clzll(unsigned long long int x)
return __builtin_popcountll(~x);
}

/* FUNCTION: __builtin_ffs */

int __builtin_clz(unsigned int x);

inline int __builtin_ffs(int x)
{
if(x == 0)
return 0;

#pragma CPROVER check push
#pragma CPROVER check disable "conversion"
unsigned int u = (unsigned int)x;
#pragma CPROVER check pop

return sizeof(int) * 8 - __builtin_clz(u & ~(u - 1));
}

/* FUNCTION: __builtin_ffsl */

int __builtin_clzl(unsigned long x);

inline int __builtin_ffsl(long x)
{
if(x == 0)
return 0;

#pragma CPROVER check push
#pragma CPROVER check disable "conversion"
unsigned long u = (unsigned long)x;
#pragma CPROVER check pop

return sizeof(long) * 8 - __builtin_clzl(u & ~(u - 1));
}

/* FUNCTION: __builtin_ffsll */

int __builtin_clzll(unsigned long long x);

inline int __builtin_ffsll(long long x)
{
if(x == 0)
return 0;

#pragma CPROVER check push
#pragma CPROVER check disable "conversion"
unsigned long long u = (unsigned long long)x;
#pragma CPROVER check pop

return sizeof(long long) * 8 - __builtin_clzll(u & ~(u - 1));
}

/* FUNCTION: __atomic_test_and_set */

void __atomic_thread_fence(int memorder);
Expand Down