RTEMS: cpukit/include/rtems/endian.h Source File

| RTEMS 5.4-rc2

Loading...
Searching...
No Matches
endian.h
Go to the documentation of this file.
1
8#ifndef _RTEMS_ENDIAN_H
9#define _RTEMS_ENDIAN_H
10
11#include <stdint.h>
12
13#include <machine/endian.h>
14
15#ifndef _BYTEORDER_FUNC_DEFINED
16#define _BYTEORDER_FUNC_DEFINED
17#define htonl(x) __htonl(x)
18#define htons(x) __htons(x)
19#define ntohl(x) __ntohl(x)
20#define ntohs(x) __ntohs(x)
21#endif
22
23#define NTOHS(x) (x) = ntohs(x)
24#define HTONS(x) (x) = htons(x)
25#define NTOHL(x) (x) = ntohl(x)
26#define HTONL(x) (x) = htonl(x)
27
28static inline uint16_t rtems_uint16_from_little_endian( const uint8_t *data)
29{
30 uint16_t value = 0;
31 int i;
32
33 for (i = 1; i >= 0; --i) {
34 value = (uint16_t) ((value << 8) + data [i]);
35 }
36
37 return value;
38}
39
40static inline uint32_t rtems_uint32_from_little_endian( const uint8_t *data)
41{
42 uint32_t value = 0;
43 int i;
44
45 for (i = 3; i >= 0; --i) {
46 value = (value << 8) + data [i];
47 }
48
49 return value;
50}
51
52static inline uint64_t rtems_uint64_from_little_endian( const uint8_t *data)
53{
54 uint64_t value = 0;
55 int i;
56
57 for (i = 7; i >= 0; --i) {
58 value = (value << 8) + (uint64_t) data [i];
59 }
60
61 return value;
62}
63
64static inline uint16_t rtems_uint16_from_big_endian( const uint8_t *data)
65{
66 uint16_t value = 0;
67 int i;
68
69 for (i = 0; i < 2; ++i) {
70 value = (uint16_t) ((value << 8) + data [i]);
71 }
72
73 return value;
74}
75
76static inline uint32_t rtems_uint32_from_big_endian( const uint8_t *data)
77{
78 uint32_t value = 0;
79 int i;
80
81 for (i = 0; i < 4; ++i) {
82 value = (value << 8) + (uint32_t) data [i];
83 }
84
85 return value;
86}
87
88static inline uint64_t rtems_uint64_from_big_endian( const uint8_t *data)
89{
90 uint64_t value = 0;
91 int i;
92
93 for (i = 0; i < 8; ++i) {
94 value = (value << 8) + (uint64_t) data [i];
95 }
96
97 return value;
98}
99
100static inline void rtems_uint16_to_little_endian( uint16_t value, uint8_t *data)
101{
102 int i;
103
104 for (i = 0; i < 2; ++i) {
105 data [i] = (uint8_t) value;
106 value >>= 8;
107 }
108}
109
110static inline void rtems_uint32_to_little_endian( uint32_t value, uint8_t *data)
111{
112 int i;
113
114 for (i = 0; i < 4; ++i) {
115 data [i] = (uint8_t) value;
116 value >>= 8;
117 }
118}
119
120static inline void rtems_uint64_to_little_endian( uint64_t value, uint8_t *data)
121{
122 int i;
123
124 for (i = 0; i < 8; ++i) {
125 data [i] = (uint8_t) value;
126 value >>= 8;
127 }
128}
129
130static inline void rtems_uint16_to_big_endian( uint16_t value, uint8_t *data)
131{
132 int i;
133
134 for (i = 1; i >= 0; --i) {
135 data [i] = (uint8_t) value;
136 value >>= 8;
137 }
138}
139
140static inline void rtems_uint32_to_big_endian( uint32_t value, uint8_t *data)
141{
142 int i;
143
144 for (i = 3; i >= 0; --i) {
145 data [i] = (uint8_t) value;
146 value >>= 8;
147 }
148}
149
150static inline void rtems_uint64_to_big_endian( uint64_t value, uint8_t *data)
151{
152 int i;
153
154 for (i = 7; i >= 0; --i) {
155 data [i] = (uint8_t) value;
156 value >>= 8;
157 }
158}
159
160#endif /* _RTEMS_ENDIAN_H */