Evidence please.
Evidence please.
#include <stdint.h>
uint32_t load_uint32_be(uint8_t* p) { return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; }
uint32_t load_uint32_le(uint8_t* p) { return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; }
===============
gcc -O3 -fomit-frame-pointer -S bo.c
===============
load_uint32_be: movl 4(%esp), %edx
movzbl (%edx), %eax
movzbl 1(%edx), %ecx
sall $24, %eax
sall $16, %ecx
orl %ecx, %eax
movzbl 3(%edx), %ecx
movzbl 2(%edx), %edx
orl %ecx, %eax
sall $8, %edx
orl %edx, %eax
ret
load_uint32_le:
movl 4(%esp), %edx movzbl 3(%edx), %eax
movzbl 2(%edx), %ecx
sall $24, %eax
sall $16, %ecx
orl %ecx, %eax
movzbl (%edx), %ecx
movzbl 1(%edx), %edx
orl %ecx, %eax
sall $8, %edx
orl %edx, %eax
ret
GCC doesn't merge the 4 byte-level reads into one 32-bit read. Thus, it does cause some performance penalty. The true impact is probably quite low, but it does exist on x86.It is true however, that GCC will take a series of bit ops and produce a 'bswap' instruction on x86, but that requires a full 32-bit word to start with.
x86 practically offers it for free in newer architectures (Sandy Bridge, Ivy Bridge and Bulldozer).[2]
[1] https://developer.apple.com/hardwaredrivers/ve/g5.html
[2] http://agner.org/optimize/instruction_tables.pdf (check MOVDQU timings)
The point made by the author addresses this issue from a different angle.
As the author say, programmers should always write endianess neutral code unless it is impossible which is generally at the interfaces, where data is read and written (I/O) by the program. If the code is correctly and intelligently optimized so that marshaling is done once, then the byte swapping may generally be expected to be a low frequency operation. In this case the most simple and portable code should be favored.
Trying to optimize this operation by word read and byte swapping provides an insignificant optimization with a higher cost on code portability and maintainability. The author is right on this.
Though it is also true that in some cases, the operation frequency is very high (i.e. reading million pixel values of an image). For these use cases, the programming overhead of using highly optimized code is perfectly justified. But then don't use half backed optimizations. Try to align data on words (twice faster), read by word (four time faster) and use byte swapping machine instruction available on the target CPU instead of the proposed shifts and bit masks.
My opinion is that good languages should provide optimized data marshaling functions in their library so that the code can be optimal and portable at the same time.