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

Summary: [PATCH] Use GCC/Clang byteswapping builtins for SDL_Swap functions
Product: SDL Reporter: Cameron Gutman <cameron.gutman>
Component: *don't know*Assignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: HG 2.0   
Hardware: All   
OS: All   
Attachments: Patch

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