blob: 21fcdd7e7f88fb877b1a888a3c3f5f2352597160 [file] [log] [blame]
Talin1a4b19e2012-02-18 21:00:49 +00001//===-- 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;
Ahmed Charles50732992012-02-18 22:56:41 +000017 if ((uintptr_t(I) & 3) == 0) {
Talin1a4b19e2012-02-18 21:00:49 +000018 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}