1/* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15package com.google.common.hash; 16 17import static com.google.common.primitives.UnsignedBytes.toInt; 18 19import java.io.Serializable; 20import java.nio.ByteBuffer; 21 22/** 23 * See http://smhasher.googlecode.com/svn/trunk/MurmurHash3.cpp 24 * MurmurHash3_x86_32 25 * 26 * @author aappleby@google.com (Austin Appleby) 27 * @author andreou@google.com (Dimitris Andreou) 28 */ 29final class Murmur3_32HashFunction extends AbstractStreamingHashFunction implements Serializable { 30 private final int seed; 31 32 Murmur3_32HashFunction(int seed) { 33 this.seed = seed; 34 } 35 36 @Override public int bits() { 37 return 32; 38 } 39 40 @Override public Hasher newHasher() { 41 return new Murmur3_32Hasher(seed); 42 } 43 44 private static final class Murmur3_32Hasher extends AbstractStreamingHasher { 45 int h1; 46 int c1 = 0xcc9e2d51; 47 int c2 = 0x1b873593; 48 int len; 49 50 Murmur3_32Hasher(int seed) { 51 super(4); 52 h1 = seed; 53 } 54 55 @Override protected void process(ByteBuffer bb) { 56 int k1 = bb.getInt(); 57 len += 4; 58 59 k1 *= c1; 60 k1 = Integer.rotateLeft(k1, 15); 61 k1 *= c2; 62 63 h1 ^= k1; 64 h1 = Integer.rotateLeft(h1, 13); 65 h1 = h1 * 5 + 0xe6546b64; 66 } 67 68 @Override protected void processRemaining(ByteBuffer bb) { 69 len += bb.remaining(); 70 int k1 = 0; 71 switch (bb.remaining()) { 72 case 3: 73 k1 ^= toInt(bb.get(2)) << 16; 74 // fall through 75 case 2: 76 k1 ^= toInt(bb.get(1)) << 8; 77 // fall through 78 case 1: 79 k1 ^= toInt(bb.get(0)); 80 // fall through 81 default: 82 k1 *= c1; 83 k1 = Integer.rotateLeft(k1, 15); 84 k1 *= c2; 85 h1 ^= k1; 86 } 87 } 88 89 @Override public HashCode makeHash() { 90 h1 ^= len; 91 92 h1 ^= h1 >>> 16; 93 h1 *= 0x85ebca6b; 94 h1 ^= h1 >>> 13; 95 h1 *= 0xc2b2ae35; 96 h1 ^= h1 >>> 16; 97 98 return HashCodes.fromInt(h1); 99 } 100 } 101 102 private static final long serialVersionUID = 0L; 103} 104