2 ms·
In the N3096 working draft it is written: "The sign representation defined in this document is called two’s complement. Previous revisions of this document addi
by kazinator 3y ago
In the N3096 working draft it is written: "The sign representation defined in this document is called two’s complement. Previous revisions of this document
additionally allowed other sign representations."
Non-two's complement machines are museum relics, and are no longer going to be supported by ISO C.
> why clutter things with extra masking.
Because even if the circular buffer is a power of two, its size doesn't necessarily line up with the range of a given unsigned type.
If the buffer doesn't have a width of 256, 65536, or 4294967296, then you're out of luck; you can't just uint8_t, uint16_t or uint32_t as the circular buffer index without masking to the actual power-of-two size.
(Note that uint16_t and uint8_t promote to int (on the overwhelming majority of platforms where their range fits into that type), so you don't get away from reasoning about signed arithmetic for those.)
- epcoa 3y agoSo then what is the advantage of using a signed type in this case? And C++20 already standardized it I know that I already acknowledged this. Should I go back and rewrite all the old correct code so you feel better?
- kazinator 3y agoThe main advantage is not foisting unsigned on the user of the API. (You can do that while using unsigned internally, but then you have to convert back and forth.) The most important decision is what is the index type at the API level of the circular buffer, not what is inside it. But it's nicer if you can just use the API one inside. The sizeof operator yielding the type size_t which is unsigned has done a lot of harm. Particularly the way it spread throughout the C library. Why do we have size_t being unsigned? Because on small systems, where we have 16 bit sizes, signed means limiting to 32767 bytes, which is a problem. In all other ways, it's a downer. Whenever you mention sizeof, you have unsigned arithmetic creeping into the calculation. The author of the above blog article has the right idea to want a sizeof operator that yields ptrdiff_t instead of size_t. (Unfortunately, the execution is bungled; he redefined a language keyword as a macro, and on top of that didn't wrap the macro expansion in parentheses, even.)
- epcoa 3y ago> If the buffer doesn't have a width of 256, 65536, or 4294967296, then you're out of luck Why so much hyperbole? You’re not out of luck. You can atomic increment/add the unsigned no matter the buffer size. You don’t worry about overflow like you would with a signed type. You can mask after. And you continue to avoid answering the simple question: what is the advantage of the signed type. I’ve already outlined the one with unsigned, especially with atomics.
- kazinator 3y agoComment said "why clutter with extra masking" (just use the unsigned types). Although unsigned types have no overflow, running to them as some sort of safe refuge is a mistaken knee-jerk reaction.