From 66dd2aa10938ca2ae6cec60c1295b2f46501e3f5 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 17 Dec 2015 10:57:47 -0800 Subject: [PATCH] Allow override for unaligned reads in SpookyHashV2 Summary: Add configuration for disabling unaligned reads in SpookyHashV2 Reviewed By: yfeldblum Differential Revision: D2750885 fb-gh-sync-id: 9f48dcdfd5af05478a38e354f6fa0332b1332c14 --- folly/Portability.h | 4 ++++ folly/SpookyHashV2.cpp | 12 +++++++----- folly/configure.ac | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/folly/Portability.h b/folly/Portability.h index 267bd82d..63318a2c 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -48,6 +48,10 @@ #endif #endif +#ifndef FOLLY_HAVE_UNALIGNED_READS +#define FOLLY_HAVE_UNALIGNED_READS 0 +#endif + // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named // MAP_ANON on OSX/BSD. #if defined(__APPLE__) || defined(__FreeBSD__) diff --git a/folly/SpookyHashV2.cpp b/folly/SpookyHashV2.cpp index e2f665f2..877e0e72 100644 --- a/folly/SpookyHashV2.cpp +++ b/folly/SpookyHashV2.cpp @@ -28,13 +28,15 @@ #include -#include +#include -#define ALLOW_UNALIGNED_READS 1 +#include namespace folly { namespace hash { +static constexpr auto kAllowUnalignedReads = bool(FOLLY_HAVE_UNALIGNED_READS); + // // short hash ... it could be used on any message, // but it's used by Spooky just for short messages. @@ -56,7 +58,7 @@ void SpookyHashV2::Short( u.p8 = (const uint8_t *)message; - if (!ALLOW_UNALIGNED_READS && (u.i & 0x7)) + if (!kAllowUnalignedReads && (u.i & 0x7)) { memcpy(buf, message, length); u.p64 = buf; @@ -176,7 +178,7 @@ void SpookyHashV2::Hash128( end = u.p64 + (length/sc_blockSize)*sc_numVars; // handle all whole sc_blockSize blocks of bytes - if (ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0)) + if (kAllowUnalignedReads || ((u.i & 0x7) == 0)) { while (u.p64 < end) { @@ -284,7 +286,7 @@ void SpookyHashV2::Update(const void *message, size_t length) // handle all whole blocks of sc_blockSize bytes end = u.p64 + (length/sc_blockSize)*sc_numVars; remainder = (uint8_t)(length-((const uint8_t *)end-u.p8)); - if (ALLOW_UNALIGNED_READS || (u.i & 0x7) == 0) + if (kAllowUnalignedReads || (u.i & 0x7) == 0) { while (u.p64 < end) { diff --git a/folly/configure.ac b/folly/configure.ac index d89061b1..a60af3c5 100644 --- a/folly/configure.ac +++ b/folly/configure.ac @@ -317,6 +317,26 @@ if test "$folly_cv_prog_cc_weak_symbols" = yes; then [Define to 1 if the linker supports weak symbols.]) fi +# Figure out whether the architecture supports unaligned reads +AC_CACHE_CHECK( + [for unaligned reads support], + [folly_cv_prog_cc_unaligned_reads], + [AC_RUN_IFELSE( + [AC_LANG_SOURCE[ + int main(int argc, char** argv) { + char buf[64] = {0}; + unsigned long *ptr = (unsigned long *)(buf + 1); + *ptr = 0xdeadbeef; + return (*ptr & 0xff) == 0xef ? 0 : 1; + } + ]], + [folly_cv_prog_cc_unaligned_reads=yes], + [folly_cv_prog_cc_unaligned_reads=no])]) + +if test "$folly_cv_prog_cc_unaligned_reads" = "yes"; then + AC_DEFINE([HAVE_UNALIGNED_READS], [1], [Define to 1 if the architecture allows unaligned reads]) +fi + AC_CACHE_CHECK( [for vsnprintf reporting bad format strings], [folly_cv_prog_vsnprintf_bad_format], -- 2.34.1