How to disable overflow protection for aggressive optimization?

As output of Compiler Explorer shows, Zig treats overflow as UB to enable better optimizations, while Rust requires explicit use of assume to have same optimizations. I want to know if there is a simpler way to achieve this goal when the input data range is known.

Check out the unchecked_* functions in std::intrinsics

This generates the exact same assembly as Zig. It still requires unsafe and core_intrinsics but it's a bit simpler to write.

Oh I did not know it before, thanks a lot.

Overflow checks are turned off in release mode by default.

Not for division by 0 or some other / and % operations.

Ah I didn't know that, thanks @quinedot.

To avoid having to rely on intrinsics, you can also do .checked_*().unwrap_unchecked(), which should have the same behavior.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.