From 39cfa67b4f56e0c79afb352b1e9bf5c914b774ca Mon Sep 17 00:00:00 2001 From: shyouhei Date: Mon, 15 Jan 2018 02:35:18 +0000 Subject: __builtin_assume_aligned for *(foo *) casts These casts are guarded. Must be safe to assume alignments. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61829 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- bignum.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'bignum.c') diff --git a/bignum.c b/bignum.c index 6656d28ea4..40d5d0e224 100644 --- a/bignum.c +++ b/bignum.c @@ -1079,6 +1079,13 @@ integer_unpack_single_bdigit(BDIGIT u, size_t size, int flags, BDIGIT *dp) return sign; } +#ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED +#define reinterpret_cast(type, value) (type) \ + __builtin_assume_aligned((value), sizeof(*(type)NULL)); +#else +#define reinterpret_cast(type, value) (type)value +#endif + static int bary_unpack_internal(BDIGIT *bdigits, size_t num_bdigits, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags, int nlp_bits) { @@ -1100,22 +1107,23 @@ bary_unpack_internal(BDIGIT *bdigits, size_t num_bdigits, const void *words, siz } #if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGIT if (wordsize == 2 && (uintptr_t)words % RUBY_ALIGNOF(uint16_t) == 0) { - uint16_t u = *(uint16_t *)buf; + uint16_t u = *reinterpret_cast(const uint16_t *, buf); return integer_unpack_single_bdigit(need_swap ? swap16(u) : u, sizeof(uint16_t), flags, dp); } #endif #if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGIT if (wordsize == 4 && (uintptr_t)words % RUBY_ALIGNOF(uint32_t) == 0) { - uint32_t u = *(uint32_t *)buf; + uint32_t u = *reinterpret_cast(const uint32_t *, buf); return integer_unpack_single_bdigit(need_swap ? swap32(u) : u, sizeof(uint32_t), flags, dp); } #endif #if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGIT if (wordsize == 8 && (uintptr_t)words % RUBY_ALIGNOF(uint64_t) == 0) { - uint64_t u = *(uint64_t *)buf; + uint64_t u = *reinterpret_cast(const uint64_t *, buf); return integer_unpack_single_bdigit(need_swap ? swap64(u) : u, sizeof(uint64_t), flags, dp); } #endif +#undef reinterpret_cast } #if !defined(WORDS_BIGENDIAN) if (nails == 0 && SIZEOF_BDIGIT == sizeof(BDIGIT) && -- cgit v1.2.3