Re: [eigen] Signed or unsigned indexing

[ Thread Index | Date Index | More Archives ]

a couple of notes:

 - i dont believe there is any chance at all of Eigen changing this now -- at least I had been writing in this thread assuming only idle discussion. That would be a very intrusive breaking change to make at this point.

 - i do agree with the argument that unsigned makes decrementing loop error-prone at 0. It is the same argument that was made on this mailing list many years ago and motivated the choice of signed indices. We are in violent agreement! :-) I just wanted to point out how it's not clear-cut as both signednesses come with strengths and weaknesses.

The C++ committee theoretically could acknowledge that 2's complement has won and make signed overflow well-defined behavior (as wrapping); if they did that, that would remove one of the main issues with signed and arguably that would start to make it look like a no-brained. But compiler authors wouldn't like that, because it would take away the optimization opportunities.


2017-01-20 13:51 GMT-05:00 Martin Beeger <martin..beeger@xxxxxxxxx>:
Some more thoughts on this topic:

* Jon Kalb (signed integer fraction) has pointed out that repeatedly obnoxious bugs with unsigned integer underflow and bounds wraparound when subtracting sizes went unnoticed very very long even in libraries like STL & boost.

* Andrej Alexandrescu pointed out that whether using indices or pointers in algorithmic loops is faster seems to change every five years or so. That means, that using indices instead of pointers is a valid strategy and may increase your performance.

* That the specified wraparound behaviour when using loop indices inhibits a class of optimizations and therefore especially for fixed but unknown loop lengths there is a inherent performance hit of using unsigned. See CppCon 2016 Michael Spencer - My Little Optimizer: undefined behaviour is magic.

* When pressed to answer why or how it came that std::size_t became unsigned Stoustrup replied: Someone had a case for needing to reserve more than half the address space in a vector and the number of elements to be strictly positive. (sadly I could not find the reference here) Which is funny, because you cannot actually use more than half, as Mark pointed out.

Am 20.01.2017 um 18:48 schrieb Mark Borgerding:

On the other side, I understand people who use std::size_t for indexing because it is consistent with the standard library.

* Eric Nieblers STL2 project has plans on using signed. See discussion here: So if Eigen now chooses unsigned for compatibility and the project STL2 ever flies, we might look like fools ;)

For me the argument that loop optimization suffers from using unsigned and this is inherent in the language (and cannot be fully solved by compiler vendors) is the strongest argument, as performance matters for Eigen's loop and indexes might likely end up being used in their exit conditions.

Regards, Martin

Am 20.01.2017 um 18:48 schrieb Mark Borgerding:
Well made points.  These greatly expand on  points I made on this list in May 2010.

Mail converted by MHonArc 2.6.19+