aherbert commented on a change in pull request #27: Murmur3fix
URL: https://github.com/apache/commons-codec/pull/27#discussion_r341852924
 
 

 ##########
 File path: src/main/java/org/apache/commons/codec/digest/MurmurHash3.java
 ##########
 @@ -207,20 +211,24 @@ public static int hash32(final byte[] data, final int 
offset, final int length,
                // tail
                final int idx = nblocks << 2;
                int k1 = 0;
+               /*
+         * The original algorithm uses unsigned bytes.
+         * We have to mask to match the behavior of the unsigned bytes and 
prevent sign extension.
+         */
                switch (length - idx) {
-               case 3:
-                       k1 ^= data[offset + idx + 2] << 16;
-               case 2:
-                       k1 ^= data[offset + idx + 1] << 8;
-               case 1:
-                       k1 ^= data[offset + idx];
-
-                       // mix functions
-                       k1 *= C1_32;
-                       k1 = Integer.rotateLeft(k1, R1_32);
-                       k1 *= C2_32;
-                       hash ^= k1;
-               }
+       case 3:
+            k1 = (data[offset + idx + 2] & UBYTE_MASK) << 16;
+            // fallthrough
+        case 2:
+            k1 |= (data[offset + idx + 1] & UBYTE_MASK) << 8;
+            // fallthrough
+        case 1:
+            k1 |= (data[offset + idx] & UBYTE_MASK);
+            k1 *= C1_32;
+            k1 = (k1 << 15) | (k1 >>> 17);  // ROTL32(k1,15);
 
 Review comment:
   I would leave the use of Integer.rotateLeft() and the `// mix functions` 
comment.
   
   The whitespace formatting of this block is incorrect so the git diff seems 
larger than the actual changes. The final mix functions should not have to be 
changed, only the unsigned masking applied to k1.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to