1 | /** |
---|
2 | * @file rtems/endian.h |
---|
3 | * |
---|
4 | * This include file provides endian information about the target. |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | #ifndef _RTEMS_ENDIAN_H |
---|
9 | #define _RTEMS_ENDIAN_H |
---|
10 | |
---|
11 | #include <rtems/score/cpu.h> |
---|
12 | |
---|
13 | /* |
---|
14 | * BSD-style endian declaration |
---|
15 | */ |
---|
16 | #define BIG_ENDIAN 4321 |
---|
17 | #define LITTLE_ENDIAN 1234 |
---|
18 | |
---|
19 | #ifndef BYTE_ORDER |
---|
20 | #if CPU_BIG_ENDIAN |
---|
21 | # define BYTE_ORDER BIG_ENDIAN |
---|
22 | #elif CPU_LITTLE_ENDIAN |
---|
23 | # define BYTE_ORDER LITTLE_ENDIAN |
---|
24 | #else |
---|
25 | # error "Can't decide which end is which!" |
---|
26 | #endif |
---|
27 | #endif |
---|
28 | |
---|
29 | #if ( CPU_BIG_ENDIAN == TRUE ) |
---|
30 | |
---|
31 | /* |
---|
32 | * Very simply on big endian CPUs |
---|
33 | */ |
---|
34 | |
---|
35 | static inline uint32_t ntohl( uint32_t _x ) |
---|
36 | { |
---|
37 | return _x; |
---|
38 | } |
---|
39 | |
---|
40 | static inline uint16_t ntohs( uint16_t _x ) |
---|
41 | { |
---|
42 | return _x; |
---|
43 | } |
---|
44 | |
---|
45 | static inline uint32_t htonl( uint32_t _x ) |
---|
46 | { |
---|
47 | return _x; |
---|
48 | } |
---|
49 | |
---|
50 | static inline uint16_t htons( uint16_t _x ) |
---|
51 | { |
---|
52 | return _x; |
---|
53 | } |
---|
54 | |
---|
55 | #define NTOHS(x) |
---|
56 | #define HTONS(x) |
---|
57 | #define NTOHL(x) |
---|
58 | #define HTONL(x) |
---|
59 | |
---|
60 | #elif ( CPU_LITTLE_ENDIAN == TRUE ) |
---|
61 | |
---|
62 | /* |
---|
63 | * A little more complicated on little endian CPUs |
---|
64 | */ |
---|
65 | |
---|
66 | static inline uint32_t ntohl( uint32_t _x ) |
---|
67 | { |
---|
68 | return CPU_swap_u32(_x); |
---|
69 | } |
---|
70 | |
---|
71 | static inline uint16_t ntohs( uint16_t _x ) |
---|
72 | { |
---|
73 | return CPU_swap_u16(_x); |
---|
74 | } |
---|
75 | |
---|
76 | static inline uint32_t htonl( uint32_t _x ) |
---|
77 | { |
---|
78 | return CPU_swap_u32(_x); |
---|
79 | } |
---|
80 | |
---|
81 | static inline uint16_t htons( uint16_t _x ) |
---|
82 | { |
---|
83 | return CPU_swap_u16(_x); |
---|
84 | } |
---|
85 | |
---|
86 | #define NTOHS(x) (x) = ntohs(x) |
---|
87 | #define HTONS(x) (x) = htons(x) |
---|
88 | #define NTOHL(x) (x) = ntohl(x) |
---|
89 | #define HTONL(x) (x) = htonl(x) |
---|
90 | |
---|
91 | #else |
---|
92 | #error "Unknown endian-ness for this cpu" |
---|
93 | #endif |
---|
94 | |
---|
95 | static inline uint16_t rtems_uint16_from_little_endian( const uint8_t *data) |
---|
96 | { |
---|
97 | uint16_t value = 0; |
---|
98 | int i; |
---|
99 | |
---|
100 | for (i = 1; i >= 0; --i) { |
---|
101 | value = (uint16_t) ((value << 8) + data [i]); |
---|
102 | } |
---|
103 | |
---|
104 | return value; |
---|
105 | } |
---|
106 | |
---|
107 | static inline uint32_t rtems_uint32_from_little_endian( const uint8_t *data) |
---|
108 | { |
---|
109 | uint32_t value = 0; |
---|
110 | int i; |
---|
111 | |
---|
112 | for (i = 3; i >= 0; --i) { |
---|
113 | value = (value << 8) + data [i]; |
---|
114 | } |
---|
115 | |
---|
116 | return value; |
---|
117 | } |
---|
118 | |
---|
119 | static inline uint64_t rtems_uint64_from_little_endian( const uint8_t *data) |
---|
120 | { |
---|
121 | uint64_t value = 0; |
---|
122 | int i; |
---|
123 | |
---|
124 | for (i = 7; i >= 0; --i) { |
---|
125 | value = (value << 8) + (uint64_t) data [i]; |
---|
126 | } |
---|
127 | |
---|
128 | return value; |
---|
129 | } |
---|
130 | |
---|
131 | static inline uint16_t rtems_uint16_from_big_endian( const uint8_t *data) |
---|
132 | { |
---|
133 | uint16_t value = 0; |
---|
134 | int i; |
---|
135 | |
---|
136 | for (i = 0; i < 2; ++i) { |
---|
137 | value = (uint16_t) ((value << 8) + data [i]); |
---|
138 | } |
---|
139 | |
---|
140 | return value; |
---|
141 | } |
---|
142 | |
---|
143 | static inline uint32_t rtems_uint32_from_big_endian( const uint8_t *data) |
---|
144 | { |
---|
145 | uint32_t value = 0; |
---|
146 | int i; |
---|
147 | |
---|
148 | for (i = 0; i < 4; ++i) { |
---|
149 | value = (value << 8) + (uint32_t) data [i]; |
---|
150 | } |
---|
151 | |
---|
152 | return value; |
---|
153 | } |
---|
154 | |
---|
155 | static inline uint64_t rtems_uint64_from_big_endian( const uint8_t *data) |
---|
156 | { |
---|
157 | uint64_t value = 0; |
---|
158 | int i; |
---|
159 | |
---|
160 | for (i = 0; i < 8; ++i) { |
---|
161 | value = (value << 8) + (uint64_t) data [i]; |
---|
162 | } |
---|
163 | |
---|
164 | return value; |
---|
165 | } |
---|
166 | |
---|
167 | static inline void rtems_uint16_to_little_endian( uint16_t value, uint8_t *data) |
---|
168 | { |
---|
169 | int i; |
---|
170 | |
---|
171 | for (i = 0; i < 2; ++i) { |
---|
172 | data [i] = (uint8_t) value; |
---|
173 | value >>= 8; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | static inline void rtems_uint32_to_little_endian( uint32_t value, uint8_t *data) |
---|
178 | { |
---|
179 | int i; |
---|
180 | |
---|
181 | for (i = 0; i < 4; ++i) { |
---|
182 | data [i] = (uint8_t) value; |
---|
183 | value >>= 8; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | static inline void rtems_uint64_to_little_endian( uint64_t value, uint8_t *data) |
---|
188 | { |
---|
189 | int i; |
---|
190 | |
---|
191 | for (i = 0; i < 8; ++i) { |
---|
192 | data [i] = (uint8_t) value; |
---|
193 | value >>= 8; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | static inline void rtems_uint16_to_big_endian( uint16_t value, uint8_t *data) |
---|
198 | { |
---|
199 | int i; |
---|
200 | |
---|
201 | for (i = 1; i >= 0; --i) { |
---|
202 | data [i] = (uint8_t) value; |
---|
203 | value >>= 8; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | static inline void rtems_uint32_to_big_endian( uint32_t value, uint8_t *data) |
---|
208 | { |
---|
209 | int i; |
---|
210 | |
---|
211 | for (i = 3; i >= 0; --i) { |
---|
212 | data [i] = (uint8_t) value; |
---|
213 | value >>= 8; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | static inline void rtems_uint64_to_big_endian( uint64_t value, uint8_t *data) |
---|
218 | { |
---|
219 | int i; |
---|
220 | |
---|
221 | for (i = 7; i >= 0; --i) { |
---|
222 | data [i] = (uint8_t) value; |
---|
223 | value >>= 8; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | #endif /* _RTEMS_ENDIAN_H */ |
---|