@@ -678,30 +678,6 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
678678 return true ;
679679}
680680
681- static bool interp__builtin_parity (InterpState &S, CodePtr OpPC,
682- const InterpFrame *Frame,
683- const CallExpr *Call) {
684- APSInt Val = popToAPSInt (S, Call->getArg (0 ));
685- pushInteger (S, Val.popcount () % 2 , Call->getType ());
686- return true ;
687- }
688-
689- static bool interp__builtin_clrsb (InterpState &S, CodePtr OpPC,
690- const InterpFrame *Frame,
691- const CallExpr *Call) {
692- APSInt Val = popToAPSInt (S, Call->getArg (0 ));
693- pushInteger (S, Val.getBitWidth () - Val.getSignificantBits (), Call->getType ());
694- return true ;
695- }
696-
697- static bool interp__builtin_bitreverse (InterpState &S, CodePtr OpPC,
698- const InterpFrame *Frame,
699- const CallExpr *Call) {
700- APSInt Val = popToAPSInt (S, Call->getArg (0 ));
701- pushInteger (S, Val.reverseBits (), Call->getType ());
702- return true ;
703- }
704-
705681static bool interp__builtin_classify_type (InterpState &S, CodePtr OpPC,
706682 const InterpFrame *Frame,
707683 const CallExpr *Call) {
@@ -736,16 +712,6 @@ static bool interp__builtin_expect(InterpState &S, CodePtr OpPC,
736712 return true ;
737713}
738714
739- static bool interp__builtin_ffs (InterpState &S, CodePtr OpPC,
740- const InterpFrame *Frame,
741- const CallExpr *Call) {
742- APSInt Value = popToAPSInt (S, Call->getArg (0 ));
743-
744- uint64_t N = Value.countr_zero ();
745- pushInteger (S, N == Value.getBitWidth () ? 0 : N + 1 , Call->getType ());
746- return true ;
747- }
748-
749715static bool interp__builtin_addressof (InterpState &S, CodePtr OpPC,
750716 const InterpFrame *Frame,
751717 const CallExpr *Call) {
@@ -3158,18 +3124,25 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
31583124 case Builtin::BI__builtin_parity:
31593125 case Builtin::BI__builtin_parityl:
31603126 case Builtin::BI__builtin_parityll:
3161- return interp__builtin_parity (S, OpPC, Frame, Call);
3162-
3127+ return interp__builtin_elementwise_int_unaryop (
3128+ S, OpPC, Call, [](const APSInt &Val) -> APInt {
3129+ return APInt (Val.getBitWidth (), Val.popcount () % 2 );
3130+ });
31633131 case Builtin::BI__builtin_clrsb:
31643132 case Builtin::BI__builtin_clrsbl:
31653133 case Builtin::BI__builtin_clrsbll:
3166- return interp__builtin_clrsb (S, OpPC, Frame, Call);
3167-
3134+ return interp__builtin_elementwise_int_unaryop (
3135+ S, OpPC, Call, [](const APSInt &Val) -> APInt {
3136+ return APInt (Val.getBitWidth (),
3137+ Val.getBitWidth () - Val.getSignificantBits ());
3138+ });
31683139 case Builtin::BI__builtin_bitreverse8:
31693140 case Builtin::BI__builtin_bitreverse16:
31703141 case Builtin::BI__builtin_bitreverse32:
31713142 case Builtin::BI__builtin_bitreverse64:
3172- return interp__builtin_bitreverse (S, OpPC, Frame, Call);
3143+ return interp__builtin_elementwise_int_unaryop (
3144+ S, OpPC, Call,
3145+ [](const APSInt &Val) -> APInt { return Val.reverseBits (); });
31733146
31743147 case Builtin::BI__builtin_classify_type:
31753148 return interp__builtin_classify_type (S, OpPC, Frame, Call);
@@ -3209,7 +3182,11 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
32093182 case Builtin::BI__builtin_ffs:
32103183 case Builtin::BI__builtin_ffsl:
32113184 case Builtin::BI__builtin_ffsll:
3212- return interp__builtin_ffs (S, OpPC, Frame, Call);
3185+ return interp__builtin_elementwise_int_unaryop (
3186+ S, OpPC, Call, [](const APSInt &Val) {
3187+ return APInt (Val.getBitWidth (),
3188+ Val.isZero () ? 0u : Val.countTrailingZeros () + 1u );
3189+ });
32133190
32143191 case Builtin::BIaddressof:
32153192 case Builtin::BI__addressof:
0 commit comments