|
9 | 9 |
|
10 | 10 | #include "MurmurHash3.h" |
11 | 11 |
|
| 12 | +#include "mongo/base/data_type_endian.h" |
| 13 | +#include "mongo/base/data_view.h" |
| 14 | +#include "mongo/platform/endian.h" |
| 15 | + |
12 | 16 | //----------------------------------------------------------------------------- |
13 | 17 | // Platform-specific functions and macros |
14 | 18 |
|
@@ -51,15 +55,22 @@ inline uint64_t rotl64 ( uint64_t x, int8_t r ) |
51 | 55 | //----------------------------------------------------------------------------- |
52 | 56 | // Block read - if your platform needs to do endian-swapping or can only |
53 | 57 | // handle aligned reads, do the conversion here |
| 58 | +// |
| 59 | +// NOTE, MongoDB code: JC - |
| 60 | +// ConstDataView handles the byte swapping and avoids unaligned reads. Note |
| 61 | +// that we need reversed versions because we actually want little endian |
| 62 | +// encoded blocks out of getblock, and our input data is in the native format. |
54 | 63 |
|
55 | 64 | FORCE_INLINE inline uint32_t getblock ( const uint32_t * p, int i ) |
56 | 65 | { |
57 | | - return p[i]; |
| 66 | + return mongo::ConstDataView(reinterpret_cast<const char*>(p)) |
| 67 | + .read<mongo::ReverseLittleEndian<uint32_t>>(i * sizeof(uint32_t)); |
58 | 68 | } |
59 | 69 |
|
60 | 70 | FORCE_INLINE inline uint64_t getblock ( const uint64_t * p, int i ) |
61 | 71 | { |
62 | | - return p[i]; |
| 72 | + return mongo::ConstDataView(reinterpret_cast<const char*>(p)) |
| 73 | + .read<mongo::ReverseLittleEndian<uint64_t>>(i * sizeof(uint64_t)); |
63 | 74 | } |
64 | 75 |
|
65 | 76 | //----------------------------------------------------------------------------- |
@@ -142,7 +153,7 @@ void MurmurHash3_x86_32 ( const void * key, int len, |
142 | 153 |
|
143 | 154 | h1 = fmix(h1); |
144 | 155 |
|
145 | | - *(uint32_t*)out = h1; |
| 156 | + *(uint32_t*)out = mongo::endian::nativeToLittle(h1); |
146 | 157 | } |
147 | 158 |
|
148 | 159 | //----------------------------------------------------------------------------- |
@@ -244,10 +255,10 @@ void MurmurHash3_x86_128 ( const void * key, const int len, |
244 | 255 | h1 += h2; h1 += h3; h1 += h4; |
245 | 256 | h2 += h1; h3 += h1; h4 += h1; |
246 | 257 |
|
247 | | - ((uint32_t*)out)[0] = h1; |
248 | | - ((uint32_t*)out)[1] = h2; |
249 | | - ((uint32_t*)out)[2] = h3; |
250 | | - ((uint32_t*)out)[3] = h4; |
| 258 | + ((uint32_t*)out)[0] = mongo::endian::nativeToLittle(h1); |
| 259 | + ((uint32_t*)out)[1] = mongo::endian::nativeToLittle(h2); |
| 260 | + ((uint32_t*)out)[2] = mongo::endian::nativeToLittle(h3); |
| 261 | + ((uint32_t*)out)[3] = mongo::endian::nativeToLittle(h4); |
251 | 262 | } |
252 | 263 |
|
253 | 264 | //----------------------------------------------------------------------------- |
@@ -327,8 +338,8 @@ void MurmurHash3_x64_128 ( const void * key, const int len, |
327 | 338 | h1 += h2; |
328 | 339 | h2 += h1; |
329 | 340 |
|
330 | | - ((uint64_t*)out)[0] = h1; |
331 | | - ((uint64_t*)out)[1] = h2; |
| 341 | + ((uint64_t*)out)[0] = mongo::endian::nativeToLittle(h1); |
| 342 | + ((uint64_t*)out)[1] = mongo::endian::nativeToLittle(h2); |
332 | 343 | } |
333 | 344 |
|
334 | 345 | //----------------------------------------------------------------------------- |
|
0 commit comments