Skip to content

Commit

Permalink
always use memcpy to enforce alignment in getblock8 (#2196)
Browse files Browse the repository at this point in the history
Always use memcpy to enforce alignment
  • Loading branch information
dimpase authored and ChrisJefferson committed Feb 20, 2018
1 parent 7dd2209 commit 16f7cc7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/intfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,18 @@ static inline uint64_t rotl64 ( uint64_t x, int8_t r )
//-----------------------------------------------------------------------------
// Block read - if your platform needs to do endian-swapping or can only
// handle aligned reads, do the conversion here
//
// The pointer p may not be aligned, which means that directly reading it can
// incur a major performance penalty or even trigger a segfault on certain
// architectures (e.g. ARM, SPARC). Thus we use memcpy here, with the implicit
// hope that on archs which don't need this, the compiler will optimize it back
// into a direct copy (verified to happen with GCC and clang on x86_64)

FORCE_INLINE uint64_t getblock8 ( const uint64_t * p, int i )
{
return p[i];
uint64_t val;
memcpy(&val, p + i, sizeof(uint64_t));
return val;
}

//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 16f7cc7

Please sign in to comment.