Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 2 commits into from

Conversation

rilysh
Copy link
Contributor

@rilysh rilysh commented Jan 27, 2024

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!

@brooksdavis
Copy link
Contributor

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.

@bsdimp bsdimp added the ready label Jan 29, 2024
@rilysh
Copy link
Contributor Author

rilysh commented Jan 30, 2024

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.

@@ -1,58 +1,44 @@
/*-
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the /*- is important here.

*
* This code is derived from software contributed to Berkeley by
* Jeffrey Mogul.
* Copyright (c) 2024 rilysh <nightquick@proton.me>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With SPDX, we've preferred to omit the actual license text where possible.
This looks like a good case, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sounds reasonable, I've updated it.

@bsdimp
Copy link
Member

bsdimp commented Feb 2, 2024

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

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

Signed-off-by: rilysh <nightquick@proton.me>
Signed-off-by: rilysh <nightquick@proton.me>
@rilysh
Copy link
Contributor Author

rilysh commented Feb 2, 2024

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

@bsdimp
Copy link
Member

bsdimp commented Apr 21, 2024

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 bsdimp self-assigned this Apr 21, 2024
@bsdimp
Copy link
Member

bsdimp commented Apr 23, 2024

pushed

@bsdimp bsdimp closed this Apr 23, 2024
@bsdimp bsdimp added merged and removed ready labels Apr 23, 2024
freebsd-git pushed a commit that referenced this pull request Apr 23, 2024
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

Signed-off-by: rilysh <nightquick@proton.me>
Reviewed by: imp
Pull Request: #1086
bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this pull request Aug 11, 2024
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

Signed-off-by: rilysh <nightquick@proton.me>
Reviewed by: imp
Pull Request: freebsd/freebsd-src#1086
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants