endian.h revision c5e77784
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
11
12#ifdef _BSD_SOURCE
13
14#include <config/HaikuConfig.h>
15#include <support/ByteOrder.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/*
22 * General byte order swapping functions.
23 */
24#define	bswap16(x)	__swap_int16(x)
25#define	bswap32(x)	__swap_int32(x)
26#define	bswap64(x)	__swap_int64(x)
27
28/*
29 * Host to big endian, host to little endian, big endian to host, and little
30 * endian to host byte order functions as detailed in byteorder(9).
31 */
32#if BYTE_ORDER == LITTLE_ENDIAN
33#define	htobe16(x)	bswap16((x))
34#define	htobe32(x)	bswap32((x))
35#define	htobe64(x)	bswap64((x))
36#define	htole16(x)	((uint16_t)(x))
37#define	htole32(x)	((uint32_t)(x))
38#define	htole64(x)	((uint64_t)(x))
39
40#define	be16toh(x)	bswap16((x))
41#define	be32toh(x)	bswap32((x))
42#define	be64toh(x)	bswap64((x))
43#define	le16toh(x)	((uint16_t)(x))
44#define	le32toh(x)	((uint32_t)(x))
45#define	le64toh(x)	((uint64_t)(x))
46#else /* BYTE_ORDER != LITTLE_ENDIAN */
47#define	htobe16(x)	((uint16_t)(x))
48#define	htobe32(x)	((uint32_t)(x))
49#define	htobe64(x)	((uint64_t)(x))
50#define	htole16(x)	bswap16((x))
51#define	htole32(x)	bswap32((x))
52#define	htole64(x)	bswap64((x))
53
54#define	be16toh(x)	((uint16_t)(x))
55#define	be32toh(x)	((uint32_t)(x))
56#define	be64toh(x)	((uint64_t)(x))
57#define	le16toh(x)	bswap16((x))
58#define	le32toh(x)	bswap32((x))
59#define	le64toh(x)	bswap64((x))
60#endif /* BYTE_ORDER == LITTLE_ENDIAN */
61
62#ifdef __cplusplus
63}
64#endif
65
66
67#endif
68
69
70#endif	/* _BSD_ENDIAN_H_ */
71