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