-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hash function improvements #145
Conversation
vouillon
commented
Aug 2, 2023
- make sure that the hash of zero is not 0
- finalize the hash and fold the result to a positive 31-bit integer
- make sure that the hash of zero is not 0 - finalize the hash and fold the result to a positive 31-bit integer
- Hash chunks in the same order as the C implementation. (submitted a change to the C stubs to get the same result on all platforms: ocaml/Zarith#145) - Call caml_hash_mix_final to compute the final hash value and restrict the result to a positive 31-bit integer. Signed-off-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
- Hash chunks in the same order as the C implementation. (submitted a change to the C stubs to get the same result on all platforms: ocaml/Zarith#145) - Call caml_hash_mix_final to compute the final hash value and restrict the result to a positive 31-bit integer. Signed-off-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
- Hash chunks in the same order as the C implementation. (submitted a change to the C stubs to get the same result on all platforms: ocaml/Zarith#145) - Call caml_hash_mix_final to compute the final hash value and restrict the result to a positive 31-bit integer. Signed-off-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
I could use more context:
Clarifications welcome! |
My initial motivation was to harmonize hash results over 32-bits architectures (between zarith_stubs_js and zarith C stubs). Beside that:
|
Agreed. Let's do that.
I still don't understand why a special case is needed for zero...
If we really want this identity, we can just define Z.hash as Hashtbl.hash + a type constraint, like we do in the OCaml stdlib for String.hash, Int32.hash, etc. While we're at it, we could also define Z.seeded_hash. I'm tempted! |
- Hash chunks in the same order as the C implementation. (submitted a change to the C stubs to get the same result on all platforms: ocaml/Zarith#145) - Call caml_hash_mix_final to compute the final hash value and restrict the result to a positive 31-bit integer. Signed-off-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
- Hash chunks in the same order as the C implementation. (submitted a change to the C stubs to get the same result on all platforms: ocaml/Zarith#145) - Call caml_hash_mix_final to compute the final hash value and restrict the result to a positive 31-bit integer. Signed-off-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
- Hash chunks in the same order as the C implementation. (submitted a change to the C stubs to get the same result on all platforms: ocaml/Zarith#145) - Call caml_hash_mix_final to compute the final hash value and restrict the result to a positive 31-bit integer. Signed-off-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
- Hash chunks in the same order as the C implementation. (submitted a change to the C stubs to get the same result on all platforms: ocaml/Zarith#145) - Call caml_hash_mix_final to compute the final hash value and restrict the result to a positive 31-bit integer. Signed-off-by: Jérôme Vouillon <jerome.vouillon@gmail.com>
For consistency with other integer types in the standard library (modules Int, Int32, Int64, Nativeint), let's use the standard hash function (`Hashtbl.hash`) for `Z.hash` instead of our variant. This is a bit slower but has several benefits (see ocaml#145): - 32/64 bit compatibility - better mixing of the bits of the result. While we're at it, add a `Z.seeded_hash` function, defined as `Hashtbl.seeded_hash`, so that the `Z` module can be used as the argument to the `Hashtbl.MakeSeeded` functor.
After thinking some more about it, I believe |