1 /*
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
188
189 // tail (always empty, as body is always 32-bit chunks)
190
191 // finalization
192
193 h1 ^= len * 4; // (Integer.SIZE / Byte.SIZE);
194
195 // finalization mix force all bits of a hash block to avalanche
196 h1 ^= h1 >> 16;
197 h1 *= 0x85ebca6b;
198 h1 ^= h1 >> 13;
199 h1 *= 0xc2b2ae35;
200 h1 ^= h1 >> 16;
201
202 return h1;
203 }
204
205 juint AltHashing::murmur3_32(const int* data, int len) {
206 return murmur3_32(0, data, len);
207 }
208
209 #ifndef PRODUCT
210 // Overloaded versions for internal test.
211 juint AltHashing::murmur3_32(const jbyte* data, int len) {
212 return murmur3_32(0, data, len);
213 }
214
215 juint AltHashing::murmur3_32(const jchar* data, int len) {
216 return murmur3_32(0, data, len);
217 }
218
219 // Internal test for alternate hashing. Translated from JDK version
220 // test/sun/misc/Hashing.java
221 static const jbyte ONE_BYTE[] = { (jbyte) 0x80};
222 static const jbyte TWO_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81};
223 static const jchar ONE_CHAR[] = { (jchar) 0x8180};
224 static const jbyte THREE_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82};
225 static const jbyte FOUR_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83};
226 static const jchar TWO_CHAR[] = { (jchar) 0x8180, (jchar) 0x8382};
227 static const jint ONE_INT[] = { (jint)0x83828180};
228 static const jbyte SIX_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85};
229 static const jchar THREE_CHAR[] = { (jchar) 0x8180, (jchar) 0x8382, (jchar) 0x8584};
230 static const jbyte EIGHT_BYTE[] = {
231 (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82,
232 (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85,
233 (jbyte) 0x86, (jbyte) 0x87};
234 static const jchar FOUR_CHAR[] = {
235 (jchar) 0x8180, (jchar) 0x8382,
236 (jchar) 0x8584, (jchar) 0x8786};
237
238 static const jint TWO_INT[] = { (jint)0x83828180, (jint)0x87868584};
239
240 static const juint MURMUR3_32_X86_CHECK_VALUE = 0xB0F57EE3;
241
242 void AltHashing::testMurmur3_32_ByteArray() {
243 // printf("testMurmur3_32_ByteArray\n");
244
245 jbyte vector[256];
246 jbyte hashes[4 * 256];
247
248 for (int i = 0; i < 256; i++) {
249 vector[i] = (jbyte) i;
250 }
251
252 // Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
253 for (int i = 0; i < 256; i++) {
254 juint hash = murmur3_32(256 - i, vector, i);
255 hashes[i * 4] = (jbyte) hash;
256 hashes[i * 4 + 1] = (jbyte)(hash >> 8);
257 hashes[i * 4 + 2] = (jbyte)(hash >> 16);
258 hashes[i * 4 + 3] = (jbyte)(hash >> 24);
259 }
260
261 // hash to get const result.
262 juint final_hash = murmur3_32(hashes, 4*256);
263
264 assert (MURMUR3_32_X86_CHECK_VALUE == final_hash,
265 "Calculated hash result not as expected. Expected %08X got %08X\n",
266 MURMUR3_32_X86_CHECK_VALUE,
267 final_hash);
268 }
269
270 void AltHashing::testEquivalentHashes() {
271 juint jbytes, jchars, ints;
272
273 // printf("testEquivalentHashes\n");
274
275 jbytes = murmur3_32(TWO_BYTE, 2);
276 jchars = murmur3_32(ONE_CHAR, 1);
277 assert (jbytes == jchars,
278 "Hashes did not match. b:%08x != c:%08x\n", jbytes, jchars);
279
280 jbytes = murmur3_32(FOUR_BYTE, 4);
281 jchars = murmur3_32(TWO_CHAR, 2);
282 ints = murmur3_32(ONE_INT, 1);
283 assert ((jbytes == jchars) && (jbytes == ints),
284 "Hashes did not match. b:%08x != c:%08x != i:%08x\n", jbytes, jchars, ints);
285
286 jbytes = murmur3_32(SIX_BYTE, 6);
287 jchars = murmur3_32(THREE_CHAR, 3);
288 assert (jbytes == jchars,
289 "Hashes did not match. b:%08x != c:%08x\n", jbytes, jchars);
290
291 jbytes = murmur3_32(EIGHT_BYTE, 8);
292 jchars = murmur3_32(FOUR_CHAR, 4);
293 ints = murmur3_32(TWO_INT, 2);
294 assert ((jbytes == jchars) && (jbytes == ints),
295 "Hashes did not match. b:%08x != c:%08x != i:%08x\n", jbytes, jchars, ints);
296 }
297
298 // Returns true if the alternate hashcode is correct
299 void AltHashing::test_alt_hash() {
300 testMurmur3_32_ByteArray();
301 testEquivalentHashes();
302 }
303
304 void AltHashing_test() {
305 AltHashing::test_alt_hash();
306 }
307 #endif // PRODUCT
|
1 /*
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
188
189 // tail (always empty, as body is always 32-bit chunks)
190
191 // finalization
192
193 h1 ^= len * 4; // (Integer.SIZE / Byte.SIZE);
194
195 // finalization mix force all bits of a hash block to avalanche
196 h1 ^= h1 >> 16;
197 h1 *= 0x85ebca6b;
198 h1 ^= h1 >> 13;
199 h1 *= 0xc2b2ae35;
200 h1 ^= h1 >> 16;
201
202 return h1;
203 }
204
205 juint AltHashing::murmur3_32(const int* data, int len) {
206 return murmur3_32(0, data, len);
207 }
|