File: hash.h

package info (click to toggle)
intel-compute-runtime 25.35.35096.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,324 kB
  • sloc: cpp: 926,243; lisp: 3,433; sh: 715; makefile: 162; python: 21
file content (115 lines) | stat: -rw-r--r-- 3,085 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2018-2022 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once
#include <cstddef>
#include <cstdint>

namespace NEO {
// clang-format off
#define HASH_JENKINS_MIX(a,b,c) \
{ \
    a -= b; a -= c; a ^= (c>>13); \
    b -= c; b -= a; b ^= (a<<8);  \
    c -= a; c -= b; c ^= (b>>13); \
    a -= b; a -= c; a ^= (c>>12); \
    b -= c; b -= a; b ^= (a<<16); \
    c -= a; c -= b; c ^= (b>>5);  \
    a -= b; a -= c; a ^= (c>>3);  \
    b -= c; b -= a; b ^= (a<<10); \
    c -= a; c -= b; c ^= (b>>15); \
}
// clang-format on
class Hash {
  public:
    Hash() {
        reset();
    };

    uint32_t getValue(const char *data, size_t size) {
        uint32_t value = 0;
        switch (size) {
        case 3:
            value = static_cast<uint32_t>(*reinterpret_cast<const unsigned char *>(data++));
            value <<= 8;
            [[fallthrough]];
        case 2:
            value |= static_cast<uint32_t>(*reinterpret_cast<const unsigned char *>(data++));
            value <<= 8;
            [[fallthrough]];
        case 1:
            value |= static_cast<uint32_t>(*reinterpret_cast<const unsigned char *>(data++));
            value <<= 8;
        }
        return value;
    }

    void update(const char *buff, size_t size) {
        if (buff == nullptr)
            return;

        if ((reinterpret_cast<uintptr_t>(buff) & 0x3) != 0) {
            const unsigned char *tmp = (const unsigned char *)buff;

            while (size >= sizeof(uint32_t)) {
                uint32_t value = (uint32_t)tmp[0] + (((uint32_t)tmp[1]) << 8) + ((uint32_t)tmp[2] << 16) + ((uint32_t)tmp[3] << 24);
                a ^= value;
                HASH_JENKINS_MIX(a, hi, lo);
                size -= sizeof(uint32_t);
                tmp += sizeof(uint32_t);
            }
            if (size > 0) {
                uint32_t value = getValue((char *)tmp, size);
                a ^= value;
                HASH_JENKINS_MIX(a, hi, lo);
            }
        } else {
            const uint32_t *tmp = reinterpret_cast<const uint32_t *>(buff);

            while (size >= sizeof(*tmp)) {
                a ^= *(tmp++);
                HASH_JENKINS_MIX(a, hi, lo);
                size -= sizeof(*tmp);
            }

            if (size > 0) {
                uint32_t value = getValue((char *)tmp, size);
                a ^= value;
                HASH_JENKINS_MIX(a, hi, lo);
            }
        }
    }

    uint64_t finish() {
        return (((uint64_t)hi) << 32) | lo;
    }

    void reset() {
        a = 0x428a2f98;
        hi = 0x71374491;
        lo = 0xb5c0fbcf;
    }

    static uint64_t hash(const char *buff, size_t size) {
        Hash hash;
        hash.update(buff, size);
        return hash.finish();
    }

  protected:
    uint32_t a, hi, lo;
};

template <typename T>
uint32_t hashPtrToU32(const T *src) {
    auto asInt = reinterpret_cast<uintptr_t>(src);
    constexpr auto m = sizeof(uintptr_t) / 8;
    asInt = asInt ^ ((asInt & ~(m - 1)) >> (m * 32));

    return static_cast<uint32_t>(asInt);
}
} // namespace NEO