Skip to content

Commit 0f8c51a

Browse files
authored
[clang][Interp] Implement __builtin_parity (#71662)
1 parent 6416b2d commit 0f8c51a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

clang/lib/AST/Interp/InterpBuiltin.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
439439
return true;
440440
}
441441

442+
static bool interp__builtin_parity(InterpState &S, CodePtr OpPC,
443+
const InterpFrame *Frame,
444+
const Function *Func, const CallExpr *Call) {
445+
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
446+
APSInt Val = peekToAPSInt(S.Stk, ArgT);
447+
pushInt(S, Val.popcount() % 2);
448+
return true;
449+
}
450+
442451
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
443452
const CallExpr *Call) {
444453
InterpFrame *Frame = S.Current;
@@ -576,6 +585,13 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
576585
return retInt(S, OpPC, Dummy);
577586
break;
578587

588+
case Builtin::BI__builtin_parity:
589+
case Builtin::BI__builtin_parityl:
590+
case Builtin::BI__builtin_parityll:
591+
if (interp__builtin_parity(S, OpPC, Frame, F, Call))
592+
return retInt(S, OpPC, Dummy);
593+
break;
594+
579595
default:
580596
return false;
581597
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ namespace SourceLocation {
274274
}
275275
}
276276

277+
#define BITSIZE(x) (sizeof(x) * 8)
277278
namespace popcount {
278279
static_assert(__builtin_popcount(~0u) == __CHAR_BIT__ * sizeof(unsigned int), "");
279280
static_assert(__builtin_popcount(0) == 0, "");
@@ -283,7 +284,6 @@ namespace popcount {
283284
static_assert(__builtin_popcountll(0) == 0, "");
284285

285286
/// From test/Sema/constant-builtins-2.c
286-
#define BITSIZE(x) (sizeof(x) * 8)
287287
char popcount1[__builtin_popcount(0) == 0 ? 1 : -1];
288288
char popcount2[__builtin_popcount(0xF0F0) == 8 ? 1 : -1];
289289
char popcount3[__builtin_popcount(~0) == BITSIZE(int) ? 1 : -1];
@@ -295,3 +295,18 @@ namespace popcount {
295295
char popcount9[__builtin_popcountll(0xF0F0LL) == 8 ? 1 : -1];
296296
char popcount10[__builtin_popcountll(~0LL) == BITSIZE(long long) ? 1 : -1];
297297
}
298+
299+
namespace parity {
300+
/// From test/Sema/constant-builtins-2.c
301+
char parity1[__builtin_parity(0) == 0 ? 1 : -1];
302+
char parity2[__builtin_parity(0xb821) == 0 ? 1 : -1];
303+
char parity3[__builtin_parity(0xb822) == 0 ? 1 : -1];
304+
char parity4[__builtin_parity(0xb823) == 1 ? 1 : -1];
305+
char parity5[__builtin_parity(0xb824) == 0 ? 1 : -1];
306+
char parity6[__builtin_parity(0xb825) == 1 ? 1 : -1];
307+
char parity7[__builtin_parity(0xb826) == 1 ? 1 : -1];
308+
char parity8[__builtin_parity(~0) == 0 ? 1 : -1];
309+
char parity9[__builtin_parityl(1L << (BITSIZE(long) - 1)) == 1 ? 1 : -1];
310+
char parity10[__builtin_parityll(1LL << (BITSIZE(long long) - 1)) == 1 ? 1 : -1];
311+
}
312+

0 commit comments

Comments
 (0)