Patch: convenience routines for hashtab.c
Horst von Brand
vonbrand@sleipnir.valparaiso.cl
Wed May 3 20:18:00 GMT 2000
Brad Lucier <lucier@math.purdue.edu> said:
> The routine in hashtab.c
>
> /* Returns a hash code for P. */
>
> static hashval_t
> hash_pointer (p)
> const void *p;
> {
> return (hashval_t) p;
> }
>
> on the alpha converts a 64-bit pointer to a 32-bit int, and so generates
> the warning:
>
> ../../../libiberty/hashtab.c: In function `hash_pointer':
> ../../../libiberty/hashtab.c:107: warning: cast from pointer to integer of diff
> erent size
> Was it intended to throw away the top 32 bits of the pointer?
No problem, as this is a hash value and most objects will differ in address
inside 4Gb, at least for a while ;-). I'd do something like:
static hashval_t
hash_pointer (p)
const void *p;
{
return (hashval_t) ((long) p >> 3);
}
This way, you loose the last 3 bits, which are very likely to be zeroes
anyway (malloc()ed objects are usually aligned at 8 byte boundaries, isn't
it?), and shift in some (perhaps significant) bits from the high half. The
intermediate cast should also get rid of the warning. And the above will
also work fine where longs and pointers are the same size, just rotating
the last 3 zeroes to the top.
--
Horst von Brand vonbrand@sleipnir.valparaiso.cl
Casilla 9G, Viña del Mar, Chile +56 32 672616
More information about the Gcc-patches
mailing list