Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Tor Browser
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tommy Webb
Tor Browser
Commits
0d2f332e
Commit
0d2f332e
authored
13 years ago
by
Justin Lebar
Browse files
Options
Downloads
Patches
Plain Diff
Bug 729952 - Part 1: Add a better hash function to mfbt. r=waldo
parent
4dff658c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
mfbt/Attributes.h
+19
-0
19 additions, 0 deletions
mfbt/Attributes.h
mfbt/HashFunctions.h
+111
-0
111 additions, 0 deletions
mfbt/HashFunctions.h
mfbt/exported_headers.mk
+1
-0
1 addition, 0 deletions
mfbt/exported_headers.mk
with
131 additions
and
0 deletions
mfbt/Attributes.h
+
19
−
0
View file @
0d2f332e
...
...
@@ -320,6 +320,25 @@
# define MOZ_FINAL
/* no support */
#endif
/**
* MOZ_WARN_UNUSED_RESULT tells the compiler to emit a warning if a function's
* return value is not used by the caller.
*
* Place this attribute at the very beginning of a function definition. For
* example, write
*
* MOZ_WARN_UNUSED_RESULT int foo();
*
* or
*
* MOZ_WARN_UNUSED_RESULT int foo() { return 42; }
*/
#if defined(__GNUC__) || defined(__clang__)
# define MOZ_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
#else
# define MOZ_WARN_UNUSED_RESULT
#endif
#endif
/* __cplusplus */
#endif
/* mozilla_Attributes_h_ */
This diff is collapsed.
Click to expand it.
mfbt/HashFunctions.h
0 → 100644
+
111
−
0
View file @
0d2f332e
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sw=4 et tw=99 ft=cpp:
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Utilities for hashing */
#ifndef mozilla_HashFunctions_h_
#define mozilla_HashFunctions_h_
#include
"mozilla/Attributes.h"
#include
"mozilla/StandardInteger.h"
#ifdef __cplusplus
namespace
mozilla
{
/**
* The golden ratio as a 32-bit fixed-point value.
*/
static
const
uint32_t
GoldenRatioU32
=
0x9E3779B9U
;
inline
uint32_t
RotateLeft32
(
uint32_t
value
,
uint8_t
bits
)
{
MOZ_ASSERT
(
bits
<
32
);
return
(
value
<<
bits
)
|
(
value
>>
(
32
-
bits
));
}
/**
* Add the given value(s) to the given hashcode and return the new hashcode.
*
* AddToHash(h, x, y) is equivalent to AddToHash(AddToHash(h, x), y).
*/
MOZ_WARN_UNUSED_RESULT
inline
uint32_t
AddToHash
(
uint32_t
hash
,
uint32_t
value
)
{
/*
* This is not a sophisticated hash routine, but it seems to work well for our
* mostly plain-text inputs. Implementation notes follow.
*
* Our use of the golden ratio here is arbitrary; we could pick almost any
* number which:
*
* * is odd (because otherwise, all our hash values will be even)
*
* * has a reasonably-even mix of 1's and 0's (consider the extreme case
* where we multiply by 0x3 or 0xeffffff -- this will not produce good
* mixing across all bits of the hash).
*
* The rotation length of 5 is also arbitrary, although an odd number is again
* preferable so our hash explores the whole universe of possible rotations.
*
* Finally, we multiply by the golden ratio *after* xor'ing, not before.
* Otherwise, if |hash| is 0 (as it often is for the beginning of a message),
* the expression
*
* (GoldenRatioU32 * RotateLeft(hash, 5)) ^ value
*
* evaluates to |value|.
*
* (Number-theoretic aside: Because any odd number |m| is relatively prime to
* our modulus (2^32), the list
*
* [x * m (mod 2^32) for 0 <= x < 2^32]
*
* has no duplicate elements. This means that multiplying by |m| does not
* cause us to skip any possible hash values.
*
* It's also nice if |m| has larger order mod 2^32 -- that is, if the smallest
* k such that m^k == 1 (mod 2^32) is large -- so we can safely multiply our
* hash value by |m| a few times without negating the multiplicative effect.
* Our golden ratio constant has order 2^29, which is more than enough for our
* purposes.)
*/
return
GoldenRatioU32
*
(
RotateLeft32
(
hash
,
5
)
^
value
);
}
MOZ_WARN_UNUSED_RESULT
inline
uint32_t
AddToHash
(
uint32_t
hash
,
uint32_t
v1
,
uint32_t
v2
)
{
return
AddToHash
(
AddToHash
(
hash
,
v1
),
v2
);
}
MOZ_WARN_UNUSED_RESULT
inline
uint32_t
AddToHash
(
uint32_t
hash
,
uint32_t
v1
,
uint32_t
v2
,
uint32_t
v3
)
{
return
AddToHash
(
AddToHash
(
hash
,
v1
,
v2
),
v3
);
}
MOZ_WARN_UNUSED_RESULT
inline
uint32_t
AddToHash
(
uint32_t
hash
,
uint32_t
v1
,
uint32_t
v2
,
uint32_t
v3
,
uint32_t
v4
)
{
return
AddToHash
(
AddToHash
(
hash
,
v1
,
v2
,
v3
),
v4
);
}
MOZ_WARN_UNUSED_RESULT
inline
uint32_t
AddToHash
(
uint32_t
hash
,
uint32_t
v1
,
uint32_t
v2
,
uint32_t
v3
,
uint32_t
v4
,
uint32_t
v5
)
{
return
AddToHash
(
AddToHash
(
hash
,
v1
,
v2
,
v3
,
v4
),
v5
);
}
}
/* namespace mozilla */
#endif
/* __cplusplus */
#endif
/* mozilla_HashFunctions_h_ */
This diff is collapsed.
Click to expand it.
mfbt/exported_headers.mk
+
1
−
0
View file @
0d2f332e
...
...
@@ -46,6 +46,7 @@ EXPORTS_mozilla += \
Attributes.h
\
BloomFilter.h
\
GuardObjects.h
\
HashFunctions.h
\
Likely.h
\
LinkedList.h
\
MSStdInt.h
\
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment