Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/PDB/Native/Hash.cpp
35293 views
//===- Hash.cpp - PDB Hash Functions --------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/PDB/Native/Hash.h"9#include "llvm/ADT/ArrayRef.h"10#include "llvm/Support/CRC.h"11#include "llvm/Support/Endian.h"12#include <cstdint>1314using namespace llvm;15using namespace llvm::support;1617// Corresponds to `Hasher::lhashPbCb` in PDB/include/misc.h.18// Used for name hash table and TPI/IPI hashes.19uint32_t pdb::hashStringV1(StringRef Str) {20uint32_t Result = 0;21uint32_t Size = Str.size();2223ArrayRef<ulittle32_t> Longs(reinterpret_cast<const ulittle32_t *>(Str.data()),24Size / 4);2526for (auto Value : Longs)27Result ^= Value;2829const uint8_t *Remainder = reinterpret_cast<const uint8_t *>(Longs.end());30uint32_t RemainderSize = Size % 4;3132// Maximum of 3 bytes left. Hash a 2 byte word if possible, then hash the33// possibly remaining 1 byte.34if (RemainderSize >= 2) {35uint16_t Value = *reinterpret_cast<const ulittle16_t *>(Remainder);36Result ^= static_cast<uint32_t>(Value);37Remainder += 2;38RemainderSize -= 2;39}4041// hash possible odd byte42if (RemainderSize == 1) {43Result ^= *(Remainder++);44}4546const uint32_t toLowerMask = 0x20202020;47Result |= toLowerMask;48Result ^= (Result >> 11);4950return Result ^ (Result >> 16);51}5253// Corresponds to `HasherV2::HashULONG` in PDB/include/misc.h.54// Used for name hash table.55uint32_t pdb::hashStringV2(StringRef Str) {56uint32_t Hash = 0xb170a1bf;5758ArrayRef<char> Buffer(Str.begin(), Str.end());5960ArrayRef<ulittle32_t> Items(61reinterpret_cast<const ulittle32_t *>(Buffer.data()),62Buffer.size() / sizeof(ulittle32_t));63for (ulittle32_t Item : Items) {64Hash += Item;65Hash += (Hash << 10);66Hash ^= (Hash >> 6);67}68Buffer = Buffer.slice(Items.size() * sizeof(ulittle32_t));69for (uint8_t Item : Buffer) {70Hash += Item;71Hash += (Hash << 10);72Hash ^= (Hash >> 6);73}7475return Hash * 1664525U + 1013904223U;76}7778// Corresponds to `SigForPbCb` in langapi/shared/crc32.h.79uint32_t pdb::hashBufferV8(ArrayRef<uint8_t> Buf) {80JamCRC JC(/*Init=*/0U);81JC.update(Buf);82return JC.getCRC();83}848586