• 0 Posts
  • 58 Comments
Joined 9 months ago
cake
Cake day: March 12th, 2025

help-circle



  • The compiler should be able to optimize all of them to the same machine code.

    1. This is already good.
    2. Easily optimized by constant folding.
    3. This one depends on the semantics of signed underflow, so it may not do what you want.
    4. The loop can only exit if x==10, so as long as the nextInt() method doesn’t have side effects, the loop should be eliminated. But, again, language semantics can affect this.

    Edit: Very wrong for 3 & 4, see replies.

















  • I hope that the language’s ints are at most 32 bits. For 8 bits it could even be written by hand & the source code for a 32 bit version would only take up avg_line_len * 4GiB space for the source code of the function. But it might take a bit of time to compile a version that supports the full range of 64 or 128 bit ints.