Line data Source code
1 : /* 2 : * Copyright (C) 2019 Andrea Mazzoleni 3 : * 4 : * This program is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This program is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /* 19 : * Derivative work from metrohash128.cpp 20 : * 21 : * https://github.com/jandrewrogers/MetroHash/blob/master/src/metrohash128.cpp 22 : * 23 : * Copyright 2015-2018 J. Andrew Rogers 24 : * 25 : * Licensed under the Apache License, Version 2.0 (the "License"); 26 : * you may not use this file except in compliance with the License. 27 : * You may obtain a copy of the License at 28 : * 29 : * http://www.apache.org/licenses/LICENSE-2.0 30 : * 31 : * Unless required by applicable law or agreed to in writing, software 32 : * distributed under the License is distributed on an "AS IS" BASIS, 33 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 : * See the License for the specific language governing permissions and 35 : * limitations under the License. 36 : */ 37 : 38 : static const uint64_t k0 = 0xC83A91E1; 39 : static const uint64_t k1 = 0x8648DBDB; 40 : static const uint64_t k2 = 0x7BDEC03B; 41 : static const uint64_t k3 = 0x2F5870A5; 42 : 43 393 : void MetroHash128(const void* data, size_t size, const uint8_t* seed, uint8_t* digest) 44 : { 45 393 : const uint8_t* ptr = data; 46 : uint64_t v[4]; 47 : 48 393 : v[0] = (util_read64(seed) - k0) * k3; 49 393 : v[1] = (util_read64(seed + 8) + k1) * k2; 50 : 51 393 : if (size >= 32) { 52 357 : v[2] = (util_read64(seed) + k0) * k2; 53 357 : v[3] = (util_read64(seed + 8) - k1) * k3; 54 : 55 : do { 56 1049484 : v[0] += util_read64(ptr) * k0; ptr += 8; v[0] = util_rotr64(v[0], 29) + v[2]; 57 1049484 : v[1] += util_read64(ptr) * k1; ptr += 8; v[1] = util_rotr64(v[1], 29) + v[3]; 58 1049484 : v[2] += util_read64(ptr) * k2; ptr += 8; v[2] = util_rotr64(v[2], 29) + v[0]; 59 1049484 : v[3] += util_read64(ptr) * k3; ptr += 8; v[3] = util_rotr64(v[3], 29) + v[1]; 60 1049484 : size -= 32; 61 1049484 : } while (size >= 32); 62 : 63 357 : v[2] ^= util_rotr64(((v[0] + v[3]) * k0) + v[1], 21) * k1; 64 357 : v[3] ^= util_rotr64(((v[1] + v[2]) * k1) + v[0], 21) * k0; 65 357 : v[0] ^= util_rotr64(((v[0] + v[2]) * k0) + v[3], 21) * k1; 66 357 : v[1] ^= util_rotr64(((v[1] + v[3]) * k1) + v[2], 21) * k0; 67 : } 68 : 69 393 : if (size >= 16) { 70 132 : v[0] += util_read64(ptr) * k2; ptr += 8; v[0] = util_rotr64(v[0], 33) * k3; 71 132 : v[1] += util_read64(ptr) * k2; ptr += 8; v[1] = util_rotr64(v[1], 33) * k3; 72 132 : v[0] ^= util_rotr64((v[0] * k2) + v[1], 45) * k1; 73 132 : v[1] ^= util_rotr64((v[1] * k3) + v[0], 45) * k0; 74 132 : size -= 16; 75 : } 76 : 77 393 : if (size >= 8) { 78 134 : v[0] += util_read64(ptr) * k2; ptr += 8; v[0] = util_rotr64(v[0], 33) * k3; 79 134 : v[0] ^= util_rotr64((v[0] * k2) + v[1], 27) * k1; 80 134 : size -= 8; 81 : } 82 : 83 393 : if (size >= 4) { 84 132 : v[1] += util_read32(ptr) * k2; ptr += 4; v[1] = util_rotr64(v[1], 33) * k3; 85 132 : v[1] ^= util_rotr64((v[1] * k3) + v[0], 46) * k0; 86 132 : size -= 4; 87 : } 88 : 89 393 : if (size >= 2) { 90 135 : v[0] += util_read16(ptr) * k2; ptr += 2; v[0] = util_rotr64(v[0], 33) * k3; 91 135 : v[0] ^= util_rotr64((v[0] * k2) + v[1], 22) * k1; 92 135 : size -= 2; 93 : } 94 : 95 393 : if (size >= 1) { 96 133 : v[1] += util_read8(ptr) * k2; v[1] = util_rotr64(v[1], 33) * k3; 97 133 : v[1] ^= util_rotr64((v[1] * k3) + v[0], 58) * k0; 98 : } 99 : 100 393 : v[0] += util_rotr64((v[0] * k0) + v[1], 13); 101 393 : v[1] += util_rotr64((v[1] * k1) + v[0], 37); 102 393 : v[0] += util_rotr64((v[0] * k2) + v[1], 13); 103 393 : v[1] += util_rotr64((v[1] * k3) + v[0], 37); 104 : 105 393 : util_write64(digest, v[0]); 106 393 : util_write64(digest + 8, v[1]); 107 393 : } 108 :