xref: /xnu-12377.41.6/libkern/os/hash.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2018 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions #ifndef _OS_HASH_H_
30*bbb1b6f9SApple OSS Distributions #define _OS_HASH_H_
31*bbb1b6f9SApple OSS Distributions #if PRIVATE
32*bbb1b6f9SApple OSS Distributions 
33*bbb1b6f9SApple OSS Distributions #include <os/base.h>
34*bbb1b6f9SApple OSS Distributions 
35*bbb1b6f9SApple OSS Distributions __BEGIN_DECLS
36*bbb1b6f9SApple OSS Distributions 
37*bbb1b6f9SApple OSS Distributions static inline uint32_t
os_hash_jenkins_update(const void * data,size_t length,uint32_t hash)38*bbb1b6f9SApple OSS Distributions os_hash_jenkins_update(const void *data, size_t length, uint32_t hash)
39*bbb1b6f9SApple OSS Distributions {
40*bbb1b6f9SApple OSS Distributions 	const uint8_t *key = (const uint8_t *)data;
41*bbb1b6f9SApple OSS Distributions 
42*bbb1b6f9SApple OSS Distributions 	for (size_t i = 0; i < length; i++) {
43*bbb1b6f9SApple OSS Distributions 		hash += key[i];
44*bbb1b6f9SApple OSS Distributions 		hash += (hash << 10);
45*bbb1b6f9SApple OSS Distributions 		hash ^= (hash >> 6);
46*bbb1b6f9SApple OSS Distributions 	}
47*bbb1b6f9SApple OSS Distributions 
48*bbb1b6f9SApple OSS Distributions 	return hash;
49*bbb1b6f9SApple OSS Distributions }
50*bbb1b6f9SApple OSS Distributions 
51*bbb1b6f9SApple OSS Distributions static inline uint32_t
os_hash_jenkins_finish(uint32_t hash)52*bbb1b6f9SApple OSS Distributions os_hash_jenkins_finish(uint32_t hash)
53*bbb1b6f9SApple OSS Distributions {
54*bbb1b6f9SApple OSS Distributions 	hash += (hash << 3);
55*bbb1b6f9SApple OSS Distributions 	hash ^= (hash >> 11);
56*bbb1b6f9SApple OSS Distributions 	hash += (hash << 15);
57*bbb1b6f9SApple OSS Distributions 
58*bbb1b6f9SApple OSS Distributions 	return hash;
59*bbb1b6f9SApple OSS Distributions }
60*bbb1b6f9SApple OSS Distributions 
61*bbb1b6f9SApple OSS Distributions /*!
62*bbb1b6f9SApple OSS Distributions  * @function os_hash_jenkins
63*bbb1b6f9SApple OSS Distributions  *
64*bbb1b6f9SApple OSS Distributions  * @brief
65*bbb1b6f9SApple OSS Distributions  * The original Jenkins "one at a time" hash.
66*bbb1b6f9SApple OSS Distributions  *
67*bbb1b6f9SApple OSS Distributions  * @discussion
68*bbb1b6f9SApple OSS Distributions  * TBD: There may be some value to unrolling here,
69*bbb1b6f9SApple OSS Distributions  * depending on the architecture.
70*bbb1b6f9SApple OSS Distributions  *
71*bbb1b6f9SApple OSS Distributions  * @param data
72*bbb1b6f9SApple OSS Distributions  * The address of the data to hash.
73*bbb1b6f9SApple OSS Distributions  *
74*bbb1b6f9SApple OSS Distributions  * @param length
75*bbb1b6f9SApple OSS Distributions  * The length of the data to hash
76*bbb1b6f9SApple OSS Distributions  *
77*bbb1b6f9SApple OSS Distributions  * @param seed
78*bbb1b6f9SApple OSS Distributions  * An optional hash seed (defaults to 0).
79*bbb1b6f9SApple OSS Distributions  *
80*bbb1b6f9SApple OSS Distributions  * @returns
81*bbb1b6f9SApple OSS Distributions  * The jenkins hash for this data.
82*bbb1b6f9SApple OSS Distributions  */
83*bbb1b6f9SApple OSS Distributions __attribute__((overloadable))
84*bbb1b6f9SApple OSS Distributions static inline uint32_t
os_hash_jenkins(const void * data,size_t length,uint32_t seed)85*bbb1b6f9SApple OSS Distributions os_hash_jenkins(const void *data, size_t length, uint32_t seed)
86*bbb1b6f9SApple OSS Distributions {
87*bbb1b6f9SApple OSS Distributions 	return os_hash_jenkins_finish(os_hash_jenkins_update(data, length, seed));
88*bbb1b6f9SApple OSS Distributions }
89*bbb1b6f9SApple OSS Distributions 
90*bbb1b6f9SApple OSS Distributions __attribute__((overloadable))
91*bbb1b6f9SApple OSS Distributions static inline uint32_t
os_hash_jenkins(const void * data,size_t length)92*bbb1b6f9SApple OSS Distributions os_hash_jenkins(const void *data, size_t length)
93*bbb1b6f9SApple OSS Distributions {
94*bbb1b6f9SApple OSS Distributions 	return os_hash_jenkins(data, length, 0);
95*bbb1b6f9SApple OSS Distributions }
96*bbb1b6f9SApple OSS Distributions 
97*bbb1b6f9SApple OSS Distributions /*!
98*bbb1b6f9SApple OSS Distributions  * @function os_hash_kernel_pointer
99*bbb1b6f9SApple OSS Distributions  *
100*bbb1b6f9SApple OSS Distributions  * @brief
101*bbb1b6f9SApple OSS Distributions  * Hashes a pointer from a zone.
102*bbb1b6f9SApple OSS Distributions  *
103*bbb1b6f9SApple OSS Distributions  * @discussion
104*bbb1b6f9SApple OSS Distributions  * This is a really cheap and fast hash that will behave well for pointers
105*bbb1b6f9SApple OSS Distributions  * allocated by the kernel.
106*bbb1b6f9SApple OSS Distributions  *
107*bbb1b6f9SApple OSS Distributions  * This should be not used for untrusted pointer values from userspace,
108*bbb1b6f9SApple OSS Distributions  * or cases when the pointer is somehow under the control of userspace.
109*bbb1b6f9SApple OSS Distributions  *
110*bbb1b6f9SApple OSS Distributions  * This hash function utilizes knowledge about the span of the kernel
111*bbb1b6f9SApple OSS Distributions  * address space and inherent alignment of zalloc/kalloc.
112*bbb1b6f9SApple OSS Distributions  *
113*bbb1b6f9SApple OSS Distributions  * @param pointer
114*bbb1b6f9SApple OSS Distributions  * The pointer to hash.
115*bbb1b6f9SApple OSS Distributions  *
116*bbb1b6f9SApple OSS Distributions  * @returns
117*bbb1b6f9SApple OSS Distributions  * The hash for this pointer.
118*bbb1b6f9SApple OSS Distributions  */
119*bbb1b6f9SApple OSS Distributions static inline uint32_t
os_hash_kernel_pointer(const void * pointer)120*bbb1b6f9SApple OSS Distributions os_hash_kernel_pointer(const void *pointer)
121*bbb1b6f9SApple OSS Distributions {
122*bbb1b6f9SApple OSS Distributions 	uintptr_t key = (uintptr_t)((intptr_t)pointer << 16) >> 20;
123*bbb1b6f9SApple OSS Distributions 	key *= 0x5052acdb;
124*bbb1b6f9SApple OSS Distributions 	return (uint32_t)key ^ __builtin_bswap32((uint32_t)key);
125*bbb1b6f9SApple OSS Distributions }
126*bbb1b6f9SApple OSS Distributions 
127*bbb1b6f9SApple OSS Distributions /*!
128*bbb1b6f9SApple OSS Distributions  * @function os_hash_uint64
129*bbb1b6f9SApple OSS Distributions  *
130*bbb1b6f9SApple OSS Distributions  * @brief
131*bbb1b6f9SApple OSS Distributions  * Hashes a 64 bit number.
132*bbb1b6f9SApple OSS Distributions  *
133*bbb1b6f9SApple OSS Distributions  * @discussion
134*bbb1b6f9SApple OSS Distributions  * This is a really cheap and fast mixer.
135*bbb1b6f9SApple OSS Distributions  *
136*bbb1b6f9SApple OSS Distributions  * This should be not used for untrusted values from userspace,
137*bbb1b6f9SApple OSS Distributions  * or cases when the pointer is somehow under the control of userspace.
138*bbb1b6f9SApple OSS Distributions  *
139*bbb1b6f9SApple OSS Distributions  * See https://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html
140*bbb1b6f9SApple OSS Distributions  *
141*bbb1b6f9SApple OSS Distributions  * @param u64
142*bbb1b6f9SApple OSS Distributions  * The value to hash
143*bbb1b6f9SApple OSS Distributions  *
144*bbb1b6f9SApple OSS Distributions  * @returns
145*bbb1b6f9SApple OSS Distributions  * The hash for this integer.
146*bbb1b6f9SApple OSS Distributions  */
147*bbb1b6f9SApple OSS Distributions static inline uint32_t
os_hash_uint64(uint64_t u64)148*bbb1b6f9SApple OSS Distributions os_hash_uint64(uint64_t u64)
149*bbb1b6f9SApple OSS Distributions {
150*bbb1b6f9SApple OSS Distributions 	u64 ^= (u64 >> 31);
151*bbb1b6f9SApple OSS Distributions 	u64 *= 0x7fb5d329728ea185ull;
152*bbb1b6f9SApple OSS Distributions 	u64 ^= (u64 >> 27);
153*bbb1b6f9SApple OSS Distributions 	u64 *= 0x81dadef4bc2dd44dull;
154*bbb1b6f9SApple OSS Distributions 	u64 ^= (u64 >> 33);
155*bbb1b6f9SApple OSS Distributions 
156*bbb1b6f9SApple OSS Distributions 	return (uint32_t)u64;
157*bbb1b6f9SApple OSS Distributions }
158*bbb1b6f9SApple OSS Distributions 
159*bbb1b6f9SApple OSS Distributions __END_DECLS
160*bbb1b6f9SApple OSS Distributions 
161*bbb1b6f9SApple OSS Distributions #endif // PRIVATE
162*bbb1b6f9SApple OSS Distributions #endif // _OS_HASH_H_
163