Permalink
Browse files

Fixes bug in multiplication.

Multiplication was not masked to 32 bits, leading to different hash
codes from the reference implementation. This fixes that.
  • Loading branch information...
1 parent aaedde2 commit 7d1a1f210115d17f12d9a1aec1287481d4719822 @TimDumol TimDumol committed Apr 6, 2011
Showing with 7 additions and 7 deletions.
  1. +7 −7 murmurhash3_gc.js
View
@@ -29,13 +29,13 @@ function murmurhash3_32_gc(key, seed) {
((key.charCodeAt(++i) & 0xff) << 24);
++i;
- k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16));
+ k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
- k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16));
+ k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
- h1b = (((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16));
+ h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
}
@@ -46,18 +46,18 @@ function murmurhash3_32_gc(key, seed) {
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
case 1: k1 ^= (key.charCodeAt(i) & 0xff);
- k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16));
+ k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
k1 = (k1 << 16) | (k1 >>> 16);
- k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16));
+ k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
h1 ^= k1;
}
h1 ^= key.length;
h1 ^= h1 >>> 16;
- h1 = (((h1 & 0xffff) * 0xca6b) + ((((h1 >>> 16) * 0x85eb) & 0xffff) << 16));
+ h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
h1 ^= h1 >>> 13;
- h1 = (((h1 & 0xffff) * 0xae35) + ((((h1 >>> 16) * 0xc2b2) & 0xffff) << 16));
+ h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
h1 ^= h1 >>> 16;
return h1 >>> 0;

0 comments on commit 7d1a1f2

Please sign in to comment.