Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : /*
24 : * byte order (endianness) support routines.
25 : */
26 :
27 : #include "precompiled.h"
28 :
29 : #include "byte_order.h"
30 :
31 : #include "lib/bits.h"
32 :
33 : #include <algorithm>
34 : #include <cstring>
35 :
36 : #ifndef swap16
37 1 : std::uint16_t swap16(const std::uint16_t x)
38 : {
39 1 : return static_cast<std::uint16_t>(((x & 0xff) << 8) | (x >> 8));
40 : }
41 : #endif
42 :
43 : #ifndef swap32
44 2 : std::uint32_t swap32(const std::uint32_t x)
45 : {
46 2 : return (x << 24) |
47 2 : (x >> 24) |
48 2 : ((x << 8) & 0x00ff0000) |
49 2 : ((x >> 8) & 0x0000ff00);
50 : }
51 : #endif
52 :
53 : #ifndef swap64
54 1 : std::uint64_t swap64(const std::uint64_t x)
55 : {
56 1 : const std::uint32_t lo = static_cast<std::uint32_t>(x & 0xFFFFFFFF);
57 1 : const std::uint32_t hi = static_cast<std::uint32_t>(x >> 32);
58 1 : std::uint64_t ret = swap32(lo);
59 1 : ret <<= 32;
60 : // careful: must shift var of type std::uint64_t, not std::uint32_t
61 1 : ret |= swap32(hi);
62 1 : return ret;
63 : }
64 : #endif
65 :
66 :
67 : //-----------------------------------------------------------------------------
68 :
69 :
70 1051 : std::uint16_t read_le16(const void* p)
71 : {
72 1051 : std::uint16_t n;
73 1051 : memcpy(&n, p, sizeof(n));
74 1051 : return to_le16(n);
75 : }
76 :
77 625 : std::uint32_t read_le32(const void* p)
78 : {
79 625 : std::uint32_t n;
80 625 : memcpy(&n, p, sizeof(n));
81 625 : return to_le32(n);
82 : }
83 :
84 521 : std::uint64_t read_le64(const void* p)
85 : {
86 521 : std::uint64_t n;
87 521 : memcpy(&n, p, sizeof(n));
88 521 : return to_le64(n);
89 : }
90 :
91 :
92 0 : std::uint16_t read_be16(const void* p)
93 : {
94 0 : std::uint16_t n;
95 0 : memcpy(&n, p, sizeof(n));
96 0 : return to_be16(n);
97 : }
98 :
99 0 : std::uint32_t read_be32(const void* p)
100 : {
101 0 : std::uint32_t n;
102 0 : memcpy(&n, p, sizeof(n));
103 0 : return to_be32(n);
104 : }
105 :
106 0 : std::uint64_t read_be64(const void* p)
107 : {
108 0 : std::uint64_t n;
109 0 : memcpy(&n, p, sizeof(n));
110 0 : return to_be64(n);
111 : }
112 :
113 :
114 0 : void write_le16(void* p, std::uint16_t x)
115 : {
116 0 : std::uint16_t n = to_le16(x);
117 0 : memcpy(p, &n, sizeof(n));
118 0 : }
119 :
120 1 : void write_le32(void* p, std::uint32_t x)
121 : {
122 1 : std::uint32_t n = to_le32(x);
123 1 : memcpy(p, &n, sizeof(n));
124 1 : }
125 :
126 0 : void write_le64(void* p, std::uint64_t x)
127 : {
128 0 : std::uint64_t n = to_le64(x);
129 0 : memcpy(p, &n, sizeof(n));
130 0 : }
131 :
132 :
133 0 : void write_be16(void* p, std::uint16_t x)
134 : {
135 0 : std::uint16_t n = to_be16(x);
136 0 : memcpy(p, &n, sizeof(n));
137 0 : }
138 :
139 0 : void write_be32(void* p, std::uint32_t x)
140 : {
141 0 : std::uint32_t n = to_be32(x);
142 0 : memcpy(p, &n, sizeof(n));
143 0 : }
144 :
145 0 : void write_be64(void* p, std::uint64_t x)
146 : {
147 0 : std::uint64_t n = to_be64(x);
148 0 : memcpy(p, &n, sizeof(n));
149 0 : }
150 :
151 :
152 9 : std::uint64_t movzx_le64(const std::uint8_t* p, size_t size_bytes)
153 : {
154 9 : std::uint64_t number = 0;
155 40 : for(size_t i = 0; i < std::min(size_bytes, (size_t)8u); i++)
156 31 : number |= static_cast<std::uint64_t>(p[i]) << (i*8);
157 :
158 9 : return number;
159 : }
160 :
161 9 : std::uint64_t movzx_be64(const std::uint8_t* p, size_t size_bytes)
162 : {
163 9 : std::uint64_t number = 0;
164 40 : for(size_t i = 0; i < std::min(size_bytes, (size_t)8u); i++)
165 : {
166 31 : number <<= 8;
167 31 : number |= p[i];
168 : }
169 :
170 9 : return number;
171 : }
172 :
173 :
174 8 : static inline std::int64_t SignExtend(std::uint64_t bits, size_t size_bytes)
175 : {
176 : // no point in sign-extending if >= 8 bytes were requested
177 8 : if(size_bytes < 8)
178 : {
179 6 : const std::uint64_t sign_bit = Bit<std::uint64_t>((size_bytes*8)-1);
180 :
181 : // number would be negative in the smaller type,
182 : // so sign-extend, i.e. set all more significant bits.
183 6 : if(bits & sign_bit)
184 : {
185 3 : const std::uint64_t valid_bit_mask = (sign_bit+sign_bit)-1;
186 3 : bits |= ~valid_bit_mask;
187 : }
188 : }
189 :
190 8 : const std::int64_t number = static_cast<std::int64_t>(bits);
191 8 : return number;
192 : }
193 :
194 4 : std::int64_t movsx_le64(const std::uint8_t* p, size_t size_bytes)
195 : {
196 4 : const std::uint64_t number = movzx_le64(p, size_bytes);
197 4 : return SignExtend(number, size_bytes);
198 : }
199 :
200 4 : std::int64_t movsx_be64(const std::uint8_t* p, size_t size_bytes)
201 : {
202 4 : const std::uint64_t number = movzx_be64(p, size_bytes);
203 4 : return SignExtend(number, size_bytes);
204 : }
|