Repository navigation
feat: stop on Int and UInt overflow, with explicit wrapping methods - #1401
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
+,-and*on anInt, and-xon the most negative one, now stop the program withpith runtime error: integer overflowwhen the result does not fit in 64 bits.UIntandUInt64do the same against 0 to 2^64 - 1. The narrow types (Int8toUInt32) keep wrapping into their range, as documented. The fused counting updatem[k] += dchecks the same way.wrapping_add,wrapping_subandwrapping_mulgive the wrapped result on purpose. The std code that relied on wrapping now uses them: the sha-512 kernel, xxhash,bits.add32/add64, a zstd byte-replication multiply and its hash, and a test seed in std.hash.How the checks are lowered:
addc,subc,mulcand the unsigneduaddc,usubc,umulc). The compiler's own index and loop arithmetic keeps the plain ops.i + 1,n * 10,0 - x) the check is one compare of the other operand against the range that cannot overflow, which fuses with its branch.pith_runtime_error, after checking that the faulting instruction is the trap.i = i + 1(ori += 1) in awhile i < eloop, orwhile i <= nfor a lengthn, and the matchingi - 1underi > e;Benchmarks against main (
t2.sh, 5 interleaved rounds, median ms):+and*in the loop)The tight_loop and path cleaning costs are the check itself in loops that do nothing else. Docs:
docs/numbers.md(a new overflow section),docs/ir-contract.mdanddocs/limitations.md.Tests:
tests/cases/test_wrapping_arithmetic.pith;The local suite passes, including the abort goldens.