From 6a60c8024bc9baf884e1407c79b486222d935b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sun, 19 Mar 2017 19:00:01 +0100 Subject: [PATCH] Define vector versions of the endianness conversion functions. --- configure.ac | 4 ++ include/cryb/endian.h | 150 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) diff --git a/configure.ac b/configure.ac index d1832c2..b026be9 100644 --- a/configure.ac +++ b/configure.ac @@ -57,9 +57,13 @@ AX_GCC_BUILTIN([__builtin_bswap32]) AX_GCC_BUILTIN([__builtin_bswap64]) AC_CHECK_DECLS([ bswap16, bswap32, bswap64, + bswap16v, bswap32v, bswap64v, be16enc, be16dec, le16enc, le16dec, + be16encv, be16decv, le16encv, le16decv, be32enc, be32dec, le32enc, le32dec, + be32encv, be32decv, le32encv, le32decv, be64enc, be64dec, le64enc, le64dec, + be64encv, be64decv, le64encv, le64decv, htobe16, be16toh, htole16, le16toh, htobe32, be32toh, htole32, le32toh, htobe64, be64toh, htole64, le64toh, diff --git a/include/cryb/endian.h b/include/cryb/endian.h index dc942fa..4eca37e 100644 --- a/include/cryb/endian.h +++ b/include/cryb/endian.h @@ -48,48 +48,93 @@ CRYB_BEGIN #if !HAVE_DECL_BSWAP16 #define bswap16 cryb_bswap16 #endif +#if !HAVE_DECL_BSWAP16V +#define bswap16v cryb_bswap16v +#endif #if !HAVE_DECL_BSWAP32 #define bswap32 cryb_bswap32 #endif +#if !HAVE_DECL_BSWAP32V +#define bswap32v cryb_bswap32v +#endif #if !HAVE_DECL_BSWAP64 #define bswap64 cryb_bswap64 #endif +#if !HAVE_DECL_BSWAP64V +#define bswap64v cryb_bswap64v +#endif #if !HAVE_DECL_BE16ENC #define be16enc cryb_be16enc #endif +#if !HAVE_DECL_BE16ENCV +#define be16encv cryb_be16encv +#endif #if !HAVE_DECL_BE16DEC #define be16dec cryb_be16dec #endif +#if !HAVE_DECL_BE16DECV +#define be16decv cryb_be16decv +#endif #if !HAVE_DECL_BE32ENC #define be32enc cryb_be32enc #endif +#if !HAVE_DECL_BE32ENCV +#define be32encv cryb_be32encv +#endif #if !HAVE_DECL_BE32DEC #define be32dec cryb_be32dec #endif +#if !HAVE_DECL_BE32DECV +#define be32decv cryb_be32decv +#endif #if !HAVE_DECL_BE64ENC #define be64enc cryb_be64enc #endif +#if !HAVE_DECL_BE64ENCV +#define be64encv cryb_be64encv +#endif #if !HAVE_DECL_BE64DEC #define be64dec cryb_be64dec #endif +#if !HAVE_DECL_BE64DECV +#define be64decv cryb_be64decv +#endif #if !HAVE_DECL_LE16ENC #define le16enc cryb_le16enc #endif +#if !HAVE_DECL_LE16ENCV +#define le16encv cryb_le16encv +#endif #if !HAVE_DECL_LE16DEC #define le16dec cryb_le16dec #endif +#if !HAVE_DECL_LE16DECV +#define le16decv cryb_le16decv +#endif #if !HAVE_DECL_LE32ENC #define le32enc cryb_le32enc #endif +#if !HAVE_DECL_LE32ENCV +#define le32encv cryb_le32encv +#endif #if !HAVE_DECL_LE32DEC #define le32dec cryb_le32dec #endif +#if !HAVE_DECL_LE32DECV +#define le32decv cryb_le32decv +#endif #if !HAVE_DECL_LE64ENC #define le64enc cryb_le64enc #endif +#if !HAVE_DECL_LE64ENCV +#define le64encv cryb_le64encv +#endif #if !HAVE_DECL_LE64DEC #define le64dec cryb_le64dec #endif +#if !HAVE_DECL_LE64DECV +#define le64decv cryb_le64decv +#endif #if !HAVE_DECL_HTOBE16 #define htobe16 cryb_htobe16 #endif @@ -138,6 +183,13 @@ cryb_bswap16(uint16_t u16) #endif } +static inline void +cryb_bswap16v(uint16_t *u16, size_t n) +{ + for (; n--; u16++) + *u16 = cryb_bswap16(*u16); +} + static inline uint32_t cryb_bswap32(uint32_t u32) { @@ -151,6 +203,13 @@ cryb_bswap32(uint32_t u32) #endif } +static inline void +cryb_bswap32v(uint32_t *u32, size_t n) +{ + for (; n--; u32++) + *u32 = cryb_bswap32(*u32); +} + static inline uint64_t cryb_bswap64(uint64_t u64) { @@ -168,6 +227,13 @@ cryb_bswap64(uint64_t u64) #endif } +static inline void +cryb_bswap64v(uint64_t *u64, size_t n) +{ + for (; n--; u64++) + *u64 = cryb_bswap64(*u64); +} + static inline void cryb_be16enc(void *p, uint16_t u16) { @@ -175,6 +241,13 @@ cryb_be16enc(void *p, uint16_t u16) ((uint8_t *)p)[0] = (u16 >> 8) & 0xff; } +static inline void +cryb_be16encv(void *p, const uint16_t *u16, size_t n) +{ + for (uint8_t *u8 = p; n--; u8 += sizeof *u16, u16++) + cryb_be16enc(u8, *u16); +} + static inline uint16_t cryb_be16dec(const void *p) { @@ -182,6 +255,13 @@ cryb_be16dec(const void *p) (uint16_t)((const uint8_t *)p)[0] << 8); } +static inline void +cryb_be16decv(uint16_t *u16, const void *p, size_t n) +{ + for (const uint8_t *u8 = p; n--; u8 += sizeof *u16, u16++) + *u16 = cryb_be16dec(u8); +} + static inline void cryb_be32enc(void *p, uint32_t u32) { @@ -191,6 +271,13 @@ cryb_be32enc(void *p, uint32_t u32) ((uint8_t *)p)[0] = (u32 >> 24) & 0xff; } +static inline void +cryb_be32encv(void *p, const uint32_t *u32, size_t n) +{ + for (uint8_t *u8 = p; n--; u8 += sizeof *u32, u32++) + cryb_be32enc(u8, *u32); +} + static inline uint32_t cryb_be32dec(const void *p) { @@ -200,6 +287,13 @@ cryb_be32dec(const void *p) (uint32_t)((const uint8_t *)p)[0] << 24); } +static inline void +cryb_be32decv(uint32_t *u32, const void *p, size_t n) +{ + for (const uint8_t *u8 = p; n--; u8 += sizeof *u32, u32++) + *u32 = cryb_be32dec(u8); +} + static inline void cryb_be64enc(void *p, uint64_t u64) { @@ -213,6 +307,13 @@ cryb_be64enc(void *p, uint64_t u64) ((uint8_t *)p)[0] = (u64 >> 56) & 0xff; } +static inline void +cryb_be64encv(void *p, const uint64_t *u64, size_t n) +{ + for (uint8_t *u8 = p; n--; u8 += sizeof *u64, u64++) + cryb_be64enc(u8, *u64); +} + static inline uint64_t cryb_be64dec(const void *p) { @@ -226,6 +327,13 @@ cryb_be64dec(const void *p) (uint64_t)((const uint8_t *)p)[0] << 56); } +static inline void +cryb_be64decv(uint64_t *u64, const void *p, size_t n) +{ + for (const uint8_t *u8 = p; n--; u8 += sizeof *u64, u64++) + *u64 = cryb_be64dec(u8); +} + static inline void cryb_le16enc(void *p, uint16_t u16) { @@ -233,6 +341,13 @@ cryb_le16enc(void *p, uint16_t u16) ((uint8_t *)p)[1] = (u16 >> 8) & 0xff; } +static inline void +cryb_le16encv(void *p, const uint16_t *u16, size_t n) +{ + for (uint8_t *u8 = p; n--; u8 += sizeof *u16, u16++) + cryb_le16enc(u8, *u16); +} + static inline uint16_t cryb_le16dec(const void *p) { @@ -240,6 +355,13 @@ cryb_le16dec(const void *p) (uint16_t)((const uint8_t *)p)[1] << 8); } +static inline void +cryb_le16decv(uint16_t *u16, const void *p, size_t n) +{ + for (const uint8_t *u8 = p; n--; u8 += sizeof *u16, u16++) + *u16 = cryb_le16dec(u8); +} + static inline void cryb_le32enc(void *p, uint32_t u32) { @@ -249,6 +371,13 @@ cryb_le32enc(void *p, uint32_t u32) ((uint8_t *)p)[3] = (u32 >> 24) & 0xff; } +static inline void +cryb_le32encv(void *p, const uint32_t *u32, size_t n) +{ + for (uint8_t *u8 = p; n--; u8 += sizeof *u32, u32++) + cryb_le32enc(u8, *u32); +} + static inline uint32_t cryb_le32dec(const void *p) { @@ -258,6 +387,13 @@ cryb_le32dec(const void *p) (uint32_t)((const uint8_t *)p)[3] << 24); } +static inline void +cryb_le32decv(uint32_t *u32, const void *p, size_t n) +{ + for (const uint8_t *u8 = p; n--; u8 += sizeof *u32, u32++) + *u32 = cryb_le32dec(u8); +} + static inline void cryb_le64enc(void *p, uint64_t u64) { @@ -271,6 +407,13 @@ cryb_le64enc(void *p, uint64_t u64) ((uint8_t *)p)[7] = (u64 >> 56) & 0xff; } +static inline void +cryb_le64encv(void *p, const uint64_t *u64, size_t n) +{ + for (uint8_t *u8 = p; n--; u8 += sizeof *u64, u64++) + cryb_le64enc(u8, *u64); +} + static inline uint64_t cryb_le64dec(const void *p) { @@ -284,6 +427,13 @@ cryb_le64dec(const void *p) (uint64_t)((const uint8_t *)p)[7] << 56); } +static inline void +cryb_le64decv(uint64_t *u64, const void *p, size_t n) +{ + for (const uint8_t *u8 = p; n--; u8 += sizeof *u64, u64++) + *u64 = cryb_le64dec(u8); +} + static inline uint16_t cryb_htobe16(uint16_t u16) {