Live data from Hacker News

Signed Integers Are Two’s Complement

open-std.org

21–30 of 126 posts

Re: Signed Integers Are Two’s Complement

#21

> Overflow in the positive direction shall wrap around This appears to be defining signed integer overflow semantics, which prevents the compiler from doing certain basic optimizations, for example, that (x*2)/2 == x. Is that part of this? Has anyone measured the perf cost on a real program?

> for example, that (x&~1*2)/2 == x

Fixed that for you.

Re: Signed Integers Are Two’s Complement

#22

Earlier quoted context omitted.

Is there a good way to represent floating points in order to do complement arithmetic?

Yes, see JavaScript: https://www.w3schools.com/js/js_numbers.asp

JS is IEEE754, not a (one's or two's) complement-based representation.

Re: Signed Integers Are Two’s Complement

#23

> Overflow in the positive direction shall wrap around This appears to be defining signed integer overflow semantics, which prevents the compiler from doing certain basic optimizations, for example, that (x*2)/2 == x. Is that part of this? Has anyone measured the perf cost on a real program?

> for example, that (x&~1*2)/2 == x Fixed that for you.

No?

If x is 3, then (x * 2) / 2 is also equal to 3 (as per the GP) but (x & ~1 * 2) / 2 is equal to 0.

(If you meant ((x & ~1) * 2) / 2, then that is equal to 2).

Re: Signed Integers Are Two’s Complement

#24
post #3

I'm curious why some old architectures didn't use two's complement for signed numbers. What advantage did one's complement or signed magnitude have over two's complement?

In addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.

Sign-magnitude for the significand, and offset-binary for the exponent. The reason for this odd combination is probably historical.

Re: Signed Integers Are Two’s Complement

#25

> Overflow in the positive direction shall wrap around This appears to be defining signed integer overflow semantics, which prevents the compiler from doing certain basic optimizations, for example, that (x*2)/2 == x. Is that part of this? Has anyone measured the perf cost on a real program?

It prevents risky optimisations; this now requires the compiler to prove that such optimisations won't change the semantics of the code, e.g. in your case by essentially proving that the high 2 bits of x (only 1 in the unsigned case, due to sign-extension) will never be set. ...and it could be argued that if the compiler couldn't prove that was true, then it just helped you find a possible overflow bug in the code. I…

However it also prevents "abort on overflow" implementations from being conforming, which look like a much better way of finding actual overflow bugs in the code.

Re: Signed Integers Are Two’s Complement

#26

Earlier quoted context omitted.

In addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.

Sign-magnitude for the significand, and offset-binary for the exponent. The reason for this odd combination is probably historical.

It doesn't seem odd to me. When describing things in nature, (+x, -x) tend to have more symmetry than (x, 1/x).

Re: Signed Integers Are Two’s Complement

#27

Earlier quoted context omitted.

In addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.

Sign-magnitude for the significand, and offset-binary for the exponent. The reason for this odd combination is probably historical.

It's done so that the bits will compare the same way whether treated as float or int. (modulo NaNs and stuff)

Re: Signed Integers Are Two’s Complement

#28

Earlier quoted context omitted.

In addition to what's mentioned in the already great sibling comments, it's worth noting that IEEE floating point is signed-magnitude.

Is there a good way to represent floating points in order to do complement arithmetic?

If you know a bit about the range you need to support you could use a fixed point representation

Re: Signed Integers Are Two’s Complement

#30

> Overflow in the positive direction shall wrap around This appears to be defining signed integer overflow semantics, which prevents the compiler from doing certain basic optimizations, for example, that (x*2)/2 == x. Is that part of this? Has anyone measured the perf cost on a real program?

It prevents risky optimisations; this now requires the compiler to prove that such optimisations won't change the semantics of the code, e.g. in your case by essentially proving that the high 2 bits of x (only 1 in the unsigned case, due to sign-extension) will never be set. ...and it could be argued that if the compiler couldn't prove that was true, then it just helped you find a possible overflow bug in the code. I…

The compiler could help you more by emitting trapping arithmetic. Code that triggered signed overflow will probably not be "fixed" by this change alone.

For example consider the classic binary search blunder: `mid = (low + high)/2`. Defining signed overflow may avoid the UB in computing mid, but now we have a surprise negative value and it's easy to guess what happens next.

It will be fun to see the trophies from this: perf regressions, bugs exposed, bugs fixed.

Post reply on HN