3 ms·
> But that code isn't portable for a different endian machine. Except it is. On x86, which is little endian, compilers are smart enough to convert it to a mov.
by messe 6y ago
> But that code isn't portable for a different endian machine.
Except it is. On x86, which is little endian, compilers are smart enough to convert it to a mov.
> These functions will either swap, or not, depending on the endianness of the target machine
So will the above. Here's the MIPS little endian version:
read_int32(unsigned char*):
lwl $2,3($4)
j $31
lwr $2,0($4)
This doesn't swap. The MIPS big endian version:
read_int32(unsigned char*):
lbu $3,1($4)
lbu $2,2($4)
sll $5,$3,8
lbu $6,0($4)
sll $3,$2,16
lbu $2,3($4)
or $3,$5,$3
or $3,$3,$6
sll $2,$2,24
j $31
or $2,$3,$2
This swaps and ors. What part of this do you not consider portable between different-endian machines?