blob: 1bd444518ab5febecb6cec6e2e7693c29359d082 [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#ifndef _ENDIAN_H
2#define _ENDIAN_H
3
Rich Felkerc1a96582012-09-07 23:13:55 -04004#include <features.h>
Rich Felkerfb247fa2012-09-02 12:46:06 -04005
Rich Felker0b44a032011-02-12 00:22:29 -05006#define __LITTLE_ENDIAN 1234
7#define __BIG_ENDIAN 4321
8#define __PDP_ENDIAN 3412
9
Rich Felkera3b20f62012-05-06 13:40:19 -040010#if defined(__GNUC__) && defined(__BYTE_ORDER__)
11#define __BYTE_ORDER __BYTE_ORDER__
12#else
Rich Felker0b44a032011-02-12 00:22:29 -050013#include <bits/endian.h>
Rich Felkera3b20f62012-05-06 13:40:19 -040014#endif
Rich Felker0b44a032011-02-12 00:22:29 -050015
16#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
Rich Felker8705a0f2012-04-22 11:08:01 -040017
Rich Felker0b44a032011-02-12 00:22:29 -050018#define BIG_ENDIAN __BIG_ENDIAN
19#define LITTLE_ENDIAN __LITTLE_ENDIAN
20#define PDP_ENDIAN __PDP_ENDIAN
21#define BYTE_ORDER __BYTE_ORDER
Rich Felker8705a0f2012-04-22 11:08:01 -040022
23#include <stdint.h>
24
Rich Felkerfb247fa2012-09-02 12:46:06 -040025static __inline uint16_t __bswap16(uint16_t __x)
Rich Felker8705a0f2012-04-22 11:08:01 -040026{
27 return __x<<8 | __x>>8;
28}
29
Rich Felkerfb247fa2012-09-02 12:46:06 -040030static __inline uint32_t __bswap32(uint32_t __x)
Rich Felker8705a0f2012-04-22 11:08:01 -040031{
32 return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
33}
34
Rich Felkerfb247fa2012-09-02 12:46:06 -040035static __inline uint64_t __bswap64(uint64_t __x)
Rich Felker8705a0f2012-04-22 11:08:01 -040036{
Rich Felker3f4de352012-04-22 11:19:17 -040037 return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
Rich Felker8705a0f2012-04-22 11:08:01 -040038}
39
40#if __BYTE_ORDER == __LITTLE_ENDIAN
41#define htobe16(x) __bswap16(x)
42#define be16toh(x) __bswap16(x)
43#define betoh16(x) __bswap16(x)
44#define htobe32(x) __bswap32(x)
45#define be32toh(x) __bswap32(x)
46#define betoh32(x) __bswap32(x)
47#define htobe64(x) __bswap64(x)
48#define be64toh(x) __bswap64(x)
49#define betoh64(x) __bswap64(x)
50#define htole16(x) (uint16_t)(x)
51#define le16toh(x) (uint16_t)(x)
52#define letoh16(x) (uint16_t)(x)
53#define htole32(x) (uint32_t)(x)
54#define le32toh(x) (uint32_t)(x)
55#define letoh32(x) (uint32_t)(x)
56#define htole64(x) (uint64_t)(x)
57#define le64toh(x) (uint64_t)(x)
58#define letoh64(x) (uint64_t)(x)
59#else
60#define htobe16(x) (uint16_t)(x)
61#define be16toh(x) (uint16_t)(x)
62#define betoh16(x) (uint16_t)(x)
63#define htobe32(x) (uint32_t)(x)
64#define be32toh(x) (uint32_t)(x)
65#define betoh32(x) (uint32_t)(x)
66#define htobe64(x) (uint64_t)(x)
67#define be64toh(x) (uint64_t)(x)
68#define betoh64(x) (uint64_t)(x)
69#define htole16(x) __bswap16(x)
70#define le16toh(x) __bswap16(x)
71#define letoh16(x) __bswap16(x)
72#define htole32(x) __bswap32(x)
73#define le32toh(x) __bswap32(x)
74#define letoh32(x) __bswap32(x)
75#define htole64(x) __bswap64(x)
76#define le64toh(x) __bswap64(x)
77#define letoh64(x) __bswap64(x)
78#endif
79
Rich Felker0b44a032011-02-12 00:22:29 -050080#endif
81
82#endif