From 8b456a8bd8cf540ad4db74326d698d0d3560a87f Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 14 May 2018 18:18:36 +0100 Subject: [PATCH] [Minor] Improve clang support (backported from t1ha) --- contrib/t1ha/t1ha1.c | 4 +- contrib/t1ha/t1ha2.c | 6 +- contrib/t1ha/t1ha_bits.h | 146 +++++++++++++++++++++------------------ 3 files changed, 85 insertions(+), 71 deletions(-) diff --git a/contrib/t1ha/t1ha1.c b/contrib/t1ha/t1ha1.c index 5baa5692f..956f7e24e 100644 --- a/contrib/t1ha/t1ha1.c +++ b/contrib/t1ha/t1ha1.c @@ -135,7 +135,7 @@ uint64_t t1ha1_le(const void *data, size_t len, uint64_t seed) { uint64_t b = len; const bool need_copy4align = - (((uintptr_t)data) & (ALIGMENT_64 - 1)) != 0 && !UNALIGNED_OK; + (((uintptr_t)data) & (ALIGNMENT_64 - 1)) != 0 && !UNALIGNED_OK; uint64_t align[4]; if (need_copy4align) { T1HA1_BODY(le, aligned, true); @@ -149,7 +149,7 @@ uint64_t t1ha1_be(const void *data, size_t len, uint64_t seed) { uint64_t b = len; const bool need_copy4align = - (((uintptr_t)data) & (ALIGMENT_64 - 1)) != 0 && !UNALIGNED_OK; + (((uintptr_t)data) & (ALIGNMENT_64 - 1)) != 0 && !UNALIGNED_OK; uint64_t align[4]; if (need_copy4align) { T1HA1_BODY(be, aligned, true); diff --git a/contrib/t1ha/t1ha2.c b/contrib/t1ha/t1ha2.c index 1f5a9d905..95f646da4 100644 --- a/contrib/t1ha/t1ha2.c +++ b/contrib/t1ha/t1ha2.c @@ -208,7 +208,7 @@ uint64_t t1ha2_atonce(const void *data, size_t length, uint64_t seed) { init_ab(&state, seed, length); const bool need_copy4align = - (((uintptr_t)data) & (ALIGMENT_64 - 1)) != 0 && !UNALIGNED_OK; + (((uintptr_t)data) & (ALIGNMENT_64 - 1)) != 0 && !UNALIGNED_OK; if (need_copy4align) { uint64_t buffer4align[4]; if (unlikely(length > 32)) { @@ -237,7 +237,7 @@ uint64_t t1ha2_atonce128(uint64_t *__restrict extra_result, init_cd(&state, seed, length); const bool need_copy4align = - (((uintptr_t)data) & (ALIGMENT_64 - 1)) != 0 && !UNALIGNED_OK; + (((uintptr_t)data) & (ALIGNMENT_64 - 1)) != 0 && !UNALIGNED_OK; if (need_copy4align) { uint64_t buffer4align[4]; if (unlikely(length > 32)) { @@ -284,7 +284,7 @@ void t1ha2_update(t1ha_context_t *__restrict ctx, const void *__restrict data, if (length >= 32) { const bool need_copy4align = - (((uintptr_t)data) & (ALIGMENT_64 - 1)) != 0 && !UNALIGNED_OK; + (((uintptr_t)data) & (ALIGNMENT_64 - 1)) != 0 && !UNALIGNED_OK; if (need_copy4align) { T1HA2_LOOP(le, aligned, ctx->buffer.u64, &ctx->state, data, length); } else { diff --git a/contrib/t1ha/t1ha_bits.h b/contrib/t1ha/t1ha_bits.h index 42daf5790..e3815a4e7 100644 --- a/contrib/t1ha/t1ha_bits.h +++ b/contrib/t1ha/t1ha_bits.h @@ -86,12 +86,12 @@ #define PAGESIZE 4096 #endif /* PAGESIZE */ -#define ALIGMENT_16 2 -#define ALIGMENT_32 4 +#define ALIGNMENT_16 2 +#define ALIGNMENT_32 4 #if UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul -#define ALIGMENT_64 8 +#define ALIGNMENT_64 8 #else -#define ALIGMENT_64 4 +#define ALIGNMENT_64 4 #endif /***************************************************************************/ @@ -100,6 +100,22 @@ #define __has_builtin(x) (0) #endif +#ifndef __has_warning +#define __has_warning(x) (0) +#endif + +#ifndef __has_feature +#define __has_feature(x) (0) +#endif + +#ifndef __has_extension +#define __has_extension(x) (0) +#endif + +#if __has_feature(address_sanitizer) +#define __SANITIZE_ADDRESS__ 1 +#endif + #if __GNUC_PREREQ(4, 4) || defined(__clang__) @@ -343,95 +359,93 @@ static __always_inline uint16_t bswap16(uint16_t v) { return v << 8 | v >> 8; } #endif #endif /* bswap16 */ -#if defined(__GNUC__) || (__has_attribute(packed) && __has_attribute(aligned)) +#ifndef read_unaligned +#if defined(__GNUC__) || __has_attribute(packed) typedef struct { uint8_t unaligned_8; uint16_t unaligned_16; uint32_t unaligned_32; uint64_t unaligned_64; -} __attribute__((packed, aligned(1))) t1ha_unaligned_proxy; +} __attribute__((packed)) t1ha_unaligned_proxy; #define read_unaligned(ptr, bits) \ (((const t1ha_unaligned_proxy *)((const uint8_t *)(ptr)-offsetof( \ t1ha_unaligned_proxy, unaligned_##bits))) \ ->unaligned_##bits) -#define read_aligned(ptr, bits) \ - (*(const __attribute__((aligned(ALIGMENT_##bits))) uint##bits##_t *)(ptr)) #elif defined(_MSC_VER) #pragma warning( \ disable : 4235) /* nonstandard extension used: '__unaligned' \ * keyword not supported on this architecture */ #define read_unaligned(ptr, bits) (*(const __unaligned uint##bits##_t *)(ptr)) -#define read_aligned(ptr, bits) \ - (*(const __declspec(align(ALIGMENT_##bits)) uint##bits##_t *)(ptr)) #else -#error FIXME +#pragma pack(push, 1) +typedef struct { + uint8_t unaligned_8; + uint16_t unaligned_16; + uint32_t unaligned_32; + uint64_t unaligned_64; +} t1ha_unaligned_proxy; +#pragma pack(pop) #define read_unaligned(ptr, bits) \ - (*(const uint##bits##_ *)((const char *)(ptr))) -#define read_aligned(ptr, bits) (*(const uint##bits##_t *)(ptr)) + (((const t1ha_unaligned_proxy *)((const uint8_t *)(ptr)-offsetof( \ + t1ha_unaligned_proxy, unaligned_##bits))) \ + ->unaligned_##bits) +#endif #endif /* read_unaligned */ -#if 0 -#ifndef DECLARE_UNALIGNED_PTR -#if defined(__LCC__) -#pragma diag_suppress wrong_entity_for_attribute -#define DECLARE_UNALIGNED_PTR(ptr) char __attribute__((packed)) * ptr -#elif defined(__clang__) -#pragma clang diagnostic ignored "-Wignored-attributes" -#define DECLARE_UNALIGNED_PTR(ptr) char __attribute__((packed)) * ptr -#elif defined(__GNUC__) -#pragma GCC diagnostic ignored "-Wpacked" -#define DECLARE_UNALIGNED_PTR(ptr) char __attribute__((packed)) * ptr +#ifndef read_aligned +#if __has_builtin(assume_aligned) +#define read_aligned(ptr, bits) \ + (*(const uint##bits##_t *)__builtin_assume_aligned(ptr, ALIGNMENT_##bits)) +#elif __has_attribute(aligned) && !defined(__clang__) +#define read_aligned(ptr, bits) \ + (*(const uint##bits##_t __attribute__((aligned(ALIGNMENT_##bits))) *)(ptr)) +#elif __has_attribute(assume_aligned) + +static __always_inline const + uint16_t *__attribute__((assume_aligned(ALIGNMENT_16))) + cast_aligned_16(const void *ptr) { + return (const uint16_t *)ptr; +} +static __always_inline const + uint32_t *__attribute__((assume_aligned(ALIGNMENT_32))) + cast_aligned_32(const void *ptr) { + return (const uint32_t *)ptr; +} +static __always_inline const + uint64_t *__attribute__((assume_aligned(ALIGNMENT_64))) + cast_aligned_64(const void *ptr) { + return (const uint64_t *)ptr; +} + +#define read_aligned(ptr, bits) (*cast_aligned_##bits(ptr)) + #elif defined(_MSC_VER) -#pragma warning( \ - disable : 4235) /* nonstandard extension used: '__unaligned' \ - * keyword not supported on this architecture */ -#define DECLARE_UNALIGNED_PTR(ptr) char __unaligned *ptr +#define read_aligned(ptr, bits) \ + (*(const __declspec(align(ALIGNMENT_##bits)) uint##bits##_t *)(ptr)) #else -#define DECLARE_UNALIGNED_PTR(ptr) char *ptr +#define read_aligned(ptr, bits) (*(const uint##bits##_t *)(ptr)) #endif -#endif /* cast_unaligned */ - -#ifndef cast_unaligned -#if defined(__LCC__) -#pragma diag_suppress wrong_entity_for_attribute -#define cast_unaligned(ptr) ((const char __attribute__((packed)) *)(ptr)) -#elif defined(__clang__) -#pragma clang diagnostic ignored "-Wignored-attributes" -#define cast_unaligned(ptr) ((const char __attribute__((packed)) *)(ptr)) +#endif /* read_aligned */ + +#if __has_warning("-Wconstant-logical-operand") +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wconstant-logical-operand" #elif defined(__GNUC__) -#pragma GCC diagnostic ignored "-Wpacked" -#define cast_unaligned(ptr) ((const char __attribute__((packed)) *)(ptr)) -#elif defined(_MSC_VER) -#pragma warning( \ - disable : 4235) /* nonstandard extension used: '__unaligned' \ - * keyword not supported on this architecture */ -#define cast_unaligned(ptr) ((const char __unaligned *)(ptr)) +#pragma GCC diagnostic ignored "-Wconstant-logical-operand" #else -#define cast_unaligned(ptr) ((const char *)(ptr)) +#pragma warning disable "constant-logical-operand" #endif -#endif /* cast_unaligned */ - -#ifndef cast_aligned -#if defined(__LCC__) -#pragma diag_suppress wrong_entity_for_attribute -#define cast_aligned(type, ptr) \ - ((const type __attribute__((aligned(sizeof(type)))) *)(ptr)) -#elif defined(__clang__) -#pragma clang diagnostic ignored "-Wignored-attributes" -#define cast_aligned(type, ptr) \ - ((const type __attribute__((aligned(sizeof(type)))) *)(ptr)) +#endif /* -Wconstant-logical-operand */ + +#if __has_warning("-Wtautological-pointer-compare") +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wtautological-pointer-compare" #elif defined(__GNUC__) -#pragma GCC diagnostic ignored "-Wpacked" -#define cast_aligned(type, ptr) \ - ((const type __attribute__((aligned(sizeof(type)))) *)(ptr)) -#elif defined(_MSC_VER) -#define cast_aligned(type, ptr) \ - ((const type __declspec((align(sizeof(type)))) *)(ptr)) +#pragma GCC diagnostic ignored "-Wtautological-pointer-compare" #else -#define cast_aligned(type, ptr) ((const type *)(ptr)) +#pragma warning disable "tautological-pointer-compare" #endif -#endif /* cast_aligned */ -#endif /* 0 */ +#endif /* -Wtautological-pointer-compare */ /***************************************************************************/ -- 2.39.5