# Pointer hashing

7 messages
Open this post in threaded view
|

## Pointer hashing

 I’m trying to hash a pointer value and found some example of hash function but not sure about translations and the process in general. 1) Should I cast “pointer” as PtrUInt to get an integer from a pointer? I’m looking for the memory address I guess and casting seems to return a unique value. Btw, these functions are for a 32 bit integer but I’m building for 64 bit. How do I deal with that? Can I remove the upper 32bits which are typically not used. 2) I’m getting incompatible type errors on my translation. What did I do wrong there? The fact the function is 32 bit may be the problem? unsigned int hash(unsigned int x) {     x = ((x >> 16) ^ x) * 0x45d9f3b;     x = ((x >> 16) ^ x) * 0x45d9f3b;     x = (x >> 16) ^ x;     return x; } function Hash (x: PtrUInt): PtrUInt; begin   x := (Power((x shr 16), x)) * \$45d9f3b;   x := (Power((x shr 16), x)) * \$45d9f3b;   x := Power((x shr 16), x);   result := x; end; Thanks for any ideas you have. Regards,         Ryan Joseph _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: Pointer hashing

 On Sun, January 29, 2017 7:37 pm, Ryan Joseph wrote: > I'm trying to hash a pointer value and found some example of hash > function but not sure about translations and the process in general. ... > x := (Power((x shr 16), x)) * \$45d9f3b; x := Power((x shr 16), x); result := The math unit, is something I've been meaning to try ... don't have much experience with it.. but did you try the power operator? It's **   x := ((x shr 16) ** x) * \$45d9f3b; Above code please do not trust it, I have no experience with operator overloading (or very little). I did not check to see what the function does, is, etc. _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: Pointer hashing

 In reply to this post by Ryan Joseph In our previous episode, Ryan Joseph said: > > unsigned int hash(unsigned int x) { >     x = ((x >> 16) ^ x) * 0x45d9f3b; >     x = ((x >> 16) ^ x) * 0x45d9f3b; >     x = (x >> 16) ^ x; >     return x; > } > > function Hash (x: PtrUInt): PtrUInt; > begin >   x := (Power((x shr 16), x)) * \$45d9f3b; >   x := (Power((x shr 16), x)) * \$45d9f3b; >   x := Power((x shr 16), x); >   result := x; > end; - In C operator crypto lingo, ^ is xor, not exponentiation. - the lower 3,4 bits of heap pointer are usually not used, - for 64-bit you probably have to chage this and have more terms also with - shr 32 and 48 _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: Pointer hashing

 In reply to this post by Ryan Joseph Hi, On Mon, 30 Jan 2017, Ryan Joseph wrote: > I?m trying to hash a pointer value and found some example of hash > function but not sure about translations and the process in general. > > 1) Should I cast ?pointer? as PtrUInt to get an integer from a pointer? > I?m looking for the memory address I guess and casting seems to return a > unique value. Btw, these functions are for a 32 bit integer but I?m > building for 64 bit. How do I deal with that? Can I remove the upper > 32bits which are typically not used. This hash function doesn't deal with the upper 32bit, therefore it's probably unsuitable to properly hash 64bit pointers. You need to find an alternative hash function which does that, if you need 64bit. (Googling reveals some relevant pages for a 64bit version, if you search that magic hex number in there...) > 2) I?m getting incompatible type errors on my translation. What did I do > wrong there? The fact the function is 32 bit may be the problem? > > unsigned int hash(unsigned int x) { >     x = ((x >> 16) ^ x) * 0x45d9f3b; >     x = ((x >> 16) ^ x) * 0x45d9f3b; >     x = (x >> 16) ^ x; >     return x; > } > > function Hash (x: PtrUInt): PtrUInt; > begin >   x := (Power((x shr 16), x)) * \$45d9f3b; >   x := (Power((x shr 16), x)) * \$45d9f3b; >   x := Power((x shr 16), x); >   result := x; > end; > > Thanks for any ideas you have. The "^" operator in C simply doesn't translate to "Power" in Pascal. It's "exclusive or", a.k.a. xor. So that line in C translates to: x:=((x shr 16) xor x) * \$45d9f3b; Furthermore the Power() function in Math unit seems to deal with Floats and returns Float, where your type errors might come. But since you don't need Power() there anyway, that should be an easy fix... Charlie _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Open this post in threaded view
|

## Re: Pointer hashing

 In reply to this post by Ryan Joseph El 30/01/2017 a las 3:37, Ryan Joseph escribió: > I’m trying to hash a pointer value and found some example of hash function but not sure about translations and the process in general. > Hello, After addressing the ^ conversion showed by other people I have a question. Why you need to hash a pointer ? Hashing a value is interesting to reduce its compare time (taking collisions into account) and/or verify message integrity, and hashing a pointer does not meet none of this goals as it is process wide unique (no collisions) and its size is the fastest compare operation (most architectures). -- _______________________________________________ fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal