1 /*
2 * Copyright 2017 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef _BSD_ENDIAN_H_
6 #define _BSD_ENDIAN_H_
7
8
9 #include_next <endian.h>
10 #include <features.h>
11 #include <stdint.h>
12
13
14 #ifdef _DEFAULT_SOURCE
15
16 #include <config/HaikuConfig.h>
17
18 #if __GNUC__ < 4
19 #include <support/ByteOrder.h>
20 #include <support/SupportDefs.h>
21 #endif
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 /*
28 * General byte order swapping functions.
29 */
30 #if __GNUC__ >= 4
31 #define bswap16(x) __builtin_bswap16(x)
32 #define bswap32(x) __builtin_bswap32(x)
33 #define bswap64(x) __builtin_bswap64(x)
34 #else
35 #define bswap16(x) __swap_int16(x)
36 #define bswap32(x) __swap_int32(x)
37 #define bswap64(x) __swap_int64(x)
38 #endif
39
40 /*
41 * Host to big endian, host to little endian, big endian to host, and little
42 * endian to host byte order functions as detailed in byteorder(9).
43 */
44 #if BYTE_ORDER == LITTLE_ENDIAN
45 #define htobe16(x) bswap16((x))
46 #define htobe32(x) bswap32((x))
47 #define htobe64(x) bswap64((x))
48 #define htole16(x) ((uint16_t)(x))
49 #define htole32(x) ((uint32_t)(x))
50 #define htole64(x) ((uint64_t)(x))
51
52 #define be16toh(x) bswap16((x))
53 #define be32toh(x) bswap32((x))
54 #define be64toh(x) bswap64((x))
55 #define le16toh(x) ((uint16_t)(x))
56 #define le32toh(x) ((uint32_t)(x))
57 #define le64toh(x) ((uint64_t)(x))
58 #else /* BYTE_ORDER != LITTLE_ENDIAN */
59 #define htobe16(x) ((uint16_t)(x))
60 #define htobe32(x) ((uint32_t)(x))
61 #define htobe64(x) ((uint64_t)(x))
62 #define htole16(x) bswap16((x))
63 #define htole32(x) bswap32((x))
64 #define htole64(x) bswap64((x))
65
66 #define be16toh(x) ((uint16_t)(x))
67 #define be32toh(x) ((uint32_t)(x))
68 #define be64toh(x) ((uint64_t)(x))
69 #define le16toh(x) bswap16((x))
70 #define le32toh(x) bswap32((x))
71 #define le64toh(x) bswap64((x))
72 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
73
74 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
75
76 static __inline uint32_t
be32dec(const void * pp)77 be32dec(const void *pp)
78 {
79 uint8_t const *p = (uint8_t const *)pp;
80
81 return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
82 }
83
84 static __inline uint64_t
be64dec(const void * pp)85 be64dec(const void *pp)
86 {
87 uint8_t const *p = (uint8_t const *)pp;
88
89 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
90 }
91
92 static __inline void
be32enc(void * pp,uint32_t u)93 be32enc(void *pp, uint32_t u)
94 {
95 uint8_t *p = (uint8_t *)pp;
96
97 p[0] = (u >> 24) & 0xff;
98 p[1] = (u >> 16) & 0xff;
99 p[2] = (u >> 8) & 0xff;
100 p[3] = u & 0xff;
101 }
102
103 static __inline void
be64enc(void * pp,uint64_t u)104 be64enc(void *pp, uint64_t u)
105 {
106 uint8_t *p = (uint8_t *)pp;
107
108 be32enc(p, (uint32_t)(u >> 32));
109 be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
110 }
111
112 #ifdef __cplusplus
113 }
114 #endif
115
116
117 #endif
118
119
120 #endif /* _BSD_ENDIAN_H_ */
121