mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-03-07 21:27:55 -05:00
Compare commits
4 commits
81d7e5cc60
...
2827011b37
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2827011b37 | ||
|
|
5a50d42352 | ||
|
|
d493f6696b | ||
|
|
bb6de2ff05 |
8 changed files with 149 additions and 28 deletions
|
|
@ -10,7 +10,6 @@
|
|||
qcheck
|
||||
oseq
|
||||
batteries
|
||||
base
|
||||
sek)
|
||||
(flags :standard -warn-error -3-5 -w -60 -safe-string -color always)
|
||||
(optional)
|
||||
|
|
|
|||
|
|
@ -1824,4 +1824,32 @@ module Str = struct
|
|||
])
|
||||
end
|
||||
|
||||
module Hash = struct
|
||||
let hash_ocaml (n : int) : int =
|
||||
let offset_basis = 0xcbf29ce484222325L in
|
||||
let prime = 0x100000001b3L in
|
||||
let h = ref offset_basis in
|
||||
for k = 0 to 7 do
|
||||
(h := Int64.(mul !h prime));
|
||||
h := Int64.(logxor !h (of_int ((n lsr (k * 8)) land 0xff)))
|
||||
done;
|
||||
Int64.to_int !h land max_int
|
||||
|
||||
let bench_hash n =
|
||||
let run_ocaml () =
|
||||
for i = 0 to n - 1 do
|
||||
opaque_ignore (hash_ocaml i)
|
||||
done
|
||||
and run_c_stub () =
|
||||
for i = 0 to n - 1 do
|
||||
opaque_ignore (CCInt.hash i)
|
||||
done
|
||||
in
|
||||
B.throughputN 3 ~repeat
|
||||
[ "ocaml_fnv", run_ocaml, (); "c_stub", run_c_stub, () ]
|
||||
|
||||
let () =
|
||||
B.Tree.register ("hash" @>>> [ "int" @>> app_ints bench_hash [ 1_000 ] ])
|
||||
end
|
||||
|
||||
let () = try B.Tree.run_global () with Arg.Help msg -> print_endline msg
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ type 'a t = 'a -> hash
|
|||
type 'a iter = ('a -> unit) -> unit
|
||||
type 'a gen = unit -> 'a option
|
||||
|
||||
(* FNV hashing
|
||||
(* FNV-1 hashing (multiply then XOR)
|
||||
https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
|
||||
*)
|
||||
let fnv_offset_basis = 0xcbf29ce484222325L
|
||||
|
|
|
|||
|
|
@ -4,20 +4,11 @@ include Int
|
|||
|
||||
type 'a iter = ('a -> unit) -> unit
|
||||
|
||||
(* use FNV:
|
||||
(* use FNV-1:
|
||||
https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function *)
|
||||
let hash (n : int) : int =
|
||||
let offset_basis = 0xcbf29ce484222325L in
|
||||
let prime = 0x100000001b3L in
|
||||
|
||||
let h = ref offset_basis in
|
||||
for k = 0 to 7 do
|
||||
(h := Int64.(mul !h prime));
|
||||
(* h := h xor (k-th byte of n) *)
|
||||
h := Int64.(logxor !h (of_int ((n lsr (k * 8)) land 0xff)))
|
||||
done;
|
||||
(* truncate back to int and remove sign *)
|
||||
Int64.to_int !h land max_int
|
||||
external hash : (int[@untagged]) -> (int[@untagged])
|
||||
= "caml_cc_hash_int_byte" "caml_cc_hash_int"
|
||||
[@@noalloc]
|
||||
|
||||
let range i j yield =
|
||||
let rec up i j yield =
|
||||
|
|
|
|||
|
|
@ -11,21 +11,14 @@ let max : t -> t -> t = Stdlib.max
|
|||
|
||||
let sign i = compare i zero
|
||||
|
||||
(* use FNV:
|
||||
(* use FNV-1:
|
||||
https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function *)
|
||||
let hash_to_int64 (n : t) =
|
||||
let offset_basis = 0xcbf29ce484222325L in
|
||||
let prime = 0x100000001b3L in
|
||||
external hash_to_int64 : (int64[@unboxed]) -> (int64[@unboxed])
|
||||
= "caml_cc_hash_int64_to_int64_byte" "caml_cc_hash_int64_to_int64"
|
||||
|
||||
let h = ref offset_basis in
|
||||
for k = 0 to 7 do
|
||||
h := mul !h prime;
|
||||
(* h := h xor (k-th byte of n) *)
|
||||
h := logxor !h (logand (shift_right n (k * 8)) 0xffL)
|
||||
done;
|
||||
logand !h max_int
|
||||
|
||||
let[@inline] hash (n : t) : int = to_int (hash_to_int64 n) land Stdlib.max_int
|
||||
external hash : (int64[@unboxed]) -> (int[@untagged])
|
||||
= "caml_cc_hash_int64_byte" "caml_cc_hash_int64"
|
||||
[@@noalloc]
|
||||
|
||||
(* see {!CCInt.popcount} for more details *)
|
||||
let[@inline] popcount (b : t) : int =
|
||||
|
|
|
|||
67
src/core/cc_stubs.c
Normal file
67
src/core/cc_stubs.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#include <caml/alloc.h>
|
||||
#include <caml/mlvalues.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* FNV-1 hash for a 64-bit integer.
|
||||
https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
|
||||
|
||||
FNV-1 order: multiply then XOR (as opposed to FNV-1a which XORs first).
|
||||
Uses the standard 64-bit FNV parameters:
|
||||
offset_basis = 0xcbf29ce484222325
|
||||
prime = 0x00000100000001b3
|
||||
|
||||
Core routine: operates on all 8 bytes of an int64_t. */
|
||||
|
||||
static inline int64_t cc_fnv_hash_int64(int64_t n) {
|
||||
uint64_t un = (uint64_t)n;
|
||||
uint64_t h = UINT64_C(0xcbf29ce484222325);
|
||||
const uint64_t prime = UINT64_C(0x100000001b3);
|
||||
for (int k = 0; k < 8; k++) {
|
||||
h *= prime;
|
||||
h ^= (un >> (k * 8)) & 0xff;
|
||||
}
|
||||
return (int64_t)h;
|
||||
}
|
||||
|
||||
/* --- CCInt.hash entry points (int -> int) --- */
|
||||
|
||||
/* Mask to the OCaml int range (63 bits on 64-bit, 31 on 32-bit)
|
||||
before hashing, so negative OCaml ints hash the same as
|
||||
the unsigned representation seen by OCaml's [lsr]. */
|
||||
#define OCAML_INT_MASK ((UINT64_C(1) << (8 * sizeof(value) - 1)) - 1)
|
||||
|
||||
/* native: untagged int in, untagged int out */
|
||||
CAMLprim intnat caml_cc_hash_int(intnat n) {
|
||||
int64_t projected = (int64_t)((uint64_t)n & OCAML_INT_MASK);
|
||||
return (intnat)((uint64_t)cc_fnv_hash_int64(projected) & Max_long);
|
||||
}
|
||||
|
||||
/* bytecode: boxed value in, boxed value out */
|
||||
CAMLprim value caml_cc_hash_int_byte(value v_n) {
|
||||
return Val_long(caml_cc_hash_int(Long_val(v_n)));
|
||||
}
|
||||
|
||||
/* --- int64 hash entry points (int64 -> int) --- */
|
||||
|
||||
/* native: unboxed int64 in, untagged int out */
|
||||
CAMLprim intnat caml_cc_hash_int64(int64_t n) {
|
||||
return (intnat)((uint64_t)cc_fnv_hash_int64(n) & Max_long);
|
||||
}
|
||||
|
||||
/* bytecode: boxed int64 value in, boxed value out */
|
||||
CAMLprim value caml_cc_hash_int64_byte(value v_n) {
|
||||
return Val_long(caml_cc_hash_int64(Int64_val(v_n)));
|
||||
}
|
||||
|
||||
/* --- int64 -> int64 entry points (for hash_to_int64) --- */
|
||||
|
||||
/* native: unboxed int64 in, unboxed int64 out.
|
||||
Masks to non-negative int64 (matches OCaml's Int64.max_int). */
|
||||
CAMLprim int64_t caml_cc_hash_int64_to_int64(int64_t n) {
|
||||
return cc_fnv_hash_int64(n) & INT64_MAX;
|
||||
}
|
||||
|
||||
/* bytecode: boxed int64 in, boxed int64 out */
|
||||
CAMLprim value caml_cc_hash_int64_to_int64_byte(value v_n) {
|
||||
return caml_copy_int64(cc_fnv_hash_int64(Int64_val(v_n)) & INT64_MAX);
|
||||
}
|
||||
|
|
@ -6,6 +6,10 @@
|
|||
(action
|
||||
(run %{project_root}/src/core/cpp/cpp.exe %{input-file})))
|
||||
(flags :standard -nolabels -open CCMonomorphic)
|
||||
(foreign_stubs
|
||||
(language c)
|
||||
(flags :standard -std=c99 -O2)
|
||||
(names cc_stubs))
|
||||
(libraries either containers.monomorphic containers.domain))
|
||||
|
||||
(ocamllex
|
||||
|
|
|
|||
|
|
@ -101,3 +101,42 @@ eq' 63 (popcount max_int);;
|
|||
eq' 1 (popcount min_int);;
|
||||
eq' 10 (popcount 0b1110010110110001010L);;
|
||||
eq' 5 (popcount 0b1101110000000000L)
|
||||
|
||||
(* hash tests *)
|
||||
let ( >= ) = Stdlib.( >= )
|
||||
let ( = ) = Stdlib.( = )
|
||||
let ( <> ) = Stdlib.( <> );;
|
||||
|
||||
(* hash is non-negative *)
|
||||
t @@ fun () -> hash 0L >= 0;;
|
||||
t @@ fun () -> hash 1L >= 0;;
|
||||
t @@ fun () -> hash (-1L) >= 0;;
|
||||
t @@ fun () -> hash max_int >= 0;;
|
||||
t @@ fun () -> hash min_int >= 0;;
|
||||
|
||||
(* hash_to_int64 is non-negative *)
|
||||
t @@ fun () -> CCInt64.compare (hash_to_int64 0L) 0L >= 0;;
|
||||
t @@ fun () -> CCInt64.compare (hash_to_int64 (-1L)) 0L >= 0;;
|
||||
t @@ fun () -> CCInt64.compare (hash_to_int64 min_int) 0L >= 0;;
|
||||
|
||||
(* hash is consistent with hash_to_int64 *)
|
||||
t @@ fun () -> hash 42L = Stdlib.(Int64.to_int (hash_to_int64 42L) land max_int)
|
||||
;;
|
||||
|
||||
t @@ fun () ->
|
||||
hash (-1L) = Stdlib.(Int64.to_int (hash_to_int64 (-1L)) land max_int)
|
||||
;;
|
||||
|
||||
(* different inputs produce different hashes *)
|
||||
t @@ fun () -> hash 0L <> hash 1L;;
|
||||
t @@ fun () -> hash 1L <> hash 2L;;
|
||||
t @@ fun () -> hash 1L <> hash (-1L);;
|
||||
t @@ fun () -> hash_to_int64 0L <> hash_to_int64 1L;;
|
||||
|
||||
(* deterministic *)
|
||||
t @@ fun () -> hash 123L = hash 123L;;
|
||||
t @@ fun () -> hash_to_int64 123L = hash_to_int64 123L;;
|
||||
|
||||
(* quickcheck: hash is always non-negative *)
|
||||
q Q.(map Int64.of_int int) (fun n -> hash n >= 0);;
|
||||
q Q.(map Int64.of_int int) (fun n -> CCInt64.compare (hash_to_int64 n) 0L >= 0)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue