1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #undef LOG_TAG
18 #define LOG_TAG "DisplayIdentification"
19 
20 #include <cstring>
21 #include <type_traits>
22 
23 #include <log/log.h>
24 
25 #include "Hash.h"
26 
27 namespace android {
28 namespace {
29 
30 template <class T>
load(const void * p)31 inline T load(const void* p) {
32     static_assert(std::is_integral<T>::value, "T must be integral");
33 
34     T r;
35     std::memcpy(&r, p, sizeof(r));
36     return r;
37 }
38 
rotateByAtLeast1(uint64_t val,uint8_t shift)39 uint64_t rotateByAtLeast1(uint64_t val, uint8_t shift) {
40     return (val >> shift) | (val << (64 - shift));
41 }
42 
shiftMix(uint64_t val)43 uint64_t shiftMix(uint64_t val) {
44     return val ^ (val >> 47);
45 }
46 
hash64Len16(uint64_t u,uint64_t v)47 uint64_t hash64Len16(uint64_t u, uint64_t v) {
48     constexpr uint64_t kMul = 0x9ddfea08eb382d69;
49     uint64_t a = (u ^ v) * kMul;
50     a ^= (a >> 47);
51     uint64_t b = (v ^ a) * kMul;
52     b ^= (b >> 47);
53     b *= kMul;
54     return b;
55 }
56 
hash64Len0To16(const char * s,uint64_t len)57 uint64_t hash64Len0To16(const char* s, uint64_t len) {
58     constexpr uint64_t k2 = 0x9ae16a3b2f90404f;
59     constexpr uint64_t k3 = 0xc949d7c7509e6557;
60 
61     if (len > 8) {
62         const uint64_t a = load<uint64_t>(s);
63         const uint64_t b = load<uint64_t>(s + len - 8);
64         return hash64Len16(a, rotateByAtLeast1(b + len, static_cast<uint8_t>(len))) ^ b;
65     }
66     if (len >= 4) {
67         const uint32_t a = load<uint32_t>(s);
68         const uint32_t b = load<uint32_t>(s + len - 4);
69         return hash64Len16(len + (a << 3), b);
70     }
71     if (len > 0) {
72         const unsigned char a = static_cast<unsigned char>(s[0]);
73         const unsigned char b = static_cast<unsigned char>(s[len >> 1]);
74         const unsigned char c = static_cast<unsigned char>(s[len - 1]);
75         const uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
76         const uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2);
77         return shiftMix(y * k2 ^ z * k3) * k2;
78     }
79     return k2;
80 }
81 
82 } // namespace
83 
cityHash64Len0To16(std::string_view sv)84 uint64_t cityHash64Len0To16(std::string_view sv) {
85     auto len = sv.length();
86     if (len > 16) {
87         ALOGE("%s called with length %zu. Only hashing the first 16 chars", __FUNCTION__, len);
88         len = 16;
89     }
90     return hash64Len0To16(sv.data(), len);
91 }
92 
93 } // namespace android