| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * Copyright (C) 2024 Descent Developers |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * $Logfile: /DescentIII/Main/lib/BYTESWAP.H $ |
| 22 | * $Revision: 8 $ |
| 23 | * $Date: 4/19/00 5:32p $ |
| 24 | * $Author: Matt $ |
| 25 | * |
| 26 | * Byteswapping macros (for big-endian machines) |
| 27 | * |
| 28 | * $Log: /DescentIII/Main/lib/BYTESWAP.H $ |
| 29 | * |
| 30 | * 8 4/19/00 5:32p Matt |
| 31 | * From Duane for 1.4 |
| 32 | * Added casts to SWAPSHORT and SWAPINT macros |
| 33 | * |
| 34 | * 7 10/21/99 9:27p Jeff |
| 35 | * B.A. Macintosh code merge |
| 36 | * |
| 37 | * 6 5/09/99 11:41p Jeff |
| 38 | * function free |
| 39 | * |
| 40 | * 5 5/05/99 5:27a Jeff |
| 41 | * renamed endian.h to psendian.h |
| 42 | * |
| 43 | * 4 5/01/99 2:52p Jeff |
| 44 | * added automatic endian detection of the system |
| 45 | * |
| 46 | * 3 4/17/99 7:49p Jeff |
| 47 | * for some reason Linux thinks it's big endian, temp fix (undef) until I |
| 48 | * get around to writting a endian check function |
| 49 | * |
| 50 | * 2 1/09/99 4:38p Jeff |
| 51 | * added some ifdefs and fixes to get files to compile under Linux |
| 52 | * |
| 53 | * 5 5/15/97 2:22p Matt |
| 54 | * Fixed (hopefully; it's not tested yet) byteswapping for floats |
| 55 | * |
| 56 | * 4 2/10/97 2:22p Matt |
| 57 | * Added cast |
| 58 | * |
| 59 | * 3 2/10/97 2:14p Matt |
| 60 | * Added BIG_ENDIAN define, & INT_FLOAT() macro |
| 61 | * |
| 62 | * $NoKeywords: $ |
| 63 | */ |
| 64 | |
| 65 | #ifndef BYTESWAP_H |
| 66 | #define BYTESWAP_H |
| 67 | |
| 68 | #include <cstdint> |
| 69 | #include <cstdlib> |
| 70 | |
| 71 | namespace D3 { |
| 72 | |
| 73 | // std::byteswap from C++23 |
| 74 | template <typename T> constexpr T byteswap(T n) { |
| 75 | T m; |
| 76 | for (size_t i = 0; i < sizeof(T); i++) |
| 77 | reinterpret_cast<uint8_t *>(&m)[i] = reinterpret_cast<uint8_t *>(&n)[sizeof(T) - 1 - i]; |
| 78 | return m; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Convert integer to/from BE order |
| 83 | */ |
| 84 | template <typename T> constexpr T convert_be(T val) { |
| 85 | #ifndef OUTRAGE_BIG_ENDIAN |
| 86 | return byteswap(val); |
| 87 | #else |
| 88 | return (val); |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Convert integer to/from LE order |
| 94 | */ |
| 95 | template <typename T> constexpr T convert_le(T val) { |
| 96 | #ifndef OUTRAGE_BIG_ENDIAN |
| 97 | return (val); |
| 98 | #else |
| 99 | return byteswap(val); |
| 100 | #endif |
| 101 | } |
| 102 | |
| 103 | } // namespace D3 |
| 104 | |
| 105 | // Compatibility macros. Use D3::convert_le / D3::convert_be when possible |
| 106 | |
| 107 | #define INTEL_INT(x) D3::convert_le(x) |
| 108 | #define INTEL_SHORT(x) D3::convert_le(x) |
| 109 | #define INTEL_FLOAT(x) D3::convert_le(x) |
| 110 | #define MOTOROLA_INT(x) D3::convert_be(x) |
| 111 | #define MOTOROLA_SHORT(x) D3::convert_be(x) |
| 112 | #define MOTOROLA_FLOAT(x) D3::convert_be(x) |
| 113 | |
| 114 | #endif |