diff --git a/CMakeLists.txt b/CMakeLists.txt index 945615ac..d704abaa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -142,6 +142,19 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND (${CPU_ARCH} STREQUAL "x86" OR ${CP add_definitions("-D_FILE_OFFSET_BITS=64") endif() +# Host endianness opt-in. +# +# All currently-supported targets (x86, x64, AArch32 LE, AArch64 LE) +# are little-endian. swanstation does not autodetect byte order and +# does not define a positive marker for the LE case - little-endian +# is the unmarked default. If you are porting to a big-endian host, +# configure with -DMSB_FIRST=ON and the code's `#ifdef MSB_FIRST` +# branches will activate. +option(MSB_FIRST "Build for a big-endian host (opt-in; no targets currently use this)." OFF) +if(MSB_FIRST) + add_definitions("-DMSB_FIRST=1") +endif() + # Recursively include the source tree. enable_testing() add_subdirectory(dep) diff --git a/Makefile.libretro b/Makefile.libretro index 5145c611..3d0712b1 100644 --- a/Makefile.libretro +++ b/Makefile.libretro @@ -630,6 +630,19 @@ ifneq ($(SANITIZER),) LDFLAGS := -fsanitize=$(SANITIZER) $(LDFLAGS) endif +# Host endianness opt-in. +# +# All currently-supported targets (x86, x64, AArch32 LE, AArch64 LE) are +# little-endian. swanstation does not autodetect byte order and does +# not define a positive marker for the LE case - little-endian is the +# unmarked default. If you are porting to a big-endian host, build with +# `make ... MSB_FIRST=1` and the code's `#ifdef MSB_FIRST` branches +# will activate. +ifeq ($(MSB_FIRST),1) + CFLAGS += -DMSB_FIRST=1 + CXXFLAGS += -DMSB_FIRST=1 +endif + # --------------------------------------------------------------------- # Build rules # --------------------------------------------------------------------- diff --git a/src/common/types.h b/src/common/types.h index 3ca7d818..9a5e9d48 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -238,3 +238,18 @@ ALWAYS_INLINE constexpr T SignExtendN(T value) static_cast::type>(rhs)); \ return lhs; \ } + +// Host endianness selection. +// +// swanstation currently only ships to little-endian targets (x86, x64, +// AArch32 and AArch64 in their normal LE configurations). To keep the +// hot paths honest we don't autodetect the host byte order, and we +// don't define a positive marker for the LE case either: little-endian +// is simply the unmarked default. +// +// Big-endian is opt-in. Pass -DMSB_FIRST=1 to the compiler (via the +// build system or by hand) to flag a big-endian build. Code that +// needs byte-order-dependent work should branch on `#ifdef MSB_FIRST` +// only - the LE path stays the unconditional default and pays no +// cost on the targets we actually support. There is no LSB_FIRST +// macro and there should never be one.