Skip to content

Commit 5b1ebeb

Browse files
committed
New expression: count_leading_zeros_exprt
Rather than ad-hoc handling __builtin_clz and __lzcnt (and their variants) in the C front-end, make counting-leading-zeros available across the code base.
1 parent a14192f commit 5b1ebeb

File tree

17 files changed

+236
-137
lines changed

17 files changed

+236
-137
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int main()
2+
{
3+
#ifdef __GNUC__
4+
_Static_assert(
5+
__builtin_clz(0xffU) == 8 * sizeof(unsigned) - 8,
6+
"GCC/Clang compile-time constant");
7+
#endif
8+
9+
return 0;
10+
}

regression/cbmc-library/__builtin_clz-02/main.c

Lines changed: 0 additions & 12 deletions
This file was deleted.

regression/cbmc-library/__builtin_clz-01/main.c renamed to regression/cbmc/__builtin_clz-01/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,9 @@ int main()
5656
__CPROVER_assume(u != 0);
5757
assert(nlz2a(u) == __builtin_clz(u));
5858

59+
#undef __builtin_clz
60+
// a failing assertion should be generated as __builtin_clz is undefined for 0
61+
__builtin_clz(0U);
62+
5963
return 0;
6064
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--bounds-check
4+
^\[main.bit_count.\d+\] line 61 count leading zeros argument in __builtin_clz\(0u\): FAILURE$
5+
^\*\* 1 of \d+ failed
6+
^VERIFICATION FAILED$
7+
^EXIT=10$
8+
^SIGNAL=0$
9+
--
10+
^warning: ignoring

src/analyses/goto_check.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ class goto_checkt
172172

173173
using conditionst = std::list<conditiont>;
174174

175-
void bounds_check(const index_exprt &, const guardt &);
175+
void bounds_check(const exprt &, const guardt &);
176+
void bounds_check_index(const index_exprt &, const guardt &);
177+
void bounds_check_clz(const count_leading_zeros_exprt &, const guardt &);
176178
void div_by_zero_check(const div_exprt &, const guardt &);
177179
void mod_by_zero_check(const mod_exprt &, const guardt &);
178180
void mod_overflow_check(const mod_exprt &, const guardt &);
@@ -1321,9 +1323,7 @@ std::string goto_checkt::array_name(const exprt &expr)
13211323
return ::array_name(ns, expr);
13221324
}
13231325

1324-
void goto_checkt::bounds_check(
1325-
const index_exprt &expr,
1326-
const guardt &guard)
1326+
void goto_checkt::bounds_check(const exprt &expr, const guardt &guard)
13271327
{
13281328
if(!enable_bounds_check)
13291329
return;
@@ -1335,6 +1335,16 @@ void goto_checkt::bounds_check(
13351335
return;
13361336
}
13371337

1338+
if(expr.id() == ID_index)
1339+
bounds_check_index(to_index_expr(expr), guard);
1340+
else if(expr.id() == ID_count_leading_zeros)
1341+
bounds_check_clz(to_count_leading_zeros_expr(expr), guard);
1342+
}
1343+
1344+
void goto_checkt::bounds_check_index(
1345+
const index_exprt &expr,
1346+
const guardt &guard)
1347+
{
13381348
typet array_type = expr.array().type();
13391349

13401350
if(array_type.id()==ID_pointer)
@@ -1510,6 +1520,19 @@ void goto_checkt::bounds_check(
15101520
}
15111521
}
15121522

1523+
void goto_checkt::bounds_check_clz(
1524+
const count_leading_zeros_exprt &expr,
1525+
const guardt &guard)
1526+
{
1527+
add_guarded_property(
1528+
notequal_exprt{expr.op(), from_integer(0, expr.op().type())},
1529+
"count leading zeros argument",
1530+
"bit count",
1531+
expr.find_source_location(),
1532+
expr,
1533+
guard);
1534+
}
1535+
15131536
void goto_checkt::add_guarded_property(
15141537
const exprt &asserted_expr,
15151538
const std::string &comment,
@@ -1729,7 +1752,7 @@ void goto_checkt::check_rec(const exprt &expr, guardt &guard)
17291752

17301753
if(expr.id()==ID_index)
17311754
{
1732-
bounds_check(to_index_expr(expr), guard);
1755+
bounds_check(expr, guard);
17331756
}
17341757
else if(expr.id()==ID_div)
17351758
{
@@ -1766,6 +1789,10 @@ void goto_checkt::check_rec(const exprt &expr, guardt &guard)
17661789
{
17671790
pointer_primitive_check(expr, guard);
17681791
}
1792+
else if(expr.id() == ID_count_leading_zeros)
1793+
{
1794+
bounds_check(expr, guard);
1795+
}
17691796
}
17701797

17711798
void goto_checkt::check(const exprt &expr)

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,27 +2689,14 @@ exprt c_typecheck_baset::do_special_functions(
26892689
throw 0;
26902690
}
26912691

2692-
side_effect_expr_function_callt try_constant{expr};
2693-
typecheck_function_call_arguments(try_constant);
2694-
exprt argument = try_constant.arguments().front();
2695-
simplify(argument, *this);
2696-
const auto int_constant = numeric_cast<mp_integer>(argument);
2697-
2698-
if(
2699-
!int_constant.has_value() || *int_constant == 0 ||
2700-
argument.type().id() != ID_unsignedbv)
2701-
{
2702-
return nil_exprt{};
2703-
}
2692+
typecheck_function_call_arguments(expr);
27042693

2705-
const std::string binary_value = integer2binary(
2706-
*int_constant, to_unsignedbv_type(argument.type()).get_width());
2707-
std::size_t n_leading_zeros = binary_value.find('1');
2708-
CHECK_RETURN(n_leading_zeros != std::string::npos);
2694+
count_leading_zeros_exprt clz{expr.arguments().front(),
2695+
has_prefix(id2string(identifier), "__lzcnt"),
2696+
expr.type()};
2697+
clz.add_source_location() = source_location;
27092698

2710-
return from_integer(
2711-
n_leading_zeros,
2712-
to_code_type(try_constant.function().type()).return_type());
2699+
return std::move(clz);
27132700
}
27142701
else if(identifier==CPROVER_PREFIX "equal")
27152702
{

src/ansi-c/expr2c.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3896,6 +3896,9 @@ std::string expr2ct::convert_with_precedence(
38963896
else if(src.id()==ID_type)
38973897
return convert(src.type());
38983898

3899+
else if(src.id() == ID_count_leading_zeros)
3900+
return convert_function(src, "__builtin_clz");
3901+
38993902
// no C language expression for internal representation
39003903
return convert_norep(src, precedence);
39013904
}

src/ansi-c/library/gcc.c

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,64 +33,6 @@ inline void __sync_synchronize(void)
3333
#endif
3434
}
3535

36-
/* FUNCTION: __builtin_clz */
37-
38-
int __builtin_popcount(unsigned int);
39-
40-
inline int __builtin_clz(unsigned int x)
41-
{
42-
__CPROVER_precondition(x != 0, "__builtin_clz(0) is undefined");
43-
44-
x = x | (x >> 1);
45-
x = x | (x >> 2);
46-
x = x | (x >> 4);
47-
x = x | (x >> 8);
48-
if(sizeof(x) >= 4)
49-
x = x | (x >> 16);
50-
51-
return __builtin_popcount(~x);
52-
}
53-
54-
/* FUNCTION: __builtin_clzl */
55-
56-
int __builtin_popcountl(unsigned long int);
57-
58-
inline int __builtin_clzl(unsigned long int x)
59-
{
60-
__CPROVER_precondition(x != 0, "__builtin_clzl(0) is undefined");
61-
62-
x = x | (x >> 1);
63-
x = x | (x >> 2);
64-
x = x | (x >> 4);
65-
x = x | (x >> 8);
66-
if(sizeof(x) >= 4)
67-
x = x | (x >> 16);
68-
if(sizeof(x) >= 8)
69-
x = x | (x >> 32);
70-
71-
return __builtin_popcountl(~x);
72-
}
73-
74-
/* FUNCTION: __builtin_clzll */
75-
76-
int __builtin_popcountll(unsigned long long int);
77-
78-
inline int __builtin_clzll(unsigned long long int x)
79-
{
80-
__CPROVER_precondition(x != 0, "__builtin_clzll(0) is undefined");
81-
82-
x = x | (x >> 1);
83-
x = x | (x >> 2);
84-
x = x | (x >> 4);
85-
x = x | (x >> 8);
86-
if(sizeof(x) >= 4)
87-
x = x | (x >> 16);
88-
if(sizeof(x) >= 8)
89-
x = x | (x >> 32);
90-
91-
return __builtin_popcountll(~x);
92-
}
93-
9436
/* FUNCTION: __builtin_ffs */
9537

9638
int __builtin_clz(unsigned int x);

src/ansi-c/library/windows.c

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,3 @@ inline HANDLE CreateThread(
5151
return handle;
5252
}
5353
#endif
54-
55-
/* FUNCTION: __lzcnt16 */
56-
57-
#ifdef _MSC_VER
58-
int __builtin_clz(unsigned int x);
59-
60-
unsigned short __lzcnt16(unsigned short value)
61-
{
62-
if(value == 0)
63-
return 16;
64-
65-
return __builtin_clz(value) -
66-
(sizeof(unsigned int) - sizeof(unsigned short)) * 8;
67-
}
68-
#endif
69-
70-
/* FUNCTION: __lzcnt */
71-
72-
#ifdef _MSC_VER
73-
int __builtin_clz(unsigned int x);
74-
75-
unsigned int __lzcnt(unsigned int value)
76-
{
77-
if(value == 0)
78-
return 32;
79-
80-
return __builtin_clz(value);
81-
}
82-
#endif
83-
84-
/* FUNCTION: __lzcnt64 */
85-
86-
#ifdef _MSC_VER
87-
int __builtin_clzll(unsigned long long x);
88-
89-
unsigned __int64 __lzcnt64(unsigned __int64 value)
90-
{
91-
if(value == 0)
92-
return 64;
93-
94-
return __builtin_clzll(value);
95-
}
96-
#endif

src/solvers/flattening/boolbv.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
228228
return convert_power(to_binary_expr(expr));
229229
else if(expr.id() == ID_popcount)
230230
return convert_bv(to_popcount_expr(expr).lower());
231+
else if(expr.id() == ID_count_leading_zeros)
232+
return convert_bv(to_count_leading_zeros_expr(expr).lower());
231233

232234
return conversion_failed(expr);
233235
}

src/solvers/smt2/smt2_conv.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,10 @@ void smt2_convt::convert_expr(const exprt &expr)
19861986
{
19871987
convert_expr(to_popcount_expr(expr).lower());
19881988
}
1989+
else if(expr.id() == ID_count_leading_zeros)
1990+
{
1991+
convert_expr(to_count_leading_zeros_expr(expr).lower());
1992+
}
19891993
else
19901994
INVARIANT_WITH_DIAGNOSTICS(
19911995
false,

src/util/bitvector_expr.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,41 @@ exprt popcount_exprt::lower() const
8787
// the result is restricted to the result type
8888
return typecast_exprt::conditional_cast(x, type());
8989
}
90+
91+
exprt count_leading_zeros_exprt::lower() const
92+
{
93+
// x = x | (x >> 1);
94+
// x = x | (x >> 2);
95+
// x = x | (x >> 4);
96+
// x = x | (x >> 8);
97+
// etc.
98+
// return popcount(~x);
99+
100+
// make sure the operand width is a power of two
101+
exprt x = op();
102+
const auto x_width = to_bitvector_type(x.type()).get_width();
103+
CHECK_RETURN(x_width >= 1);
104+
const std::size_t bits = address_bits(x_width);
105+
const std::size_t new_width = numeric_cast_v<std::size_t>(power(2, bits));
106+
107+
const bool need_typecast =
108+
new_width > x_width || x.type().id() != ID_unsignedbv;
109+
110+
if(need_typecast)
111+
x = typecast_exprt(x, unsignedbv_typet(new_width));
112+
113+
// repeatedly compute x = x | (x >> shift)
114+
for(std::size_t shift = 1; shift < new_width; shift <<= 1)
115+
{
116+
// x >> shift
117+
lshr_exprt shifted_x(
118+
x, from_integer(shift, unsignedbv_typet(address_bits(shift) + 1)));
119+
// build the expression
120+
x = bitor_exprt{x, shifted_x};
121+
}
122+
123+
// the result is restricted to the result type
124+
return popcount_exprt{
125+
bitnot_exprt{typecast_exprt::conditional_cast(x, op().type())}, type()}
126+
.lower();
127+
}

0 commit comments

Comments
 (0)