Two notes:
a) If you use in the last version (table with right shift) as POLYNIAL the value 0x8408, then you get the same values as with the first variant without tables. It puzzled me that the resutls were different. If you look at the bit pattern 0x8408 and 0o10201 = 0x1081 are quite similar.
b) One can also use a linear congruential generator (LCG) as hash function. It is for instance used in the hash method of micropython.
two variants:
b.1)
Code: Select all
@micropython.native
def hash(str):
result = 0xceed
for v in str:
result = (result * 17 ^ v) & 0xffff
return result
b.2) The same with an additional lookup table, which is told to be better. The lookup table is just a table of random values.
Code: Select all
@micropython.native
def hash(str):
table = b'\\~A!\x90\xb2\xe03H\x81rQI\xd3#\'\x83b\x90\xe8"+s56\x1b2u'\
b'\xe4)[t\x01\x0f\x8fR\x04&&[X\xfe\xbc\x80e64\r\x1b\xc1\xa0\x951\xec7'\
b'\x1f\x00\xc9\xb7q@\xfa\'P\x01?&\x04Q\xa7\x17\xafS\xe6\xcf\xad\xa1i'\
b'\xb6Drs\tO\xe5i\xf9\xc8_\x18\xdb\xad;\xdbq\xca\xcc\xe4\xbd\x0c\x84Gv'\
b'\xfd\xeb\xfdq\xcd\x04\xa3B\x1e\xa0M\xb4\xfe\x82\xf6\x0c\xdf\xd73\xef'\
b'\xdc\xbb\x855\x8b^D\xd7\x10\xf2I\x80\xea\x95\x99n\xe2\xeb\x11\xec\xf7'\
b'\xed\x17\x15y\xd8F\x97\xf13\xe9\xc6\xa0\x91\x0eh\x00\xbd\xa1<\x1f\x9d'\
b'\x0f\xbb\x07\x16\xe3\x07\xc1\xc0\xee\xc4\t\xc0\xd3\xc2\xeaqQ?\x84h\x95'\
b'\x08\x98\xf6\xf3\xab8G\x86\\\xba\xa3\'M\xa0\x82\x99\xc0\xe3*:\x88\x8f'\
b'\xb0=\xe7\xca\xfe\t\xd5\x87\xf6\xe5\xb7\xd6\xeaL@\xe3\x06\xfa\xc3\xb4'\
b'\xf4E\xa3<W\x92m\xb8Y\x15\xae\x91G\xcf\x1e\xc2\x12\x9f$\x99%\x1a]\xef\x0bjE\x
result = 0xceed
for v in str:
result = (result * 17 ^ table[v]) & 0xffff
return result
In my test, the numbers on a rp2040 for 400 bytes are
4.6 ms for the non.table CRC
4.0 ms for the table based crc, table locally linked
1.7 ms for the hash without table
2.2 ms for the hash with table