source: mainline/kernel/generic/include/adt/hash.h@ 82cbf8c6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 82cbf8c6 was 82cbf8c6, checked in by Jakub Jermar <jakub@…>, 8 years ago

Replace the old hash table implementation in the kernel with the newer one

This replaces the original hash table implementation with the resizable one
already used in uspace. Along the way, the IRQ hash table code was streamlined
and cleaned up.

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * Copyright (c) 2012 Adam Hraska
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericadt
30 * @{
31 */
32/** @file
33 */
34#ifndef KERN_HASH_H_
35#define KERN_HASH_H_
36
37#include <stdint.h>
38
39/** Produces a uniform hash affecting all output bits from the skewed input. */
40static inline uint32_t hash_mix32(uint32_t hash)
41{
42 /*
43 * Thomas Wang's modification of Bob Jenkin's hash mixing function:
44 * http://www.concentric.net/~Ttwang/tech/inthash.htm
45 * Public domain.
46 */
47 hash = ~hash + (hash << 15);
48 hash = hash ^ (hash >> 12);
49 hash = hash + (hash << 2);
50 hash = hash ^ (hash >> 4);
51 hash = hash * 2057;
52 hash = hash ^ (hash >> 16);
53 return hash;
54}
55
56/** Produces a uniform hash affecting all output bits from the skewed input. */
57static inline uint64_t hash_mix64(uint64_t hash)
58{
59 /*
60 * Thomas Wang's public domain 64-bit hash mixing function:
61 * http://www.concentric.net/~Ttwang/tech/inthash.htm
62 */
63 hash = (hash ^ 61) ^ (hash >> 16);
64 hash = hash + (hash << 3);
65 hash = hash ^ (hash >> 4);
66 hash = hash * 0x27d4eb2d;
67 hash = hash ^ (hash >> 15);
68 /*
69 * Lower order bits are mixed more thoroughly. Swap them with
70 * the higher order bits and make the resulting higher order bits
71 * more usable.
72 */
73 return (hash << 32) | (hash >> 32);
74}
75
76/** Produces a uniform hash affecting all output bits from the skewed input. */
77static inline size_t hash_mix(size_t hash)
78{
79#ifdef __32_BITS__
80 return hash_mix32(hash);
81#elif defined(__64_BITS__)
82 return hash_mix64(hash);
83#else
84#error Unknown size_t size - cannot select proper hash mix function.
85#endif
86}
87
88/** Use to create a hash from multiple values.
89 *
90 * Typical usage:
91 * @code
92 * int car_id;
93 * bool car_convertible;
94 * // ..
95 * size_t hash = 0;
96 * hash = hash_combine(hash, car_id);
97 * hash = hash_combine(hash, car_convertible);
98 * // Now use hash as a hash of both car_id and car_convertible.
99 * @endcode
100 */
101static inline size_t hash_combine(size_t seed, size_t hash)
102{
103 /*
104 * todo: use Bob Jenkin's proper mixing hash pass:
105 * http://burtleburtle.net/bob/c/lookup3.c
106 */
107 seed ^= hash + 0x9e3779b9
108 + ((seed << 5) | (seed >> (sizeof(size_t) * 8 - 5)));
109 return seed;
110}
111
112#endif
Note: See TracBrowser for help on using the repository browser.