FreeNOS
ByteOrder.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __LIB_LIBSTD_BYTEORDER_H
19#define __LIB_LIBSTD_BYTEORDER_H
20
21#include "Types.h"
22#include "MemoryBlock.h"
23
32#ifndef __BYTE_ORDER__
33#error "__BYTE_ORDER__ not defined"
34#endif /* __BYTE_ORDER__ */
35
36#ifndef __ORDER_LITTLE_ENDIAN__
37#error "__ORDER_LITTLE_ENDIAN__ not defined"
38#endif /* __ORDER_LITTLE_ENDIAN__ */
39
40#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
41#error "This implementation only supports little-endian targets: define __BYTE_ORDER__ to __ORDER_LITTLE_ENDIAN__"
42#endif
43
56#define SWAP16(x) ((u16)( \
57 (((u16)(x) & (u16)0x00ffU) << 8) | \
58 (((u16)(x) & (u16)0xff00U) >> 8)))
59
68#define SWAP32(x) ((u32)( \
69 (((u32)(x) & (u32)0x000000ffUL) << 24) | \
70 (((u32)(x) & (u32)0x0000ff00UL) << 8) | \
71 (((u32)(x) & (u32)0x00ff0000UL) >> 8) | \
72 (((u32)(x) & (u32)0xff000000UL) >> 24)))
73
82#define SWAP64(x) ((u64)( \
83 (((u64)(x) & (u64)0x00000000000000ffUL) << 56) | \
84 (((u64)(x) & (u64)0x000000000000ff00UL) << 40) | \
85 (((u64)(x) & (u64)0x0000000000ff0000UL) << 24) | \
86 (((u64)(x) & (u64)0x00000000ff000000UL) << 8) | \
87 (((u64)(x) & (u64)0x000000ff00000000UL) >> 8) | \
88 (((u64)(x) & (u64)0x0000ff0000000000UL) >> 24) | \
89 (((u64)(x) & (u64)0x00ff000000000000UL) >> 40) | \
90 (((u64)(x) & (u64)0xff00000000000000UL) >> 56)))
91
108#define cpu_to_le64(x) ((le64)(u64)(x))
109
117#define le64_to_cpu(x) ((u64)(le64)(x))
118
126#define cpu_to_le32(x) ((le32)(u32)(x))
127
135#define le32_to_cpu(x) ((u32)(le32)(x))
136
144#define cpu_to_le16(x) ((le16)(u16)(x))
145
153#define le16_to_cpu(x) ((u16)(le16)(x))
154
162#define cpu_to_be64(x) ((be64)SWAP64((x)))
163
171#define be64_to_cpu(x) SWAP64((u64)(be64)(x))
172
180#define cpu_to_be32(x) ((be32)SWAP32((x)))
181
189#define be32_to_cpu(x) SWAP32((u32)(be32)(x))
190
198#define cpu_to_be16(x) ((be16)SWAP16((x)))
199
207#define be16_to_cpu(x) SWAP16((u16)(be16)(x))
208
228inline const u64 read64(const void *data)
229{
230 u64 value;
231 MemoryBlock::copy(&value, data, sizeof(value));
232 return value;
233}
234
242inline const u32 read32(const void *data)
243{
244 u32 value;
245 MemoryBlock::copy(&value, data, sizeof(value));
246 return value;
247}
248
256inline const u16 read16(const void *data)
257{
258 u16 value;
259 MemoryBlock::copy(&value, data, sizeof(value));
260 return value;
261}
262
270inline const u8 read8(const void *data)
271{
272 u8 value;
273 MemoryBlock::copy(&value, data, sizeof(value));
274 return value;
275}
276
283inline void write64(void *data, const u64 input)
284{
285 MemoryBlock::copy(data, &input, sizeof(input));
286}
287
294inline void write32(void *data, const u32 input)
295{
296 MemoryBlock::copy(data, &input, sizeof(input));
297}
298
305inline void write16(void *data, const u16 input)
306{
307 MemoryBlock::copy(data, &input, sizeof(input));
308}
309
316inline void write8(void *data, const u8 input)
317{
318 MemoryBlock::copy(data, &input, sizeof(input));
319}
320
328inline const u64 readLe64(const void *data)
329{
330 u64 value;
331 MemoryBlock::copy(&value, data, sizeof(value));
332 return le64_to_cpu(value);
333}
334
342inline const u32 readLe32(const void *data)
343{
344 u32 value;
345 MemoryBlock::copy(&value, data, sizeof(value));
346 return le32_to_cpu(value);
347}
348
356inline const u16 readLe16(const void *data)
357{
358 u16 value;
359 MemoryBlock::copy(&value, data, sizeof(value));
360 return le16_to_cpu(value);
361}
362
370inline const u64 readBe64(const void *data)
371{
372 u64 value;
373 MemoryBlock::copy(&value, data, sizeof(value));
374 return be64_to_cpu(value);
375}
376
384inline const u32 readBe32(const void *data)
385{
386 u32 value;
387 MemoryBlock::copy(&value, data, sizeof(value));
388 return be32_to_cpu(value);
389}
390
398inline const u16 readBe16(const void *data)
399{
400 u16 value;
401 MemoryBlock::copy(&value, data, sizeof(value));
402 return be16_to_cpu(value);
403}
404
411inline void writeLe64(void *data, const u64 input)
412{
413 const u64 value = cpu_to_le64(input);
414 MemoryBlock::copy(data, &value, sizeof(value));
415}
416
423inline void writeLe32(void *data, const u32 input)
424{
425 const u32 value = cpu_to_le32(input);
426 MemoryBlock::copy(data, &value, sizeof(value));
427}
428
435inline void writeLe16(void *data, const u16 input)
436{
437 const u16 value = cpu_to_le16(input);
438 MemoryBlock::copy(data, &value, sizeof(value));
439}
440
447inline void writeBe64(void *data, const u64 input)
448{
449 const u64 value = cpu_to_be64(input);
450 MemoryBlock::copy(data, &value, sizeof(value));
451}
452
459inline void writeBe32(void *data, const u32 input)
460{
461 const u32 value = cpu_to_be32(input);
462 MemoryBlock::copy(data, &value, sizeof(value));
463}
464
471inline void writeBe16(void *data, const u16 input)
472{
473 const u16 value = cpu_to_be16(input);
474 MemoryBlock::copy(data, &value, sizeof(value));
475}
476
486#endif /* __LIB_LIBSTD_BYTEORDER_H */
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
void writeBe32(void *data, const u32 input)
Write 32-bit big endian integer.
Definition ByteOrder.h:459
#define le64_to_cpu(x)
Little endian 64-bit to CPU byte order.
Definition ByteOrder.h:117
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
void write32(void *data, const u32 input)
Write 32-bit integer (no conversion)
Definition ByteOrder.h:294
#define cpu_to_le32(x)
CPU byte order to little endian 32-bit.
Definition ByteOrder.h:126
const u64 readBe64(const void *data)
Read 64-bit big endian integer.
Definition ByteOrder.h:370
void writeLe16(void *data, const u16 input)
Write 16-bit little endian integer.
Definition ByteOrder.h:435
const u32 read32(const void *data)
Read 32-bit integer (no conversion)
Definition ByteOrder.h:242
const u16 readBe16(const void *data)
Read 16-bit big endian integer.
Definition ByteOrder.h:398
#define be16_to_cpu(x)
Big endian 16-bit to CPU byte order.
Definition ByteOrder.h:207
#define le32_to_cpu(x)
Little endian 32-bit to CPU byte order.
Definition ByteOrder.h:135
void write64(void *data, const u64 input)
Write 64-bit integer (no conversion)
Definition ByteOrder.h:283
#define cpu_to_be64(x)
CPU byte order to big endian 64-bit.
Definition ByteOrder.h:162
#define be32_to_cpu(x)
Big endian 32-bit to CPU byte order.
Definition ByteOrder.h:189
#define le16_to_cpu(x)
Little endian 16-bit to CPU byte order.
Definition ByteOrder.h:153
void writeLe32(void *data, const u32 input)
Write 32-bit little endian integer.
Definition ByteOrder.h:423
void writeBe64(void *data, const u64 input)
Write 64-bit big endian integer.
Definition ByteOrder.h:447
const u16 readLe16(const void *data)
Read 16-bit little endian integer.
Definition ByteOrder.h:356
const u32 readBe32(const void *data)
Read 32-bit big endian integer.
Definition ByteOrder.h:384
const u16 read16(const void *data)
Read 16-bit integer (no conversion)
Definition ByteOrder.h:256
#define be64_to_cpu(x)
Big endian 64-bit to CPU byte order.
Definition ByteOrder.h:171
const u64 readLe64(const void *data)
Read 64-bit little endian integer.
Definition ByteOrder.h:328
unsigned short u16
Unsigned 16-bit number.
Definition Types.h:56
#define cpu_to_le64(x)
Integer conversion functions.
Definition ByteOrder.h:108
void write8(void *data, const u8 input)
Write 8-bit integer.
Definition ByteOrder.h:316
void write16(void *data, const u16 input)
Write 16-bit integer (no conversion)
Definition ByteOrder.h:305
const u8 read8(const void *data)
Read 8-bit integer.
Definition ByteOrder.h:270
void writeLe64(void *data, const u64 input)
Write 64-bit little endian integer.
Definition ByteOrder.h:411
unsigned long long u64
Unsigned 64-bit number.
Definition Types.h:50
const u64 read64(const void *data)
Memory read/write functions.
Definition ByteOrder.h:228
void writeBe16(void *data, const u16 input)
Write 16-bit big endian integer.
Definition ByteOrder.h:471
const u32 readLe32(const void *data)
Read 32-bit little endian integer.
Definition ByteOrder.h:342
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
#define cpu_to_le16(x)
CPU byte order to little endian 16-bit.
Definition ByteOrder.h:144
#define cpu_to_be16(x)
CPU byte order to big endian 16-bit.
Definition ByteOrder.h:198
#define cpu_to_be32(x)
CPU byte order to big endian 32-bit.
Definition ByteOrder.h:180