Zero  0.1.0
w_endian.h
Go to the documentation of this file.
1 #ifndef __W_ENDIAN_H
2 #define __W_ENDIAN_H
3 
4 #include "shore-config.h"
5 #include <cstdint>
6 
9 // tells the endianness. Rather than using this function,
10 // consider #ifdef on WORDS_BIGENDIAN/WORDS_LITTLEENDIAN.
11 // but, sometimes this function is useful.
12 #ifdef WORDS_BIGENDIAN
13 inline bool is_big_endian() { return true; }
14 inline bool is_little_endian() { return false; }
15 // ok
16 #else // WORDS_BIGENDIAN
17 #ifdef WORDS_LITTLEENDIAN
18 
19 inline bool is_big_endian() {
20  return false;
21 }
22 
23 inline bool is_little_endian() {
24  return true;
25 }
26 
27 #else // WORDS_LITTLEENDIAN
28 // sanity check on endian definition
29 #error "Huh?! Neither BigEndian nor LittleEndian? config.h seems not included in the path."
30 #endif // WORDS_LITTLEENDIAN
31 #endif // WORDS_BIGENDIAN
32 
33 // in the following function names,
34 // ho: the default endianness of the host machine. useful to restore the original value.
35 // be: means the output is ALWAYS big-endian. useful to make sure memcmp works in all architecture.
37 // note that unsigned types are assumed to make sure memcmp works after serialize_be().
38 // if your data type is signed, convert it to unsigned type first.
39 
41 inline void serialize16_be(void* dest, uint16_t value) {
42 #ifdef WORDS_BIGENDIAN
43  *reinterpret_cast<uint16_t*>(dest) = value;
44 #else // WORDS_BIGENDIAN
45  unsigned char* b = reinterpret_cast<unsigned char*> (dest);
46  b[0] = value >> 8;
47  b[1] = value & 0xFF;
48 #endif // WORDS_BIGENDIAN
49 }
50 
52 inline void serialize32_be(void* dest, uint32_t value) {
53 #ifdef WORDS_BIGENDIAN
54  *reinterpret_cast<uint32_t*>(dest) = value;
55 #else // WORDS_BIGENDIAN
56  unsigned char* b = reinterpret_cast<unsigned char*> (dest);
57  b[0] = value >> 24;
58  b[1] = (value & 0xFF0000) >> 16;
59  b[2] = (value & 0xFF00) >> 8;
60  b[3] = value & 0xFF;
61 #endif // WORDS_BIGENDIAN
62 }
63 
65 inline void serialize64_be(void* dest, uint64_t value) {
66 #ifdef WORDS_BIGENDIAN
67  *reinterpret_cast<uint64_t*>(dest) = value;
68 #else // WORDS_BIGENDIAN
69  unsigned char* b = reinterpret_cast<unsigned char*> (dest);
70  b[0] = value >> 56;
71  b[1] = (value & 0xFF000000000000) >> 48;
72  b[2] = (value & 0xFF0000000000) >> 40;
73  b[3] = (value & 0xFF00000000) >> 32;
74  b[4] = (value & 0xFF000000) >> 24;
75  b[5] = (value & 0xFF0000) >> 16;
76  b[6] = (value & 0xFF00) >> 8;
77  b[7] = value & 0xFF;
78 #endif // WORDS_BIGENDIAN
79 }
80 
82 inline uint16_t deserialize16_ho(const void* buf) {
83 #ifdef WORDS_BIGENDIAN
84  return *reinterpret_cast<const uint16_t*>(buf);
85 #else // WORDS_BIGENDIAN
86  const unsigned char* b = reinterpret_cast<const unsigned char*> (buf);
87  return (b[0] << 8) | b[1];
88 #endif // WORDS_BIGENDIAN
89 }
90 
92 inline uint32_t deserialize32_ho(const void* buf) {
93 #ifdef WORDS_BIGENDIAN
94  return *reinterpret_cast<const uint32_t*>(buf);
95 #else // WORDS_BIGENDIAN
96  const unsigned char* b = reinterpret_cast<const unsigned char*> (buf);
97  return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3];
98 #endif // WORDS_BIGENDIAN
99 }
100 
102 inline uint64_t deserialize64_ho(const void* buf) {
103 #ifdef WORDS_BIGENDIAN
104  return *reinterpret_cast<const uint64_t*>(buf);
105 #else // WORDS_BIGENDIAN
106  const unsigned char* b = reinterpret_cast<const unsigned char*> (buf);
107  return ((uint64_t)b[0] << 56) | ((uint64_t)b[1] << 48)
108  | ((uint64_t)b[2] << 40) | ((uint64_t)b[3] << 32)
109  | ((uint64_t)b[4] << 24) | ((uint64_t)b[5] << 16)
110  | ((uint64_t)b[6] << 8) | (uint64_t)b[7];
111 #endif // WORDS_BIGENDIAN
112 }
113 
114 // overloading for easier use. But, be VERY careful when using
115 // the following methods. The above methods are recommended to avoid
116 // unexpected type cast.
117 inline void serialize_be(void* dest, uint16_t value) {
118  serialize16_be(dest, value);
119 }
120 
121 inline void serialize_be(void* dest, uint32_t value) {
122  serialize32_be(dest, value);
123 }
124 
125 inline void serialize_be(void* dest, uint64_t value) {
126  serialize64_be(dest, value);
127 }
128 
129 inline void deserialize_ho(uint16_t& value) {
130  value = deserialize16_ho(&value);
131 }
132 
133 inline void deserialize_ho(uint32_t& value) {
134  value = deserialize32_ho(&value);
135 }
136 
137 inline void deserialize_ho(uint64_t& value) {
138  value = deserialize64_ho(&value);
139 }
140 
141 #endif // __W_ENDIAN_H
void deserialize_ho(uint16_t &value)
Definition: w_endian.h:129
void serialize_be(void *dest, uint16_t value)
Definition: w_endian.h:117
uint16_t deserialize16_ho(const void *buf)
Definition: w_endian.h:82
void serialize16_be(void *dest, uint16_t value)
Definition: w_endian.h:41
void serialize32_be(void *dest, uint32_t value)
Definition: w_endian.h:52
void serialize64_be(void *dest, uint64_t value)
Definition: w_endian.h:65
uint64_t deserialize64_ho(const void *buf)
Definition: w_endian.h:102
uint32_t deserialize32_ho(const void *buf)
Definition: w_endian.h:92