added byteorder change functions and macros

This commit is contained in:
hyunghwan.chung 2018-01-10 14:39:31 +00:00
parent 4666b5efff
commit b31049f808
2 changed files with 178 additions and 0 deletions

View File

@ -89,6 +89,33 @@
} while(0); } while(0);
*/ */
#define MOO_CONST_SWAP16(x) \
((qse_uint16_t)((((qse_uint16_t)(x) & (qse_uint16_t)0x00ffU) << 8) | \
(((qse_uint16_t)(x) & (qse_uint16_t)0xff00U) >> 8) ))
#define MOO_CONST_SWAP32(x) \
((qse_uint32_t)((((qse_uint32_t)(x) & (qse_uint32_t)0x000000ffUL) << 24) | \
(((qse_uint32_t)(x) & (qse_uint32_t)0x0000ff00UL) << 8) | \
(((qse_uint32_t)(x) & (qse_uint32_t)0x00ff0000UL) >> 8) | \
(((qse_uint32_t)(x) & (qse_uint32_t)0xff000000UL) >> 24) ))
#if defined(MOO_ENDIAN_LITTLE)
# define MOO_CONST_NTOH16(x) MOO_CONST_SWAP16(x)
# define MOO_CONST_HTON16(x) MOO_CONST_SWAP16(x)
# define MOO_CONST_NTOH32(x) MOO_CONST_SWAP32(x)
# define MOO_CONST_HTON32(x) MOO_CONST_SWAP32(x)
#elif defined(MOO_ENDIAN_BIG)
# define MOO_CONST_NTOH16(x) (x)
# define MOO_CONST_HTON16(x) (x)
# define MOO_CONST_NTOH32(x) (x)
# define MOO_CONST_HTON32(x) (x)
#else
# error UNKNOWN ENDIAN
#endif
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif
@ -421,6 +448,50 @@ MOO_EXPORT int moo_ucwidth (
moo_uch_t uc moo_uch_t uc
); );
/* ------------------------------------------------------------------------- */
#if defined(MOO_HAVE_UINT16_T)
MOO_EXPORT moo_uint16_t moo_ntoh16 (
moo_uint16_t x
);
MOO_EXPORT moo_uint16_t moo_hton16 (
moo_uint16_t x
);
#endif
#if defined(MOO_HAVE_UINT32_T)
MOO_EXPORT moo_uint32_t moo_ntoh32 (
moo_uint32_t x
);
MOO_EXPORT moo_uint32_t moo_hton32 (
moo_uint32_t x
);
#endif
#if defined(MOO_HAVE_UINT64_T)
MOO_EXPORT moo_uint64_t moo_ntoh64 (
moo_uint64_t x
);
MOO_EXPORT moo_uint64_t moo_hton64 (
moo_uint64_t x
);
#endif
#if defined(MOO_HAVE_UINT128_T)
MOO_EXPORT moo_uint128_t moo_ntoh128 (
moo_uint128_t x
);
MOO_EXPORT moo_uint128_t moo_hton128 (
moo_uint128_t x
);
#endif
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@ -1002,3 +1002,110 @@ moo_uint32_t moo_hton32 (moo_uint32_t x)
#endif #endif
} }
#endif #endif
#if defined(MOO_HAVE_UINT64_T)
moo_uint64_t moo_ntoh64 (moo_uint64_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint64_t)(
((moo_uint64_t)c[0] << 56) |
((moo_uint64_t)c[1] << 48) |
((moo_uint64_t)c[2] << 40) |
((moo_uint64_t)c[3] << 32) |
((moo_uint64_t)c[4] << 24) |
((moo_uint64_t)c[5] << 16) |
((moo_uint64_t)c[6] << 8) |
((moo_uint64_t)c[7] << 0));
#else
# error Unknown endian
#endif
}
moo_uint64_t moo_hton64 (moo_uint64_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint64_t)(
((moo_uint64_t)c[0] << 56) |
((moo_uint64_t)c[1] << 48) |
((moo_uint64_t)c[2] << 40) |
((moo_uint64_t)c[3] << 32) |
((moo_uint64_t)c[4] << 24) |
((moo_uint64_t)c[5] << 16) |
((moo_uint64_t)c[6] << 8) |
((moo_uint64_t)c[7] << 0));
#else
# error Unknown endian
#endif
}
#endif
/* --------------------------------------------------------------- */
#if defined(MOO_HAVE_UINT128_T)
moo_uint128_t moo_ntoh128 (moo_uint128_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint128_t)(
((moo_uint128_t)c[0] << 120) |
((moo_uint128_t)c[1] << 112) |
((moo_uint128_t)c[2] << 104) |
((moo_uint128_t)c[3] << 96) |
((moo_uint128_t)c[4] << 88) |
((moo_uint128_t)c[5] << 80) |
((moo_uint128_t)c[6] << 72) |
((moo_uint128_t)c[7] << 64) |
((moo_uint128_t)c[8] << 56) |
((moo_uint128_t)c[9] << 48) |
((moo_uint128_t)c[10] << 40) |
((moo_uint128_t)c[11] << 32) |
((moo_uint128_t)c[12] << 24) |
((moo_uint128_t)c[13] << 16) |
((moo_uint128_t)c[14] << 8) |
((moo_uint128_t)c[15] << 0));
#else
# error Unknown endian
#endif
}
moo_uint128_t moo_hton128 (moo_uint128_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint128_t)(
((moo_uint128_t)c[0] << 120) |
((moo_uint128_t)c[1] << 112) |
((moo_uint128_t)c[2] << 104) |
((moo_uint128_t)c[3] << 96) |
((moo_uint128_t)c[4] << 88) |
((moo_uint128_t)c[5] << 80) |
((moo_uint128_t)c[6] << 72) |
((moo_uint128_t)c[7] << 64) |
((moo_uint128_t)c[8] << 56) |
((moo_uint128_t)c[9] << 48) |
((moo_uint128_t)c[10] << 40) |
((moo_uint128_t)c[11] << 32) |
((moo_uint128_t)c[12] << 24) |
((moo_uint128_t)c[13] << 16) |
((moo_uint128_t)c[14] << 8) |
((moo_uint128_t)c[15] << 0));
#else
# error Unknown endian
#endif
}
#endif