3 ms·
> it's more complicated to implement this in the case where your integer type doesn't actually match the register size of your hardware (which is true with int
by chrisseaton 8y ago
> it's more complicated to implement this in the case where your integer type doesn't actually match the register size of your hardware (which is true with int on LP64)
If you do arithmetic on a 32 bit register on AMD64 you still get 32 bit wraparound don't you?
- saagarjha 8y agoThis is generally an issue with loops that involve memory accesses, because it causes annoying problems when optimizing: https://gist.github.com/rygorous/e0f055bfb74e3d5f0af20690759de5a7 https://gist.github.com/rygorous/e0f055bfb74e3d5f0af20690759...
- xenadu02 8y agoMaking signed overflow/underflow trapping behavior would also be acceptable. I don't think the performance improvements are worth the security vulnerabilities, besides which the loop variable issue could be solved by adding `auto` and promoting its use - then the compiler can choose the loop counter type appropriate for the machine since the int-as-64-bit ship already sailed. (Clang implements this as `__auto_type` and it harmonizes with C++; I've done #define auto __auto_type in projects and it cleans up a lot of C code quite nicely. I'm somewhat surprised no one has offered it as a solution since it is already a reserved keyword).
- saagarjha 8y ago> I don't think the performance improvements are worth the security vulnerabilities This is C++ you're talking about, that's never the right trade-off :P > the loop variable issue could be solved by adding `auto` and promoting its use - then the compiler can choose the loop counter type appropriate for the machine since the int-as-64-bit ship already sailed I'm not sure this completely solves the problem, since often you're doing something like this (I know, I know, iterators are the way to do this, but people do write code like this): for (int i = 0; i < some_container.size(); ++i) { // do something } Auto doesn't really help here, because if you do something like for (auto i = 0; i < some_container.size(); ++i) { // do something } the compiler can't look at your code and say "i really looks like it was meant to be a 64-bit index", it needs to be stupid and deduce it to be an int or at least behave as an int would. Unless I'm misunderstanding what you're trying to say? > (Clang implements this as `__auto_type` and it harmonizes with C++; I've done #define auto __auto_type in projects and it cleans up a lot of C code quite nicely. I'm somewhat surprised no one has offered it as a solution since it is already a reserved keyword). Well, you're technically breaking code that actually uses auto the way it was intended to be used, though of course since it's so useless this kind of code is very rare.
- colanderman 8y ago> the loop variable issue could be solved by adding `auto` I'm not sure how this helps? The compiler doesn't get to choose based on the machine register width – it's bound to choose based on the type of the expression, which is defined by C/C++ integer promotion rules. The expression `0` is an `int` regardless of the context in which it appears, and thus will be an `auto` variable initialized to `0`. You're better off using `ptrdiff_t` than `auto` for loop indices if you want something sized to native register width.
- grandmczeb 8y ago> The expression `0` is an `int` regardless of the context in which it appears Not totally true. In C, 0 is a special value that can be an integer or a null pointer literal depending on the context.
- Joky 8y ago> Making signed overflow/underflow trapping behavior would also be acceptable. You can just add the right compiler flag if this is the right tradeoff for you application (it isn't for everyone).
- usefulcat 8y agoI wonder, would using int_fast32_t be a viable solution to the problem described above?