File: byte_order.cpp

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (200 lines) | stat: -rw-r--r-- 3,987 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Copyright (C) 2010 Wildfire Games.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * byte order (endianness) support routines.
 */

#include "precompiled.h"
#include "lib/byte_order.h"

#include "lib/bits.h"

#ifndef swap16
u16 swap16(const u16 x)
{
	return (u16)(((x & 0xff) << 8) | (x >> 8));
}
#endif

#ifndef swap32
u32 swap32(const u32 x)
{
	return (x << 24) |
		(x >> 24) |
		((x << 8) & 0x00ff0000) |
		((x >> 8) & 0x0000ff00);
}
#endif

#ifndef swap64
u64 swap64(const u64 x)
{
	const u32 lo = (u32)(x & 0xFFFFFFFF);
	const u32 hi = (u32)(x >> 32);
	u64 ret = swap32(lo);
	ret <<= 32;
	// careful: must shift var of type u64, not u32
	ret |= swap32(hi);
	return ret;
}
#endif


//-----------------------------------------------------------------------------


u16 read_le16(const void* p)
{
	u16 n;
	memcpy(&n, p, sizeof(n));
	return to_le16(n);
}

u32 read_le32(const void* p)
{
	u32 n;
	memcpy(&n, p, sizeof(n));
	return to_le32(n);
}

u64 read_le64(const void* p)
{
	u64 n;
	memcpy(&n, p, sizeof(n));
	return to_le64(n);
}


u16 read_be16(const void* p)
{
	u16 n;
	memcpy(&n, p, sizeof(n));
	return to_be16(n);
}

u32 read_be32(const void* p)
{
	u32 n;
	memcpy(&n, p, sizeof(n));
	return to_be32(n);
}

u64 read_be64(const void* p)
{
	u64 n;
	memcpy(&n, p, sizeof(n));
	return to_be64(n);
}


void write_le16(void* p, u16 x)
{
	u16 n = to_le16(x);
	memcpy(p, &n, sizeof(n));
}

void write_le32(void* p, u32 x)
{
	u32 n = to_le32(x);
	memcpy(p, &n, sizeof(n));
}

void write_le64(void* p, u64 x)
{
	u64 n = to_le64(x);
	memcpy(p, &n, sizeof(n));
}


void write_be16(void* p, u16 x)
{
	u16 n = to_be16(x);
	memcpy(p, &n, sizeof(n));
}

void write_be32(void* p, u32 x)
{
	u32 n = to_be32(x);
	memcpy(p, &n, sizeof(n));
}

void write_be64(void* p, u64 x)
{
	u64 n = to_be64(x);
	memcpy(p, &n, sizeof(n));
}


u64 movzx_le64(const u8* p, size_t size_bytes)
{
	u64 number = 0;
	for(size_t i = 0; i < std::min(size_bytes, (size_t)8u); i++)
		number |= ((u64)p[i]) << (i*8);

	return number;
}

u64 movzx_be64(const u8* p, size_t size_bytes)
{
	u64 number = 0;
	for(size_t i = 0; i < std::min(size_bytes, (size_t)8u); i++)
	{
		number <<= 8;
		number |= p[i];
	}

	return number;
}


static inline i64 SignExtend(u64 bits, size_t size_bytes)
{
	// no point in sign-extending if >= 8 bytes were requested
	if(size_bytes < 8)
	{
		const u64 sign_bit = Bit<u64>((size_bytes*8)-1);

		// number would be negative in the smaller type,
		// so sign-extend, i.e. set all more significant bits.
		if(bits & sign_bit)
		{
			const u64 valid_bit_mask = (sign_bit+sign_bit)-1;
			bits |= ~valid_bit_mask;
		}
	}

	const i64 number = static_cast<i64>(bits);
	return number;
}

i64 movsx_le64(const u8* p, size_t size_bytes)
{
	const u64 number = movzx_le64(p, size_bytes);
	return SignExtend(number, size_bytes);
}

i64 movsx_be64(const u8* p, size_t size_bytes)
{
	const u64 number = movzx_be64(p, size_bytes);
	return SignExtend(number, size_bytes);
}