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