ocamlformat

This commit is contained in:
Simon Cruanes 2026-02-17 02:19:04 +00:00
parent 5a50d42352
commit 2827011b37
4 changed files with 13 additions and 12 deletions

View file

@ -1830,7 +1830,7 @@ module Hash = struct
let prime = 0x100000001b3L in let prime = 0x100000001b3L in
let h = ref offset_basis in let h = ref offset_basis in
for k = 0 to 7 do for k = 0 to 7 do
h := Int64.(mul !h prime); (h := Int64.(mul !h prime));
h := Int64.(logxor !h (of_int ((n lsr (k * 8)) land 0xff))) h := Int64.(logxor !h (of_int ((n lsr (k * 8)) land 0xff)))
done; done;
Int64.to_int !h land max_int Int64.to_int !h land max_int
@ -1846,10 +1846,7 @@ module Hash = struct
done done
in in
B.throughputN 3 ~repeat B.throughputN 3 ~repeat
[ [ "ocaml_fnv", run_ocaml, (); "c_stub", run_c_stub, () ]
"ocaml_fnv", run_ocaml, ();
"c_stub", run_c_stub, ();
]
let () = let () =
B.Tree.register ("hash" @>>> [ "int" @>> app_ints bench_hash [ 1_000 ] ]) B.Tree.register ("hash" @>>> [ "int" @>> app_ints bench_hash [ 1_000 ] ])

View file

@ -7,7 +7,8 @@ type 'a iter = ('a -> unit) -> unit
(* use FNV-1: (* use FNV-1:
https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function *) https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function *)
external hash : (int[@untagged]) -> (int[@untagged]) external hash : (int[@untagged]) -> (int[@untagged])
= "caml_cc_hash_int_byte" "caml_cc_hash_int" [@@noalloc] = "caml_cc_hash_int_byte" "caml_cc_hash_int"
[@@noalloc]
let range i j yield = let range i j yield =
let rec up i j yield = let rec up i j yield =

View file

@ -17,7 +17,8 @@ external hash_to_int64 : (int64[@unboxed]) -> (int64[@unboxed])
= "caml_cc_hash_int64_to_int64_byte" "caml_cc_hash_int64_to_int64" = "caml_cc_hash_int64_to_int64_byte" "caml_cc_hash_int64_to_int64"
external hash : (int64[@unboxed]) -> (int[@untagged]) external hash : (int64[@unboxed]) -> (int[@untagged])
= "caml_cc_hash_int64_byte" "caml_cc_hash_int64" [@@noalloc] = "caml_cc_hash_int64_byte" "caml_cc_hash_int64"
[@@noalloc]
(* see {!CCInt.popcount} for more details *) (* see {!CCInt.popcount} for more details *)
let[@inline] popcount (b : t) : int = let[@inline] popcount (b : t) : int =

View file

@ -103,8 +103,8 @@ eq' 10 (popcount 0b1110010110110001010L);;
eq' 5 (popcount 0b1101110000000000L) eq' 5 (popcount 0b1101110000000000L)
(* hash tests *) (* hash tests *)
let ( >= ) = Stdlib.( >= );; let ( >= ) = Stdlib.( >= )
let ( = ) = Stdlib.( = );; let ( = ) = Stdlib.( = )
let ( <> ) = Stdlib.( <> );; let ( <> ) = Stdlib.( <> );;
(* hash is non-negative *) (* hash is non-negative *)
@ -120,10 +120,12 @@ t @@ fun () -> CCInt64.compare (hash_to_int64 (-1L)) 0L >= 0;;
t @@ fun () -> CCInt64.compare (hash_to_int64 min_int) 0L >= 0;; t @@ fun () -> CCInt64.compare (hash_to_int64 min_int) 0L >= 0;;
(* hash is consistent with hash_to_int64 *) (* hash is consistent with hash_to_int64 *)
t @@ fun () -> hash 42L = Stdlib.(Int64.to_int (hash_to_int64 42L) land max_int)
;;
t @@ fun () -> t @@ fun () ->
hash 42L = Stdlib.(Int64.to_int (hash_to_int64 42L) land max_int);; hash (-1L) = Stdlib.(Int64.to_int (hash_to_int64 (-1L)) land max_int)
t @@ fun () -> ;;
hash (-1L) = Stdlib.(Int64.to_int (hash_to_int64 (-1L)) land max_int);;
(* different inputs produce different hashes *) (* different inputs produce different hashes *)
t @@ fun () -> hash 0L <> hash 1L;; t @@ fun () -> hash 0L <> hash 1L;;