7 ms·
One thing jumped out for me. No integer overflow. In my embedded work we use that all the time. We represent angles as a int16_t or uint16_t where you can add a
by phkahler 9y ago
One thing jumped out for me. No integer overflow. In my embedded work we use that all the time. We represent angles as a int16_t or uint16_t where you can add and subtract angles and never have to worry about a discontinuity at 2pi because 2pi=65536.
Is this a hack that should not be allowed? I don't think so. Every processor made (that I'm aware of) in the last 30+ years has used 2's complement arithmetic. I don't think overflow is a bad thing, I think C made a mistake in calling it "undefined". They had to because when the language came along there were other options fresh in peoples minds.
I've also worked on systems in asm that had saturating arithmetic, and that has it's own nice use cases. My preference is still to have the rollover. People can implement bounds checking when they need to.
- heinrich5991 9y agoIn Rust, you can make your intent explicit by calling `wrapping_add` instead of using `+`.
- chewbacha 9y agoAnd here’s some documentation: https://doc.rust-lang.org/std/num/struct.Wrapping.html https://doc.rust-lang.org/std/num/struct.Wrapping.html https://doc.rust-lang.org/std/primitive.u32.html#method.wrapping_add https://doc.rust-lang.org/std/primitive.u32.html#method.wrap... :)
- tspiteri 9y agoOr if this type is used a lot, you can wrap the 16-bit integer inside its own type and overload addition and/or other operators. struct Angle(i16); impl Add for Angle { type Output = Angle; fn add(self, rhs: Angle) -> Angle { Angle(self.0.wrapping_add(rhs.0)) } }
- chewbacha 9y agoThere’s already a `Wrapping<T>` struct in std for you. I don’t think you need to roll your own. Of course, it might make sense in the embedded space to do so.
- deleted 9y ago[deleted]
- jsheard 9y agoWrapping<T> actually comes from core and is just re-exported by std, so embedded targets that lack std support can still use it by importing directly from core. https://doc.rust-lang.org/core/num/struct.Wrapping.html https://doc.rust-lang.org/core/num/struct.Wrapping.html
- chewbacha 9y agoFantastic!
- Sharlin 9y ago`Wrapping<T>` is a great solution. When in doubt, encode the behavior in the type system.
- simias 9y agoUsing 2's complement is not the entire story, some CPUs can trap on overflows (MIPS for instance). X86 doesn't but it does set the carry/overflow flags in case of unsigned/signed overlow to make it easy to test for it if necessary. Leaving the freedom to the implementation to catch signed overflows like it can catch divisions by zero might actually be a feature. Admittedly it's not very consistent to make unsigned overflow defined. I like rust's approach that you can explicitly decide to use overflowing arithmetic if you want, otherwise it triggers a runtime error in debug mode. That alongside with the checked_ variants make it very easy to deal with potentially overflowing code whereas in C it's a minefield, it's not difficult to trigger an UB while trying to defend against an other UB. Ask a beginner to write a "checked_add" function in C (no Stack Overflow allowed) and see how many bugs and UB you can spot. On the other hand advocating for implicit wrapping overflow all the time sounds risky to me. In my experience there's only a very tiny subset of operations where an overflow is desired and wouldn't lead to a bug.
- deleted 9y ago[deleted]
- VMG 9y ago> Is this a hack that should not be allowed? not by default, no
- tdbgamer 9y agoYou just have to intentionally use a method on integers called wrapping_add() to explicitly allow overflows. That said I believe overflows are only checked when you compile with the debug target. So when you compile to the release target it doesn't check them I believe. I personally think it's a good thing to only allow overflows when explicitly allowed.
- steveklabnik 9y agoThe rules are: 1. overflow is a "program error" 2. If `debug_assertions` are enabled, then overflow must panic 3. If overflow does not panic, then it must two's compliment wrap This leaves the door open to always requiring a panic someday in the future, if performance gets there.
- jacquesm 9y ago> If `debug_assertions` are enabled, then overflow must panic It is very risky to have different behavior on such things in test and production.
- steveklabnik 9y agoRust cannot fix all bugs. This isn't a memory safety issue. Sometimes, you have to make tough calls. Maybe Rust++ will fix this, someday :p (Or, once people have the tolerance for the performance degradation and we can turn it on in rustc, exactly why we specified it the way we did.) (Or, you can tweak it to include this behavior in release mode; always an option)
- jacquesm 9y agoThis is not about 'fixing all bugs', it is about re-visiting a specific class of bug that we already know about and have already seen many bad instances of in the wild. Few things are harder to debug than things that pass the tests but fail in production and anything that inserts behavior like that should be avoided like the proverbial plague. If you're already of a mindset to have a Rust++ then you are missing the point, Rust quite possibly has a window of opportunity to displace C but for that to work at a level where it succeeds rather than as an 'also ran' you will need to religiously avoid repeating the past.
- jcelerier 9y ago> Every processor made (that I'm aware of) in the last 30+ years has used 2's complement arithmetic DSPs often use saturating overflow IIRC
- phkahler 9y ago>> DSPs often use saturating overflow IIRC I've used them, and used that feature. It was an option that could be turned on and off. The basic arithmetic was the same as any other CPU unless that was enabled.
- steveklabnik 9y agoSimilar to above comments about wrapping_add, Rust has saturating_add as well.
- Symmetry 9y agoIdeally your ISA would provide adds that wrapped, adds that saturated, and adds that threw an exception on overflow.
- jwilk 9y agoTo clarify, only signed integer overflow is undefined behavior in C. Unsigned integers have wrap-around semantics. (But beware that unsigned integers smaller than int could be automatically promoted to (signed) int!)
- RealityVoid 9y agoThis should be higher up since it's a tiny but important distinction.
- chewbacha 9y ago> (But beware that unsigned integers smaller than int could be automatically promoted to (signed) int!) This caveat sounds an awful lot like undefined behavior ;p
- gpm 9y agoWhen things are promoted is well defined, just subtle behavior.
- masklinn 9y ago> One thing jumped out for me. No integer overflow. FWIW that's not quite true, Rust will panic on overflow in debug but can (and currently does on all platforms I think) allow them in release. They're not UB though, they're defined as 2's complement. > In my embedded work we use that all the time. We represent angles as a int16_t or uint16_t where you can add and subtract angles and never have to worry about a discontinuity at 2pi because 2pi=65536. > Is this a hack that should not be allowed? I don't think so. Rust provides APIs to make these behaviours explicit decisions rather than implicit hacks: https://doc.rust-lang.org/core/num/struct.Wrapping.html https://doc.rust-lang.org/core/num/struct.Wrapping.html, https://doc.rust-lang.org/std/?search=wrapping_ https://doc.rust-lang.org/std/?search=wrapping_, https://doc.rust-lang.org/std/?search=saturating_ https://doc.rust-lang.org/std/?search=saturating_ (and https://doc.rust-lang.org/std/?search=checked_ https://doc.rust-lang.org/std/?search=checked_). > People can implement bounds checking when they need to. Being unsafe by default always ends up in tears. Checking Mitre, just this year there have already been 11 public CVE for integer overflows.
- phkahler 9y agoAfter reading responses and reflecting a bit I guess rust got it mostly right. Undefined behavior is bad, so Rust defined it. Overflow is apparently a problem worth checking for. I don't understand how people are overflowing 32 and 64 bit integers, but OK it's common enough. Having the ability to declare overflow/wraparound as being OK is a good thing so long as it compiles down to the same add or subtract instruction and doesn't add overhead. Rust claims to have zero overhead abstractions, so I'll have to try this some day. TL;DR the decisions made in the design of Rust seem reasonable.
- steveklabnik 9y agohttps://play.rust-lang.org/?gist=5b5df368283645fbec18c4fbaeb3c960&version=stable https://play.rust-lang.org/?gist=5b5df368283645fbec18c4fbaeb... Click "release" and then "asm", you'll see that they are compiled to the exact same thing.
- woolvalley 9y agoYou can overflow 32bit integers fairly easily if your doing math with unix epoch microseconds.
- carlmr 9y agoI'm also working in embedded, and you're picking one of the few examples where you want wrapping behavior. So it's better IMHO to do it the Rust way and only allow wrapping with the Wrapping<T> type. That way you can see where wrapping is part of the program design.
- vbarrielle 9y ago> We represent angles as a int16_t or uint16_t where you can add and subtract angles and never have to worry about a discontinuity at 2pi because 2pi=65536. Not sure I get your trick, 2pi=6.2832 so I don't see how you avoid accumulating errors this way.
- Sean1708 9y agoIt's basically fixed-point arithmetic[0] applied to base tau. So any integer value x that your computer is actually storing is conceptually the value 2 * pi * x / 65536. [0]: https://en.wikipedia.org/wiki/Fixed-point_arithmetic https://en.wikipedia.org/wiki/Fixed-point_arithmetic
- mattkrause 9y agoYou don’t use the value directly. Instead, divide the circle up so that there are 65535 steps around the unit circle between [0, 2*pi) and use these units instead of degrees or radians. This makes integer overflow and wrapping around the circle correspond: going a quarter way around the circle five times, adds up to 90 degrees, rather than 450.
- badpun 9y agoThis is pretty awesome little trick, thanks!
- marcosdumay 9y ago> Is this a hack that should not be allowed? Yep. This is a hack that should not be allowed. If yo want to use it, you should create your own type, with explicit modular arithmetic. Not try to ride architectural dependent assumptions.
- myrrlyn 9y agoFwiw, Rust provides checked_op, saturating_op, and wrapping_op versions of arithmetic, as well as a Wrapping … wrapper https://doc.rust-lang.org/std/num/struct.Wrapping.html https://doc.rust-lang.org/std/num/struct.Wrapping.html
- hsivonen 9y agoRust has all these arithmetic modes on its integer primitives. The operators, such as +, panic on overflow in debug buids and wrap as two's complement in release builds. As methods you get: 1) Two's complement wrapping (even in debug builds) 2) Saturation 3) Overflow checking (these return Option<T>
- masklinn 9y ago> 1) Two's complement wrapping (even in debug builds) As it's a relatively common need, there's also a wrapper struct for this one: https://doc.rust-lang.org/core/num/struct.Wrapping.html https://doc.rust-lang.org/core/num/struct.Wrapping.html
- quelsolaar 9y agoI agree, Overflow is useful and it reflects how the hardware works, so its not something you want to emulate with modulo. The mistake of the C standard was not having it "Platform dependent", rather then "Undefined". The difference is a big deal. Since overflow is undefined the optimizer is allowed to assume it never happens and therefor optimize away things, even though the behavior is defined on all CPUs known to man.
- gpderetta 9y agounsigned overflow is well defined in C.
- pjc50 9y agoIt should be "allowed", but not by default in normal arithmetic. This has cost Etherum developers and their victims a lot of money e.g.: https://github.com/ethereum/solidity/issues/796 https://github.com/ethereum/solidity/issues/796 Ideally you'd be able to specify which mechanism you wanted for arithmetic as a property of a type, so you could have int_saturating and int_wraparound and int_nooverflow in the same code with the compiler preventing you from combining them by mistake. C made it undefined because at the time of its manufacture twos-complement had not quite comprehensively won. There were still a few ones-complement systems in the 80s. https://en.wikipedia.org/wiki/UNIVAC_1100/2200_series https://en.wikipedia.org/wiki/UNIVAC_1100/2200_series