From 56edcee48c0f8ce7546d1458dc1d734e320b82ad Mon Sep 17 00:00:00 2001 From: Martin Natano Date: Thu, 20 Apr 2023 20:21:51 +0200 Subject: [PATCH] Use compiler intrinsics for byte swapping --- src/CMakeData-os.cmake | 3 +- src/RageUtil.h | 35 +++------------ src/StepMania.cpp | 2 +- src/archutils/Common/gcc_byte_swaps.h | 64 --------------------------- src/archutils/Darwin/arch_setup.h | 6 --- src/archutils/Unix/arch_setup.h | 1 - src/archutils/Win32/arch_setup.h | 4 -- src/global.h | 3 -- 8 files changed, 9 insertions(+), 109 deletions(-) delete mode 100644 src/archutils/Common/gcc_byte_swaps.h diff --git a/src/CMakeData-os.cmake b/src/CMakeData-os.cmake index 93c3fc0700..cd6b84f40d 100644 --- a/src/CMakeData-os.cmake +++ b/src/CMakeData-os.cmake @@ -97,8 +97,7 @@ else() "archutils/Unix/GetSysInfo.h" "archutils/Unix/RunningUnderValgrind.h" "archutils/Unix/SignalHandler.h" - "archutils/Unix/SpecialDirs.h" - "archutils/Common/gcc_byte_swaps.h") + "archutils/Unix/SpecialDirs.h") if(X11_FOUND) list(APPEND SMDATA_OS_SRC "archutils/Unix/X11Helper.cpp") list(APPEND SMDATA_OS_HPP "archutils/Unix/X11Helper.h") diff --git a/src/RageUtil.h b/src/RageUtil.h index a28f61f606..b7d54eb3b2 100644 --- a/src/RageUtil.h +++ b/src/RageUtil.h @@ -204,35 +204,14 @@ namespace Endian #endif } -/* We only have unsigned swaps; byte swapping a signed value doesn't make sense. - * - * Platform-specific, optimized versions are defined in arch_setup, with the names - * ArchSwap32, ArchSwap24, and ArchSwap16; we define them to their real names here, - * to force inclusion of this file when swaps are in use (to prevent different dependencies - * on different systems). - */ -#ifdef HAVE_BYTE_SWAPS -#define Swap32 ArchSwap32 -#define Swap24 ArchSwap24 -#define Swap16 ArchSwap16 +#ifdef _WIN32 +#define Swap32(n) _byteswap_ulong(n) +#define Swap24(n) _byteswap_ulong(n) >> 8 +#define Swap16(n) _byteswap_ushort(n) #else -inline std::uint32_t Swap32( std::uint32_t n ) -{ - return (n >> 24) | - ((n >> 8) & 0x0000FF00) | - ((n << 8) & 0x00FF0000) | - (n << 24); -} - -inline std::uint32_t Swap24( std::uint32_t n ) -{ - return Swap32( n ) >> 8; // xx223344 -> 443322xx -> 00443322 -} - -inline std::uint16_t Swap16( std::uint16_t n ) -{ - return (n >> 8) | (n << 8); -} +#define Swap32(n) __builtin_bswap32(n) +#define Swap24(n) __builtin_bswap32(n) >> 8 +#define Swap16(n) __builtin_bswap16(n) #endif inline std::uint32_t Swap32LE( std::uint32_t n ) { return Endian::little ? n : Swap32( n ); } diff --git a/src/StepMania.cpp b/src/StepMania.cpp index ab330feb2f..9facfefd6c 100644 --- a/src/StepMania.cpp +++ b/src/StepMania.cpp @@ -17,7 +17,6 @@ #include "arch/ArchHooks/ArchHooks.h" #include "arch/LoadingWindow/LoadingWindow.h" #include "arch/Dialog/Dialog.h" -#include #include "ProductInfo.h" @@ -71,6 +70,7 @@ #include "ver.h" #include +#include #include #if defined(WIN32) diff --git a/src/archutils/Common/gcc_byte_swaps.h b/src/archutils/Common/gcc_byte_swaps.h deleted file mode 100644 index 1cef37098a..0000000000 --- a/src/archutils/Common/gcc_byte_swaps.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef GCC_BYTE_SWAPS_H -#define GCC_BYTE_SWAPS_H - -#if defined(CPU_X86) - -#include - -inline std::uint32_t ArchSwap32( std::uint32_t n ) -{ - asm( - "xchg %b0, %h0\n" - "rorl $16, %0\n" - "xchg %b0, %h0": - "=q" (n): "0" (n) ); - return n; -} - -inline std::uint32_t ArchSwap24( std::uint32_t n ) -{ - asm( - "xchg %b0, %h0\n" - "rorl $16, %0\n" - "xchg %b0, %h0\n" - "shrl $8, %0\n": - "=q" (n): "0" (n) ); - return n; -} - -inline std::uint16_t ArchSwap16( std::uint16_t n ) -{ - asm( - "xchg %b0, %h0\n": - "=q" (n): "0" (n) ); - return n; -} - -#define HAVE_BYTE_SWAPS -#endif - -#endif -/* - * (c) 2004 Glenn Maynard - * All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, and/or sell copies of the Software, and to permit persons to - * whom the Software is furnished to do so, provided that the above - * copyright notice(s) and this permission notice appear in all copies of - * the Software and that both the above copyright notice(s) and this - * permission notice appear in supporting documentation. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF - * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS - * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT - * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS - * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR - * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ diff --git a/src/archutils/Darwin/arch_setup.h b/src/archutils/Darwin/arch_setup.h index 80ce67c4c8..555ff21b62 100644 --- a/src/archutils/Darwin/arch_setup.h +++ b/src/archutils/Darwin/arch_setup.h @@ -29,12 +29,6 @@ extern "C" int sm_main( int argc, char *argv[] ); # define __MACOSX__ #endif -#include -#define ArchSwap32(n) OSSwapInt32((n)) -#define ArchSwap24(n) (ArchSwap32((n)) >> 8) -#define ArchSwap16(n) OSSwapInt16((n)) -#define HAVE_BYTE_SWAPS - #endif /* diff --git a/src/archutils/Unix/arch_setup.h b/src/archutils/Unix/arch_setup.h index 69afb2073d..aa15db2282 100644 --- a/src/archutils/Unix/arch_setup.h +++ b/src/archutils/Unix/arch_setup.h @@ -17,7 +17,6 @@ #undef MACHINE #endif -#include "archutils/Common/gcc_byte_swaps.h" #endif /* diff --git a/src/archutils/Win32/arch_setup.h b/src/archutils/Win32/arch_setup.h index f5095a9351..331ffd42e5 100644 --- a/src/archutils/Win32/arch_setup.h +++ b/src/archutils/Win32/arch_setup.h @@ -82,10 +82,6 @@ void my_usleep( unsigned long usec ); #define CRASH_HANDLER #endif -#define ArchSwap32(n) _byteswap_ulong(n) -#define ArchSwap24(n) _byteswap_ulong(n) >> 8 -#define ArchSwap16(n) _byteswap_ushort(n) - #endif /* diff --git a/src/global.h b/src/global.h index 576a3344b9..cdb58ba0f5 100644 --- a/src/global.h +++ b/src/global.h @@ -24,9 +24,6 @@ /* Make sure everyone has min and max: */ #include -/* Everything will need string for one reason or another: */ -#include - /* Branch optimizations: */ #if defined(__GNUC__) #define likely(x) (__builtin_expect(!!(x), 1))