x86: Carry flag calculated incorrectly for LZCNT if source=dest #6556
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The carry flag for the LZCNT instruction should be calculated based on the input value of the source operand. However, if the destination operand is the same as the source operand (e.g.,
LZCNT EAX,EAX
) then the carry flag is computed based the result of the LZCNT operation instead of the input value.This PR fixes this by storing the input value into a temporary which is later used for computing the carry flag.
e.g.,
66f30fbdc0 "LZCNT AX,AX" wwith AX=ffff
x86:LE:64:default
(Existing): { AX=0x0, ZF=1, CF=1 }x86:LE:64:default
(This patch): { AX=0x0, ZF=1, CF=0 }f30fbdc0 "LZCNT EAX,EAX" with EAX=0xffff_ffff
x86:LE:64:default
(Existing): { EAX=0x0, ZF=1, CF=1 }x86:LE:64:default
(This patch): { EAX=0x0, ZF=1, CF=0 }f3480fbdc0 "LZCNT RAX,RAX" with RAX=0xffff_ffff_ffff_ffff
x86:LE:64:default
(Existing): { RAX=0x0, ZF=1, CF=1 }x86:LE:64:default
(This patch): { RAX=0x0, ZF=1, CF=0 }