Hi,
Does anyone know of a nice way to detect 64 x 64 -> 64 multiply overflow on aarch64?
On x86 you can just use the V flag, but aarch64 seems to have no option to set the flags after a MUL instruction which I find very odd.
The only solution I can think of is to do the equivalent of
r = a * b; if (r/a != b) overflow();
There is some short circuiting you can do to avoid the div if you know there is no possibility it could have overflowed.
Eg.
if (clz(labs(a)) + clz(labs(b)) < 32) { r = a * b; if (r/a != b) overflow(); }
Then there is the special case of a = 0x8000000000000000 and b == -1.
All in all its a mess just to detect overflow.
Regards, Ed.
On 4 July 2014 18:13, Edward Nevill edward.nevill@linaro.org wrote:
Hi,
Does anyone know of a nice way to detect 64 x 64 -> 64 multiply overflow on aarch64?
On x86 you can just use the V flag, but aarch64 seems to have no option to set the flags after a MUL instruction which I find very odd.
The only solution I can think of is to do the equivalent of
r = a * b; if (r/a != b) overflow();
There is some short circuiting you can do to avoid the div if you know there is no possibility it could have overflowed.
Eg.
if (clz(labs(a)) + clz(labs(b)) < 32) { r = a * b; if (r/a != b) overflow(); }
Then there is the special case of a = 0x8000000000000000 and b == -1.
All in all its a mess just to detect overflow.
If you are saying AArch64, I am assuming you can use inline asm and umulh, no?
On Fri, 2014-07-04 at 18:17 +0200, Ard Biesheuvel wrote:
On 4 July 2014 18:13, Edward Nevill edward.nevill@linaro.org wrote:
If you are saying AArch64, I am assuming you can use inline asm and umulh, no?
Thank you, thank you, thank you,
You just saved me some very messy coding, Ed.