Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/module/zcommon/cityhash.c
48383 views
1
// SPDX-License-Identifier: MIT
2
//
3
// Copyright (c) 2011 Google, Inc.
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included in
13
// all copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
// THE SOFTWARE.
22
23
/*
24
* Copyright (c) 2017 by Delphix. All rights reserved.
25
*/
26
27
#include <cityhash.h>
28
29
#define HASH_K1 0xb492b66fbe98f273ULL
30
#define HASH_K2 0x9ae16a3b2f90404fULL
31
32
/*
33
* Bitwise right rotate. Normally this will compile to a single
34
* instruction.
35
*/
36
static inline uint64_t
37
rotate(uint64_t val, int shift)
38
{
39
// Avoid shifting by 64: doing so yields an undefined result.
40
return (shift == 0 ? val : (val >> shift) | (val << (64 - shift)));
41
}
42
43
static inline uint64_t
44
cityhash_helper(uint64_t u, uint64_t v, uint64_t mul)
45
{
46
uint64_t a = (u ^ v) * mul;
47
a ^= (a >> 47);
48
uint64_t b = (v ^ a) * mul;
49
b ^= (b >> 47);
50
b *= mul;
51
return (b);
52
}
53
54
static inline uint64_t
55
cityhash_impl(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
56
{
57
uint64_t mul = HASH_K2 + 64;
58
uint64_t a = w1 * HASH_K1;
59
uint64_t b = w2;
60
uint64_t c = w4 * mul;
61
uint64_t d = w3 * HASH_K2;
62
return (cityhash_helper(rotate(a + b, 43) + rotate(c, 30) + d,
63
a + rotate(b + HASH_K2, 18) + c, mul));
64
}
65
66
/*
67
* Passing w as the 2nd argument could save one 64-bit multiplication.
68
*/
69
uint64_t
70
cityhash1(uint64_t w)
71
{
72
return (cityhash_impl(0, w, 0, 0));
73
}
74
75
uint64_t
76
cityhash2(uint64_t w1, uint64_t w2)
77
{
78
return (cityhash_impl(w1, w2, 0, 0));
79
}
80
81
uint64_t
82
cityhash3(uint64_t w1, uint64_t w2, uint64_t w3)
83
{
84
return (cityhash_impl(w1, w2, w3, 0));
85
}
86
87
uint64_t
88
cityhash4(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
89
{
90
return (cityhash_impl(w1, w2, w3, w4));
91
}
92
93
#if defined(_KERNEL)
94
EXPORT_SYMBOL(cityhash1);
95
EXPORT_SYMBOL(cityhash2);
96
EXPORT_SYMBOL(cityhash3);
97
EXPORT_SYMBOL(cityhash4);
98
#endif
99
100