Skip to content

Commit bffa8e1

Browse files
authored
[clang][Interp] Implement __builtin_clrsb (#72243)
1 parent c3b9c36 commit bffa8e1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

clang/lib/AST/Interp/InterpBuiltin.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,15 @@ static bool interp__builtin_parity(InterpState &S, CodePtr OpPC,
448448
return true;
449449
}
450450

451+
static bool interp__builtin_clrsb(InterpState &S, CodePtr OpPC,
452+
const InterpFrame *Frame,
453+
const Function *Func, const CallExpr *Call) {
454+
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
455+
APSInt Val = peekToAPSInt(S.Stk, ArgT);
456+
pushInt(S, Val.getBitWidth() - Val.getSignificantBits());
457+
return true;
458+
}
459+
451460
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
452461
const CallExpr *Call) {
453462
InterpFrame *Frame = S.Current;
@@ -592,6 +601,13 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
592601
return retInt(S, OpPC, Dummy);
593602
break;
594603

604+
case Builtin::BI__builtin_clrsb:
605+
case Builtin::BI__builtin_clrsbl:
606+
case Builtin::BI__builtin_clrsbll:
607+
if (interp__builtin_clrsb(S, OpPC, Frame, F, Call))
608+
return retInt(S, OpPC, Dummy);
609+
break;
610+
595611
default:
596612
return false;
597613
}

clang/test/AST/Interp/builtin-functions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,17 @@ namespace parity {
310310
char parity10[__builtin_parityll(1LL << (BITSIZE(long long) - 1)) == 1 ? 1 : -1];
311311
}
312312

313+
namespace clrsb {
314+
char clrsb1[__builtin_clrsb(0) == BITSIZE(int) - 1 ? 1 : -1];
315+
char clrsb2[__builtin_clrsbl(0L) == BITSIZE(long) - 1 ? 1 : -1];
316+
char clrsb3[__builtin_clrsbll(0LL) == BITSIZE(long long) - 1 ? 1 : -1];
317+
char clrsb4[__builtin_clrsb(~0) == BITSIZE(int) - 1 ? 1 : -1];
318+
char clrsb5[__builtin_clrsbl(~0L) == BITSIZE(long) - 1 ? 1 : -1];
319+
char clrsb6[__builtin_clrsbll(~0LL) == BITSIZE(long long) - 1 ? 1 : -1];
320+
char clrsb7[__builtin_clrsb(1) == BITSIZE(int) - 2 ? 1 : -1];
321+
char clrsb8[__builtin_clrsb(~1) == BITSIZE(int) - 2 ? 1 : -1];
322+
char clrsb9[__builtin_clrsb(1 << (BITSIZE(int) - 1)) == 0 ? 1 : -1];
323+
char clrsb10[__builtin_clrsb(~(1 << (BITSIZE(int) - 1))) == 0 ? 1 : -1];
324+
char clrsb11[__builtin_clrsb(0xf) == BITSIZE(int) - 5 ? 1 : -1];
325+
char clrsb12[__builtin_clrsb(~0x1f) == BITSIZE(int) - 6 ? 1 : -1];
326+
}

0 commit comments

Comments
 (0)