Usually when you're loading an unaligned value you're also loading a
particular representation and endianness, which might not be the
native one, so I've often written code like this:
uint32_t load32(void *p)
{
unsigned char *c = p;
return
c[0] | (uint32_t)c[1] << 8 | (uint32_t)c[2] << 16 | (uint32_t)c[3] << 24;
}
(The first of the three casts isn't really necessary ...)
Unfortunately, GCC doesn't seem to turn that into a single load
instruction even when it could be. (Why not?)
If you know that you want the native representation and endianness
there's this possibility:
uint32_t load32(void *p)
{
uint32_t r;
memcpy(&r, p, sizeof(r));
return r;
}
GCC turns that into a single load instruction on i386, amd64 and arm64.
Edmund
--
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]
Archive:
https://lists.debian.org/CAHDciUexgfHwWX_ijWrC1NWqhX7sc_3ktGwU8Ya4Ntjq_UX=f...@mail.gmail.com