3 ms·
You can use +% to have a fully defined wraparound addition: https://ziglang.org/documentation/master/#Wrapping-Operations https://ziglang.org/documentation/mast
by roblabla 7y ago
You can use +% to have a fully defined wraparound addition: https://ziglang.org/documentation/master/#Wrapping-Operations https://ziglang.org/documentation/master/#Wrapping-Operation.... For lshift, there is @shlWithOverflow.
- jforberg 7y agoNice! That's something I often wish GCC had.
- MaxBarraclough 7y agoI google'd this - GCC comes close. There are built-ins for overflow-checking add/sub/mul arithmetic. [0] There are also compiler flags to enable explode-on-overflow or wrap-on-overflow, except for division and remainder. [1] [0] https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins... [1] https://www.gnu.org/software/autoconf/manual/autoconf-2.63/html_node/Signed-Overflow-Advice.html https://www.gnu.org/software/autoconf/manual/autoconf-2.63/h...
- jforberg 7y agoIndeed. What I would really like is a way to toggle it on case-by-case basis. With -fwrapv you have to convince the code owner to toggle it globally, which can complicate things unless you are the owner.
- MaxBarraclough 7y agoLooks like GCC doesn't have a pragma to enable it on a per-object-file basis. Pity. From the pragma docs I get the impression it's not so much that it would be a bad idea, as simply not something they've got round to implementing. I don't imagine it's possible to check using a static assert, either. https://gcc.gnu.org/onlinedocs/gcc/Pragmas.html#Pragmas https://gcc.gnu.org/onlinedocs/gcc/Pragmas.html#Pragmas