/src/mozilla-central/mfbt/HashFunctions.cpp
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /* Implementations of hash functions. */ |
8 | | |
9 | | #include "mozilla/HashFunctions.h" |
10 | | #include "mozilla/Types.h" |
11 | | |
12 | | #include <string.h> |
13 | | |
14 | | namespace mozilla { |
15 | | |
16 | | uint32_t |
17 | | HashBytes(const void* aBytes, size_t aLength) |
18 | 1.62M | { |
19 | 1.62M | uint32_t hash = 0; |
20 | 1.62M | const char* b = reinterpret_cast<const char*>(aBytes); |
21 | 1.62M | |
22 | 1.62M | /* Walk word by word. */ |
23 | 1.62M | size_t i = 0; |
24 | 4.95M | for (; i < aLength - (aLength % sizeof(size_t)); i += sizeof(size_t)) { |
25 | 3.32M | /* Do an explicitly unaligned load of the data. */ |
26 | 3.32M | size_t data; |
27 | 3.32M | memcpy(&data, b + i, sizeof(size_t)); |
28 | 3.32M | |
29 | 3.32M | hash = AddToHash(hash, data, sizeof(data)); |
30 | 3.32M | } |
31 | 1.62M | |
32 | 1.62M | /* Get the remaining bytes. */ |
33 | 1.63M | for (; i < aLength; i++) { |
34 | 6.01k | hash = AddToHash(hash, b[i]); |
35 | 6.01k | } |
36 | 1.62M | return hash; |
37 | 1.62M | } |
38 | | |
39 | | } /* namespace mozilla */ |