xref: /openbmc/linux/include/linux/stringhash.h (revision 19b9ad67)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2f4bcbe79SGeorge Spelvin #ifndef __LINUX_STRINGHASH_H
3f4bcbe79SGeorge Spelvin #define __LINUX_STRINGHASH_H
4f4bcbe79SGeorge Spelvin 
5fcfd2fbfSGeorge Spelvin #include <linux/compiler.h>	/* For __pure */
6fcfd2fbfSGeorge Spelvin #include <linux/types.h>	/* For u32, u64 */
7703b5fafSGeorge Spelvin #include <linux/hash.h>
8f4bcbe79SGeorge Spelvin 
9f4bcbe79SGeorge Spelvin /*
10f4bcbe79SGeorge Spelvin  * Routines for hashing strings of bytes to a 32-bit hash value.
11f4bcbe79SGeorge Spelvin  *
12f4bcbe79SGeorge Spelvin  * These hash functions are NOT GUARANTEED STABLE between kernel
13f4bcbe79SGeorge Spelvin  * versions, architectures, or even repeated boots of the same kernel.
14f4bcbe79SGeorge Spelvin  * (E.g. they may depend on boot-time hardware detection or be
15f4bcbe79SGeorge Spelvin  * deliberately randomized.)
16f4bcbe79SGeorge Spelvin  *
17f4bcbe79SGeorge Spelvin  * They are also not intended to be secure against collisions caused by
18f4bcbe79SGeorge Spelvin  * malicious inputs; much slower hash functions are required for that.
19f4bcbe79SGeorge Spelvin  *
20f4bcbe79SGeorge Spelvin  * They are optimized for pathname components, meaning short strings.
21f4bcbe79SGeorge Spelvin  * Even if a majority of files have longer names, the dynamic profile of
22f4bcbe79SGeorge Spelvin  * pathname components skews short due to short directory names.
23f4bcbe79SGeorge Spelvin  * (E.g. /usr/lib/libsesquipedalianism.so.3.141.)
24f4bcbe79SGeorge Spelvin  */
25f4bcbe79SGeorge Spelvin 
26f4bcbe79SGeorge Spelvin /*
27f4bcbe79SGeorge Spelvin  * Version 1: one byte at a time.  Example of use:
28f4bcbe79SGeorge Spelvin  *
29f4bcbe79SGeorge Spelvin  * unsigned long hash = init_name_hash;
30f4bcbe79SGeorge Spelvin  * while (*p)
31f4bcbe79SGeorge Spelvin  *	hash = partial_name_hash(tolower(*p++), hash);
32f4bcbe79SGeorge Spelvin  * hash = end_name_hash(hash);
33f4bcbe79SGeorge Spelvin  *
34f4bcbe79SGeorge Spelvin  * Although this is designed for bytes, fs/hfsplus/unicode.c
35f4bcbe79SGeorge Spelvin  * abuses it to hash 16-bit values.
36f4bcbe79SGeorge Spelvin  */
37f4bcbe79SGeorge Spelvin 
38f4bcbe79SGeorge Spelvin /* Hash courtesy of the R5 hash in reiserfs modulo sign bits */
398387ff25SLinus Torvalds #define init_name_hash(salt)		(unsigned long)(salt)
40f4bcbe79SGeorge Spelvin 
41f4bcbe79SGeorge Spelvin /* partial hash update function. Assume roughly 4 bits per character */
42f4bcbe79SGeorge Spelvin static inline unsigned long
partial_name_hash(unsigned long c,unsigned long prevhash)43f4bcbe79SGeorge Spelvin partial_name_hash(unsigned long c, unsigned long prevhash)
44f4bcbe79SGeorge Spelvin {
45f4bcbe79SGeorge Spelvin 	return (prevhash + (c << 4) + (c >> 4)) * 11;
46f4bcbe79SGeorge Spelvin }
47f4bcbe79SGeorge Spelvin 
48f4bcbe79SGeorge Spelvin /*
49f4bcbe79SGeorge Spelvin  * Finally: cut down the number of bits to a int value (and try to avoid
50703b5fafSGeorge Spelvin  * losing bits).  This also has the property (wanted by the dcache)
51703b5fafSGeorge Spelvin  * that the msbits make a good hash table index.
52f4bcbe79SGeorge Spelvin  */
end_name_hash(unsigned long hash)5319b9ad67SAmir Goldstein static inline unsigned int end_name_hash(unsigned long hash)
54f4bcbe79SGeorge Spelvin {
5519b9ad67SAmir Goldstein 	return hash_long(hash, 32);
56f4bcbe79SGeorge Spelvin }
57f4bcbe79SGeorge Spelvin 
58f4bcbe79SGeorge Spelvin /*
59f4bcbe79SGeorge Spelvin  * Version 2: One word (32 or 64 bits) at a time.
60f4bcbe79SGeorge Spelvin  * If CONFIG_DCACHE_WORD_ACCESS is defined (meaning <asm/word-at-a-time.h>
61f4bcbe79SGeorge Spelvin  * exists, which describes major Linux platforms like x86 and ARM), then
62f4bcbe79SGeorge Spelvin  * this computes a different hash function much faster.
63f4bcbe79SGeorge Spelvin  *
64f4bcbe79SGeorge Spelvin  * If not set, this falls back to a wrapper around the preceding.
65f4bcbe79SGeorge Spelvin  */
668387ff25SLinus Torvalds extern unsigned int __pure full_name_hash(const void *salt, const char *, unsigned int);
67f4bcbe79SGeorge Spelvin 
68f4bcbe79SGeorge Spelvin /*
69f4bcbe79SGeorge Spelvin  * A hash_len is a u64 with the hash of a string in the low
70f4bcbe79SGeorge Spelvin  * half and the length in the high half.
71f4bcbe79SGeorge Spelvin  */
72f4bcbe79SGeorge Spelvin #define hashlen_hash(hashlen) ((u32)(hashlen))
73f4bcbe79SGeorge Spelvin #define hashlen_len(hashlen)  ((u32)((hashlen) >> 32))
74f4bcbe79SGeorge Spelvin #define hashlen_create(hash, len) ((u64)(len)<<32 | (u32)(hash))
75f4bcbe79SGeorge Spelvin 
76fcfd2fbfSGeorge Spelvin /* Return the "hash_len" (hash and length) of a null-terminated string */
778387ff25SLinus Torvalds extern u64 __pure hashlen_string(const void *salt, const char *name);
78fcfd2fbfSGeorge Spelvin 
79f4bcbe79SGeorge Spelvin #endif	/* __LINUX_STRINGHASH_H */
80