Skip to content

Commit

Permalink
Update to 1.1.3
Browse files Browse the repository at this point in the history
Update to 1.1.3
  • Loading branch information
Wangxing Shi committed Sep 28, 2021
1 parent a3d57ad commit 89cc185
Show file tree
Hide file tree
Showing 80 changed files with 9,932 additions and 4,741 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ RPC框架也一样。
| CentOS 7 | GCC/G++ 4.8.5 |
| CentOS 8 | GCC/G++ 8 |
| Ubuntu 20 | GCC/G++ 9 |
| MacOS 11.5 | XCode/Apple clang 12 |

1. 第三方库依赖

Expand Down
142 changes: 142 additions & 0 deletions base/Endian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,145 @@ void Endian::exchange8(void * dest, void * src)
d[7] = s[0];
}

#ifdef __APPLE__
static bool isLittleEndian()
{
const int32_t i = 0x12345678;
const char *c = (const char *)&i;

return (c[0] == 0x78);
}

static uint16_t exchange2(uint16_t value)
{
unsigned char *c = (unsigned char *)&value;

c[0] ^= c[1];
c[1] ^= c[0];
c[0] ^= c[1];

return value;
}

static uint32_t exchange4(uint32_t value)
{
unsigned char *c = (unsigned char *)&value;
unsigned char a = c[0], b = c[1];

c[0] = c[3];
c[1] = c[2];
c[2] = b;
c[3] = a;

return value;
}

static uint64_t exchange8(uint64_t value)
{
unsigned char *c = (unsigned char *)&value;
unsigned char a = c[0], b = c[1], x = c[2], y = c[3];

c[0] = c[7];
c[1] = c[6];
c[2] = c[5];
c[3] = c[4];
c[4] = y;
c[5] = x;
c[6] = b;
c[7] = a;

return value;
}

static bool gc_hostIsLittleEndian = isLittleEndian();

uint16_t Endian::htobe16(uint16_t host_16bits)
{
if (gc_hostIsLittleEndian)
return ::exchange2(host_16bits);
else
return host_16bits;
}
uint16_t Endian::htole16(uint16_t host_16bits)
{
if (gc_hostIsLittleEndian)
return host_16bits;
else
return ::exchange2(host_16bits);
}
uint16_t Endian::be16toh(uint16_t big_endian_16bits)
{
if (gc_hostIsLittleEndian)
return ::exchange2(big_endian_16bits);
else
return big_endian_16bits;
}
uint16_t Endian::le16toh(uint16_t little_endian_16bits)
{
if (gc_hostIsLittleEndian)
return little_endian_16bits;
else
return ::exchange2(little_endian_16bits);
}


uint32_t Endian::htobe32(uint32_t host_32bits)
{
if (gc_hostIsLittleEndian)
return ::exchange4(host_32bits);
else
return host_32bits;
}
uint32_t Endian::htole32(uint32_t host_32bits)
{
if (gc_hostIsLittleEndian)
return host_32bits;
else
return ::exchange4(host_32bits);
}
uint32_t Endian::be32toh(uint32_t big_endian_32bits)
{
if (gc_hostIsLittleEndian)
return ::exchange4(big_endian_32bits);
else
return big_endian_32bits;
}
uint32_t Endian::le32toh(uint32_t little_endian_32bits)
{
if (gc_hostIsLittleEndian)
return little_endian_32bits;
else
return ::exchange4(little_endian_32bits);
}


uint64_t Endian::htobe64(uint64_t host_64bits)
{
if (gc_hostIsLittleEndian)
return ::exchange8(host_64bits);
else
return host_64bits;
}
uint64_t Endian::htole64(uint64_t host_64bits)
{
if (gc_hostIsLittleEndian)
return host_64bits;
else
return ::exchange8(host_64bits);
}
uint64_t Endian::be64toh(uint64_t big_endian_64bits)
{
if (gc_hostIsLittleEndian)
return ::exchange8(big_endian_64bits);
else
return big_endian_64bits;
}
uint64_t Endian::le64toh(uint64_t little_endian_64bits)
{
if (gc_hostIsLittleEndian)
return little_endian_64bits;
else
return ::exchange8(little_endian_64bits);
}
#endif

35 changes: 35 additions & 0 deletions base/Endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,42 @@ class Endian

static void exchange4(void * dest, void * src);
static void exchange8(void * dest, void * src);

#ifdef __APPLE__
public:
static uint16_t htobe16(uint16_t host_16bits);
static uint16_t htole16(uint16_t host_16bits);
static uint16_t be16toh(uint16_t big_endian_16bits);
static uint16_t le16toh(uint16_t little_endian_16bits);

static uint32_t htobe32(uint32_t host_32bits);
static uint32_t htole32(uint32_t host_32bits);
static uint32_t be32toh(uint32_t big_endian_32bits);
static uint32_t le32toh(uint32_t little_endian_32bits);

static uint64_t htobe64(uint64_t host_64bits);
static uint64_t htole64(uint64_t host_64bits);
static uint64_t be64toh(uint64_t big_endian_64bits);
static uint64_t le64toh(uint64_t little_endian_64bits);
#endif
};
}

#ifdef __APPLE__
inline uint16_t htobe16(uint16_t host_16bits) { return fpnn::Endian::htobe16(host_16bits); }
inline uint16_t htole16(uint16_t host_16bits) { return fpnn::Endian::htole16(host_16bits); }
inline uint16_t be16toh(uint16_t big_endian_16bits) { return fpnn::Endian::be16toh(big_endian_16bits); }
inline uint16_t le16toh(uint16_t little_endian_16bits) { return fpnn::Endian::le16toh(little_endian_16bits); }

inline uint32_t htobe32(uint32_t host_32bits) { return fpnn::Endian::htobe32(host_32bits); }
inline uint32_t htole32(uint32_t host_32bits) { return fpnn::Endian::htole32(host_32bits); }
inline uint32_t be32toh(uint32_t big_endian_32bits) { return fpnn::Endian::be32toh(big_endian_32bits); }
inline uint32_t le32toh(uint32_t little_endian_32bits) { return fpnn::Endian::le32toh(little_endian_32bits); }

inline uint64_t htobe64(uint64_t host_64bits) { return fpnn::Endian::htobe64(host_64bits); }
inline uint64_t htole64(uint64_t host_64bits) { return fpnn::Endian::htole64(host_64bits); }
inline uint64_t be64toh(uint64_t big_endian_64bits) { return fpnn::Endian::be64toh(big_endian_64bits); }
inline uint64_t le64toh(uint64_t little_endian_64bits) { return fpnn::Endian::le64toh(little_endian_64bits); }
#endif

#endif
Loading

0 comments on commit 89cc185

Please sign in to comment.