LLVM  8.0.1
DJB.cpp
Go to the documentation of this file.
1 //===-- Support/DJB.cpp ---DJB Hash -----------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for the DJ Bernstein hash function.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/DJB.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Unicode.h"
19 
20 using namespace llvm;
21 
22 static UTF32 chopOneUTF32(StringRef &Buffer) {
23  UTF32 C;
24  const UTF8 *const Begin8Const =
25  reinterpret_cast<const UTF8 *>(Buffer.begin());
26  const UTF8 *Begin8 = Begin8Const;
27  UTF32 *Begin32 = &C;
28 
29  // In lenient mode we will always end up with a "reasonable" value in C for
30  // non-empty input.
31  assert(!Buffer.empty());
32  ConvertUTF8toUTF32(&Begin8, reinterpret_cast<const UTF8 *>(Buffer.end()),
33  &Begin32, &C + 1, lenientConversion);
34  Buffer = Buffer.drop_front(Begin8 - Begin8Const);
35  return C;
36 }
37 
39  const UTF32 *Begin32 = &C;
40  UTF8 *Begin8 = Storage.begin();
41 
42  // The case-folded output should always be a valid unicode character, so use
43  // strict mode here.
44  ConversionResult CR = ConvertUTF32toUTF8(&Begin32, &C + 1, &Begin8,
45  Storage.end(), strictConversion);
46  assert(CR == conversionOK && "Case folding produced invalid char?");
47  (void)CR;
48  return StringRef(reinterpret_cast<char *>(Storage.begin()),
49  Begin8 - Storage.begin());
50 }
51 
53  // DWARF v5 addition to the unicode folding rules.
54  // Fold "Latin Small Letter Dotless I" and "Latin Capital Letter I With Dot
55  // Above" into "i".
56  if (C == 0x130 || C == 0x131)
57  return 'i';
59 }
60 
62  UTF32 C = chopOneUTF32(Buffer);
63 
64  C = foldCharDwarf(C);
65 
66  std::array<UTF8, UNI_MAX_UTF8_BYTES_PER_CODE_POINT> Storage;
67  StringRef Folded = toUTF8(C, Storage);
68  return djbHash(Folded, H);
69 }
70 
72  while (!Buffer.empty()) {
73  unsigned char C = Buffer.front();
74  if (LLVM_LIKELY(C <= 0x7f)) {
75  // US-ASCII, encoded as one character in utf-8.
76  // This is by far the most common case, so handle this specially.
77  if (C >= 'A' && C <= 'Z')
78  C = 'a' + (C - 'A'); // fold uppercase into lowercase
79  H = (H << 5) + H + C;
80  Buffer = Buffer.drop_front();
81  continue;
82  }
83  H = caseFoldingDjbHashCharSlow(Buffer, H);
84  }
85  return H;
86 }
unsigned int UTF32
Definition: ConvertUTF.h:110
uint64_t CallInst * C
static UTF32 foldCharDwarf(UTF32 C)
Definition: DJB.cpp:52
static uint32_t caseFoldingDjbHashCharSlow(StringRef &Buffer, uint32_t H)
Definition: DJB.cpp:61
This class represents lattice values for constants.
Definition: AllocatorList.h:24
ConversionResult
Definition: ConvertUTF.h:127
ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
Definition: ConvertUTF.cpp:711
unsigned char UTF8
Definition: ConvertUTF.h:112
static UTF32 chopOneUTF32(StringRef &Buffer)
Definition: DJB.cpp:22
int foldCharSimple(int C)
Fold input unicode character according the Simple unicode case folding rules.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:318
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
#define H(x, y, z)
Definition: MD5.cpp:57
static StringRef toUTF8(UTF32 C, MutableArrayRef< UTF8 > Storage)
Definition: DJB.cpp:38
uint32_t djbHash(StringRef Buffer, uint32_t H=5381)
The Bernstein hash function used by the DWARF accelerator tables.
Definition: DJB.h:22
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the first N elements dropped.
Definition: StringRef.h:645
uint32_t caseFoldingDjbHash(StringRef Buffer, uint32_t H=5381)
Computes the Bernstein hash after folding the input according to the Dwarf 5 standard case folding ru...
Definition: DJB.cpp:71
iterator begin() const
Definition: ArrayRef.h:331
iterator begin() const
Definition: StringRef.h:106
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:142
iterator end() const
Definition: ArrayRef.h:332
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:191
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
iterator end() const
Definition: StringRef.h:108