• Re: Constants and undefined behavior

    From dave_thompson_2@3:633/10 to All on Tue Sep 15 21:14:45 2026
    On 14 Jun 2026 17:22:22 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
    wrote:
    [quoting a chatbot]
    | Or, consider a loop:
    |
    | for (int i = 0; i < n; i++) {
    | arr[i] = 0;
    | }
    |
    | If out-of-bounds array access had defined behavior, the compiler would
    | have to insert a bounds check ("if (i >= array_length)") on every single
    | iteration. Because out-of-bounds access is UB, the compiler can assume
    | n is always within bounds. This allows it to unroll the loop,
    | vectorize it using SIMD instructions, and process 8 or 16 elements per
    | CPU cycle, yielding massive performance gains.

    Not so. If n is <= the length of arr (or the usable portion of an
    array that arr is a pointer to), and also <= MAX_INT so the loop is well-defined, then no check is needed on each assignment, only before
    starting the loop.

    Well, there are some tests that can be taken out of loops (as
    in Java), but other tests can't.

    Not in the language defined by JLS and JVMS; those check every array subscripting operation, period. OTOH an implementation can recognize
    this pattern and hoist the check in JITted or otherwise postcompiled
    code, and I think HotSpot in particular does.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Keith Thompson@3:633/10 to All on Wed Sep 16 00:27:59 2026
    dave_thompson_2@comcast.net writes:
    On 14 Jun 2026 17:22:22 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
    wrote:
    [quoting a chatbot]
    | Or, consider a loop:
    |
    | for (int i = 0; i < n; i++) {
    | arr[i] = 0;
    | }
    |
    | If out-of-bounds array access had defined behavior, the compiler would
    | have to insert a bounds check ("if (i >= array_length)") on every single >> | iteration. Because out-of-bounds access is UB, the compiler can assume
    | n is always within bounds. This allows it to unroll the loop,
    | vectorize it using SIMD instructions, and process 8 or 16 elements per
    | CPU cycle, yielding massive performance gains.

    Not so. If n is <= the length of arr (or the usable portion of an
    array that arr is a pointer to), and also <= MAX_INT so the loop is well-defined, then no check is needed on each assignment, only before starting the loop.

    Maybe. The first part of the quoted paragraph (apparently from
    a chatbot) discusses the consequences *if* out-of-bounds array
    access had defined behavior, which it clearly doesn't in C. But it
    doesn't necessarily follow that there has to be a bounds check on
    every iteration. That depends on what the hypothetical defined
    behavior actually is.

    I can't think of a plausible or useful defined behavior that would
    require an explicit check on every iteration, but I haven't given
    it much thought.

    (In Ada, for example, an out-of-bounds array access is specified
    to raise an exception, which terminates execution of the enclosing
    construct. Ada compilers can certainly hoist bounds checks out
    of loops.)

    [...]

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)