News for hackers. Stuff that matters.
Re:The Byte Order Fiasco by rwmj on Saturday May 08, 2021 @07:49AM (#27086274)
If you can assume GCC or Clang then __builtin_bswap{16,32,64} functions are provided which will be considerably more efficient, less error-prone, and easier to use than anything you can homebrew.
Re:The Byte Order Fiasco by st_goliath on Saturday May 08, 2021 @09:22AM (#27086904)
Well, yes. The only thing missing is knowing if you have to swap or not, if you don't want to assume your code will run on little endian systems exclusively.

Or, on Linux and BSD systems at least, you can use the <endian.h> or <sys/endian.h> functions (https://linux.die.net/man/3/endian) and rely on the libc implementation to do the system/compiler detection for you and use an appropriate compiler builtin inside of an inline function instead of bothering to hack something together in your own code.

The article mentions those functions at the bottom, but strangely still recommends hacking up your own macros.

Re:The Byte Order Fiasco by nly on Saturday May 08, 2021 @08:33AM (#27086570)
My favourite builtins are the overflow checked integer operations:

https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins...

Re:The Byte Order Fiasco by fanf2 on Saturday May 08, 2021 @08:15AM (#27086442)
But then you have to #ifdef the endianness of the target architecture. If you do it the right way as Russ Cox and Justine Tunney say, then your code can serialize and deserialize correctly regardless of the platform endianness.
Re:The Byte Order Fiasco by jart on Saturday May 08, 2021 @08:06AM (#27086397)
That's not true. If you write the byte swap in ANSI C using the gigantic mask+shift expression it'll optimize down to the bswap instruction under both GCC and Clang, as the blog post points out.
Re:The Byte Order Fiasco by borman on Saturday May 08, 2021 @08:33AM (#27086573)
Funny that compilers (e.g. clang: https://github.com/llvm/llvm-project/blob/b04148f77713c92ee5... ) might be able to do that only because someone on the compiler team has hand-coded a bswap expression detector.
Re:The Byte Order Fiasco by rwmj on Saturday May 08, 2021 @08:23AM (#27086499)
Assuming the macros or your giant expression are correct. But you might as well use the compiler intrinsics which you know are both correct and the most efficient possible, and get on with your life.
Re:The Byte Order Fiasco by jart on Saturday May 08, 2021 @08:46AM (#27086653)
Sorry I'd rather place my faith in arithmetic rather than someone's API provided the compiler is smart enough to understand the arithmetic and optimize accordingly.
Re:The Byte Order Fiasco by loeg on Saturday May 08, 2021 @12:14PM (#27088438)
"Someone" here is the same compiler you're trusting to optimize your giant arithmetic expression of the same idea. Your statement is internally inconsistent.
Re:The Byte Order Fiasco by lanstin on Saturday May 08, 2021 @02:50PM (#27089732)
There is a value to keeping it completely clear in your head the difference between a value with arithmetic semantics vs a value with octets in a stream semantics. That thinking will work in all contexts, while the compiler knowledge is limited. The thinking will help you write correct ways to encode data in the URL or into a file being uploaded that your code generates for discord or whatever, in Python, without knowledge of the true endianness of the system the code is running on.
Re:The Byte Order Fiasco by russdill on Saturday May 08, 2021 @02:33PM (#27089619)
The fallacy in the article is that anyone should code these functions. There's plenty of public domain libraries that do this correctly.

https://github.com/rustyrussell/ccan/blob/master/ccan/endian...

Re:The Byte Order Fiasco by dataflow on Saturday May 08, 2021 @07:56AM (#27086336)
_byteswap_{ushort,ulong,uint64} for MSVC. Together with yours on x86 these should take care of the three major compilers.
Re:The Byte Order Fiasco by savant2 on Saturday May 08, 2021 @08:14AM (#27086436)
The article explicitly shows that the provided macros are very efficient with a modern compiler. You can check on godbolt.org that they emit the same code.

Though the article only mentions bswap64 and mentioning __builtin_bswap64 would be a nice addition.

Re:The Byte Order Fiasco by bombcar on Saturday May 08, 2021 @08:11AM (#27086417)
Given it can be done with careful code AND many processors have a single instruction to do it I’m surprised it hasn’t been added to the C standard.
Re:The Byte Order Fiasco by chrisseaton on Saturday May 08, 2021 @08:19AM (#27086472)
__builtin_bswap does exactly the same thing as the macros.