| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* code to swap bytes because of big/little endian problems. |
| 23 |
* contains the macros: |
| 24 |
* SWAP{INT64,INT,SHORT}(x): returns a swapped version of x |
| 25 |
* INTEL_{INT64,INT,SHORT}(x): returns x after conversion to/from little endian |
| 26 |
* GET_INTEL_{INT64,INT,SHORT}(src): gets value from little-endian buffer src |
| 27 |
* PUT_INTEL_{INT64,INT,SHORT}(dest, src): puts src into little-endian buffer dest |
| 28 |
* |
| 29 |
* the GET/PUT macros are safe to use on platforms which segfault on unaligned word access |
| 30 |
* |
| 31 |
*/ |
| 32 |
|
| 33 |
#pragma once |
| 34 |
|
| 35 |
#include <concepts> |
| 36 |
#include <cstdint> |
| 37 |
#include <string.h> // for memcpy |
| 38 |
#include <type_traits> |
| 39 |
#include "dxxsconf.h" |
| 40 |
#include "pstypes.h" |
| 41 |
|
| 42 |
[[nodiscard]] |
| 43 |
static constexpr uint16_t SWAPSHORT(const uint16_t &x) |
| 44 |
{ |
| 45 |
#ifdef DXX_HAVE_BUILTIN_BSWAP |
| 46 |
return __builtin_bswap16(x); |
| 47 |
#else |
| 48 |
return (x << 8) | (x >> 8); |
| 49 |
#endif |
| 50 |
} |
| 51 |
|
| 52 |
[[nodiscard]] |
| 53 |
static constexpr int16_t SWAPSHORT(const int16_t &i) |
| 54 |
{ |
| 55 |
return SWAPSHORT(static_cast<uint16_t>(i)); |
| 56 |
} |
| 57 |
|
| 58 |
[[nodiscard]] |
| 59 |
static constexpr uint32_t SWAPINT(const uint32_t &x) |
| 60 |
{ |
| 61 |
#ifdef DXX_HAVE_BUILTIN_BSWAP |
| 62 |
return __builtin_bswap32(x); |
| 63 |
#else |
| 64 |
return (x << 24) | (x >> 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00); |
| 65 |
#endif |
| 66 |
} |
| 67 |
|
| 68 |
[[nodiscard]] |
| 69 |
static constexpr int32_t SWAPINT(const int32_t &i) |
| 70 |
{ |
| 71 |
return SWAPINT(static_cast<uint32_t>(i)); |
| 72 |
} |
| 73 |
|
| 74 |
#if !DXX_WORDS_BIGENDIAN |
| 75 |
#define byteutil_choose_endian(F,a) (a) |
| 76 |
#else // ! WORDS_BIGENDIAN |
| 77 |
#define byteutil_choose_endian(F,a) (F(a)) |
| 78 |
#endif // ! WORDS_BIGENDIAN |
| 79 |
constexpr std::integral_constant<int, DXX_WORDS_BIGENDIAN> words_bigendian{}; |
| 80 |
|
| 81 |
#if DXX_WORDS_NEED_ALIGNMENT |
| 82 |
#define byteutil_unaligned_copy(dt, d, s) ( DXX_BEGIN_COMPOUND_STATEMENT { dt &destination_reference = d; memcpy(&destination_reference, (s), sizeof(d)); } DXX_END_COMPOUND_STATEMENT ) |
| 83 |
#else // WORDS_NEED_ALIGNMENT |
| 84 |
#define byteutil_unaligned_copy(dt, d, s) ( DXX_BEGIN_COMPOUND_STATEMENT { dt &destination_reference = d; destination_reference = *reinterpret_cast<const dt *>(s); } DXX_END_COMPOUND_STATEMENT ) |
| 85 |
#endif // WORDS_NEED_ALIGNMENT |
| 86 |
|
| 87 |
[[nodiscard]] |
| 88 |
static constexpr uint16_t INTEL_SHORT(const uint16_t &x) |
| 89 |
{ |
| 90 |
return byteutil_choose_endian(SWAPSHORT, x); |
| 91 |
} |
| 92 |
|
| 93 |
[[nodiscard]] |
| 94 |
static constexpr int16_t INTEL_SHORT(const int16_t &x) |
| 95 |
{ |
| 96 |
return byteutil_choose_endian(SWAPSHORT, x); |
| 97 |
} |
| 98 |
|
| 99 |
[[nodiscard]] |
| 100 |
static constexpr uint32_t INTEL_INT(const uint32_t &x) |
| 101 |
{ |
| 102 |
return byteutil_choose_endian(SWAPINT, x); |
| 103 |
} |
| 104 |
|
| 105 |
[[nodiscard]] |
| 106 |
static constexpr int32_t INTEL_INT(const int32_t &x) |
| 107 |
{ |
| 108 |
return byteutil_choose_endian(SWAPINT, x); |
| 109 |
} |
| 110 |
#undef byteutil_choose_endian |
| 111 |
|
| 112 |
template <std::integral result_type = std::uint32_t> |
| 113 |
requires( |
| 114 |
sizeof(result_type) == sizeof(uint32_t) |
| 115 |
) |
| 116 |
[[nodiscard]] |
| 117 |
static inline result_type GET_INTEL_INT(const auto *p) |
| 118 |
{ |
| 119 |
result_type u; |
| 120 |
byteutil_unaligned_copy(result_type, u, p); |
| 121 |
return INTEL_INT(u); |
| 122 |
} |
| 123 |
|
| 124 |
template <std::integral result_type = std::uint16_t> |
| 125 |
requires( |
| 126 |
sizeof(result_type) == sizeof(uint16_t) |
| 127 |
) |
| 128 |
[[nodiscard]] |
| 129 |
static inline result_type GET_INTEL_SHORT(const auto *p) |
| 130 |
{ |
| 131 |
result_type u; |
| 132 |
byteutil_unaligned_copy(result_type, u, p); |
| 133 |
return INTEL_SHORT(u); |
| 134 |
} |
| 135 |
|
| 136 |
template <typename T> |
| 137 |
requires( |
| 138 |
std::same_as<T, int16_t> || |
| 139 |
requires(T t) { uint16_t{t}; } |
| 140 |
) |
| 141 |
static inline void PUT_INTEL_SHORT(uint16_t *d, const T s) |
| 142 |
{ |
| 143 |
const uint16_t u = INTEL_SHORT(s); |
| 144 |
byteutil_unaligned_copy(uint16_t, *d, &u); |
| 145 |
} |
| 146 |
|
| 147 |
static inline void PUT_INTEL_SHORT(uint8_t *d, const auto s) |
| 148 |
{ |
| 149 |
PUT_INTEL_SHORT(reinterpret_cast<uint16_t *>(d), s); |
| 150 |
} |
| 151 |
|
| 152 |
template <typename T> |
| 153 |
requires( |
| 154 |
std::same_as<T, int16_t> || |
| 155 |
std::same_as<T, int32_t> || |
| 156 |
requires(T t) { uint32_t{t}; } |
| 157 |
) |
| 158 |
static inline void PUT_INTEL_INT(uint32_t *d, const T s) |
| 159 |
{ |
| 160 |
const uint32_t u = INTEL_INT(s); |
| 161 |
byteutil_unaligned_copy(uint32_t, *d, &u); |
| 162 |
} |
| 163 |
|
| 164 |
static inline void PUT_INTEL_INT(uint8_t *d, const auto s) |
| 165 |
{ |
| 166 |
PUT_INTEL_INT(reinterpret_cast<uint32_t *>(d), s); |
| 167 |
} |
| 168 |
|
| 169 |
#undef byteutil_unaligned_copy |