Skip to content

Commit 7fe90da

Browse files
Implement __builtin_clzll builtin
1 parent e21285a commit 7fe90da

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/frontc/cabs2cil.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4752,6 +4752,28 @@ and doExp (asconst: bool) (* This expression is used as a constant *)
47524752
prestype := intType
47534753
| _ -> ignore (warn "Invalid call to builtin_types_compatible_p");
47544754
end
4755+
else if fv.vname = "__builtin_clzll" then
4756+
begin
4757+
(* Constant-fold the argument and see if it is a constant *)
4758+
let rec countLeadingZeros (arg: cilint) pos count =
4759+
if is_zero_cilint (logand_cilint (shift_right_cilint arg pos) one_cilint) then
4760+
match pos with
4761+
0 -> count + 1
4762+
| _ -> countLeadingZeros arg (pos - 1) (count + 1)
4763+
else
4764+
count
4765+
in
4766+
match !pargs with
4767+
[ arg ] -> begin
4768+
match constFold true arg with
4769+
(Const CInt (arg, kind, _)) ->
4770+
piscall := false;
4771+
pres := integer (countLeadingZeros arg 63 0);
4772+
prestype := intType
4773+
| _ -> ()
4774+
end
4775+
| _ -> ignore (warn "Invalid call to __builtin_clzll");
4776+
end
47554777
end
47564778
| _ -> ()
47574779
);

test/small1/builtin_clzll.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct S1 {
2+
int x : __builtin_choose_expr(__builtin_clzll(0ull) == 64, 1, -1),
3+
: __builtin_choose_expr(__builtin_clzll(~0ull) == 0, 1, -1),
4+
: __builtin_choose_expr(__builtin_clzll(1) == 63, 1, -1),
5+
: __builtin_choose_expr(__builtin_clzll(2) == 62, 1, -1),
6+
: __builtin_choose_expr(__builtin_clzll(4) == 61, 1, -1),
7+
: __builtin_choose_expr(__builtin_clzll(1024) == 53, 1, -1),
8+
: __builtin_choose_expr(__builtin_clzll(1024 * 1024) == 43, 1, -1);
9+
} s1;

0 commit comments

Comments
 (0)