Commit d7a2b20b authored by Kyle Nekritz's avatar Kyle Nekritz Committed by Facebook Github Bot 6

Make Bits.h respect FOLLY_HAVE_UNALIGNED_ACCESS.

Summary: This was causing a bunch of SIGBUS's on ARM platforms.

Reviewed By: yfeldblum, mzlee

Differential Revision: D3013458

fb-gh-sync-id: 6d3f60c3f59d16cd3454e3a4231b967e5378724a
shipit-source-id: 6d3f60c3f59d16cd3454e3a4231b967e5378724a
parent 4700a31c
...@@ -550,7 +550,13 @@ template <class T> ...@@ -550,7 +550,13 @@ template <class T>
inline T loadUnaligned(const void* p) { inline T loadUnaligned(const void* p) {
static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size"); static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment"); static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
return static_cast<const Unaligned<T>*>(p)->value; if (kHasUnalignedAccess) {
return static_cast<const Unaligned<T>*>(p)->value;
} else {
T value;
memcpy(&value, p, sizeof(T));
return value;
}
} }
/** /**
...@@ -560,7 +566,11 @@ template <class T> ...@@ -560,7 +566,11 @@ template <class T>
inline void storeUnaligned(void* p, T value) { inline void storeUnaligned(void* p, T value) {
static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size"); static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment"); static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
new (p) Unaligned<T>(value); if (kHasUnalignedAccess) {
new (p) Unaligned<T>(value);
} else {
memcpy(p, &value, sizeof(T));
}
} }
} // namespace folly } // namespace folly
......
...@@ -48,9 +48,14 @@ ...@@ -48,9 +48,14 @@
#endif #endif
#endif #endif
#ifndef FOLLY_HAVE_UNALIGNED_READS // Unaligned loads and stores
#define FOLLY_HAVE_UNALIGNED_READS 0 namespace folly {
#if FOLLY_HAVE_UNALIGNED_ACCESS
constexpr bool kHasUnalignedAccess = true;
#else
constexpr bool kHasUnalignedAccess = false;
#endif #endif
}
// A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
// MAP_ANON on OSX/BSD. // MAP_ANON on OSX/BSD.
......
...@@ -35,8 +35,6 @@ ...@@ -35,8 +35,6 @@
namespace folly { namespace folly {
namespace hash { namespace hash {
static constexpr auto kAllowUnalignedReads = bool(FOLLY_HAVE_UNALIGNED_READS);
// //
// short hash ... it could be used on any message, // short hash ... it could be used on any message,
// but it's used by Spooky just for short messages. // but it's used by Spooky just for short messages.
...@@ -58,7 +56,7 @@ void SpookyHashV2::Short( ...@@ -58,7 +56,7 @@ void SpookyHashV2::Short(
u.p8 = (const uint8_t *)message; u.p8 = (const uint8_t *)message;
if (!kAllowUnalignedReads && (u.i & 0x7)) if (!kHasUnalignedAccess && (u.i & 0x7))
{ {
memcpy(buf, message, length); memcpy(buf, message, length);
u.p64 = buf; u.p64 = buf;
...@@ -178,7 +176,7 @@ void SpookyHashV2::Hash128( ...@@ -178,7 +176,7 @@ void SpookyHashV2::Hash128(
end = u.p64 + (length/sc_blockSize)*sc_numVars; end = u.p64 + (length/sc_blockSize)*sc_numVars;
// handle all whole sc_blockSize blocks of bytes // handle all whole sc_blockSize blocks of bytes
if (kAllowUnalignedReads || ((u.i & 0x7) == 0)) if (kHasUnalignedAccess || ((u.i & 0x7) == 0))
{ {
while (u.p64 < end) while (u.p64 < end)
{ {
...@@ -286,7 +284,7 @@ void SpookyHashV2::Update(const void *message, size_t length) ...@@ -286,7 +284,7 @@ void SpookyHashV2::Update(const void *message, size_t length)
// handle all whole blocks of sc_blockSize bytes // handle all whole blocks of sc_blockSize bytes
end = u.p64 + (length/sc_blockSize)*sc_numVars; end = u.p64 + (length/sc_blockSize)*sc_numVars;
remainder = (uint8_t)(length-((const uint8_t *)end-u.p8)); remainder = (uint8_t)(length-((const uint8_t *)end-u.p8));
if (kAllowUnalignedReads || (u.i & 0x7) == 0) if (kHasUnalignedAccess || (u.i & 0x7) == 0)
{ {
while (u.p64 < end) while (u.p64 < end)
{ {
......
...@@ -339,10 +339,10 @@ if test "$folly_cv_prog_cc_weak_symbols" = yes; then ...@@ -339,10 +339,10 @@ if test "$folly_cv_prog_cc_weak_symbols" = yes; then
[Define to 1 if the linker supports weak symbols.]) [Define to 1 if the linker supports weak symbols.])
fi fi
# Figure out whether the architecture supports unaligned reads # Figure out whether the architecture supports unaligned accesses
AC_CACHE_CHECK( AC_CACHE_CHECK(
[for unaligned reads support], [for unaligned access support],
[folly_cv_prog_cc_unaligned_reads], [folly_cv_prog_cc_unaligned_access],
[AC_RUN_IFELSE( [AC_RUN_IFELSE(
[AC_LANG_SOURCE[ [AC_LANG_SOURCE[
int main(int argc, char** argv) { int main(int argc, char** argv) {
...@@ -352,11 +352,11 @@ AC_CACHE_CHECK( ...@@ -352,11 +352,11 @@ AC_CACHE_CHECK(
return (*ptr & 0xff) == 0xef ? 0 : 1; return (*ptr & 0xff) == 0xef ? 0 : 1;
} }
]], ]],
[folly_cv_prog_cc_unaligned_reads=yes], [folly_cv_prog_cc_unaligned_access=yes],
[folly_cv_prog_cc_unaligned_reads=no])]) [folly_cv_prog_cc_unaligned_access=no])])
if test "$folly_cv_prog_cc_unaligned_reads" = "yes"; then if test "$folly_cv_prog_cc_unaligned_access" = "yes"; then
AC_DEFINE([HAVE_UNALIGNED_READS], [1], [Define to 1 if the architecture allows unaligned reads]) AC_DEFINE([HAVE_UNALIGNED_ACCESS], [1], [Define to 1 if the architecture allows unaligned accesses])
fi fi
AC_CACHE_CHECK( AC_CACHE_CHECK(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment