From b7fea22c7d5716f0f1b6094504f3bc9d9b84b553 Mon Sep 17 00:00:00 2001 From: offtkp Date: Mon, 2 Sep 2024 02:35:13 +0300 Subject: [PATCH] Set CF correctly on BLSMSK patch --- src/core/cpu_patches.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/core/cpu_patches.cpp b/src/core/cpu_patches.cpp index 5021616c5..a7624e303 100644 --- a/src/core/cpu_patches.cpp +++ b/src/core/cpu_patches.cpp @@ -323,9 +323,26 @@ static void GenerateBLSMSK(const ZydisDecodedOperand* operands, Xbyak::CodeGener SaveRegisters(c, {scratch}); + Xbyak::Label set_carry, clear_carry, end; + + // BLSMSK sets CF to zero if source is NOT zero, otherwise it sets CF to one. c.mov(scratch, *src); + c.test(scratch, scratch); + c.jz(set_carry); + c.jmp(clear_carry); + + c.L(set_carry); c.dec(scratch); c.xor_(scratch, *src); + c.stc(); + c.jmp(end); + + c.L(clear_carry); + c.dec(scratch); + c.xor_(scratch, *src); + // We don't need to clear carry here since XOR does that for us + + c.L(end); c.mov(dst, scratch); RestoreRegisters(c, {scratch});