Very low cost hash function

后端 未结 5 2005
死守一世寂寞
死守一世寂寞 2020-12-25 08:33

I need a hash function for a Look Up table, so that if my values are from 0 to N, I need a hash function that give me a value from 0 to n, being n << N. Another piece

5条回答
  •  难免孤独
    2020-12-25 09:32

    If you're truly talking hardware (vs. software, or hardware implementation of software), and your number of hash buckets n can be written as n = 2m - 1, the easiest is probably a maximum-length linear feedback shift register (LFSR) of which CRC is an instance.

    Here's one way you could use an m-bit shift register to create a hash of a data packet (make sure all data is represented consistently as a K-bit string, if you have shorter strings then pad one end with zeros):

    1. Initialize the state of the LFSR (CRC-32 uses all 1's; all zeros is probably bad)
    2. Shift in the bits of your data
    3. (Optional) Shift in an additional j zeros (j between m and 2m is probably a good choice); this adds some additional hashing to reduce direct correlation between input/output bits
    4. Use the contents of the m-bit shift register as your hashed value.

提交回复
热议问题