40 #include "utils/wrap_stdint.h" 45 inline uint16_t Swap16(uint16_t v)
48 (((v & 0x00ffU) << 8) & 0xff00) |
49 (((v & 0xff00U) >> 8) & 0x00ff);
52 inline uint32_t Swap32(uint32_t v)
55 ((v & static_cast<uint32_t>(0x000000ffUL)) << 24) |
56 ((v &
static_cast<uint32_t
>(0x0000ff00UL)) << 8) |
57 ((v & static_cast<uint32_t>(0x00ff0000UL)) >> 8) |
58 ((v &
static_cast<uint32_t
>(0xff000000UL)) >> 24);
61 inline uint64_t Swap64(uint64_t v)
64 ((v & static_cast<uint64_t>(0x00000000000000ffULL)) << 56) |
65 ((v &
static_cast<uint64_t
>(0x000000000000ff00ULL)) << 40) |
66 ((v & static_cast<uint64_t>(0x0000000000ff0000ULL)) << 24) |
67 ((v &
static_cast<uint64_t
>(0x00000000ff000000ULL)) << 8) |
68 ((v & static_cast<uint64_t>(0x000000ff00000000ULL)) >> 8) |
69 ((v &
static_cast<uint64_t
>(0x0000ff0000000000ULL)) >> 24) |
70 ((v & static_cast<uint64_t>(0x00ff000000000000ULL)) >> 40) |
71 ((v &
static_cast<uint64_t
>(0xff00000000000000ULL)) >> 56);
74 inline uint32_t Reverse32(uint32_t v)
76 v = ((v & 0x55555555) << 1) | ((v & 0xaaaaaaaa) >> 1);
77 v = ((v & 0x33333333) << 2) | ((v & 0xcccccccc) >> 2);
78 v = ((v & 0x0f0f0f0f) << 4) | ((v & 0xf0f0f0f0) >> 4);
79 v = ((v & 0x00ff00ff) << 8) | ((v & 0xff00ff00) >> 8);
80 v = ((v & 0x0000ffff) << 16) | ((v & 0xffff0000) >> 16);
85 #define BitVectorBytes(x) \ 86 (((x) + (CHAR_BIT-1)) / CHAR_BIT) 90 #if BYTE_ORDER_BIG_ENDIAN 92 inline uint16_t HostSwap16(uint16_t v) {
return v; }
93 inline uint32_t HostSwap32(uint32_t v) {
return v; }
94 inline uint64_t HostSwap64(uint64_t v) {
return v; }
98 inline uint16_t HostSwap16(uint16_t v) {
return Swap16(v); }
99 inline uint32_t HostSwap32(uint32_t v) {
return Swap32(v); }
100 inline uint64_t HostSwap64(uint64_t v) {
return Swap64(v); }
102 #endif // LITTLE_ENDIAN 114 return static_cast<uint16_t
>((aBuffer[0] << 8) | aBuffer[1]);
127 return ((static_cast<uint32_t>(aBuffer[0]) << 24) |
128 (static_cast<uint32_t>(aBuffer[1]) << 16) |
129 (static_cast<uint32_t>(aBuffer[2]) << 8) |
130 (static_cast<uint32_t>(aBuffer[3]) << 0));
142 aBuffer[0] = (aValue >> 8) & 0xff;
143 aBuffer[1] = (aValue >> 0) & 0xff;
155 aBuffer[0] = (aValue >> 24) & 0xff;
156 aBuffer[1] = (aValue >> 16) & 0xff;
157 aBuffer[2] = (aValue >> 8) & 0xff;
158 aBuffer[3] = (aValue >> 0) & 0xff;
163 namespace LittleEndian {
165 #if BYTE_ORDER_BIG_ENDIAN 167 inline uint16_t HostSwap16(uint16_t v) {
return Swap16(v); }
168 inline uint32_t HostSwap32(uint32_t v) {
return Swap32(v); }
169 inline uint64_t HostSwap64(uint64_t v) {
return Swap64(v); }
173 inline uint16_t HostSwap16(uint16_t v) {
return v; }
174 inline uint32_t HostSwap32(uint32_t v) {
return v; }
175 inline uint64_t HostSwap64(uint64_t v) {
return v; }
187 inline uint16_t ReadUint16(
const uint8_t *aBuffer)
189 return static_cast<uint16_t
>(aBuffer[0] | (aBuffer[1] << 8));
200 inline uint32_t ReadUint32(
const uint8_t *aBuffer)
202 return ((static_cast<uint32_t>(aBuffer[0]) << 0) |
203 (static_cast<uint32_t>(aBuffer[1]) << 8) |
204 (static_cast<uint32_t>(aBuffer[2]) << 16) |
205 (static_cast<uint32_t>(aBuffer[3]) << 24));
215 inline void WriteUint16(uint16_t aValue, uint8_t *aBuffer)
217 aBuffer[0] = (aValue >> 0) & 0xff;
218 aBuffer[1] = (aValue >> 8) & 0xff;
228 inline void WriteUint32(uint32_t aValue, uint8_t *aBuffer)
230 aBuffer[0] = (aValue >> 0) & 0xff;
231 aBuffer[1] = (aValue >> 8) & 0xff;
232 aBuffer[2] = (aValue >> 16) & 0xff;
233 aBuffer[3] = (aValue >> 24) & 0xff;
240 #endif // ENCODING_HPP_ void WriteUint32(uint32_t aValue, uint8_t *aBuffer)
This function writes a uint32_t value to a given buffer using big-ending encoding.
Definition: encoding.hpp:153
uint16_t ReadUint16(const uint8_t *aBuffer)
This function reads a uint16_t value from a given buffer assuming big-ending encoding.
Definition: encoding.hpp:112
void WriteUint16(uint16_t aValue, uint8_t *aBuffer)
This function writes a uint16_t value to a given buffer using big-ending encoding.
Definition: encoding.hpp:140
uint32_t ReadUint32(const uint8_t *aBuffer)
This function reads a uint32_t value from a given buffer assuming big-ending encoding.
Definition: encoding.hpp:125
This file includes compile-time configuration constants for OpenThread.