freebsd-src icon indicating copy to clipboard operation
freebsd-src copied to clipboard

swab.c(libc): use a simplified version of byte swapping

Open rilysh opened this issue 1 year ago • 4 comments

This version of swab function simplifies the logic of swapping adjacent bytes. Previous version of swab() used an arbitrary unrolling, which was relevant back in the day but unnecessary for modern compilers, as if the input size is known at compile time, they can do it automatically.

This version of swab() is inspired by musl. A similar version can be found at: https://github.com/openbsd/src/blob/master/lib/libc/string/swab.c Godbolt testing: https://godbolt.org/z/8TMxx1jos

Note: I didn't remove the "derived from ..." section. I'm not sure whether this would be necessary to do, and if so, please let me know!

rilysh avatar Jan 27 '24 16:01 rilysh

This seems like a fine cleanup. With this change there is no copyrightable content left so I'd replace the license block entirely, ideally with a 2-clause BSD or MIT license.

but unnecessary for modern compilers, as if the input size is known at compile time, they can do it automatically.

The size is never known within the translation unit of swab.c so short of using LTO, the compiler's ability to optimize it is zero, but this "optimization" doesn't seems to make much sense on a modern processor.

brooksdavis avatar Jan 29 '24 17:01 brooksdavis

This seems like a fine cleanup. With this change there is no copyrightable content left so I'd replace the license block entirely, ideally with a 2-clause BSD or MIT license.

Thanks for the brief explanation! I've updated the license information. However, I also added "copyright by rilysh" part (in OpenBSD swab, they also seem added this), but if it isn't required, I'll remove it too!

The size is never known within the translation unit of swab.c so short of using LTO, the compiler's ability to optimize it is zero.

I agree, I forgot (didn't notice) that the call would be from a different source unit...sorry for the confusion.

rilysh avatar Jan 30 '24 07:01 rilysh

Looking very close. A couple minor nits on the license. If you change, could you also fold these two commits together? Thanks!

bsdimp avatar Feb 02 '24 16:02 bsdimp

Style checker seems didn't run (https://github.com/freebsd/freebsd-src/actions/runs/7758887131/job/21161673147?pr=1086) here, hmm, but seems unrelated...

rilysh avatar Feb 02 '24 16:02 rilysh

I think this is ready, but it may still bounce due to style (I'll re-run it before I land, but the change looks good so far).

bsdimp avatar Apr 21 '24 05:04 bsdimp

pushed

bsdimp avatar Apr 23 '24 05:04 bsdimp