e991ffd37b
TEST=ubsan Bug: https://github.com/dart-lang/sdk/issues/63452 Change-Id: I112b26c08b5ae186dc5264d99edbed9e6e60368d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/506660 Reviewed-by: Alexander Aprelev <aam@google.com> Commit-Queue: Ryan Macnak <rmacnak@google.com>
57 lines
1.8 KiB
C++
57 lines
1.8 KiB
C++
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#ifndef RUNTIME_VM_HASH_H_
|
|
#define RUNTIME_VM_HASH_H_
|
|
|
|
#include "platform/globals.h"
|
|
#include "platform/unaligned.h"
|
|
|
|
namespace dart {
|
|
|
|
inline uint32_t CombineHashes(uint32_t hash, uint32_t other_hash) {
|
|
// Keep in sync with AssemblerBase::CombineHashes.
|
|
hash += other_hash;
|
|
hash += hash << 10;
|
|
hash ^= hash >> 6; // Logical shift, unsigned hash.
|
|
return hash;
|
|
}
|
|
|
|
inline uint32_t FinalizeHash(uint32_t hash, intptr_t hashbits = kBitsPerInt32) {
|
|
// Keep in sync with AssemblerBase::FinalizeHash.
|
|
hash += hash << 3;
|
|
hash ^= hash >> 11; // Logical shift, unsigned hash.
|
|
hash += hash << 15;
|
|
if (hashbits < kBitsPerInt32) {
|
|
hash &= (static_cast<uint32_t>(1) << hashbits) - 1;
|
|
}
|
|
return (hash == 0) ? 1 : hash;
|
|
}
|
|
|
|
// The value returned by HashBytes when the length is 0. Used to avoid storing
|
|
// canonical hashes to and loading from the heap for empty container Instances.
|
|
static constexpr uint32_t kEmptyContainerHash = 1;
|
|
|
|
inline uint32_t HashBytes(const void* bytes,
|
|
intptr_t len,
|
|
intptr_t hashbits = kBitsPerInt32) {
|
|
if (len == 0) {
|
|
return kEmptyContainerHash;
|
|
}
|
|
uint32_t hash = len;
|
|
const intptr_t chunks = len / kInt32Size;
|
|
for (intptr_t i = 0; i < chunks; i++) {
|
|
hash = CombineHashes(
|
|
hash, LoadUnaligned(&(reinterpret_cast<const uint32_t*>(bytes)[i])));
|
|
}
|
|
for (intptr_t i = chunks * kInt32Size; i < len; i++) {
|
|
hash = CombineHashes(hash, reinterpret_cast<const uint8_t*>(bytes)[i]);
|
|
}
|
|
return FinalizeHash(hash, hashbits);
|
|
}
|
|
|
|
} // namespace dart
|
|
|
|
#endif // RUNTIME_VM_HASH_H_
|