1*f9e90c24SHans Petter Selasky /*
2*f9e90c24SHans Petter Selasky * SPDX-License-Identifier: BSD-2-Clause
3*f9e90c24SHans Petter Selasky *
4*f9e90c24SHans Petter Selasky * Copyright (c) 2022 NVIDIA corporation & affiliates.
5*f9e90c24SHans Petter Selasky * Copyright (c) 2013 François Tigeot
6*f9e90c24SHans Petter Selasky *
7*f9e90c24SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
8*f9e90c24SHans Petter Selasky * modification, are permitted provided that the following conditions
9*f9e90c24SHans Petter Selasky * are met:
10*f9e90c24SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
11*f9e90c24SHans Petter Selasky * notice unmodified, this list of conditions, and the following
12*f9e90c24SHans Petter Selasky * disclaimer.
13*f9e90c24SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
14*f9e90c24SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
15*f9e90c24SHans Petter Selasky * documentation and/or other materials provided with the distribution.
16*f9e90c24SHans Petter Selasky *
17*f9e90c24SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*f9e90c24SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*f9e90c24SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*f9e90c24SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*f9e90c24SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*f9e90c24SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*f9e90c24SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*f9e90c24SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*f9e90c24SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*f9e90c24SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*f9e90c24SHans Petter Selasky */
28*f9e90c24SHans Petter Selasky
29*f9e90c24SHans Petter Selasky #ifndef _LINUXKPI_LINUX_HASH_H_
30*f9e90c24SHans Petter Selasky #define _LINUXKPI_LINUX_HASH_H_
31*f9e90c24SHans Petter Selasky
32*f9e90c24SHans Petter Selasky #include <sys/hash.h>
33*f9e90c24SHans Petter Selasky #include <sys/param.h>
34*f9e90c24SHans Petter Selasky #include <sys/systm.h>
35*f9e90c24SHans Petter Selasky
36*f9e90c24SHans Petter Selasky #include <asm/types.h>
37*f9e90c24SHans Petter Selasky
38*f9e90c24SHans Petter Selasky #include <linux/bitops.h>
39*f9e90c24SHans Petter Selasky
40*f9e90c24SHans Petter Selasky static inline u64
hash_64(u64 val,u8 bits)41*f9e90c24SHans Petter Selasky hash_64(u64 val, u8 bits)
42*f9e90c24SHans Petter Selasky {
43*f9e90c24SHans Petter Selasky u64 ret;
44*f9e90c24SHans Petter Selasky u8 x;
45*f9e90c24SHans Petter Selasky
46*f9e90c24SHans Petter Selasky ret = bits;
47*f9e90c24SHans Petter Selasky
48*f9e90c24SHans Petter Selasky for (x = 0; x != sizeof(ret); x++) {
49*f9e90c24SHans Petter Selasky u64 chunk = (val >> (8 * x)) & 0xFF;
50*f9e90c24SHans Petter Selasky ret = HASHSTEP(ret, chunk);
51*f9e90c24SHans Petter Selasky }
52*f9e90c24SHans Petter Selasky return (ret >> (64 - bits));
53*f9e90c24SHans Petter Selasky }
54*f9e90c24SHans Petter Selasky
55*f9e90c24SHans Petter Selasky static inline u32
hash_32(u32 val,u8 bits)56*f9e90c24SHans Petter Selasky hash_32(u32 val, u8 bits)
57*f9e90c24SHans Petter Selasky {
58*f9e90c24SHans Petter Selasky u32 ret;
59*f9e90c24SHans Petter Selasky u8 x;
60*f9e90c24SHans Petter Selasky
61*f9e90c24SHans Petter Selasky ret = bits;
62*f9e90c24SHans Petter Selasky
63*f9e90c24SHans Petter Selasky for (x = 0; x != sizeof(ret); x++) {
64*f9e90c24SHans Petter Selasky u32 chunk = (val >> (8 * x)) & 0xFF;
65*f9e90c24SHans Petter Selasky ret = HASHSTEP(ret, chunk);
66*f9e90c24SHans Petter Selasky }
67*f9e90c24SHans Petter Selasky return (ret >> (32 - bits));
68*f9e90c24SHans Petter Selasky }
69*f9e90c24SHans Petter Selasky
70*f9e90c24SHans Petter Selasky #if BITS_PER_LONG == 64
71*f9e90c24SHans Petter Selasky #define hash_long(...) hash_64(__VA_ARGS__)
72*f9e90c24SHans Petter Selasky #else
73*f9e90c24SHans Petter Selasky #define hash_long(...) hash_32(__VA_ARGS__)
74*f9e90c24SHans Petter Selasky #endif
75*f9e90c24SHans Petter Selasky
76*f9e90c24SHans Petter Selasky #endif /* _LINUXKPI_LINUX_HASH_H_ */
77