Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP/RFC: Add explicitly wrapping versions of integer arithmetic #50790

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Aug 3, 2023

  1. WIP: Add explicitly wrapping versions of integer arithmetic

    This adds operators `+%`, `-%`, `*%`, which are equivalent to the
    non-`%` versions, but indicate an explicit semantic expectation that
    twos completement wrapping behavior is expected and correct. As discussed
    at JuliaCon 2014 and every year since, users have often requested
    a way to opt into explicit overflow checking of arithmetic, whether
    for debugging or because they have regulatory or procedural requirements
    that expect to be able to do this. Having explicit operators for
    overflowing semantics allows use cases that depend on overflow behavior
    for correct functioning to explicitly opt-out of any such checking.
    
    I want to explicitly emphasize that there are no plans to change
    the default behavior of arithmetic in Julia, neither by introducing
    error checking nor by making it undefined behavior (as in C). The
    general consensus here is that while overflow checking can be useful,
    and would be a fine default, even if hardware supported it efficiently
    (which it doesn't), the performance costs of performing the check
    (through inhibition of other optimization) is too high. In our experience
    it also tends to be relatively harmless, even if it can be a very
    rude awakeing to users coming from Python or other languages with
    big-default integers.
    
    The idea here is simply to give users another tool in their arsenal
    for checking correctness. Think sanitizers, not language change.
    This PR includes a macro `@Base.Experimental.make_all_arithmetic_checked`,
    that will define overrides to make arithmetic checked, but does not
    include any mechanism (e.g. #50239) to make this fast.
    
    What is included in this PR:
     - Flisp parser changes to parse the new operators
     - Definitions of the new operators
     - Some basic replacements in base to give a flavor for using the
       new operator and make sure it works
    
    Still to be done:
     - [] Parser changes in JuliaSyntax
     - [] Correct parsing for `+%` by itself, which currently parses as `+(%)`
    
    The places to change in base were found by using the above-mentioned
    macro and running the test suite. I did not work through the tests
    exhaustively. We have many tests that explicitly expect overflow and
    many others that we should go through on a case by case basis. The
    idea here is merely to give an idea of the kind of changes that
    may be required if overflow checking is enabled. I think they can
    broadly be classed into:
    
    - Crypto and hashing code that explicitly want modular arithmetic
    - Bit twidelling code for arithmetic tricks (though many of these,
      particularly in Ryu, could probably be replaced with better
      abstractions).
    - UInt8 range checks written by Stefan
    - Misc
    Keno committed Aug 3, 2023
    Configuration menu
    Copy the full SHA
    3b57645 View commit details
    Browse the repository at this point in the history