1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* The generic hashing primitive */
|
|
|
|
|
2011-05-29 02:52:27 -07:00
|
|
|
/* The interface of this file is in "mlvalues.h" (for [caml_hash_variant])
|
|
|
|
and in "hash.h" (for the other exported functions). */
|
2003-12-15 10:10:51 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "mlvalues.h"
|
2000-02-10 06:04:59 -08:00
|
|
|
#include "custom.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "memory.h"
|
2011-05-29 02:52:27 -07:00
|
|
|
#include "hash.h"
|
|
|
|
|
|
|
|
#ifdef ARCH_INT64_TYPE
|
|
|
|
#include "int64_native.h"
|
|
|
|
#else
|
|
|
|
#include "int64_emul.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The new implementation, based on MurmurHash 3,
|
|
|
|
http://code.google.com/p/smhasher/ */
|
|
|
|
|
|
|
|
#define ROTL32(x,n) ((x) << n | (x) >> (32-n))
|
|
|
|
|
|
|
|
#define MIX(h,d) \
|
|
|
|
d *= 0xcc9e2d51; \
|
|
|
|
d = ROTL32(d, 15); \
|
|
|
|
d *= 0x1b873593; \
|
|
|
|
h ^= d; \
|
|
|
|
h = ROTL32(h, 13); \
|
|
|
|
h = h * 5 + 0xe6546b64;
|
|
|
|
|
|
|
|
#define FINAL_MIX(h) \
|
|
|
|
h ^= h >> 16; \
|
|
|
|
h *= 0x85ebca6b; \
|
|
|
|
h ^= h >> 13; \
|
|
|
|
h *= 0xc2b2ae35; \
|
|
|
|
h ^= h >> 16;
|
|
|
|
|
|
|
|
CAMLexport uint32 caml_hash_mix_uint32(uint32 h, uint32 d)
|
|
|
|
{
|
|
|
|
MIX(h, d);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2012-01-23 07:09:07 -08:00
|
|
|
/* Mix a platform-native integer. */
|
2011-05-29 02:52:27 -07:00
|
|
|
|
|
|
|
CAMLexport uint32 caml_hash_mix_intnat(uint32 h, intnat d)
|
|
|
|
{
|
|
|
|
uint32 n;
|
|
|
|
#ifdef ARCH_SIXTYFOUR
|
|
|
|
/* Mix the low 32 bits and the high 32 bits, in a way that preserves
|
|
|
|
32/64 compatibility: we want n = (uint32) d
|
|
|
|
if d is in the range [-2^31, 2^31-1]. */
|
|
|
|
n = (d >> 32) ^ (d >> 63) ^ d;
|
|
|
|
/* If 0 <= d < 2^31: d >> 32 = 0 d >> 63 = 0
|
|
|
|
If -2^31 <= d < 0: d >> 32 = -1 d >> 63 = -1
|
|
|
|
In both cases, n = (uint32) d. */
|
|
|
|
#else
|
|
|
|
n = d;
|
|
|
|
#endif
|
|
|
|
MIX(h, n);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mix a 64-bit integer. */
|
|
|
|
|
|
|
|
CAMLexport uint32 caml_hash_mix_int64(uint32 h, int64 d)
|
|
|
|
{
|
|
|
|
uint32 hi, lo;
|
|
|
|
|
|
|
|
I64_split(d, hi, lo);
|
|
|
|
MIX(h, lo);
|
|
|
|
MIX(h, hi);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mix a double-precision float.
|
|
|
|
Treats +0.0 and -0.0 identically.
|
|
|
|
Treats all NaNs identically.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CAMLexport uint32 caml_hash_mix_double(uint32 hash, double d)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
double d;
|
|
|
|
#if defined(ARCH_BIG_ENDIAN) || (defined(__arm__) && !defined(__ARM_EABI__))
|
|
|
|
struct { uint32 h; uint32 l; } i;
|
|
|
|
#else
|
|
|
|
struct { uint32 l; uint32 h; } i;
|
|
|
|
#endif
|
|
|
|
} u;
|
|
|
|
uint32 h, l;
|
|
|
|
/* Convert to two 32-bit halves */
|
|
|
|
u.d = d;
|
|
|
|
h = u.i.h; l = u.i.l;
|
|
|
|
/* Normalize NaNs */
|
|
|
|
if ((h & 0x7FF00000) == 0x7FF00000 && (l | (h & 0xFFFFF)) != 0) {
|
|
|
|
h = 0x7FF00000;
|
|
|
|
l = 0x00000001;
|
|
|
|
}
|
|
|
|
/* Normalize -0 into +0 */
|
|
|
|
else if (h == 0x80000000 && l == 0) {
|
|
|
|
h = 0;
|
|
|
|
}
|
|
|
|
MIX(hash, l);
|
|
|
|
MIX(hash, h);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mix a single-precision float.
|
|
|
|
Treats +0.0 and -0.0 identically.
|
|
|
|
Treats all NaNs identically.
|
|
|
|
*/
|
|
|
|
|
|
|
|
CAMLexport uint32 caml_hash_mix_float(uint32 hash, float d)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
float f;
|
|
|
|
uint32 i;
|
|
|
|
} u;
|
|
|
|
uint32 n;
|
|
|
|
/* Convert to int32 */
|
|
|
|
u.f = d; n = u.i;
|
|
|
|
/* Normalize NaNs */
|
|
|
|
if ((n & 0x7F800000) == 0x7F800000 && (n & 0x007FFFFF) != 0) {
|
|
|
|
n = 0x7F800001;
|
|
|
|
}
|
|
|
|
/* Normalize -0 into +0 */
|
|
|
|
else if (n == 0x80000000) {
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
MIX(hash, n);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2012-02-10 08:15:24 -08:00
|
|
|
/* Mix an OCaml string */
|
2011-05-29 02:52:27 -07:00
|
|
|
|
|
|
|
CAMLexport uint32 caml_hash_mix_string(uint32 h, value s)
|
|
|
|
{
|
|
|
|
mlsize_t len = caml_string_length(s);
|
|
|
|
mlsize_t i;
|
|
|
|
uint32 w;
|
|
|
|
|
|
|
|
/* Mix by 32-bit blocks (little-endian) */
|
|
|
|
for (i = 0; i + 4 <= len; i += 4) {
|
|
|
|
#ifdef ARCH_BIG_ENDIAN
|
|
|
|
w = Byte_u(s, i)
|
|
|
|
| (Byte_u(s, i+1) << 8)
|
|
|
|
| (Byte_u(s, i+2) << 16)
|
|
|
|
| (Byte_u(s, i+3) << 24);
|
|
|
|
#else
|
|
|
|
w = *((uint32 *) &Byte_u(s, i));
|
|
|
|
#endif
|
|
|
|
MIX(h, w);
|
|
|
|
}
|
|
|
|
/* Finish with up to 3 bytes */
|
|
|
|
w = 0;
|
|
|
|
switch (len & 3) {
|
|
|
|
case 3: w = Byte_u(s, i+2) << 16; /* fallthrough */
|
|
|
|
case 2: w |= Byte_u(s, i+1) << 8; /* fallthrough */
|
|
|
|
case 1: w |= Byte_u(s, i);
|
|
|
|
MIX(h, w);
|
|
|
|
default: /*skip*/; /* len & 3 == 0, no extra bytes, do nothing */
|
|
|
|
}
|
|
|
|
/* Finally, mix in the length. Ignore the upper 32 bits, generally 0. */
|
|
|
|
h ^= (uint32) len;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Maximal size of the queue used for breadth-first traversal. */
|
|
|
|
#define HASH_QUEUE_SIZE 256
|
|
|
|
|
|
|
|
/* The generic hash function */
|
|
|
|
|
|
|
|
CAMLprim value caml_hash(value count, value limit, value seed, value obj)
|
|
|
|
{
|
|
|
|
value queue[HASH_QUEUE_SIZE]; /* Queue of values to examine */
|
|
|
|
intnat rd; /* Position of first value in queue */
|
|
|
|
intnat wr; /* One past position of last value in queue */
|
|
|
|
intnat sz; /* Max number of values to put in queue */
|
|
|
|
intnat num; /* Max number of meaningful values to see */
|
|
|
|
uint32 h; /* Rolling hash */
|
|
|
|
value v;
|
|
|
|
mlsize_t i, len;
|
|
|
|
|
|
|
|
sz = Long_val(limit);
|
|
|
|
if (sz < 0 || sz > HASH_QUEUE_SIZE) sz = HASH_QUEUE_SIZE;
|
|
|
|
num = Long_val(count);
|
|
|
|
h = Int_val(seed);
|
|
|
|
queue[0] = obj; rd = 0; wr = 1;
|
|
|
|
|
|
|
|
while (rd < wr && num > 0) {
|
|
|
|
v = queue[rd++];
|
|
|
|
again:
|
|
|
|
if (Is_long(v)) {
|
|
|
|
h = caml_hash_mix_intnat(h, v);
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
else if (Is_in_value_area(v)) {
|
2011-07-23 11:13:18 -07:00
|
|
|
switch (Tag_val(v)) {
|
2011-05-29 02:52:27 -07:00
|
|
|
case String_tag:
|
|
|
|
h = caml_hash_mix_string(h, v);
|
|
|
|
num--;
|
|
|
|
break;
|
|
|
|
case Double_tag:
|
|
|
|
h = caml_hash_mix_double(h, Double_val(v));
|
|
|
|
num--;
|
|
|
|
break;
|
|
|
|
case Double_array_tag:
|
|
|
|
for (i = 0, len = Wosize_val(v) / Double_wosize; i < len; i++) {
|
|
|
|
h = caml_hash_mix_double(h, Double_field(v, i));
|
|
|
|
num--;
|
|
|
|
if (num < 0) break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Abstract_tag:
|
|
|
|
/* Block contents unknown. Do nothing. */
|
|
|
|
break;
|
|
|
|
case Infix_tag:
|
2012-01-23 07:09:07 -08:00
|
|
|
/* Mix in the offset to distinguish different functions from
|
2011-05-29 02:52:27 -07:00
|
|
|
the same mutually-recursive definition */
|
|
|
|
h = caml_hash_mix_uint32(h, Infix_offset_val(v));
|
|
|
|
v = v - Infix_offset_val(v);
|
|
|
|
goto again;
|
|
|
|
case Forward_tag:
|
|
|
|
v = Forward_val(v);
|
|
|
|
goto again;
|
|
|
|
case Object_tag:
|
|
|
|
h = caml_hash_mix_intnat(h, Oid_val(v));
|
|
|
|
num--;
|
|
|
|
break;
|
|
|
|
case Custom_tag:
|
|
|
|
/* If no hashing function provided, do nothing. */
|
|
|
|
/* Only use low 32 bits of custom hash, for 32/64 compatibility */
|
|
|
|
if (Custom_ops_val(v)->hash != NULL) {
|
|
|
|
uint32 n = (uint32) Custom_ops_val(v)->hash(v);
|
|
|
|
h = caml_hash_mix_uint32(h, n);
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Mix in the tag and size, but do not count this towards [num] */
|
|
|
|
h = caml_hash_mix_uint32(h, Whitehd_hd(Hd_val(v)));
|
|
|
|
/* Copy fields into queue, not exceeding the total size [sz] */
|
|
|
|
for (i = 0, len = Wosize_val(v); i < len; i++) {
|
|
|
|
if (wr >= sz) break;
|
|
|
|
queue[wr++] = Field(v, i);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* v is a pointer outside the heap, probably a code pointer.
|
|
|
|
Shall we count it? Let's say yes by compatibility with old code. */
|
|
|
|
h = caml_hash_mix_intnat(h, v);
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Final mixing of bits */
|
|
|
|
FINAL_MIX(h);
|
|
|
|
/* Fold result to the range [0, 2^30-1] so that it is a nonnegative
|
2012-02-10 08:15:24 -08:00
|
|
|
OCaml integer both on 32 and 64-bit platforms. */
|
2011-05-29 02:52:27 -07:00
|
|
|
return Val_int(h & 0x3FFFFFFFU);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The old implementation */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
static uintnat hash_accu;
|
|
|
|
static intnat hash_univ_limit, hash_univ_count;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void hash_aux(value obj);
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLprim value caml_hash_univ_param(value count, value limit, value obj)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
hash_univ_limit = Long_val(limit);
|
|
|
|
hash_univ_count = Long_val(count);
|
|
|
|
hash_accu = 0;
|
|
|
|
hash_aux(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. */
|
|
|
|
}
|
|
|
|
|
|
|
|
#define Alpha 65599
|
|
|
|
#define Beta 19
|
|
|
|
#define Combine(new) (hash_accu = hash_accu * Alpha + (new))
|
|
|
|
#define Combine_small(new) (hash_accu = hash_accu * Beta + (new))
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void hash_aux(value obj)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
|
|
|
unsigned char * p;
|
1995-07-27 10:41:09 -07:00
|
|
|
mlsize_t i, j;
|
1995-05-04 03:15:53 -07:00
|
|
|
tag_t tag;
|
|
|
|
|
|
|
|
hash_univ_limit--;
|
|
|
|
if (hash_univ_count < 0 || hash_univ_limit < 0) return;
|
|
|
|
|
2002-01-20 09:39:10 -08:00
|
|
|
again:
|
1995-05-04 03:15:53 -07:00
|
|
|
if (Is_long(obj)) {
|
|
|
|
hash_univ_count--;
|
|
|
|
Combine(Long_val(obj));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1995-07-13 02:02:41 -07:00
|
|
|
/* Pointers into the heap are well-structured blocks. So are atoms.
|
1995-05-04 03:15:53 -07:00
|
|
|
We can inspect the block contents. */
|
2002-01-20 09:39:10 -08:00
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
Assert (Is_block (obj));
|
2008-01-03 01:37:10 -08:00
|
|
|
if (Is_in_value_area(obj)) {
|
1995-05-04 03:15:53 -07:00
|
|
|
tag = Tag_val(obj);
|
|
|
|
switch (tag) {
|
|
|
|
case String_tag:
|
|
|
|
hash_univ_count--;
|
2003-12-16 10:09:44 -08:00
|
|
|
i = caml_string_length(obj);
|
1995-05-04 03:15:53 -07:00
|
|
|
for (p = &Byte_u(obj, 0); i > 0; i--, p++)
|
|
|
|
Combine_small(*p);
|
|
|
|
break;
|
|
|
|
case Double_tag:
|
|
|
|
/* For doubles, we inspect their binary representation, LSB first.
|
|
|
|
The results are consistent among all platforms with IEEE floats. */
|
|
|
|
hash_univ_count--;
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_BIG_ENDIAN
|
1995-05-04 03:15:53 -07:00
|
|
|
for (p = &Byte_u(obj, sizeof(double) - 1), i = sizeof(double);
|
|
|
|
i > 0;
|
|
|
|
p--, i--)
|
|
|
|
#else
|
|
|
|
for (p = &Byte_u(obj, 0), i = sizeof(double);
|
|
|
|
i > 0;
|
|
|
|
p++, i--)
|
|
|
|
#endif
|
|
|
|
Combine_small(*p);
|
|
|
|
break;
|
1995-07-27 10:41:09 -07:00
|
|
|
case Double_array_tag:
|
|
|
|
hash_univ_count--;
|
|
|
|
for (j = 0; j < Bosize_val(obj); j += sizeof(double)) {
|
1996-07-01 05:43:28 -07:00
|
|
|
#ifdef ARCH_BIG_ENDIAN
|
1995-07-27 10:41:09 -07:00
|
|
|
for (p = &Byte_u(obj, j + sizeof(double) - 1), i = sizeof(double);
|
|
|
|
i > 0;
|
|
|
|
p--, i--)
|
|
|
|
#else
|
|
|
|
for (p = &Byte_u(obj, j), i = sizeof(double);
|
|
|
|
i > 0;
|
|
|
|
p++, i--)
|
|
|
|
#endif
|
|
|
|
Combine_small(*p);
|
|
|
|
}
|
|
|
|
break;
|
1995-05-04 03:15:53 -07:00
|
|
|
case Abstract_tag:
|
|
|
|
/* We don't know anything about the contents of the block.
|
|
|
|
Better do nothing. */
|
|
|
|
break;
|
1995-07-10 02:48:27 -07:00
|
|
|
case Infix_tag:
|
|
|
|
hash_aux(obj - Infix_offset_val(obj));
|
|
|
|
break;
|
2002-01-20 09:39:10 -08:00
|
|
|
case Forward_tag:
|
|
|
|
obj = Forward_val (obj);
|
|
|
|
goto again;
|
1997-05-11 15:42:38 -07:00
|
|
|
case Object_tag:
|
|
|
|
hash_univ_count--;
|
|
|
|
Combine(Oid_val(obj));
|
|
|
|
break;
|
2000-02-10 06:04:59 -08:00
|
|
|
case Custom_tag:
|
|
|
|
/* If no hashing function provided, do nothing */
|
|
|
|
if (Custom_ops_val(obj)->hash != NULL) {
|
|
|
|
hash_univ_count--;
|
|
|
|
Combine(Custom_ops_val(obj)->hash(obj));
|
|
|
|
}
|
|
|
|
break;
|
1995-05-04 03:15:53 -07:00
|
|
|
default:
|
|
|
|
hash_univ_count--;
|
|
|
|
Combine_small(tag);
|
|
|
|
i = Wosize_val(obj);
|
|
|
|
while (i != 0) {
|
|
|
|
i--;
|
|
|
|
hash_aux(Field(obj, i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, obj is a pointer outside the heap, to an object with
|
|
|
|
a priori unknown structure. Use its physical address as hash key. */
|
2005-09-22 07:21:50 -07:00
|
|
|
Combine((intnat) obj);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2000-04-07 07:43:31 -07:00
|
|
|
|
|
|
|
/* Hashing variant tags */
|
|
|
|
|
2008-08-01 07:10:36 -07:00
|
|
|
CAMLexport value caml_hash_variant(char const * tag)
|
2000-04-07 07:43:31 -07:00
|
|
|
{
|
|
|
|
value accu;
|
|
|
|
/* Same hashing algorithm as in ../typing/btype.ml, function hash_variant */
|
2010-01-22 04:48:24 -08:00
|
|
|
for (accu = Val_int(0); *tag != 0; tag++)
|
2000-04-07 07:43:31 -07:00
|
|
|
accu = Val_int(223 * Int_val(accu) + *((unsigned char *) tag));
|
2000-04-10 08:00:42 -07:00
|
|
|
#ifdef ARCH_SIXTYFOUR
|
2000-04-14 02:41:56 -07:00
|
|
|
accu = accu & Val_long(0x7FFFFFFFL);
|
2000-04-10 08:00:42 -07:00
|
|
|
#endif
|
2000-04-07 07:43:31 -07:00
|
|
|
/* Force sign extension of bit 31 for compatibility between 32 and 64-bit
|
|
|
|
platforms */
|
|
|
|
return (int32) accu;
|
|
|
|
}
|