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.
This commit is contained in:
Lib 2026-05-07 22:18:33 +00:00 • committed by U-DESKTOP-SPFP6AQ\twistedtechre
commit 46b57e3486
3 changed files with 41 additions and 0 deletions

View file

@ -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)

View file

@ -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
# ---------------------------------------------------------------------

View file

@ -238,3 +238,18 @@ ALWAYS_INLINE constexpr T SignExtendN(T value)
static_cast<std::underlying_type<type_>::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.