Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Portable hash
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Florent Monnier <fmonnier@l...>
Subject: Re: [Caml-list] Portable hash
> How hard would it
> be to tailor it to, say, work always with 31 bits?

Hashtbl.hash will return a 31 bit integers on both 32 or 64 architectures:

file: ocaml-3.10.2/byterun/hash.c

CAMLprim value caml_hash_univ_param(value count, value limit, value obj)

  return Val_long(hash_accu & 0x3FFFFFFF);
  /* The & has two purposes: ensure that the return value is positive
     and give the same result on 32 bit and 64 bit architectures. */

# max_int (* the 31 bit one *) = 0x3FFFFFFF ;;
- : bool = true

> 2. and should not change with a platform or compiler version.
If you wish to get a code that won't change for a futur ocaml version, just 
extract the current hash function of ocaml to include it in your own code.
You can do this because the code of the stdlib is LGPL.

currently I have some problems with Hashtbl.hash because it doesn't hash 
values of kind integers, so if (x = y + 1) I get ((hash x) = (hash y) + 1) 
which results in a poor repartition.

Does someone know how to hash an integer ?
Here there are hashing functions for integers:
but they are for 32 bit unsigned integers.
How can I adapt it for 31 bit integers ?

Or would it be a good solution to convert the bits of the integer to a 
bool list and then give it to Hashtbl.hash ?
At least with this solution I haven't ((hash x) = (hash y) + 1) anymore.