Does anyone know if Apple has posted a technical comparison or summary of the differences between Intel and ARM processor capabilities/limitations?
Specifically, I have code that de-serializes a custom stream of data. Right now, this can result in non-word-aligned fetches and stores of word and double word values.
68K CPUs used to choke on this, but all modern 64-bit Intel processors handle it just fine. What about ARM?
Also curious to know if 64-bit fetches and stores are always atomic.
Specifically, I have code that de-serializes a custom stream of data. Right now, this can result in non-word-aligned fetches and stores of word and double word values.
68K CPUs used to choke on this, but all modern 64-bit Intel processors handle it just fine. What about ARM?
Also curious to know if 64-bit fetches and stores are always atomic.
ARM64 correctly handles unaligned loads and stores at most widths, but that doesn't usually matter for most programmers, because C requires pointers to be adequately aligned for their type regardless of the underlying processor, and programs that violate this rule have undefined behavior. The fact that the code will appear to work if the compiler emits a normal load or store doesn't change the fact that it's not allowed and that the behavior isn't guaranteed. You need to tell the compiler that you're accessing unaligned memory.
There are several ways to tell the compiler that, and the best approach depends on how you're doing the access. If you're reading from a struct, you can simply make the struct packed to tell the compiler that its fields aren't required to be aligned. If you're reading from a pointer, you can make sure that the pointer is to a typedef declared with attribute((aligned(1))), e.g.:
(Apologies, attribute is supposed to be surrounded by double underscores, but the forum software appears to mangle it.)
Similarly, 64-bit loads and stores are guaranteed by the processor to be done without tearing, but the compiler does not make such a guarantee. If you need this guarantee, you should use relaxed atomics.
There are several ways to tell the compiler that, and the best approach depends on how you're doing the access. If you're reading from a struct, you can simply make the struct packed to tell the compiler that its fields aren't required to be aligned. If you're reading from a pointer, you can make sure that the pointer is to a typedef declared with attribute((aligned(1))), e.g.:
Code Block typedef uint32_t unaligned_int32 attribute((aligned(1)));
(Apologies, attribute is supposed to be surrounded by double underscores, but the forum software appears to mangle it.)
Similarly, 64-bit loads and stores are guaranteed by the processor to be done without tearing, but the compiler does not make such a guarantee. If you need this guarantee, you should use relaxed atomics.