Sujet : Re: Good hash for pointers
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 23. May 2024, 23:51:29
Autres entêtes
Organisation : None to speak of
Message-ID : <875xv43zwu.fsf@nosuchdomain.example.com>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Richard Harnden <
richard.nospam@gmail.invalid> writes:
On 23/05/2024 12:11, Malcolm McLean wrote:
What is a good hash function for pointers to use in portable ANSI C?
>
All your pointers from malloc are going to be unique.
All of their low bits are going to be zero, because they need to align
on some n-bit boundary.
All of their high bit are going to be the same, because that's just
how it works.
>
So just take the middle: hash = ((intptr_t) ptr) >> 4 & 0xffff;
I'd use uintptr_t, not intptr_t. I prefer not to think about how
bitwise operations work on signed values unless I have to.
You're assuming a 16-bit hash. I have no idea whether that suits
Malcolm's requirements.
Another approach might be to divide the pointer representation into
N-bit chunks and xor them together.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */