How is the sign flag calculated with the imul instruction?
Asked Answered
I

1

7

The documentation for imul states that:

SF is updated according to the most significant bit of the operand-size-truncated result in the destination.

For a 64-bit operation, then, my understanding is that SF = (a * b) >> 63, or more simply if a and b are signed, SF = a * b < 0.

However, I'm getting an unexpected result multiplying two large numbers:

mov rax, 0x9090909090909095
mov rdx, 0x4040404040404043
imul rax, rdx

The result of 0x9090909090909095 * 0x4040404040404043 is 0xefcba7835f3b16ff. It has the sign bit set, however the SF flag is cleared after the imul instruction. What's going on?


This was cross-posted to the Intel forums some time ago.

Inadequate answered 27/4, 2015 at 16:51 Comment(8)
I spy an error in that documentation: SF ← TMP_XP[32];, should be 31 obviously. What does Intel have to say about this instruction, by the way?Refreshment
@Jongware, that's true. I'll check if the most up-to-date Volume 2A still has that. This documentation was automatically extracted from the September 2014 version, so that's what Intel had to say about it at that time.Inadequate
@Jongware, the most current version of Volume 2A still has that error.Inadequate
@usr2564301: Looks like it's fixed now in the version on felixcloutier.com/x86/IMUL.html, but the formatting is garbage. github.com/HJLebbink/asm-dude/wiki/IMUL is a better-maintained extract of the PDF (more up to date, and better formatting). Anyway, Intel's manual now says "SF, ZF, AF, and PF flags are undefined.", and both HTML-extract versions have that wording. Only OF and CF are defined after IMUL, apparently, so the documentation now matches what we see on real CPUs. That wasn't the first documentation error, and it won't be the last.Complimentary
@PeterCordes, I edited my answer to say that. Regarding the current state of the IMUL translation on my site, I'm working on it and will probably have something better to show in a few days. HJLebbink's conversion script is a fork of mine and shares some issues (the lists are broken, for instance; which is probably a step up from missing, but that type of mis-translation is the reason I'm working on a new version of it). Regarding being up-to-date, I can't find the December 2017 revision that this is apparently coming from; the most recent I can find is September 2016.Inadequate
felixcloutier.com is your site? Cool. Nice work, I used to link from there all the time in my SO answers, but for some reason the formatting got worse in the last few months (especially of the opcode table that lists the various forms and which mode they're valid in) so I went looking for an alternate PDF->HTML scrape of Intel's PDF.Complimentary
I'm acutely aware of a lot of problems with the latest update that I did and I believe that I can address a lot of them. If you want to talk about it, you can grab an email that I check by cloning the git repo for the project and checking git log.Inadequate
I probably shouldn't take the time to look at the parsing code; and I don't have Python experience anyway. I'll post these comments as issues on the github page, just so everyone else can see they're already known issues.Complimentary
I
5

Other sources say that SF is undefined after imul. This most likely means that the result of SF is well-defined on newer processors, but older ones don't offer the feature. My computer being 5 years old, I probably fall with the second category.

EDIT: using Archive.org's Wayback Machine, I found that the documentation changed from stating that SF is undefined to SF is defined in the September 2014 revision. The previous revision, June 2014, still says that SF is undefined. This is documented in the accompanying Documentation Changes document, though the rationale for the change is not.

EDIT 2 My CPU is an i7 M 620. I had access to an even older Core2Duo P7550 and was able to confirm that imul doesn't set SF on it either.

EDIT 3 Starting from the September 2016 edition, IMUL says that SF is undefined, so this resolves the issue.

Inadequate answered 27/4, 2015 at 17:8 Comment(5)
Five years isn't that old, Intel hasn't made any fundamental changes to their architecture in that time.Fifteenth
Be sure to post any better explanation you find. Do you know any place that tracks the changes between documentation sets?Inadequate
Well, the document states that it applies to processors all the way back to the Pentium. Variations between processors is supposed be documented. So it's either a bug in your processor, an error in the documentation, you're not using an Intel processor, or something else is going on.Fifteenth
@RossRidge, there is nothing in the change history document about it, but I dug up a 2003 version that states that SF is undefined after imul. I'm still looking for when exactly this change happened. Archive.org has a copy of every manual since 2012 (and the March 2013 version also says SF is undefined).Inadequate
The problem is knowing when it changed in the manual doesn't tell us anything. There have been changes made to the manual like this in the past that weren't a result of a change in behaviour, just describing old behaviour that wasn't previously documented.Fifteenth

© 2022 - 2024 — McMap. All rights reserved.