Skip to content

replace the hash algorithm with the one used in byterun/hash.c #269

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

Merged
merged 2 commits into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jscomp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<!-- <script src="./async.js"></script> -->
<script src="./require1k.js"></script>
<script>
R('./test/class_repr', function(err,v){
R('./test/hash_test', function(err,v){
window.v = v
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion jscomp/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ let format = "Caml_format"
let string = "Caml_string"

let float = "Caml_float"

let hash = "Caml_hash"
let oo = "Caml_oo"
let curry = "Caml_curry"
let internalMod = "Caml_internalMod"
Expand Down
1 change: 1 addition & 0 deletions jscomp/js_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ val bigarray : string
val unix : string
val int64 : string
val md5 : string
val hash : string
5 changes: 4 additions & 1 deletion jscomp/lam_dispatch_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@ let query (prim : Lam_compile_env.primitive_description)
end
| "caml_md5_string"
-> E.runtime_call Js_config.md5 prim.prim_name args
| "caml_hash"
-> E.runtime_call Js_config.hash prim.prim_name args

| "caml_output_value_to_buffer"
| "caml_marshal_data_size"
| "caml_input_value_from_string"
Expand All @@ -799,7 +802,7 @@ let query (prim : Lam_compile_env.primitive_description)
| "caml_output_value_to_string"

| "caml_md5_chan"
| "caml_hash"

| "caml_hash_univ_param"
| "caml_weak_set"
| "caml_weak_create"
Expand Down
10 changes: 10 additions & 0 deletions jscomp/runtime/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ caml_bigarray.cmi :
caml_builtin_exceptions.cmi :
caml_float.cmi :
caml_format.cmi :
caml_hash.cmi :
caml_int64.cmi :
caml_lexer.cmi :
caml_md5.cmi :
caml_obj.cmi :
caml_oo.cmi :
caml_parser.cmi :
caml_primitive.cmi :
caml_queue.cmi :
caml_string.cmi :
caml_sys.cmi :
caml_utils.cmi :
Expand All @@ -25,6 +27,8 @@ caml_float.cmo : typed_array.cmo js.cmo caml_float.cmi
caml_float.cmx : typed_array.cmx js.cmx caml_float.cmi
caml_format.cmo : js.cmo caml_utils.cmi caml_format.cmi
caml_format.cmx : js.cmx caml_utils.cmx caml_format.cmi
caml_hash.cmo : js.cmo caml_queue.cmi caml_hash.cmi
caml_hash.cmx : js.cmx caml_queue.cmx caml_hash.cmi
caml_int64.cmo : typed_array.cmo js.cmo caml_utils.cmi caml_int64.cmi
caml_int64.cmx : typed_array.cmx js.cmx caml_utils.cmx caml_int64.cmi
caml_io.cmo : js.cmo
Expand All @@ -41,6 +45,8 @@ caml_parser.cmo : caml_parser.cmi
caml_parser.cmx : caml_parser.cmi
caml_primitive.cmo : caml_primitive.cmi
caml_primitive.cmx : caml_primitive.cmi
caml_queue.cmo : caml_queue.cmi
caml_queue.cmx : caml_queue.cmi
caml_string.cmo : js.cmo caml_string.cmi
caml_string.cmx : js.cmx caml_string.cmi
caml_sys.cmo : js.cmo caml_sys.cmi
Expand All @@ -67,6 +73,8 @@ caml_float.cmo : typed_array.cmo js.cmo caml_float.cmi
caml_float.cmj : typed_array.cmj js.cmj caml_float.cmi
caml_format.cmo : js.cmo caml_utils.cmi caml_format.cmi
caml_format.cmj : js.cmj caml_utils.cmj caml_format.cmi
caml_hash.cmo : js.cmo caml_queue.cmi caml_hash.cmi
caml_hash.cmj : js.cmj caml_queue.cmj caml_hash.cmi
caml_int64.cmo : typed_array.cmo js.cmo caml_utils.cmi caml_int64.cmi
caml_int64.cmj : typed_array.cmj js.cmj caml_utils.cmj caml_int64.cmi
caml_io.cmo : js.cmo
Expand All @@ -83,6 +91,8 @@ caml_parser.cmo : caml_parser.cmi
caml_parser.cmj : caml_parser.cmi
caml_primitive.cmo : caml_primitive.cmi
caml_primitive.cmj : caml_primitive.cmi
caml_queue.cmo : caml_queue.cmi
caml_queue.cmj : caml_queue.cmi
caml_string.cmo : js.cmo caml_string.cmi
caml_string.cmj : js.cmj caml_string.cmi
caml_sys.cmo : js.cmo caml_sys.cmi
Expand Down
111 changes: 111 additions & 0 deletions jscomp/runtime/caml_hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Generated CODE, PLEASE EDIT WITH CARE
'use strict';

var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
var Caml_queue = require("./caml_queue");
var Caml_primitive = require("./caml_primitive");

function rotl32(x, n) {
return (x << n) | (x >>> (32 - n | 0));
}

function mix(h, d) {
var d$1 = d;
d$1 = Caml_primitive.imul(d$1, 3432918353);
d$1 = rotl32(d$1, 15);
d$1 = Caml_primitive.imul(d$1, 461845907);
var h$1 = h ^ d$1;
h$1 = rotl32(h$1, 13);
return (h$1 + (h$1 << 2) | 0) + 3864292196 | 0;
}

function final_mix(h) {
var h$1 = h ^ (h >>> 16);
h$1 = Caml_primitive.imul(h$1, 2246822507);
h$1 = h$1 ^ (h$1 >>> 13);
h$1 = Caml_primitive.imul(h$1, 3266489909);
return h$1 ^ (h$1 >>> 16);
}

function caml_hash_mix_string(h, s) {
var len = s.length;
var block = (len / 4 | 0) - 1 | 0;
var hash = h;
for(var i = 0; i<= block; ++i){
var j = (i << 2);
var w = s.charCodeAt(j) | (s.charCodeAt(j + 1 | 0) << 8) | (s.charCodeAt(j + 2 | 0) << 16) | (s.charCodeAt(j + 3 | 0) << 24);
hash = mix(hash, w);
}
var modulo = len & 3;
if (modulo !== 0) {
var w$1 = modulo === 3 ? (s.charCodeAt(len - 1 | 0) << 16) | (s.charCodeAt(len - 2 | 0) << 8) | s.charCodeAt(len - 3 | 0) : (
modulo === 2 ? (s.charCodeAt(len - 1 | 0) << 8) | s.charCodeAt(len - 2 | 0) : s.charCodeAt(len - 1 | 0)
);
hash = mix(hash, w$1);
}
hash = hash ^ len;
return hash;
}

function caml_hash(count, _, seed, obj) {
var hash = seed;
if (typeof obj === "number") {
var u = obj | 0;
hash = mix(hash, (u + u | 0) + 1 | 0);
return final_mix(hash);
}
else if (typeof obj === "string") {
hash = caml_hash_mix_string(hash, obj);
return final_mix(hash);
}
else {
var queue = /* record */[
0,
/* None */0
];
var num = count;
Caml_queue.push(obj, queue);
num = num - 1 | 0;
while(queue[/* length */0] !== 0 && num > 0) {
var obj$1 = Caml_queue.unsafe_pop(queue);
if (typeof obj$1 === "number") {
hash = mix(hash, obj$1 | 0);
num = num - 1 | 0;
}
else if (typeof obj$1 === "string") {
hash = caml_hash_mix_string(hash, obj$1);
num = num - 1 | 0;
}
else if (typeof obj$1 !== "boolean") {
if (typeof obj$1 !== "undefined") {
if (typeof obj$1 === "symbol") {
throw [
Caml_builtin_exceptions.assert_failure,
[
"caml_hash.ml",
124,
8
]
];
}
else if (typeof obj$1 !== "function") {
var tag = obj$1.tag | 0;
hash = mix(hash, tag);
var v = obj$1.length - 1 | 0;
var block = v < num ? v : num;
for(var i = 0; i<= block; ++i){
Caml_queue.push(obj$1[i], queue);
}
}

}

}

};
return final_mix(hash);
}
}

exports.caml_hash = caml_hash;
/* No side effect */
137 changes: 137 additions & 0 deletions jscomp/runtime/caml_hash.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
(* BuckleScript compiler
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

(* Author: Hongbo Zhang *)

let (<< ) = Nativeint.shift_left
let (>>>) = Nativeint.shift_right_logical
let (|~) = Nativeint.logor
let (^) = Nativeint.logxor

external ( *~ ) : nativeint -> nativeint -> nativeint = "caml_int32_mul"
external ( +~ ) : nativeint -> nativeint -> nativeint = "caml_int32_add"



let rotl32 (x : nativeint) n =
(x << n) |~ (x >>> (32 - n))


let mix h d =
let d = ref d in
d := !d *~ 0xcc9e2d51n ;
d := rotl32 !d 15 ;
d := !d *~ 0x1b873593n ;
let h = ref (h ^ !d) in
h := rotl32 !h 13 ;
!h +~ (!h << 2) +~ 0xe6546b64n

let final_mix h =
let h = ref (h ^ (h >>> 16)) in
h := !h *~ 0x85ebca6bn ;
h := !h ^ (!h >>> 13);
h := !h *~ 0xc2b2ae35n ;
!h ^ (!h >>> 16)
(* Nativeint.logand (!h ^ (!h >>> 16)) 0x3FFFFFFFn *)

let caml_hash_mix_string h s =
let len = String.length s in
let block = len / 4 - 1 in
let hash = ref h in
for i = 0 to block do
let j = 4 * i in
let w =
Char.code s.[j] lor
(Char.code s.[j+1] lsl 8) lor
(Char.code s.[j+2] lsl 16) lor
(Char.code s.[j+3] lsl 24)
in
hash := mix !hash (Nativeint.of_int w)
done ;
let modulo = len land 0b11 in
if modulo <> 0 then
begin
let w =
if modulo = 3 then
(Char.code s.[len - 1] lsl 16) lor
(Char.code s.[len - 2] lsl 8) lor
(Char.code s.[len - 3])
else if modulo = 2 then
(Char.code s.[len -1] lsl 8) lor
Char.code s.[len -2]
else Char.code s.[len - 1]
in
hash := mix !hash (Nativeint.of_int w)
end;
hash := !hash ^ (Nativeint.of_int len) ;
!hash


let caml_hash count _limit seed obj =
let hash = ref seed in
if Js.typeof obj = "number" then
begin
let u = (Nativeint.of_float (Obj.magic obj)) in
hash := mix !hash (u +~ u +~ 1n) ;
final_mix !hash
end
else if Js.typeof obj = "string" then
begin
hash := caml_hash_mix_string !hash (Obj.magic obj : string);
final_mix !hash
end
(* TODO: hash [null] [undefined] as well *)
else

let queue = Caml_queue.create () in
let num = ref count in
let () =
Caml_queue.push obj queue;
decr num
in
while not @@ Caml_queue.is_empty queue && !num > 0 do
let obj = Caml_queue.unsafe_pop queue in
if Js.typeof obj = "number" then
begin
hash := mix !hash (Nativeint.of_float (Obj.magic obj));
decr num ;
end
else if Js.typeof obj = "string" then
begin
hash := caml_hash_mix_string !hash (Obj.magic obj : string);
decr num
end
else if Js.typeof obj = "boolean" then
()
else if Js.typeof obj = "undefined" then
()
else if Js.typeof obj = "symbol" then
assert false (* TODO *)
else if Js.typeof obj = "function" then
()
else
let tag = Obj.tag obj in
hash := mix !hash (Nativeint.of_int tag) ;
let block =
let v = Obj.size obj - 1 in if v < !num then v else !num in
for i = 0 to block do
Caml_queue.push (Obj.field obj i ) queue
done
done;
final_mix !hash

21 changes: 21 additions & 0 deletions jscomp/runtime/caml_hash.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(* BuckleScript compiler
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

(* Author: Hongbo Zhang *)

val caml_hash : int -> 'a -> nativeint -> Obj.t -> nativeint
Loading