Hashing.cpp revision 1a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52
1//===-- llvm/ADT/Hashing.cpp - Utilities for hashing ------------*- 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#include "llvm/ADT/Hashing.h"
11
12namespace llvm {
13
14// Add a possibly unaligned sequence of bytes.
15void GeneralHash::addUnaligned(const uint8_t *I, const uint8_t *E) {
16  ptrdiff_t Length = E - I;
17  if (uintptr_t(I) & 3 == 0) {
18    while (Length > 3) {
19      mix(*reinterpret_cast<const uint32_t *>(I));
20      I += 4;
21      Length -= 4;
22    }
23  } else {
24    while (Length > 3) {
25      mix(
26        uint32_t(I[0]) +
27        (uint32_t(I[1]) << 8) +
28        (uint32_t(I[2]) << 16) +
29        (uint32_t(I[3]) << 24));
30      I += 4;
31      Length -= 4;
32    }
33  }
34
35  if (Length & 3) {
36    uint32_t Data = 0;
37    switch (Length & 3) {
38      case 3: Data |= uint32_t(I[2]) << 16;   // fall through
39      case 2: Data |= uint32_t(I[1]) << 8;    // fall through
40      case 1: Data |= uint32_t(I[0]); break;
41    }
42    mix(Data);
43  }
44}
45
46}
47