Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
byte_order.h
Go to the documentation of this file.
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#ifndef INCLUDED_BYTE_ORDER
28#define INCLUDED_BYTE_ORDER
29
30#include "lib/sysdep/compiler.h"
31
32#include <cstddef>
33
34// detect byte order via predefined macros.
35#ifndef BYTE_ORDER
36# define LITTLE_ENDIAN 0x4321
37# define BIG_ENDIAN 0x1234
38# if ARCH_IA32 || ARCH_IA64 || ARCH_AMD64 || ARCH_ALPHA || ARCH_ARM || ARCH_AARCH64 || ARCH_MIPS || ARCH_E2K || ARCH_PPC64 || defined(__LITTLE_ENDIAN__)
39# define BYTE_ORDER LITTLE_ENDIAN
40# else
41# define BYTE_ORDER BIG_ENDIAN
42# endif
43#endif
44
45
46/**
47 * convert 4 characters to std::uint32_t (at compile time) for easy comparison.
48 * output is in native byte order; e.g. FOURCC_LE can be used instead.
49 **/
50#define FOURCC(a,b,c,d) // real definition is below
51#undef FOURCC
52
53// implementation rationale:
54// - can't pass code as string, and use s[0]..s[3], because
55// VC6/7 don't realize the macro is constant
56// (it should be usable as a switch{} expression)
57// - the casts are ugly but necessary. std::uint32_t is required because
58// std::uint8_t << 8 == 0; the additional std::uint8_t cast ensures each
59// character is treated as unsigned (otherwise, they'd be promoted to signed
60// int before the std::uint32_t cast, which would break things).
61
62/// big-endian version of FOURCC
63#define FOURCC_BE(a,b,c,d) ( static_cast<std::uint32_t>(static_cast<std::uint8_t>(a)) << 24 | \
64 static_cast<std::uint32_t>(static_cast<std::uint8_t>(b)) << 16 | \
65 static_cast<std::uint32_t>(static_cast<std::uint8_t>(c)) << 8 | \
66 static_cast<std::uint32_t>(static_cast<std::uint8_t>(d)) << 0 )
67
68/// little-endian version of FOURCC
69#define FOURCC_LE(a,b,c,d) ( static_cast<std::uint32_t>(static_cast<std::uint8_t>(a)) << 0 | \
70 static_cast<std::uint32_t>(static_cast<std::uint8_t>(b)) << 8 | \
71 static_cast<std::uint32_t>(static_cast<std::uint8_t>(c)) << 16 | \
72 static_cast<std::uint32_t>(static_cast<std::uint8_t>(d)) << 24 )
73
74#if BYTE_ORDER == BIG_ENDIAN
75# define FOURCC FOURCC_BE
76#else
77# define FOURCC FOURCC_LE
78#endif
79
80
81#if BYTE_ORDER == BIG_ENDIAN
82// convert a little-endian number to/from native byte order.
83# define to_le16(x) swap16(x)
84# define to_le32(x) swap32(x)
85# define to_le64(x) swap64(x)
86// convert a big-endian number to/from native byte order.
87# define to_be16(x) (x)
88# define to_be32(x) (x)
89# define to_be64(x) (x)
90#else // LITTLE_ENDIAN
91// convert a little-endian number to/from native byte order.
92# define to_le16(x) (x)
93# define to_le32(x) (x)
94# define to_le64(x) (x)
95// convert a big-endian number to/from native byte order.
96# define to_be16(x) swap16(x)
97# define to_be32(x) swap32(x)
98# define to_be64(x) swap64(x)
99#endif
100
101/// read a little-endian number from memory into native byte order.
102std::uint16_t read_le16(const void* p);
103std::uint32_t read_le32(const void* p); /// see read_le16
104std::uint64_t read_le64(const void* p); /// see read_le16
105
106/// read a big-endian number from memory into native byte order.
107std::uint16_t read_be16(const void* p);
108std::uint32_t read_be32(const void* p); /// see read_be16
109std::uint64_t read_be64(const void* p); /// see read_be16
110
111/// write a little-endian number to memory in native byte order.
112void write_le16(void* p, std::uint16_t x);
113void write_le32(void* p, std::uint32_t x); /// see write_le16
114void write_le64(void* p, std::uint64_t x); /// see write_le16
115
116/// write a big-endian number to memory in native byte order.
117void write_be16(void* p, std::uint16_t x);
118void write_be32(void* p, std::uint32_t x); /// see write_be16
119void write_be64(void* p, std::uint64_t x); /// see write_be16
120
121/**
122 * zero-extend <size> (truncated to 8) bytes of little-endian data to
123 * std::uint64_t, starting at address <p> (need not be aligned).
124 **/
125std::uint64_t movzx_le64(const std::uint8_t* p, size_t size);
126std::uint64_t movzx_be64(const std::uint8_t* p, size_t size);
127
128/**
129 * sign-extend <size> (truncated to 8) bytes of little-endian data to
130 * std::int64_t, starting at address <p> (need not be aligned).
131 **/
132std::int64_t movsx_le64(const std::uint8_t* p, size_t size);
133std::int64_t movsx_be64(const std::uint8_t* p, size_t size);
134
135
136#if MSC_VERSION
137extern unsigned short _byteswap_ushort(unsigned short);
138extern unsigned long _byteswap_ulong(unsigned long);
139extern unsigned __int64 _byteswap_uint64(unsigned __int64);
140#pragma intrinsic(_byteswap_ushort)
141#pragma intrinsic(_byteswap_ulong)
142#pragma intrinsic(_byteswap_uint64)
143# define swap16 _byteswap_ushort
144# define swap32 _byteswap_ulong
145# define swap64 _byteswap_uint64
146#elif defined(linux)
147# include <asm/byteorder.h>
148# if defined(__arch__swab16) && !defined(swap16)
149# define swap16 __arch__swab16
150# endif
151# if defined(__arch__swab32) && !defined(swap32)
152# define swap32 __arch__swab32
153# endif
154# if defined(__arch__swab64) && !defined(swap64)
155# define swap64 __arch__swab64
156# endif
157#endif
158
159#ifndef swap16
160std::uint16_t swap16(const std::uint16_t x);
161#endif
162#ifndef swap32
163std::uint32_t swap32(const std::uint32_t x);
164#endif
165#ifndef swap64
166std::uint64_t swap64(const std::uint64_t x);
167#endif
168
169#endif // #ifndef INCLUDED_BYTE_ORDER
std::uint16_t swap16(const std::uint16_t x)
Definition: byte_order.cpp:37
std::uint32_t read_le32(const void *p)
Definition: byte_order.cpp:77
void write_be64(void *p, std::uint64_t x)
see write_be16
Definition: byte_order.cpp:145
void write_le64(void *p, std::uint64_t x)
see write_le16
Definition: byte_order.cpp:126
std::uint16_t read_be16(const void *p)
see read_le16
Definition: byte_order.cpp:92
std::uint32_t swap32(const std::uint32_t x)
Definition: byte_order.cpp:44
void write_be16(void *p, std::uint16_t x)
see write_le16
Definition: byte_order.cpp:133
std::uint64_t swap64(const std::uint64_t x)
Definition: byte_order.cpp:54
std::uint32_t read_be32(const void *p)
Definition: byte_order.cpp:99
void write_le16(void *p, std::uint16_t x)
see read_be16
Definition: byte_order.cpp:114
std::uint64_t read_le64(const void *p)
see read_le16
Definition: byte_order.cpp:84
void write_le32(void *p, std::uint32_t x)
Definition: byte_order.cpp:120
std::int64_t movsx_be64(const std::uint8_t *p, size_t size)
Definition: byte_order.cpp:200
void write_be32(void *p, std::uint32_t x)
Definition: byte_order.cpp:139
std::uint16_t read_le16(const void *p)
read a little-endian number from memory into native byte order.
Definition: byte_order.cpp:70
std::uint64_t read_be64(const void *p)
see read_be16
Definition: byte_order.cpp:106
std::uint64_t movzx_be64(const std::uint8_t *p, size_t size)
Definition: byte_order.cpp:161
std::uint64_t movzx_le64(const std::uint8_t *p, size_t size)
see write_be16
Definition: byte_order.cpp:152
std::int64_t movsx_le64(const std::uint8_t *p, size_t size)
sign-extend <size> (truncated to 8) bytes of little-endian data to std::int64_t, starting at address ...
Definition: byte_order.cpp:194