ea1e21bf1440d0ce5a00aa74ea8a6aa58a87c1d6
[vpp.git] / vppinfra / vppinfra / xxhash.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16   Original license for the code used to construct
17   clib_xxhash(...).
18
19   xxHash - Fast Hash algorithm
20   Copyright (C) 2012-2014, Yann Collet.
21   BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
22
23   Redistribution and use in source and binary forms, with or without
24   modification, are permitted provided that the following conditions are
25   met:
26
27   * Redistributions of source code must retain the above copyright
28   notice, this list of conditions and the following disclaimer.
29   * Redistributions in binary form must reproduce the above
30   copyright notice, this list of conditions and the following disclaimer
31   in the documentation and/or other materials provided with the
32   distribution.
33
34   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 */
46
47 #ifndef __included_xxhash_h__
48 #define __included_xxhash_h__
49
50 #define PRIME64_1 11400714785074694791ULL
51 #define PRIME64_2 14029467366897019727ULL
52 #define PRIME64_3  1609587929392839161ULL
53 #define PRIME64_4  9650029242287828579ULL
54 #define PRIME64_5  2870177450012600261ULL
55 #define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
56
57 static inline u64
58 clib_xxhash (u64 key)
59 {
60   u64 k1, h64;
61
62   k1 = key;
63   h64 = 0x9e3779b97f4a7c13LL + PRIME64_5 + 8;
64   k1 *= PRIME64_2;
65   k1 = XXH_rotl64 (k1, 31);
66   k1 *= PRIME64_1;
67   h64 ^= k1;
68   h64 = XXH_rotl64 (h64, 27) * PRIME64_1 + PRIME64_4;
69
70   h64 ^= h64 >> 33;
71   h64 *= PRIME64_2;
72   h64 ^= h64 >> 29;
73   h64 *= PRIME64_3;
74   h64 ^= h64 >> 32;
75   return h64;
76 }
77
78 #endif /* __included_xxhash_h__ */
79
80 /*
81  * fd.io coding-style-patch-verification: ON
82  *
83  * Local Variables:
84  * eval: (c-set-style "gnu")
85  * End:
86  */