6 ms·
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r4.html#survey http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p090..., concludes ”the onl
by Someone 8y ago
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r4.html#survey http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p090..., concludes
”the only machine the author could find using non-two’s complement are made by Unisys, and no counter-example was brought by any member of the C++ standards committee. Nowadays Unisys emulates their old architecture using x86 CPUs with attached FPGAs for customers who have legacy applications which they’ve been unable to migrate. These applications are unlikely to be well served by modern C++, signed integers are the least of their problem. Post-modern C++ should focus on serving its existing users well, and incoming users should be blissfully unaware of integer esoterica.”
That led to the decision that ”Signed Integers are Two’s Complement” in C++ 20.
That makes it too easy to write portable code :-)
- thestoicattack 8y agoA talk by that paper's author: https://www.youtube.com/watch?v=JhUxIVf1qok https://www.youtube.com/watch?v=JhUxIVf1qok
- favorited 8y agoReally cool that this change is likely to make it through WG14 as well.
- xenadu02 8y agoUnfortunately the working group rejected the author's proposal that signed integers have wrapping behavior, so signed overflow/underflow remain undefined: > Status-quo If a signed operation would naturally produce a value that is not within the range of the result type, the behavior is undefined. The author had hoped to make this well-defined as wrapping (the operations produce the same value bits as for the corresponding unsigned type), but WG21 had strong resistance against this. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r4.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p090...
- saagarjha 8y agoI consider this less of a shoo-in, in my opinion, since 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). With unsigned integers you can just mask off the extra bits when overflow occurs, but with signed integers this is more complicated. Having this be implementation-specific behavior might be useful, so you don't have undefined behavior in your program, but I am generally of the opinion that overflowing an int is usually a bug rather than a feature.
- 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).
- reality_czech 8y ago"Post-modern" C++? Finally someone admits that the language was all a huge prank.
- arximboldi 8y agohttps://www.youtube.com/watch?v=y2___qaG_9Q https://www.youtube.com/watch?v=y2___qaG_9Q https://www.youtube.com/watch?v=QTLn3goa3A8 https://www.youtube.com/watch?v=QTLn3goa3A8
- Koshkin 8y agoC++ is more like a drug - having used it makes it impossible to go back to C.
- kazinator 8y agoIt is absolutely possible. I did C++ development as a regular job years ago, now I'm coding in C (for that type of coding). What will get you off the C++ drug easily is Lisp. Once I discovered Lisp, C++ had no place in my "personal spectrum" any more, but C (including the C-like subset of C++) still did. Well, it wasn't so sudden, mind you. More like: the more Lisp I knew, the less interest I had in C++. (Idiotic lambda implementations and whatnot will not woo me back, sorry.)
- svnpenn 8y agoSorry if this is a hated question - but are all Lisp the same rats nest of parenthesis? ive always been interested in Lisp but that is so ugly - i would a JavaScript/Ruby/Python method chaining to 5 levels of nested parenthesis
- billsix 8y agoI implemented a compile-time test framework in Gambit Scheme in 7 lines of code, and Python's yield in about 20 or so. http://billsix.github.io/bug.html#_make_generator http://billsix.github.io/bug.html#_make_generator Lisp code has few wasted moves. Programming in it does require changing your mindset of syntactic beauty. And once your mindset changes, you can make whatever syntax you want. Perhaps the biggest hurdle is that the code is not best read linearly. The reader must understand the order of evaluation, which follows very simple rules, in order to understand the code correctly. That hurdle is definitely worth the jump.
- kazinator 8y ago... but overflowing them is still UB? One advantage of pinning down two's complement is that the result of arithmetic is known down to the bit level, even when it overflows. Also, certain bit operations. For instance, if we know that the representation is two's complement, we are justified in expecting sign extension on a right shift: the sign bit is repeated to fill the top bit positions. (The shift of a signed number should be "arithmetic"; that of unsigned "logical".) Also, truncations (converting a wider type to a narrower type) should have predictable results. For instance if we chop the value -238 to 8 bits, we should get 18, because -238 is 0xFF..FF12, which truncates to 0x12.
- ameliaquining 8y agoSigned integer overflow being UB enables compiler optimizations. Most famously, when compiling for (int i = 0; i < bound; i++) in an LP64 environment (i.e., one where the native machine word is 64 bits but int is 32 bits, as is the case in most modern compilers targeting most modern architectures), it allows i to be represented by a native machine word, increasing performance. You can, and people constantly do, argue about whether this is worth it, but I suspect it's the primary reason why a lot of the committee didn't want to change this behavior.
- kazinator 8y agoIf it's okay to screw programs that make nonportable assumptions, then int should just be 64 bits. After all, int is supposed to be the machine's natural integer. The current fashion is that we like programs that assume int is 32 bits, and don't like ones that assume wraparound arithmetic.
- slededit 8y ago32-bit ints have a more compact encoding in x86 because it was assumed they would be the most common. The "native" size isn't a simple matter these days. Back in the old dark days of 8 and 16-bits common values regularly exceeded the size. But 4 billion is large enough to still be the default.
- 8y ago