File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -5,9 +5,25 @@ include Int64
5
5
6
6
let min : t -> t -> t = Stdlib. min
7
7
let max : t -> t -> t = Stdlib. max
8
- let hash x = Stdlib. abs (to_int x)
9
8
let sign i = compare i zero
10
9
10
+ (* use FNV:
11
+ https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function *)
12
+ let hash_to_int64 (n : t ) =
13
+ let offset_basis = 0xcbf29ce484222325L in
14
+ let prime = 0x100000001b3L in
15
+
16
+ let h = ref offset_basis in
17
+ for k = 0 to 7 do
18
+ h := mul ! h prime;
19
+ (* h := h xor (k-th bit of n) *)
20
+ h := logxor ! h (logand (shift_left n (k * 8 )) 0xffL )
21
+ done ;
22
+ logand ! h max_int
23
+
24
+ let [@ inline] hash (n : t ) : int =
25
+ to_int (hash_to_int64 n) land CCShims_.Stdlib. max_int
26
+
11
27
(* see {!CCInt.popcount} for more details *)
12
28
let [@ inline] popcount (b : t ) : int =
13
29
let m1 = 0x5555555555555555L in
Original file line number Diff line number Diff line change @@ -27,8 +27,13 @@ val max : t -> t -> t
27
27
@since 3.0 *)
28
28
29
29
val hash : t -> int
30
- (* * [hash x] computes the hash of [x].
31
- Like {!Stdlib.abs (to_int x)}. *)
30
+ (* * [hash x] computes the hash of [x], a non-negative integer.
31
+ Uses FNV since NEXT_RELEASE *)
32
+
33
+ val hash_to_int64 : t -> t
34
+ (* * Like {!hash} but does not truncate.
35
+ Uses FNV.
36
+ @since NEXT_RELEASE *)
32
37
33
38
val popcount : t -> int
34
39
(* * Number of bits set to 1.
You can’t perform that action at this time.
0 commit comments