Skip to content

Commit f86f3c8

Browse files
committed
Avoid misaligned loads from StaticRandomData in the size [4, 8] hashing case.
1 parent 1601635 commit f86f3c8

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

common/hashing.h

+18-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
#define CARBON_COMMON_HASHING_H_
77

88
#include <concepts>
9+
#include <cstdint>
910
#include <string>
1011
#include <tuple>
1112
#include <type_traits>
1213
#include <utility>
1314

14-
#include "common/check.h"
15-
#include "common/ostream.h"
16-
#include "llvm/ADT/APFloat.h"
17-
#include "llvm/ADT/APInt.h"
18-
#include "llvm/ADT/ArrayRef.h"
19-
#include "llvm/ADT/SmallVector.h"
20-
#include "llvm/ADT/StringRef.h"
21-
#include "llvm/Support/FormatVariadic.h"
15+
#include "third_party/carbon/lang/common/check.h"
16+
#include "third_party/carbon/lang/common/ostream.h"
17+
#include "third_party/llvm/llvm-project/llvm/include/llvm/ADT/APFloat.h"
18+
#include "third_party/llvm/llvm-project/llvm/include/llvm/ADT/APInt.h"
19+
#include "third_party/llvm/llvm-project/llvm/include/llvm/ADT/ArrayRef.h"
20+
#include "third_party/llvm/llvm-project/llvm/include/llvm/ADT/SmallVector.h"
21+
#include "third_party/llvm/llvm-project/llvm/include/llvm/ADT/StringRef.h"
22+
#include "third_party/llvm/llvm-project/llvm/include/llvm/Support/FormatVariadic.h"
2223

2324
#ifdef __ARM_ACLE
2425
#include <arm_acle.h>
@@ -387,6 +388,13 @@ class Hasher {
387388
return data;
388389
}
389390

391+
// As above, but for small offsets, we can use aligned loads, which are
392+
// faster. The offset must be in the range [0, 8).
393+
static auto SampleAlignedRandomData(ssize_t offset) -> uint64_t {
394+
CARBON_DCHECK(offset < sizeof(StaticRandomData) / sizeof(uint64_t));
395+
return StaticRandomData[offset];
396+
}
397+
390398
// Random data taken from the hexadecimal digits of Pi's fractional component,
391399
// written in lexical order for convenience of reading. The resulting
392400
// byte-stream will be different due to little-endian integers. These can be
@@ -408,7 +416,7 @@ class Hasher {
408416
// | sed -e "s/.\{4\}/&'/g" \
409417
// | sed -e "s/\(.\{4\}'.\{4\}'.\{4\}'.\{4\}\)'/0x\1,\n/g"
410418
// ```
411-
alignas(64) static constexpr std::array<uint64_t, 8> StaticRandomData = {
419+
static constexpr std::array<uint64_t, 8> StaticRandomData = {
412420
0x243f'6a88'85a3'08d3, 0x1319'8a2e'0370'7344, 0xa409'3822'299f'31d0,
413421
0x082e'fa98'ec4e'6c89, 0x4528'21e6'38d0'1377, 0xbe54'66cf'34e9'0c6c,
414422
0xc0ac'29b7'c97c'50dd, 0x3f84'd5b5'b547'0917,
@@ -950,7 +958,7 @@ inline auto Hasher::HashSizedBytes(llvm::ArrayRef<std::byte> bytes) -> void {
950958
// Note that we don't drop to the `WeakMix` routine here because we want
951959
// to use sampled random data to encode the size, which may not be as
952960
// effective without the full 128-bit folded result.
953-
buffer = Mix(data ^ buffer, SampleRandomData(size));
961+
buffer = Mix(data ^ buffer, SampleAlignedRandomData(size - 1));
954962
CARBON_MCA_END("dynamic-8b");
955963
return;
956964
}

0 commit comments

Comments
 (0)