Re: Uncounted loop vectorization example doesn't work with unsigned index

Ionuț Nicula via Gcc <[email protected]>
Newsgroups gmane.comp.gcc.devel
Message-ID <lRgr6aAfr-6Hs1HzgqxS0isas1akVI1lEi8U3Gct0yd6Qmp7jmeMZHHDJmimt-0WZmt22Hu0DY7pjszxR0V4Ckr0-uZCtov-fn-3b14XJa0=@nicula.xyz>
On Monday, May 11th, 2026 at 12:55, Richard Biener
<[email protected]> wrote:

> The reason is that i might overflow and thus
> the evolution of 'i' for the haystack[i] access isn't affine.
> Use unsigned long and it works again.
> 
> This is a general issue with unsigned IVs smaller than
> pointer size.

Sorry, I somehow messed up my tests and misunderstood what was going on.

I was more interested in this example. This doesn't get vectorized (`-O3
-march=znver5 -ffreestanding`):

    unsigned long foo(uint8_t *data) {
        unsigned long i = 0;
        while (1) {
            if (data[i] == 0)
                return i;
            i++;
        }
    }

But this does:

    long foo(uint8_t *data) {
        long i = 0;
        while (1) {
            if (data[i] == 0)
                return i;
            i++;
        }
    }

`unsigned long` index coupled `uint16_t *` data does get vectorized,
though.

Is it because technically `uint8_t[size_t]` can span the whole address
space and therefore the `size_t` index can validly wrap around, whereas
`uint16_t[size_t]` is bounded by the address space so you can never
reach `uint16_t[2 + (SIZE_MAX/2)]`?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.