4 ms·
Of course, a better solution would be for all compilers, and gcc specifically, to properly translate `memcpy()` into efficient assembly for all targets. An eve
by userbinator 11y ago
Of course, a better solution would be for all compilers, and gcc specifically, to properly translate `memcpy()` into efficient assembly for all targets.
An even better solution would be for newer non-x86 hardware to handle unaligned accesses automatically:
http://lemire.me/blog/archives/2012/05/31/data-alignment-for-speed-myth-or-reality/ http://lemire.me/blog/archives/2012/05/31/data-alignment-for...
ARMv7 is similar to x86 in that it has "natural unaligned access", hence the great speedup afforded by using packed structures.
IMHO data alignment restrictions were an implementation artifact of the early RISCs which might've seemed a good idea at the time, when the width of the databus was the same as the wordsize, but makes little sense now with caches and very wide datapaths; the amount of extra circuitry needed is really insignificant compared to the rest of the CPU (including the caches), and memory bandwidth is often the bottleneck such that packing data structures to utilise the cache most efficiently becomes important.
(I say this having worked with MIPS code to do unaligned multibyte reads/writes; taking the appropriate bytes and shifting them the right amount and then combining them is really trivial to do in hardware - it's just wiring, essentially - but turns into annoying multi-instruction sequences when done in software.)
- caf 11y agoUnaligned words within a cacheline is one thing, but what about unaligned words that straddle the end of a cacheline - or worse, the end of a page? In particular, consider how these accesses will interact with atomic operations.
- userbinator 11y agoIn particular, consider how these accesses will interact with atomic operations. For x86, Intel has guaranteed that since the P6, any accesses within a cache line are atomic. The LOCK prefix can be used to enforce atomicity even with accesses that cross cachelines. or worse, the end of a page? I suppose you're thinking about page faults? If a #PF occurs, if the handler returns with a valid mapping, the instruction just gets restarted as usual. This reduces to the crossing cacheline case. I'm not saying that alignment is entirely unimportant and can always be ignored, but the number of cases in which it is important, seems to be decreasing with the newer CPUs.
- caf 11y agoI was responding mainly to the assertion about the trivial amount of extra circuitry required. The cacheline-crossing case means that it's a more complicated affair than just shuffling some bytes around as in the within-cacheline case. The "accesses across cachelines require LOCK to be atomic" x86 case is also illustrative - for example should the pthreads implementation include the LOCK prefix just in case you have embedded your pthread_mutex_t in a packed structure where it ends up unaligned?
- kabdib 11y agoThe 68000 series had alignment restrictions (as did the PDP-11). Pretty sure that a bunch of the bigger, older iron of the 70s required alignment for most of their data types. Alignment requirements predate RISC by a fair amount, across many architectures.
- hga 11y agoAh, yes, the PDP-11 "bus error", SIGBUS. I remember hitting one of those in the early '80s and having to look up what it meant. So, yeah, in my personal experience over the years the architectures without hard alignment restrictions were less common. I suppose lacking them except for speed didn't hurt adoption of the Intel x86 family.