News for hackers. Stuff that matters.
Re:New integer types I’d like to see by chrisseaton on Friday September 30, 2022 @01:17PM (#33037681)
AMD64 addressing modes cannot detect overflow, and this is where in practice 90% of arithmetic is done!
Re:New integer types I’d like to see by mikewarot on Friday September 30, 2022 @02:08PM (#33038425)
That can't be true. Why would a compiler use memory access to do math? Math operations are register based, none of the quantities are addresses.

How can you use memory addressing modes to do math, like 987*765?

Re:New integer types I’d like to see by chrisseaton on Friday September 30, 2022 @02:36PM (#33038809)
> That can't be true.

Lol. See for yourself if you don't believe me.

https://godbolt.org/z/o14f7cdsc

> Why would a compiler use memory access to do math?

Because there are efficient instructions for it. Have you written an arithmetic code generation pass? It's normal to do it like this.

> Math operations are register based, none of the quantities are addresses.

An address is just a number. Yeah they're in registers or can be immediate. So what?

> How can you use memory addressing modes to do math, like 987*765?

Because AMD64 addressing modes have a base, a scale, and an offset component. That's multiplying and adding.

Let me know how you think you can work out if that lea overflowed!

Re:New integer types I’d like to see by mikewarot on Friday September 30, 2022 @03:22PM (#33039498)
Wow, that's nuts. I'd have never expected that abuse.

You can use -ftrapv to check for integer overflows, like this

https://godbolt.org/z/snbdadv8b

>Let me know how you think you can work out if that lea overflowed!

LEA (Load Effective Address) can't overflow... memory is supposed to wrap, the byte after the last byte is byte 0.

Re:New integer types I’d like to see by chrisseaton on Friday September 30, 2022 @03:23PM (#33039512)
Right but have you seen what that’s done to your machine code? That’s why people don’t do it normally.

> LEA (Load Effective Address) can't overflow...

You're mistaken - it's defined to overflow.

https://reverseengineering.stackexchange.com/questions/11442...

Re:New integer types I’d like to see by zorgmonkey on Friday September 30, 2022 @05:07PM (#33040832)
I'm not 100% sure, but I think that the behavior of LEA is to wrap, but it does not set the overflow flag
Re:New integer types I’d like to see by mikequinlan on Friday September 30, 2022 @01:27PM (#33037825)
I don't know specific AMD64 addressing modes, but you should always be able to detect overflow in signed 2s complement addition by checking to see if both addends are positive and the result is negative. There is a similar check for subtraction.
Re:New integer types I’d like to see by chrisseaton on Friday September 30, 2022 @01:34PM (#33037952)
Yeah, but the point is addressing modes fuse addition and multiplication and using the result, so there's no opportunity to check anything after the operations.