mfbt/HashFunctions.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/HashFunctions.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,38 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/* Implementations of hash functions. */
    1.10 +
    1.11 +#include "mozilla/HashFunctions.h"
    1.12 +#include "mozilla/Types.h"
    1.13 +
    1.14 +#include <string.h>
    1.15 +
    1.16 +namespace mozilla {
    1.17 +
    1.18 +uint32_t
    1.19 +HashBytes(const void* bytes, size_t length)
    1.20 +{
    1.21 +  uint32_t hash = 0;
    1.22 +  const char* b = reinterpret_cast<const char*>(bytes);
    1.23 +
    1.24 +  /* Walk word by word. */
    1.25 +  size_t i = 0;
    1.26 +  for (; i < length - (length % sizeof(size_t)); i += sizeof(size_t)) {
    1.27 +    /* Do an explicitly unaligned load of the data. */
    1.28 +    size_t data;
    1.29 +    memcpy(&data, b + i, sizeof(size_t));
    1.30 +
    1.31 +    hash = AddToHash(hash, data, sizeof(data));
    1.32 +  }
    1.33 +
    1.34 +  /* Get the remaining bytes. */
    1.35 +  for (; i < length; i++)
    1.36 +    hash = AddToHash(hash, b[i]);
    1.37 +
    1.38 +  return hash;
    1.39 +}
    1.40 +
    1.41 +} /* namespace mozilla */

mercurial