4 ms·
Is this equivalent ? At least to me this seems a more common pattern than using memcpy(), and I expect to work better on old compilers. static uint32 read(co
by amadvance 10y ago
Is this equivalent ? At least to me this seems a more common pattern than using memcpy(), and I expect to work better on old compilers.
static uint32 read(const void *ptr) {
const uint8 *b = (const uint8 *)ptr;
return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
}
- qb45 10y agoFunctionally it seems equivalent, except that it doesn't work on big endian machines (assuming the ints are in native endian). On dumb compilers it may be faster if a few bitops happen to be faster than a call to memcpy. Which sounds plausible. On modern compilers it boils down to whether your compiler can optimize either one or both of these patterns into a single unaligned load. GCC for example certainly optimizes 4 byte memcpy, probably already at -O2, but whether it recognizes your pattern I don't know. Compile it and check.