Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions src/frontc/cabs2cil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4752,6 +4752,28 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
prestype := intType
| _ -> ignore (warn "Invalid call to builtin_types_compatible_p");
end
else if fv.vname = "__builtin_clzll" then
begin
(* Constant-fold the argument and see if it is a constant *)
let rec countLeadingZeros (arg: cilint) pos count =
if is_zero_cilint (logand_cilint (shift_right_cilint arg pos) one_cilint) then
match pos with
0 -> count + 1
| _ -> countLeadingZeros arg (pos - 1) (count + 1)
else
count
in
match !pargs with
[ arg ] -> begin
match constFold true arg with
(Const CInt (arg, kind, _)) ->
piscall := false;
pres := integer (countLeadingZeros arg 63 0);
prestype := intType
| _ -> ()
end
| _ -> ignore (warn "Invalid call to __builtin_clzll");
end
end
| _ -> ()
);
Expand Down
9 changes: 9 additions & 0 deletions test/small1/builtin_clzll.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct S1 {
int x : __builtin_choose_expr(__builtin_clzll(0ull) == 64, 1, -1),
: __builtin_choose_expr(__builtin_clzll(~0ull) == 0, 1, -1),
: __builtin_choose_expr(__builtin_clzll(1) == 63, 1, -1),
: __builtin_choose_expr(__builtin_clzll(2) == 62, 1, -1),
: __builtin_choose_expr(__builtin_clzll(4) == 61, 1, -1),
: __builtin_choose_expr(__builtin_clzll(1024) == 53, 1, -1),
: __builtin_choose_expr(__builtin_clzll(1024 * 1024) == 43, 1, -1);
} s1;