xref: /llvm-project/llvm/lib/DebugInfo/PDB/Native/Hash.cpp (revision 1e1e3ba2526ecab1ad3c18c97abef36df00b3813)
16b6b8c4fSAdrian McCarthy //===- Hash.cpp - PDB Hash Functions --------------------------------------===//
26b6b8c4fSAdrian McCarthy //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66b6b8c4fSAdrian McCarthy //
76b6b8c4fSAdrian McCarthy //===----------------------------------------------------------------------===//
86b6b8c4fSAdrian McCarthy 
96b6b8c4fSAdrian McCarthy #include "llvm/DebugInfo/PDB/Native/Hash.h"
106b6b8c4fSAdrian McCarthy #include "llvm/ADT/ArrayRef.h"
11*1e1e3ba2SHans Wennborg #include "llvm/Support/CRC.h"
126b6b8c4fSAdrian McCarthy #include "llvm/Support/Endian.h"
134fcfc199SEugene Zelenko #include <cstdint>
146b6b8c4fSAdrian McCarthy 
156b6b8c4fSAdrian McCarthy using namespace llvm;
166b6b8c4fSAdrian McCarthy using namespace llvm::support;
176b6b8c4fSAdrian McCarthy 
186b6b8c4fSAdrian McCarthy // Corresponds to `Hasher::lhashPbCb` in PDB/include/misc.h.
196b6b8c4fSAdrian McCarthy // Used for name hash table and TPI/IPI hashes.
hashStringV1(StringRef Str)206b6b8c4fSAdrian McCarthy uint32_t pdb::hashStringV1(StringRef Str) {
216b6b8c4fSAdrian McCarthy   uint32_t Result = 0;
226b6b8c4fSAdrian McCarthy   uint32_t Size = Str.size();
236b6b8c4fSAdrian McCarthy 
246b6b8c4fSAdrian McCarthy   ArrayRef<ulittle32_t> Longs(reinterpret_cast<const ulittle32_t *>(Str.data()),
256b6b8c4fSAdrian McCarthy                               Size / 4);
266b6b8c4fSAdrian McCarthy 
276b6b8c4fSAdrian McCarthy   for (auto Value : Longs)
286b6b8c4fSAdrian McCarthy     Result ^= Value;
296b6b8c4fSAdrian McCarthy 
306b6b8c4fSAdrian McCarthy   const uint8_t *Remainder = reinterpret_cast<const uint8_t *>(Longs.end());
316b6b8c4fSAdrian McCarthy   uint32_t RemainderSize = Size % 4;
326b6b8c4fSAdrian McCarthy 
336b6b8c4fSAdrian McCarthy   // Maximum of 3 bytes left.  Hash a 2 byte word if possible, then hash the
346b6b8c4fSAdrian McCarthy   // possibly remaining 1 byte.
356b6b8c4fSAdrian McCarthy   if (RemainderSize >= 2) {
366b6b8c4fSAdrian McCarthy     uint16_t Value = *reinterpret_cast<const ulittle16_t *>(Remainder);
376b6b8c4fSAdrian McCarthy     Result ^= static_cast<uint32_t>(Value);
386b6b8c4fSAdrian McCarthy     Remainder += 2;
396b6b8c4fSAdrian McCarthy     RemainderSize -= 2;
406b6b8c4fSAdrian McCarthy   }
416b6b8c4fSAdrian McCarthy 
426b6b8c4fSAdrian McCarthy   // hash possible odd byte
436b6b8c4fSAdrian McCarthy   if (RemainderSize == 1) {
446b6b8c4fSAdrian McCarthy     Result ^= *(Remainder++);
456b6b8c4fSAdrian McCarthy   }
466b6b8c4fSAdrian McCarthy 
476b6b8c4fSAdrian McCarthy   const uint32_t toLowerMask = 0x20202020;
486b6b8c4fSAdrian McCarthy   Result |= toLowerMask;
496b6b8c4fSAdrian McCarthy   Result ^= (Result >> 11);
506b6b8c4fSAdrian McCarthy 
516b6b8c4fSAdrian McCarthy   return Result ^ (Result >> 16);
526b6b8c4fSAdrian McCarthy }
536b6b8c4fSAdrian McCarthy 
546b6b8c4fSAdrian McCarthy // Corresponds to `HasherV2::HashULONG` in PDB/include/misc.h.
556b6b8c4fSAdrian McCarthy // Used for name hash table.
hashStringV2(StringRef Str)566b6b8c4fSAdrian McCarthy uint32_t pdb::hashStringV2(StringRef Str) {
576b6b8c4fSAdrian McCarthy   uint32_t Hash = 0xb170a1bf;
586b6b8c4fSAdrian McCarthy 
596b6b8c4fSAdrian McCarthy   ArrayRef<char> Buffer(Str.begin(), Str.end());
606b6b8c4fSAdrian McCarthy 
616b6b8c4fSAdrian McCarthy   ArrayRef<ulittle32_t> Items(
626b6b8c4fSAdrian McCarthy       reinterpret_cast<const ulittle32_t *>(Buffer.data()),
636b6b8c4fSAdrian McCarthy       Buffer.size() / sizeof(ulittle32_t));
646b6b8c4fSAdrian McCarthy   for (ulittle32_t Item : Items) {
656b6b8c4fSAdrian McCarthy     Hash += Item;
666b6b8c4fSAdrian McCarthy     Hash += (Hash << 10);
676b6b8c4fSAdrian McCarthy     Hash ^= (Hash >> 6);
686b6b8c4fSAdrian McCarthy   }
696b6b8c4fSAdrian McCarthy   Buffer = Buffer.slice(Items.size() * sizeof(ulittle32_t));
706b6b8c4fSAdrian McCarthy   for (uint8_t Item : Buffer) {
716b6b8c4fSAdrian McCarthy     Hash += Item;
726b6b8c4fSAdrian McCarthy     Hash += (Hash << 10);
736b6b8c4fSAdrian McCarthy     Hash ^= (Hash >> 6);
746b6b8c4fSAdrian McCarthy   }
756b6b8c4fSAdrian McCarthy 
766b6b8c4fSAdrian McCarthy   return Hash * 1664525U + 1013904223U;
776b6b8c4fSAdrian McCarthy }
786b6b8c4fSAdrian McCarthy 
796b6b8c4fSAdrian McCarthy // Corresponds to `SigForPbCb` in langapi/shared/crc32.h.
hashBufferV8(ArrayRef<uint8_t> Buf)806b6b8c4fSAdrian McCarthy uint32_t pdb::hashBufferV8(ArrayRef<uint8_t> Buf) {
816b6b8c4fSAdrian McCarthy   JamCRC JC(/*Init=*/0U);
82*1e1e3ba2SHans Wennborg   JC.update(Buf);
836b6b8c4fSAdrian McCarthy   return JC.getCRC();
846b6b8c4fSAdrian McCarthy }
85