Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/altHashing.cpp
32285 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* halfsiphash code adapted from reference implementation26* (https://github.com/veorq/SipHash/blob/master/halfsiphash.c)27* which is distributed with the following copyright:28*29* SipHash reference C implementation30*31* Copyright (c) 2016 Jean-Philippe Aumasson <[email protected]>32*33* To the extent possible under law, the author(s) have dedicated all copyright34* and related and neighboring rights to this software to the public domain35* worldwide. This software is distributed without any warranty.36*37* You should have received a copy of the CC0 Public Domain Dedication along38* with this software. If not, see39* <http://creativecommons.org/publicdomain/zero/1.0/>.40*/4142#include "precompiled.hpp"43#include "classfile/altHashing.hpp"44#include "classfile/systemDictionary.hpp"45#include "oops/markOop.hpp"46#include "runtime/os.hpp"4748// Get the hash code of the classes mirror if it exists, otherwise just49// return a random number, which is one of the possible hash code used for50// objects. We don't want to call the synchronizer hash code to install51// this value because it may safepoint.52intptr_t object_hash(Klass* k) {53intptr_t hc = k->java_mirror()->mark()->hash();54return hc != markOopDesc::no_hash ? hc : os::random();55}5657// Seed value used for each alternative hash calculated.58uint64_t AltHashing::compute_seed() {59uint64_t nanos = os::javaTimeNanos();60uint64_t now = os::javaTimeMillis();61uint32_t SEED_MATERIAL[8] = {62(uint32_t) object_hash(SystemDictionary::String_klass()),63(uint32_t) object_hash(SystemDictionary::System_klass()),64(uint32_t) os::random(), // current thread isn't a java thread65(uint32_t) (((uint64_t)nanos) >> 32),66(uint32_t) nanos,67(uint32_t) (((uint64_t)now) >> 32),68(uint32_t) now,69(uint32_t) (os::javaTimeNanos() >> 2)70};7172return halfsiphash_64(SEED_MATERIAL, 8);73}7475// utility function copied from java/lang/Integer76static uint32_t Integer_rotateLeft(uint32_t i, int distance) {77return (i << distance) | (i >> (32 - distance));78}7980static void halfsiphash_rounds(uint32_t v[4], int rounds) {81while (rounds-- > 0) {82v[0] += v[1];83v[1] = Integer_rotateLeft(v[1], 5);84v[1] ^= v[0];85v[0] = Integer_rotateLeft(v[0], 16);86v[2] += v[3];87v[3] = Integer_rotateLeft(v[3], 8);88v[3] ^= v[2];89v[0] += v[3];90v[3] = Integer_rotateLeft(v[3], 7);91v[3] ^= v[0];92v[2] += v[1];93v[1] = Integer_rotateLeft(v[1], 13);94v[1] ^= v[2];95v[2] = Integer_rotateLeft(v[2], 16);96}97}9899static void halfsiphash_adddata(uint32_t v[4], uint32_t newdata, int rounds) {100v[3] ^= newdata;101halfsiphash_rounds(v, rounds);102v[0] ^= newdata;103}104105static void halfsiphash_init32(uint32_t v[4], uint64_t seed) {106v[0] = seed & 0xffffffff;107v[1] = seed >> 32;108v[2] = 0x6c796765 ^ v[0];109v[3] = 0x74656462 ^ v[1];110}111112static void halfsiphash_init64(uint32_t v[4], uint64_t seed) {113halfsiphash_init32(v, seed);114v[1] ^= 0xee;115}116117uint32_t halfsiphash_finish32(uint32_t v[4], int rounds) {118v[2] ^= 0xff;119halfsiphash_rounds(v, rounds);120return (v[1] ^ v[3]);121}122123static uint64_t halfsiphash_finish64(uint32_t v[4], int rounds) {124uint64_t rv;125v[2] ^= 0xee;126halfsiphash_rounds(v, rounds);127rv = v[1] ^ v[3];128v[1] ^= 0xdd;129halfsiphash_rounds(v, rounds);130rv |= (uint64_t)(v[1] ^ v[3]) << 32;131return rv;132}133134// HalfSipHash-2-4 (32-bit output) for Symbols135uint32_t AltHashing::halfsiphash_32(uint64_t seed, const uint8_t* data, int len) {136uint32_t v[4];137uint32_t newdata;138int off = 0;139int count = len;140141halfsiphash_init32(v, seed);142// body143while (count >= 4) {144// Avoid sign extension with 0x0ff145newdata = (data[off] & 0x0FF)146| (data[off + 1] & 0x0FF) << 8147| (data[off + 2] & 0x0FF) << 16148| data[off + 3] << 24;149150count -= 4;151off += 4;152153halfsiphash_adddata(v, newdata, 2);154}155156// tail157newdata = ((uint32_t)len) << 24; // (Byte.SIZE / Byte.SIZE);158159if (count > 0) {160switch (count) {161case 3:162newdata |= (data[off + 2] & 0x0ff) << 16;163// fall through164case 2:165newdata |= (data[off + 1] & 0x0ff) << 8;166// fall through167case 1:168newdata |= (data[off] & 0x0ff);169// fall through170}171}172173halfsiphash_adddata(v, newdata, 2);174175// finalization176return halfsiphash_finish32(v, 4);177}178179// HalfSipHash-2-4 (32-bit output) for Strings180uint32_t AltHashing::halfsiphash_32(uint64_t seed, const uint16_t* data, int len) {181uint32_t v[4];182uint32_t newdata;183int off = 0;184int count = len;185186halfsiphash_init32(v, seed);187188// body189while (count >= 2) {190uint16_t d1 = data[off++] & 0x0FFFF;191uint16_t d2 = data[off++];192newdata = (d1 | d2 << 16);193194count -= 2;195196halfsiphash_adddata(v, newdata, 2);197}198199// tail200newdata = ((uint32_t)len * 2) << 24; // (Character.SIZE / Byte.SIZE);201if (count > 0) {202newdata |= (uint32_t)data[off];203}204halfsiphash_adddata(v, newdata, 2);205206// finalization207return halfsiphash_finish32(v, 4);208}209210// HalfSipHash-2-4 (64-bit output) for integers (used to create seed)211uint64_t AltHashing::halfsiphash_64(uint64_t seed, const uint32_t* data, int len) {212uint32_t v[4];213214int off = 0;215int end = len;216217halfsiphash_init64(v, seed);218219// body220while (off < end) {221halfsiphash_adddata(v, (uint32_t)data[off++], 2);222}223224// tail (always empty, as body is always 32-bit chunks)225226// finalization227halfsiphash_adddata(v, ((uint32_t)len * 4) << 24, 2); // (Integer.SIZE / Byte.SIZE);228return halfsiphash_finish64(v, 4);229}230231// HalfSipHash-2-4 (64-bit output) for integers (used to create seed)232uint64_t AltHashing::halfsiphash_64(const uint32_t* data, int len) {233return halfsiphash_64((uint64_t)0, data, len);234}235236#ifndef PRODUCT237void AltHashing::testHalfsiphash_32_ByteArray() {238const int factor = 4;239240uint8_t vector[256];241uint8_t hashes[factor * 256];242243for (int i = 0; i < 256; i++) {244vector[i] = (uint8_t) i;245}246247// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}248for (int i = 0; i < 256; i++) {249uint32_t hash = AltHashing::halfsiphash_32(256 - i, vector, i);250hashes[i * factor] = (uint8_t) hash;251hashes[i * factor + 1] = (uint8_t)(hash >> 8);252hashes[i * factor + 2] = (uint8_t)(hash >> 16);253hashes[i * factor + 3] = (uint8_t)(hash >> 24);254}255256// hash to get const result.257uint32_t final_hash = AltHashing::halfsiphash_32(0, hashes, factor*256);258259// Value found using reference implementation for the hashes array.260//uint64_t k = 0; // seed261//uint32_t reference;262//halfsiphash((const uint8_t*)hashes, factor*256, (const uint8_t *)&k, (uint8_t*)&reference, 4);263//printf("0x%x", reference);264265static const uint32_t HALFSIPHASH_32_BYTE_CHECK_VALUE = 0xd2be7fd8;266267assert (HALFSIPHASH_32_BYTE_CHECK_VALUE == final_hash,268err_msg(269"Calculated hash result not as expected. Expected " UINT32_FORMAT " got " UINT32_FORMAT,270HALFSIPHASH_32_BYTE_CHECK_VALUE,271final_hash));272}273274void AltHashing::testHalfsiphash_32_CharArray() {275const int factor = 2;276277uint16_t vector[256];278uint16_t hashes[factor * 256];279280for (int i = 0; i < 256; i++) {281vector[i] = (uint16_t) i;282}283284// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}285for (int i = 0; i < 256; i++) {286uint32_t hash = AltHashing::halfsiphash_32(256 - i, vector, i);287hashes[i * factor] = (uint16_t) hash;288hashes[i * factor + 1] = (uint16_t)(hash >> 16);289}290291// hash to get const result.292uint32_t final_hash = AltHashing::halfsiphash_32(0, hashes, factor*256);293294// Value found using reference implementation for the hashes array.295//uint64_t k = 0; // seed296//uint32_t reference;297//halfsiphash((const uint8_t*)hashes, 2*factor*256, (const uint8_t *)&k, (uint8_t*)&reference, 4);298//printf("0x%x", reference);299300static const uint32_t HALFSIPHASH_32_CHAR_CHECK_VALUE = 0x428bf8a5;301302assert(HALFSIPHASH_32_CHAR_CHECK_VALUE == final_hash,303err_msg(304"Calculated hash result not as expected. Expected " UINT32_FORMAT " got " UINT32_FORMAT,305HALFSIPHASH_32_CHAR_CHECK_VALUE,306final_hash));307}308309// Test against sample hashes published with the reference implementation:310// https://github.com/veorq/SipHash311void AltHashing::testHalfsiphash_64_FromReference() {312313const uint64_t seed = 0x0706050403020100;314const uint64_t results[16] = {3150xc83cb8b9591f8d21, 0xa12ee55b178ae7d5,3160x8c85e4bc20e8feed, 0x99c7f5ae9f1fc77b,3170xb5f37b5fd2aa3673, 0xdba7ee6f0a2bf51b,3180xf1a63fae45107470, 0xb516001efb5f922d,3190x6c6211d8469d7028, 0xdc7642ec407ad686,3200x4caec8671cc8385b, 0x5ab1dc27adf3301e,3210x3e3ea94bc0a8eaa9, 0xe150f598795a4402,3220x1d5ff142f992a4a1, 0x60e426bf902876d6323};324uint32_t vector[16];325326for (int i = 0; i < 16; i++)327vector[i] = 0x03020100 + i * 0x04040404;328329for (int i = 0; i < 16; i++) {330uint64_t hash = AltHashing::halfsiphash_64(seed, vector, i);331assert(results[i] == hash,332err_msg(333"Calculated hash result not as expected. Round %d: "334"Expected " UINT64_FORMAT_X " got " UINT64_FORMAT_X "\n",335i,336results[i],337hash));338}339}340341void AltHashing::test_alt_hash() {342testHalfsiphash_32_ByteArray();343testHalfsiphash_32_CharArray();344testHalfsiphash_64_FromReference();345}346#endif // PRODUCT347348349