4 ms·
This knowledge is not obsolete. REP STOS/MOVS is the right way to move memory now, and its operands are SI, DI, and CX. It is still useful to know what the regi
by jeffbee 6y ago
This knowledge is not obsolete. REP STOS/MOVS is the right way to move memory now, and its operands are SI, DI, and CX. It is still useful to know what the registers are supposed to mean.
- moonchild 6y agoERMS is extremely slow for small sizes. In my testing, rep stos wins over sse for sizes above ~1k, and over avx for sizes greater than l1. It also requires correct alignment, and isn't fast on most AMD cpus. For smaller sizes, rep stos can be as much as 20x slower than the alternative.
- jeffbee 6y agoYou can’t make a statement like that without mentioning which part you’re testing with.
- moonchild 6y agoNot sure what you mean by 'part'. I reproduced this across the following CPUs, which I think are fairly representative: - intel xeon e3-1246 - intel core i3-5005u - amd ryzen 3960X - amd epyc 7402p Comparing my own[0] memset implementation, glibc's, and bionic's. 0. https://github.com/moon-chilled/fancy-memset https://github.com/moon-chilled/fancy-memset
- jeffbee 6y agoIt seems that you do know what I mean by part. Only the latest Zen3 parts have ERMS and FSRM. I think that benchmarking memset by itself is folly, because you may end up with a very large function that is fast on its own, but which trashes your icache when run in a real application. That is the conclusion of the paper I linked elsewhere in this thread: that rep cmpsb, even on old CPUs where it is slow, beats glibc memcmp in real full-scale applications because glibc memcmp is 6KB long and has a tendency to evict dozens of lines of icache. memset, memcpy, and memmove have the same hazard.
- moonchild 6y agoThat is a good point, and I do intend to do full-scale application benchmarks; I just haven't gotten around to it yet. That being said, all the implementations of memset I tested except for glibc's are in the neighborhood of 100-300 bytes (also including the freebsd version, which doesn't use any simd), which is a fair sight smaller than 6k.
- dragontamer 6y agoFrom my understanding, looping avx512 or avx2 mov might be faster than rep stos in many cases.
- jeffbee 6y agoThe number of such cases is decreasing, though. Recent (ICL and "later" whatever that means) Intel cores have fast-short-rep-movsb which reduces the startup time of the REP. Considering the icache pressure of a hyperoptimized memcpy, it is probably the right way to go for new code. If you're interested see Google's paper, AsmDB: Understanding and Mitigating Front-End Stalls, section 4.4 Memcmp and the perils of micro-optimization, in which they say that REP CMPS beats glibc memcmp in full-scale benchmarks. "On large-footprint workloads like websearch, it reduced cycles in memcmp more than twofold and showed an overall 0.5%-1% end-toend performance improvement." And that was on Haswell, before people started selling parts with "fast short cmpsb"
- deleted 6y ago[deleted]
- dragontamer 6y agoGood to hear. Rep movs is the obvious way to represent memmove and memcpy. Avx moves should have never been faster than that sequence.
- chrisseaton 6y agoDon't Intel now recommend 'implementing memcpy using Enhanced REP MOVSB and STOSB might not reach the same level of throughput as using 256-bit or 128-bit AVX alternatives'?
- saagarjha 6y agoFor implementing an optimized memcpy, I believe that is still true (although the gap between rep movsb and a nontemporal copy using vector registers is not all that large). But for many cases the code size improvement (a couple of bytes versus hundreds for a typical vectorized implementation) or restrictions on register use (kernel) mean that it can be a good choice.