Commit | Line | Data |
---|---|---|
0c1f3509 MG |
1 | #ifndef _SYSENDIAN_H_ |
2 | #define _SYSENDIAN_H_ | |
3 | ||
4 | #include <stdint.h> | |
5 | ||
6 | /* Avoid namespace collisions with BSD <sys/endian.h>. */ | |
7 | #define be16dec libcperciva_be16dec | |
8 | #define be16enc libcperciva_be16enc | |
9 | #define be32dec libcperciva_be32dec | |
10 | #define be32enc libcperciva_be32enc | |
11 | #define be64dec libcperciva_be64dec | |
12 | #define be64enc libcperciva_be64enc | |
13 | #define le16dec libcperciva_le16dec | |
14 | #define le16enc libcperciva_le16enc | |
15 | #define le32dec libcperciva_le32dec | |
16 | #define le32enc libcperciva_le32enc | |
17 | #define le64dec libcperciva_le64dec | |
18 | #define le64enc libcperciva_le64enc | |
19 | ||
20 | static inline uint16_t | |
21 | be16dec(const void * pp) | |
22 | { | |
23 | const uint8_t * p = (uint8_t const *)pp; | |
24 | ||
25 | return (uint16_t)((uint16_t)(p[1]) + ((uint16_t)(p[0]) << 8)); | |
26 | } | |
27 | ||
28 | static inline void | |
29 | be16enc(void * pp, uint16_t x) | |
30 | { | |
31 | uint8_t * p = (uint8_t *)pp; | |
32 | ||
33 | p[1] = x & 0xff; | |
34 | p[0] = (x >> 8) & 0xff; | |
35 | } | |
36 | ||
37 | static inline uint32_t | |
38 | be32dec(const void * pp) | |
39 | { | |
40 | const uint8_t * p = (uint8_t const *)pp; | |
41 | ||
42 | return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + | |
43 | ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); | |
44 | } | |
45 | ||
46 | static inline void | |
47 | be32enc(void * pp, uint32_t x) | |
48 | { | |
49 | uint8_t * p = (uint8_t *)pp; | |
50 | ||
51 | p[3] = x & 0xff; | |
52 | p[2] = (x >> 8) & 0xff; | |
53 | p[1] = (x >> 16) & 0xff; | |
54 | p[0] = (x >> 24) & 0xff; | |
55 | } | |
56 | ||
57 | static inline uint64_t | |
58 | be64dec(const void * pp) | |
59 | { | |
60 | const uint8_t * p = (uint8_t const *)pp; | |
61 | ||
62 | return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) + | |
63 | ((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) + | |
64 | ((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) + | |
65 | ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56)); | |
66 | } | |
67 | ||
68 | static inline void | |
69 | be64enc(void * pp, uint64_t x) | |
70 | { | |
71 | uint8_t * p = (uint8_t *)pp; | |
72 | ||
73 | p[7] = x & 0xff; | |
74 | p[6] = (x >> 8) & 0xff; | |
75 | p[5] = (x >> 16) & 0xff; | |
76 | p[4] = (x >> 24) & 0xff; | |
77 | p[3] = (x >> 32) & 0xff; | |
78 | p[2] = (x >> 40) & 0xff; | |
79 | p[1] = (x >> 48) & 0xff; | |
80 | p[0] = (x >> 56) & 0xff; | |
81 | } | |
82 | ||
83 | static inline uint16_t | |
84 | le16dec(const void * pp) | |
85 | { | |
86 | const uint8_t * p = (uint8_t const *)pp; | |
87 | ||
88 | return (uint16_t)((uint16_t)(p[0]) + ((uint16_t)(p[1]) << 8)); | |
89 | } | |
90 | ||
91 | static inline void | |
92 | le16enc(void * pp, uint16_t x) | |
93 | { | |
94 | uint8_t * p = (uint8_t *)pp; | |
95 | ||
96 | p[0] = x & 0xff; | |
97 | p[1] = (x >> 8) & 0xff; | |
98 | } | |
99 | ||
100 | static inline uint32_t | |
101 | le32dec(const void * pp) | |
102 | { | |
103 | const uint8_t * p = (uint8_t const *)pp; | |
104 | ||
105 | return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + | |
106 | ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); | |
107 | } | |
108 | ||
109 | static inline void | |
110 | le32enc(void * pp, uint32_t x) | |
111 | { | |
112 | uint8_t * p = (uint8_t *)pp; | |
113 | ||
114 | p[0] = x & 0xff; | |
115 | p[1] = (x >> 8) & 0xff; | |
116 | p[2] = (x >> 16) & 0xff; | |
117 | p[3] = (x >> 24) & 0xff; | |
118 | } | |
119 | ||
120 | static inline uint64_t | |
121 | le64dec(const void * pp) | |
122 | { | |
123 | const uint8_t * p = (uint8_t const *)pp; | |
124 | ||
125 | return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) + | |
126 | ((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) + | |
127 | ((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) + | |
128 | ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56)); | |
129 | } | |
130 | ||
131 | static inline void | |
132 | le64enc(void * pp, uint64_t x) | |
133 | { | |
134 | uint8_t * p = (uint8_t *)pp; | |
135 | ||
136 | p[0] = x & 0xff; | |
137 | p[1] = (x >> 8) & 0xff; | |
138 | p[2] = (x >> 16) & 0xff; | |
139 | p[3] = (x >> 24) & 0xff; | |
140 | p[4] = (x >> 32) & 0xff; | |
141 | p[5] = (x >> 40) & 0xff; | |
142 | p[6] = (x >> 48) & 0xff; | |
143 | p[7] = (x >> 56) & 0xff; | |
144 | } | |
145 | ||
146 | #endif /* !_SYSENDIAN_H_ */ |