OpcodeDispatcher: optimize bextr's (1 << x) - 1

little algebraic trick I cribbed from llvm

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
This commit is contained in:
Alyssa Rosenzweig committed 2024-01-30 22:28:06 -04:00
1 parent c9461d9997
commit f66085f4a7
1 file changed
+4 -4
@@ -2138,12 +2138,12 @@ void OpDispatchBuilder::BEXTRBMIOp(OpcodeArgs) {
Length, MaxSrcBitOp);
// Now build up the mask
// (1 << SanitizedLength) - 1
auto One = _Constant(SrcSize, 1);
auto Mask = _Sub(IR::SizeToOpSize(Size), _Lshl(IR::SizeToOpSize(Size), One, SanitizedLength), One);
// (1 << SanitizedLength) - 1 = ~(~0 << SanitizedLength)
auto AllOnes = _Constant(~0ull);
auto InvertedMask = _Lshl(IR::SizeToOpSize(Size), AllOnes, SanitizedLength);
// Now put it all together and make the result.
auto Dest = _And(IR::SizeToOpSize(Size), SanitizedShifted, Mask);
auto Dest = _Andn(IR::SizeToOpSize(Size), SanitizedShifted, InvertedMask);
// Finally store the result.
StoreResult(GPRClass, Op, Dest, -1);