fixed flaws in constant endian conversion macros

This commit is contained in:
hyunghwan.chung 2019-05-02 08:01:58 +00:00
parent b48b839a3d
commit 582c8203ff
5 changed files with 68 additions and 36 deletions

View File

@ -620,8 +620,8 @@ static int fmtoutv (moo_t* moo, const fmtchar_t* fmt, moo_fmtout_data_t* data, v
/* zeropad must not take effect for 'k' and 'K'
*
* 'h' & 'l' is not used to differentiate qse_mchar_t and qse_wchar_t
* because 'k' means qse_byte_t.
* 'h' & 'l' is not used to differentiate moo_bch_t and moo_uch_t
* because 'k' means moo_byte_t.
* 'l', results in uppercase hexadecimal letters.
* 'h' drops the leading \x in the output
* --------------------------------------------------------

View File

@ -88,48 +88,48 @@
*/
/* =========================================================================
* ENDIAN CHANGE
* ENDIAN CHANGE OF A CONSTANT
* ========================================================================= */
#define MOO_CONST_BSWAP16(x) \
((qse_uint16_t)((((qse_uint16_t)(x) & (qse_uint16_t)0x00ff) << 8) | \
(((qse_uint16_t)(x) & (qse_uint16_t)0xff00) >> 8) ))
((moo_uint16_t)((((moo_uint16_t)(x) & ((moo_uint16_t)0xff << 0)) << 8) | \
(((moo_uint16_t)(x) & ((moo_uint16_t)0xff << 8)) >> 8)))
#define MOO_CONST_BSWAP32(x) \
((qse_uint32_t)((((qse_uint32_t)(x) & (qse_uint32_t)0x000000ff) << 24) | \
(((qse_uint32_t)(x) & (qse_uint32_t)0x0000ff00) << 8) | \
(((qse_uint32_t)(x) & (qse_uint32_t)0x00ff0000) >> 8) | \
(((qse_uint32_t)(x) & (qse_uint32_t)0xff000000) >> 24) ))
((moo_uint32_t)((((moo_uint32_t)(x) & ((moo_uint32_t)0xff << 0)) << 24) | \
(((moo_uint32_t)(x) & ((moo_uint32_t)0xff << 8)) << 8) | \
(((moo_uint32_t)(x) & ((moo_uint32_t)0xff << 16)) >> 8) | \
(((moo_uint32_t)(x) & ((moo_uint32_t)0xff << 24)) >> 24)))
#if defined(MOO_HAVE_UINT64_T)
#define MOO_CONST_BSWAP64(x) \
((qse_uint64_t)((((qse_uint64_t)(x) & (qse_uint64_t)0x00000000000000ff) << 56) | \
(((qse_uint64_t)(x) & (qse_uint64_t)0x000000000000ff00) << 40) | \
(((qse_uint64_t)(x) & (qse_uint64_t)0x0000000000ff0000) << 24) | \
(((qse_uint64_t)(x) & (qse_uint64_t)0x00000000ff000000) << 8) | \
(((qse_uint64_t)(x) & (qse_uint64_t)0x000000ff00000000) >> 8) | \
(((qse_uint64_t)(x) & (qse_uint64_t)0x0000ff0000000000) >> 24) | \
(((qse_uint64_t)(x) & (qse_uint64_t)0x00ff000000000000) >> 40) | \
(((qse_uint64_t)(x) & (qse_uint64_t)0xff00000000000000) >> 56))
((moo_uint64_t)((((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 0)) << 56) | \
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 8)) << 40) | \
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 16)) << 24) | \
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 24)) << 8) | \
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 32)) >> 8) | \
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 40)) >> 24) | \
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 48)) >> 40) | \
(((moo_uint64_t)(x) & ((moo_uint64_t)0xff << 56)) >> 56)))
#endif
#if defined(MOO_HAVE_UINT128_T)
#define MOO_CONST_BSWAP128(x) \
((qse_uint128_t)((((qse_uint128_t)(x) & (qse_uint128_t)0x000000000000000000000000000000ff) << 120) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x0000000000000000000000000000ff00) << 104) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x00000000000000000000000000ff0000) << 88) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x000000000000000000000000ff000000) << 72) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x0000000000000000000000ff00000000) << 56) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x00000000000000000000ff0000000000) << 40) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x000000000000000000ff000000000000) << 24) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x0000000000000000ff00000000000000) << 8) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x00000000000000ff0000000000000000) >> 8) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x000000000000ff000000000000000000) >> 24) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x0000000000ff00000000000000000000) >> 40) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x00000000ff0000000000000000000000) >> 56) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x000000ff000000000000000000000000) >> 72) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x0000ff00000000000000000000000000) >> 88) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0x00ff0000000000000000000000000000) >> 104) | \
(((qse_uint128_t)(x) & (qse_uint128_t)0xff000000000000000000000000000000) >> 120))
((moo_uint128_t)((((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 0)) << 120) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 8)) << 104) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 16)) << 88) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 24)) << 72) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 32)) << 56) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 40)) << 40) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 48)) << 24) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 56)) << 8) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 64)) >> 8) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 72)) >> 24) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 80)) >> 40) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 88)) >> 56) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 96)) >> 72) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 104)) >> 88) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 112)) >> 104) | \
(((moo_uint128_t)(x) & ((moo_uint128_t)0xff << 120)) >> 120)))
#endif
#if defined(MOO_ENDIAN_LITTLE)
@ -738,8 +738,6 @@ MOO_EXPORT moo_oow_t moo_utf16_to_uc (
/* ------------------------------------------------------------------------- */
#if defined(MOO_HAVE_INLINE)
#if defined(MOO_HAVE_UINT16_T)
@ -918,7 +916,7 @@ static MOO_INLINE moo_uint128_t moo_bswap128 (moo_uint128_t x)
# endif
#else
# error unknown endian
# error UNKNOWN ENDIAN
#endif
#if defined(__cplusplus)

View File

@ -5,6 +5,7 @@ AM_CFLAGS = $(PTHREAD_CFLAGS)
AM_CPPFLAGS = \
-I$(abs_builddir) \
-I$(abs_srcdir) \
-I$(abs_srcdir)/../lib \
-I$(includedir)
AM_LDFLAGS = -L$(libdir)

View File

@ -337,6 +337,7 @@ AM_CFLAGS = $(PTHREAD_CFLAGS)
AM_CPPFLAGS = \
-I$(abs_builddir) \
-I$(abs_srcdir) \
-I$(abs_srcdir)/../lib \
-I$(includedir)
AM_LDFLAGS = -L$(libdir)

View File

@ -22,6 +22,14 @@ int main ()
T_ASSERT1 (x.u16 != moo_htole16(x.u16) || x.u16 != moo_htobe16(x.u16), "u16 endian conversion #0");
T_ASSERT1 (x.u16 == moo_le16toh(moo_htole16(x.u16)), "u16 endian conversion #1");
T_ASSERT1 (x.u16 == moo_be16toh(moo_htobe16(x.u16)), "u16 endian conversion #2");
T_ASSERT1 (x.u16 == moo_ntoh16(moo_hton16(x.u16)), "u16 endian conversion #3");
#define X_CONST (0x1122)
T_ASSERT1 (X_CONST != MOO_CONST_HTOLE16(X_CONST) || X_CONST != MOO_CONST_HTOBE16(X_CONST), "u16 constant endian conversion #0");
T_ASSERT1 (X_CONST == MOO_CONST_LE16TOH(MOO_CONST_HTOLE16(X_CONST)), "u16 constant endian conversion #1");
T_ASSERT1 (X_CONST == MOO_CONST_BE16TOH(MOO_CONST_HTOBE16(X_CONST)), "u16 constant endian conversion #2");
T_ASSERT1 (X_CONST == MOO_CONST_NTOH16(MOO_CONST_HTON16(X_CONST)), "u16 constant endian conversion #3");
#undef X_CONST
}
@ -43,6 +51,14 @@ int main ()
T_ASSERT1 (x.u32 != moo_htole32(x.u32) || x.u32 != moo_htobe32(x.u32), "u32 endian conversion #0");
T_ASSERT1 (x.u32 == moo_le32toh(moo_htole32(x.u32)), "u32 endian conversion #1");
T_ASSERT1 (x.u32 == moo_be32toh(moo_htobe32(x.u32)), "u32 endian conversion #2");
T_ASSERT1 (x.u32 == moo_ntoh32(moo_hton32(x.u32)), "u32 endian conversion #3");
#define X_CONST (0x11223344)
T_ASSERT1 (X_CONST != MOO_CONST_HTOLE32(X_CONST) || X_CONST != MOO_CONST_HTOBE32(X_CONST), "u32 constant endian conversion #0");
T_ASSERT1 (X_CONST == MOO_CONST_LE32TOH(MOO_CONST_HTOLE32(X_CONST)), "u32 constant endian conversion #1");
T_ASSERT1 (X_CONST == MOO_CONST_BE32TOH(MOO_CONST_HTOBE32(X_CONST)), "u32 constant endian conversion #2");
T_ASSERT1 (X_CONST == MOO_CONST_NTOH32(MOO_CONST_HTON32(X_CONST)), "u32 constant endian conversion #3");
#undef X_CONST
}
#if defined(MOO_HAVE_UINT64_T)
@ -68,6 +84,14 @@ int main ()
T_ASSERT1 (x.u64 != moo_htole64(x.u64) || x.u64 != moo_htobe64(x.u64), "u64 endian conversion #0");
T_ASSERT1 (x.u64 == moo_le64toh(moo_htole64(x.u64)), "u64 endian conversion #1");
T_ASSERT1 (x.u64 == moo_be64toh(moo_htobe64(x.u64)), "u64 endian conversion #2");
T_ASSERT1 (x.u64 == moo_ntoh64(moo_hton64(x.u64)), "u64 endian conversion #3");
#define X_CONST (((moo_uint64_t)0x11223344 << 32) | (moo_uint64_t)0x55667788)
T_ASSERT1 (X_CONST != MOO_CONST_HTOLE64(X_CONST) || X_CONST != MOO_CONST_HTOBE64(X_CONST), "u64 constant endian conversion #0");
T_ASSERT1 (X_CONST == MOO_CONST_LE64TOH(MOO_CONST_HTOLE64(X_CONST)), "u64 constant endian conversion #1");
T_ASSERT1 (X_CONST == MOO_CONST_BE64TOH(MOO_CONST_HTOBE64(X_CONST)), "u64 constant endian conversion #2");
T_ASSERT1 (X_CONST == MOO_CONST_NTOH64(MOO_CONST_HTON64(X_CONST)), "u64 constant endian conversion #3");
#undef X_CONST
}
#endif
@ -107,6 +131,14 @@ int main ()
T_ASSERT1 (x.u128 != moo_htole128(x.u128) || x.u128 != moo_htobe128(x.u128), "u128 endian conversion #0");
T_ASSERT1 (x.u128 == moo_le128toh(moo_htole128(x.u128)), "u128 endian conversion #1");
T_ASSERT1 (x.u128 == moo_be128toh(moo_htobe128(x.u128)), "u128 endian conversion #2");
T_ASSERT1 (x.u128 == moo_ntoh128(moo_hton128(x.u128)), "u128 endian conversion #3");
#define X_CONST (((moo_uint128_t)0x11223344 << 96) | ((moo_uint128_t)0x55667788 << 64) | ((moo_uint128_t)0x99aabbcc << 32) | ((moo_uint128_t)0xddeefffa))
T_ASSERT1 (X_CONST != MOO_CONST_HTOLE128(X_CONST) || X_CONST != MOO_CONST_HTOBE128(X_CONST), "u128 constant endian conversion #0");
T_ASSERT1 (X_CONST == MOO_CONST_LE128TOH(MOO_CONST_HTOLE128(X_CONST)), "u128 constant endian conversion #1");
T_ASSERT1 (X_CONST == MOO_CONST_BE128TOH(MOO_CONST_HTOBE128(X_CONST)), "u128 constant endian conversion #2");
T_ASSERT1 (X_CONST == MOO_CONST_NTOH128(MOO_CONST_HTON128(X_CONST)), "u128 constant endian conversion #3");
#undef X_CONST
}
#endif