3 ms·
The type of "uint8_t << 1" being "uint8_t" would be confusing because it would always lose the top bits (unlike the current definition), but presumably the inte
by astrange 5y ago
The type of "uint8_t << 1" being "uint8_t" would be confusing because it would always lose the top bits (unlike the current definition), but presumably the intent is to insert it into a larger bitfield or multiply by 2.
An alternative safer model called as-if-infinitely ranged integers solves this problem:
https://resources.sei.cmu.edu/library/asset-view.cfm?assetid=9019 https://resources.sei.cmu.edu/library/asset-view.cfm?assetid...
- kazinator 5y agoNote that, say, SHL 1, AL ;; x86 assembly will not affect any part of the EAX register other than the low 8 bits specified by AL. This is very simple and obvious. An operation involving uint8_t staying entirely in that type is a complete no-brainer. If you want to shift bits out of the uint8_t, you cast the value to something wider, and shift that. And note that this is what you have to do when working with types that are as wide as int, or wider, which do not promote. uint32_t << 1 will lose the top bits! Losing promotion would make everything consistent: uint8_t << 1 loses the top bit the same way like uint16_t << 1 and uint32_t << 1. "As if infinitely ranged" (AIIR) is an obvious idea but it has flaws. One is the run-time checking that it requires, which is unattractive in a C-like language. The other is semantic issues. In a language like C, the values of calculations eventually have to settle into typed boxes. Under AIIR, these are not equivalent: int x = 42; int y = x * x + 3; int z = 2 * y / x; int z = 2 * (x * x + 3) / x; In the second version, we have not bound the (x * x + 3) term into a variable, but inlined it into the z calculation. Therefore, its result of that subexpression permitted to be outside of the range of int by AIIR. This is completely unacceptable, though; simply introducing a variable of the precisely correct type to hold the value of an intermediate calculation changes the semantics of the calculation. Only the values being juggled in the AIIR, so to speak, benefit; not values that have landed.
- astrange 5y ago> "As if infinitely ranged" (AIIR) is an obvious idea but it has flaws. One is the run-time checking that it requires, which is unattractive in a C-like language. It seems to be acceptable in Swift so far. (Swift traps on overflow on all expressions though, it doesn't use AIR.) It is unpleasant if you don't like to distinguish between statements and expressions, but it might be usable. Anything is as long as it's well defined. The main advantage is that it makes it easier to fold away intermediate steps like (x + 1 - 1). This is the kind of thing that looks pointless, but is needed to remove abstractions like in macros or C++y generic code. If you'd like your functions total (not trapping), then there could be a set of wrapping/saturating operations to go with the default infinitely-ranged ones. Or you could declare x/y/z to be unsigned.