Skip to content

Commit ccf257d

Browse files
authored
Merge pull request #206 from jprotopopov-ut/builtin-clzll-1
Implement `__builtin_clzll` builtin
2 parents 2e73628 + 01006dd commit ccf257d

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/frontc/cabs2cil.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4752,6 +4752,22 @@ 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" && asconst && isEmpty (!prechunk ()) then
4756+
begin
4757+
(* Constant-fold the argument and see if it is a constant *)
4758+
let countLeadingZeros (arg: cilint) pos = pos - Z.numbits arg
4759+
in
4760+
match !pargs with
4761+
[ arg ] -> begin
4762+
match constFold true arg with
4763+
(Const CInt (arg, kind, _)) ->
4764+
piscall := false;
4765+
pres := integer (countLeadingZeros arg 64);
4766+
prestype := intType
4767+
| _ -> ()
4768+
end
4769+
| _ -> ignore (warn "Invalid call to __builtin_clzll");
4770+
end
47554771
end
47564772
| _ -> ()
47574773
);

test/small1/builtin_clzll.c

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

test/testcil.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ sub addToGroup {
557557
addTest("testrun/builtin4 ");
558558
addTest("test/builtin5 ");
559559
addTest("test/builtin6 ");
560+
addTest("testrun/builtin_clzll ");
560561
addTest("test/sync-1 _GNUCC=1");
561562
addTest("test/sync-2 _GNUCC=1");
562563
addTest("test/sync-3 _GNUCC=1");

0 commit comments

Comments
 (0)