github.com/platonnetwork/platon-go@v0.7.6/cases/tool/win/bls_win/include/cybozu/endian.hpp (about) 1 #pragma once 2 3 /** 4 @file 5 @brief deal with big and little endian 6 7 @author MITSUNARI Shigeo(@herumi) 8 */ 9 #include <cybozu/inttype.hpp> 10 #include <string.h> 11 #include <stdlib.h> 12 #include <stdio.h> 13 14 namespace cybozu { 15 16 #ifdef _MSC_VER 17 inline uint16_t byteSwap(uint16_t x) { return _byteswap_ushort(x); } 18 inline uint32_t byteSwap(uint32_t x) { return _byteswap_ulong(x); } 19 inline uint64_t byteSwap(uint64_t x) { return _byteswap_uint64(x); } 20 #else 21 #if (((__GNUC__) << 16) + (__GNUC_MINOR__)) >= ((4 << 16) + 8) 22 inline uint16_t byteSwap(uint16_t x) { return __builtin_bswap16(x); } 23 #else 24 inline uint16_t byteSwap(uint16_t x) { return (x >> 8) | (x << 8); } 25 #endif 26 inline uint32_t byteSwap(uint32_t x) { return __builtin_bswap32(x); } 27 inline uint64_t byteSwap(uint64_t x) { return __builtin_bswap64(x); } 28 #endif 29 30 /** 31 get 16bit integer as little endian 32 @param src [in] pointer 33 */ 34 inline uint16_t Get16bitAsLE(const void *src) 35 { 36 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 37 uint16_t x; 38 memcpy(&x, src, sizeof(x)); 39 return x; 40 #else 41 const uint8_t *p = static_cast<const uint8_t *>(src); 42 return p[0] | (p[1] << 8); 43 #endif 44 } 45 46 /** 47 get 32bit integer as little endian 48 @param src [in] pointer 49 */ 50 inline uint32_t Get32bitAsLE(const void *src) 51 { 52 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 53 uint32_t x; 54 memcpy(&x, src, sizeof(x)); 55 return x; 56 #else 57 const uint8_t *p = static_cast<const uint8_t *>(src); 58 return Get16bitAsLE(p) | (static_cast<uint32_t>(Get16bitAsLE(p + 2)) << 16); 59 #endif 60 } 61 62 /** 63 get 64bit integer as little endian 64 @param src [in] pointer 65 */ 66 inline uint64_t Get64bitAsLE(const void *src) 67 { 68 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 69 uint64_t x; 70 memcpy(&x, src, sizeof(x)); 71 return x; 72 #else 73 const uint8_t *p = static_cast<const uint8_t *>(src); 74 return Get32bitAsLE(p) | (static_cast<uint64_t>(Get32bitAsLE(p + 4)) << 32); 75 #endif 76 } 77 78 /** 79 get 16bit integer as bit endian 80 @param src [in] pointer 81 */ 82 inline uint16_t Get16bitAsBE(const void *src) 83 { 84 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 85 uint16_t x; 86 memcpy(&x, src, sizeof(x)); 87 return byteSwap(x); 88 #else 89 const uint8_t *p = static_cast<const uint8_t *>(src); 90 return p[1] | (p[0] << 8); 91 #endif 92 } 93 94 /** 95 get 32bit integer as bit endian 96 @param src [in] pointer 97 */ 98 inline uint32_t Get32bitAsBE(const void *src) 99 { 100 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 101 uint32_t x; 102 memcpy(&x, src, sizeof(x)); 103 return byteSwap(x); 104 #else 105 const uint8_t *p = static_cast<const uint8_t *>(src); 106 return Get16bitAsBE(p + 2) | (static_cast<uint32_t>(Get16bitAsBE(p)) << 16); 107 #endif 108 } 109 110 /** 111 get 64bit integer as big endian 112 @param src [in] pointer 113 */ 114 inline uint64_t Get64bitAsBE(const void *src) 115 { 116 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 117 uint64_t x; 118 memcpy(&x, src, sizeof(x)); 119 return byteSwap(x); 120 #else 121 const uint8_t *p = static_cast<const uint8_t *>(src); 122 return Get32bitAsBE(p + 4) | (static_cast<uint64_t>(Get32bitAsBE(p)) << 32); 123 #endif 124 } 125 126 /** 127 set 16bit integer as little endian 128 @param src [out] pointer 129 @param x [in] integer 130 */ 131 inline void Set16bitAsLE(void *src, uint16_t x) 132 { 133 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 134 memcpy(src, &x, sizeof(x)); 135 #else 136 uint8_t *p = static_cast<uint8_t *>(src); 137 p[0] = static_cast<uint8_t>(x); 138 p[1] = static_cast<uint8_t>(x >> 8); 139 #endif 140 } 141 /** 142 set 32bit integer as little endian 143 @param src [out] pointer 144 @param x [in] integer 145 */ 146 inline void Set32bitAsLE(void *src, uint32_t x) 147 { 148 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 149 memcpy(src, &x, sizeof(x)); 150 #else 151 uint8_t *p = static_cast<uint8_t *>(src); 152 p[0] = static_cast<uint8_t>(x); 153 p[1] = static_cast<uint8_t>(x >> 8); 154 p[2] = static_cast<uint8_t>(x >> 16); 155 p[3] = static_cast<uint8_t>(x >> 24); 156 #endif 157 } 158 /** 159 set 64bit integer as little endian 160 @param src [out] pointer 161 @param x [in] integer 162 */ 163 inline void Set64bitAsLE(void *src, uint64_t x) 164 { 165 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 166 memcpy(src, &x, sizeof(x)); 167 #else 168 uint8_t *p = static_cast<uint8_t *>(src); 169 Set32bitAsLE(p, static_cast<uint32_t>(x)); 170 Set32bitAsLE(p + 4, static_cast<uint32_t>(x >> 32)); 171 #endif 172 } 173 /** 174 set 16bit integer as big endian 175 @param src [out] pointer 176 @param x [in] integer 177 */ 178 inline void Set16bitAsBE(void *src, uint16_t x) 179 { 180 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 181 x = byteSwap(x); 182 memcpy(src, &x, sizeof(x)); 183 #else 184 uint8_t *p = static_cast<uint8_t *>(src); 185 p[0] = static_cast<uint8_t>(x >> 8); 186 p[1] = static_cast<uint8_t>(x); 187 #endif 188 } 189 /** 190 set 32bit integer as big endian 191 @param src [out] pointer 192 @param x [in] integer 193 */ 194 inline void Set32bitAsBE(void *src, uint32_t x) 195 { 196 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 197 x = byteSwap(x); 198 memcpy(src, &x, sizeof(x)); 199 #else 200 uint8_t *p = static_cast<uint8_t *>(src); 201 p[0] = static_cast<uint8_t>(x >> 24); 202 p[1] = static_cast<uint8_t>(x >> 16); 203 p[2] = static_cast<uint8_t>(x >> 8); 204 p[3] = static_cast<uint8_t>(x); 205 #endif 206 } 207 /** 208 set 64bit integer as big endian 209 @param src [out] pointer 210 @param x [in] integer 211 */ 212 inline void Set64bitAsBE(void *src, uint64_t x) 213 { 214 #if CYBOZU_ENDIAN == CYBOZU_ENDIAN_LITTLE 215 x = byteSwap(x); 216 memcpy(src, &x, sizeof(x)); 217 #else 218 uint8_t *p = static_cast<uint8_t *>(src); 219 Set32bitAsBE(p, static_cast<uint32_t>(x >> 32)); 220 Set32bitAsBE(p + 4, static_cast<uint32_t>(x)); 221 #endif 222 } 223 224 } // cybozu