Forgot to serialize a new unitMotion variable, resulting in OOS on rejoin.
[0ad.git] / source / lib / byte_order.h
blob72763f04e348f2b475d4ac5426f4f52b3f32a0fb
1 /* Copyright (c) 2015 Wildfire Games
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
24 * byte order (endianness) support routines.
27 #ifndef INCLUDED_BYTE_ORDER
28 #define INCLUDED_BYTE_ORDER
30 #include "lib/sysdep/cpu.h"
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 || defined(__LITTLE_ENDIAN__)
37 # define BYTE_ORDER LITTLE_ENDIAN
38 # else
39 # define BYTE_ORDER BIG_ENDIAN
40 # endif
41 #endif
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
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).
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 )
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 )
68 #if BYTE_ORDER == BIG_ENDIAN
69 # define FOURCC FOURCC_BE
70 #else
71 # define FOURCC FOURCC_LE
72 #endif
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
95 /// read a little-endian number from memory into native byte order.
96 LIB_API u16 read_le16(const void* p);
97 LIB_API u32 read_le32(const void* p); /// see read_le16
98 LIB_API u64 read_le64(const void* p); /// see read_le16
100 /// read a big-endian number from memory into native byte order.
101 LIB_API u16 read_be16(const void* p);
102 LIB_API u32 read_be32(const void* p); /// see read_be16
103 LIB_API u64 read_be64(const void* p); /// see read_be16
105 /// write a little-endian number to memory in native byte order.
106 LIB_API void write_le16(void* p, u16 x);
107 LIB_API void write_le32(void* p, u32 x); /// see write_le16
108 LIB_API void write_le64(void* p, u64 x); /// see write_le16
110 /// write a big-endian number to memory in native byte order.
111 LIB_API void write_be16(void* p, u16 x);
112 LIB_API void write_be32(void* p, u32 x); /// see write_be16
113 LIB_API void write_be64(void* p, u64 x); /// see write_be16
116 * zero-extend \<size\> (truncated to 8) bytes of little-endian data to u64,
117 * starting at address \<p\> (need not be aligned).
119 LIB_API u64 movzx_le64(const u8* p, size_t size);
120 LIB_API u64 movzx_be64(const u8* p, size_t size);
123 * sign-extend \<size\> (truncated to 8) bytes of little-endian data to i64,
124 * starting at address \<p\> (need not be aligned).
126 LIB_API i64 movsx_le64(const u8* p, size_t size);
127 LIB_API i64 movsx_be64(const u8* p, size_t size);
130 #if ICC_VERSION
131 #define swap32 _bswap
132 #define swap64 _bswap64
133 #elif MSC_VERSION
134 extern unsigned short _byteswap_ushort(unsigned short);
135 extern unsigned long _byteswap_ulong(unsigned long);
136 extern 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
156 #ifndef swap16
157 LIB_API u16 swap16(const u16 x);
158 #endif
159 #ifndef swap32
160 LIB_API u32 swap32(const u32 x);
161 #endif
162 #ifndef swap64
163 LIB_API u64 swap64(const u64 x);
164 #endif
166 #endif // #ifndef INCLUDED_BYTE_ORDER