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