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