top | item 36158695

(no title)

pasmafaute | 2 years ago

If I recall, you can absolutely loop through the elements in a tight loop and the compiler (e.g. GCC) will auto-vectorize for you (if you have the relevant optimization flag set).

The trick with coding for auto-vectorization is to keep your loops small and free of clutter.

I don't have the documentation handy but I think you only need to follow a couple rules:

- loop must have a defined size (for-loop instead of while-loop)

- don't muck with pointers inside the loop (simple pointer increment is okay)

- don't modify other variables (only the array should be modified)

discuss

order

dragontamer|2 years ago

The linked document describes Intel's autovectorizer, it's warnings and compiler flags that point out which loops autovectorized or not, as well as listing specific reason codes why.

Microsoft, GCC and Clang all do this too, though with different compiler flags and messages.

I'd say that the whole point of this document listed here is to build up the programmer to understanding these error messages and specifically know how to fix the errors that causes a autovectorization-fail.

galangalalgol|2 years ago

In rust I've noticed using iterators instead of for loops tends to trigger auto vectorization more often.

Veliladon|2 years ago

In Rust using iterators is almost always better than for loops. The compiler is virtually guaranteed to optimize out bounds checks vs indexing in.