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