From fb6781015a19a8aa9464f072da961d508c5becaa Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 11 Jun 2024 18:47:16 +0100 Subject: [PATCH] Codechange: Use std::endian instead of TTD_ENDIAN defines. --- src/core/CMakeLists.txt | 1 - src/core/endian_func.hpp | 72 +++++++++++++++++++++++++-------------- src/core/endian_type.hpp | 22 ------------ src/saveload/saveload.cpp | 8 ++--- src/smallmap_gui.cpp | 2 +- src/table/palettes.h | 2 -- 6 files changed, 52 insertions(+), 55 deletions(-) delete mode 100644 src/core/endian_type.hpp diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6307dd2dff..6344bd2ea8 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -5,7 +5,6 @@ add_files( backup_type.hpp bitmath_func.hpp endian_func.hpp - endian_type.hpp enum_type.hpp format.hpp geometry_func.cpp diff --git a/src/core/endian_func.hpp b/src/core/endian_func.hpp index 780fab85da..c48c0f024b 100644 --- a/src/core/endian_func.hpp +++ b/src/core/endian_func.hpp @@ -10,32 +10,54 @@ #ifndef ENDIAN_FUNC_HPP #define ENDIAN_FUNC_HPP -#include "endian_type.hpp" #include "bitmath_func.hpp" -/* Setup alignment and conversion macros */ -#if TTD_ENDIAN == TTD_BIG_ENDIAN -# define FROM_BE16(x) (x) -# define FROM_BE32(x) (x) -# define TO_BE16(x) (x) -# define TO_BE32(x) (x) -# define TO_BE32X(x) (x) -# define FROM_LE16(x) std::byteswap(x) -# define FROM_LE32(x) std::byteswap(x) -# define TO_LE16(x) std::byteswap(x) -# define TO_LE32(x) std::byteswap(x) -# define TO_LE32X(x) std::byteswap(x) -#else -# define FROM_BE16(x) std::byteswap(x) -# define FROM_BE32(x) std::byteswap(x) -# define TO_BE16(x) std::byteswap(x) -# define TO_BE32(x) std::byteswap(x) -# define TO_BE32X(x) std::byteswap(x) -# define FROM_LE16(x) (x) -# define FROM_LE32(x) (x) -# define TO_LE16(x) (x) -# define TO_LE32(x) (x) -# define TO_LE32X(x) (x) -#endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */ +static constexpr uint16_t FROM_BE16(uint16_t x) +{ + if constexpr (std::endian::native == std::endian::big) return x; + return std::byteswap(x); +} + +static constexpr uint32_t FROM_BE32(uint32_t x) +{ + if constexpr (std::endian::native == std::endian::big) return x; + return std::byteswap(x); +} + +static constexpr uint16_t TO_BE16(uint16_t x) +{ + if constexpr (std::endian::native == std::endian::big) return x; + return std::byteswap(x); +} + +static constexpr uint32_t TO_BE32(uint32_t x) +{ + if constexpr (std::endian::native == std::endian::big) return x; + return std::byteswap(x); +} + +static constexpr uint16_t FROM_LE16(uint16_t x) +{ + if constexpr (std::endian::native == std::endian::little) return x; + return std::byteswap(x); +} + +static constexpr uint32_t FROM_LE32(uint32_t x) +{ + if constexpr (std::endian::native == std::endian::little) return x; + return std::byteswap(x); +} + +static constexpr uint16_t TO_LE16(uint16_t x) +{ + if constexpr (std::endian::native == std::endian::little) return x; + return std::byteswap(x); +} + +static constexpr uint32_t TO_LE32(uint32_t x) +{ + if constexpr (std::endian::native == std::endian::little) return x; + return std::byteswap(x); +} #endif /* ENDIAN_FUNC_HPP */ diff --git a/src/core/endian_type.hpp b/src/core/endian_type.hpp deleted file mode 100644 index 025bae8987..0000000000 --- a/src/core/endian_type.hpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * This file is part of OpenTTD. - * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. - * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . - */ - -/** @file endian_type.hpp Definition of various endian-dependant macros. */ - -#ifndef ENDIAN_TYPE_HPP -#define ENDIAN_TYPE_HPP - -/** Little endian builds use this for TTD_ENDIAN. */ -#define TTD_LITTLE_ENDIAN 0 -/** Big endian builds use this for TTD_ENDIAN. */ -#define TTD_BIG_ENDIAN 1 - -#if !defined(TTD_ENDIAN) -# error "TTD_ENDIAN is not defined; please set it to either TTD_LITTLE_ENDIAN or TTD_BIG_ENDIAN" -#endif /* !TTD_ENDIAN */ - -#endif /* ENDIAN_TYPE_HPP */ diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 6f64b632da..7bd2b30845 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -2697,10 +2697,10 @@ struct SaveLoadFormat { uint8_t max_compression; ///< the maximum compression level of this format }; -static const uint32_t SAVEGAME_TAG_LZO = TO_BE32X('OTTD'); -static const uint32_t SAVEGAME_TAG_NONE = TO_BE32X('OTTN'); -static const uint32_t SAVEGAME_TAG_ZLIB = TO_BE32X('OTTZ'); -static const uint32_t SAVEGAME_TAG_LZMA = TO_BE32X('OTTX'); +static const uint32_t SAVEGAME_TAG_LZO = TO_BE32('OTTD'); +static const uint32_t SAVEGAME_TAG_NONE = TO_BE32('OTTN'); +static const uint32_t SAVEGAME_TAG_ZLIB = TO_BE32('OTTZ'); +static const uint32_t SAVEGAME_TAG_LZMA = TO_BE32('OTTX'); /** The different saveload formats known/understood by OpenTTD. */ static const SaveLoadFormat _saveload_formats[] = { diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index 1005d63f4e..1b480bfad9 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -256,7 +256,7 @@ static const LegendAndColour * const _legend_table[] = { _legend_land_owners, }; -#define MKCOLOUR(x) TO_LE32X(x) +#define MKCOLOUR(x) TO_LE32(x) #define MKCOLOUR_XXXX(x) (MKCOLOUR(0x01010101) * (uint)(x)) #define MKCOLOUR_0XX0(x) (MKCOLOUR(0x00010100) * (uint)(x)) diff --git a/src/table/palettes.h b/src/table/palettes.h index e5d1684b3f..ec0a62a0ba 100644 --- a/src/table/palettes.h +++ b/src/table/palettes.h @@ -7,8 +7,6 @@ /** @file palettes.h The colour translation of the GRF palettes. */ -#include "../core/endian_type.hpp" - #define M(r, g, b) Colour(r, g, b) /** Colour palette (DOS) */