-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathendian.h
More file actions
69 lines (52 loc) · 2.06 KB
/
endian.h
File metadata and controls
69 lines (52 loc) · 2.06 KB
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
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <sys/cdefs.h>
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html
// "Inclusion of the <endian.h> header may also make visible all symbols from <stdint.h>."
#include <stdint.h>
__BEGIN_DECLS
// "BYTE_ORDER
// This macro shall have a value equal to one of the *_ENDIAN macros in this header."
#define BYTE_ORDER __BYTE_ORDER__
// "LITTLE_ENDIAN
// If BYTE_ORDER == LITTLE_ENDIAN, the host byte order is from least significant to most significant."
#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
// "BIG_ENDIAN
// If BYTE_ORDER == BIG_ENDIAN, the host byte order is from most significant to least significant."
#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
// These definitions are not required by POSIX, but are assumed to be present by some ports.
#define __BYTE_ORDER BYTE_ORDER
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#define __BIG_ENDIAN BIG_ENDIAN
#if BYTE_ORDER == LITTLE_ENDIAN
# define be16toh(x) (__builtin_bswap16(x))
# define be32toh(x) (__builtin_bswap32(x))
# define be64toh(x) (__builtin_bswap64(x))
# define htobe16(x) (__builtin_bswap16(x))
# define htobe32(x) (__builtin_bswap32(x))
# define htobe64(x) (__builtin_bswap64(x))
# define htole16(x) ((uint16_t)(x))
# define htole32(x) ((uint32_t)(x))
# define htole64(x) ((uint64_t)(x))
# define le16toh(x) ((uint16_t)(x))
# define le32toh(x) ((uint32_t)(x))
# define le64toh(x) ((uint64_t)(x))
#else
# define be16toh(x) ((uint16_t)(x))
# define be32toh(x) ((uint32_t)(x))
# define be64toh(x) ((uint64_t)(x))
# define htobe16(x) ((uint16_t)(x))
# define htobe32(x) ((uint32_t)(x))
# define htobe64(x) ((uint64_t)(x))
# define htole16(x) (__builtin_bswap16(x))
# define htole32(x) (__builtin_bswap32(x))
# define htole64(x) (__builtin_bswap64(x))
# define le16toh(x) (__builtin_bswap16(x))
# define le32toh(x) (__builtin_bswap32(x))
# define le64toh(x) (__builtin_bswap64(x))
#endif
__END_DECLS