3 ms·
I’m wonder how well this handles unaligned memory? That used to be stable stakes for this kind of thing, but maybe it doesn’t matter much anymore?
by jmull 5y ago
I’m wonder how well this handles unaligned memory?
That used to be stable stakes for this kind of thing, but maybe it doesn’t matter much anymore?
- mananaysiempre 5y agoOn the x86, in the P4 times the best performing bulk operations essentially required using SIMD, and that SIMD hated you unless you aligned your memory accesses. The result was horrible bloated code to handle leading and trailing data and thus also a need to split off the implementations for small sizes. The unaligned access penalty is much lower now, and REP-prefixed operations have microcoded implementations that use the maximum memory access width (which you can’t do otherwise without SIMD instructions). I’m curious about what the referenced code compiles down to, actually, because not only could GCC be auto-vectorizing it, it could be replacing it with a REP STOSQ or indeed a call to memset.
- ninkendo 5y agoHere's the code in godbolt: gcc: https://godbolt.org/z/6xG5dKjj9 https://godbolt.org/z/6xG5dKjj9 clang: https://godbolt.org/z/Mh9zozjvK https://godbolt.org/z/Mh9zozjvK I'm no asm expert, but it doesn't look like a lot of vector instructions in the gcc compilation of this, while the clang compilation seems to have more calls with the 128-bit xmm registers (at least on x86_64.) You can also just see visibly how many more instructions the gcc version outputs.
- mananaysiempre 5y agoThank you! Indeed GCC does not use SIMD here unless you set -O3 (... I seem to remember this enables some vectorization?) or allow it to use AVX with -mavx or -march=x86-64-v3. For some reason I’m unable to get it to use plain SSE (always available on x86-64) with any -mtune setting or even with -march=x86-64-v2.
- The_rationalist 5y agohttps://www.phoronix.com/scan.php?page=news_item&px=GCC-12-Auto-Vec-O2 https://www.phoronix.com/scan.php?page=news_item&px=GCC-12-A...
- kevin_b_er 5y agoProbably poorly. It is a violation to cast an unaligned pointer to an aligned type that the base pointer is not aligned for. And the code looks like it does just that right here: https://github.com/nadavrot/memset_benchmark/blob/main/src/lib.c#L25 https://github.com/nadavrot/memset_benchmark/blob/main/src/l... This is undefined behavior under C99 §6.3.2.3 Paragraph 7. "If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined." The musl code referenced has handling for this.