We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5446 - [PATCH] Use GCC/Clang byteswapping builtins for SDL_Swap functions
Summary: [PATCH] Use GCC/Clang byteswapping builtins for SDL_Swap functions
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: *don't know* (show other bugs)
Version: HG 2.0
Hardware: All All
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-01-05 03:17 UTC by Cameron Gutman
Modified: 2021-01-07 20:05 UTC (History)
0 users

See Also:


Attachments
Patch (2.07 KB, patch)
2021-01-05 03:19 UTC, Cameron Gutman
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Cameron Gutman 2021-01-05 03:17:52 UTC
GCC and Clang have builtins for byteswapping that we can use to avoid having to write assembly for each architecture. GCC 8.3 does recognize the C implementation of SDL_Swap32/SDL_Swap64 as a byteswap and generate optimized code, but it doesn't quite generate optimal code for SDL_Swap16.

On ARMv7 with GCC 8.3, the SDL_Swap16 C implementation generates:
lsr	r0, r3, #8
orr	r0, r0, r3, lsl #8

but using __builtin_bswap16, we get the optimal implementation using rev16:
rev16	r0, r0

Rather than depending on the optimizer to figure out the 32 and 64 bit byteswaps, we can just use the builtins for all of them to guarantee it.
Comment 1 Cameron Gutman 2021-01-05 03:19:04 UTC
Created attachment 4638 [details]
Patch

For the exact compiler versions, I used https://code.qt.io/cgit/qt/qtbase.git/commit/src/corelib/global/qendian.h?h=6.0&id=a31b75b766b87d0210ed174f587aee33ad639f9b as a reference.
Comment 2 Sam Lantinga 2021-01-07 20:05:51 UTC
Patch added, thanks!
https://hg.libsdl.org/SDL/rev/6ec80d45949c