From 46b57e3486071fdae1d0c9d9012e7f4d5c581d33 Mon Sep 17 00:00:00 2001 From: Lib Date: Thu, 7 May 2026 22:18:33 +0000 Subject: [PATCH] build: add MSB_FIRST opt-in flag for big-endian hosts All currently-supported swanstation targets - x86, x64, AArch32 LE, AArch64 LE - are little-endian, and there is no plan to ship a big-endian build today. To make endian-conditional code possible without paying a runtime cost on the LE targets, add a single opt-in compile flag rather than autodetecting at configure time: - src/common/types.h is a universally-included header. It now documents the convention and reserves the macro name. There is deliberately no LSB_FIRST counterpart - little-endian is the unmarked default. Code that needs byte-order-conditional behaviour branches on only, so the LE path is the unconditional default. - Makefile.libretro forwards MSB_FIRST=1 from the make command line to -DMSB_FIRST=1 on the compiler. - CMakeLists.txt exposes a CMake option(MSB_FIRST OFF) that does the same via add_definitions(-DMSB_FIRST=1). No source files use the macro yet; this commit only puts the gate in place. Existing endian-dependent code (e.g. CDImageCHD's audio sector byte-swap) can be converted in follow-up patches once the flag is available. Verified: builds clean both with and without MSB_FIRST=1; .so size, exports unchanged on the default LE path. --- CMakeLists.txt | 13 +++++++++++++ Makefile.libretro | 13 +++++++++++++ src/common/types.h | 15 +++++++++++++++ 3 files changed, 41 insertions(+) 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.