aboutsummaryrefslogtreecommitdiffstats
path: root/util/digest/numeric.h
blob: 791bce55a96082c29b9a518f53c97d07c20b6c0e (plain) (blame)
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
#pragma once

#include <util/generic/typelist.h>
#include <util/system/defaults.h>

#include <type_traits>

/*
 * original url (now dead): http://www.cris.com/~Ttwang/tech/inthash.htm
 * copy: https://gist.github.com/badboy/6267743
 */

static constexpr ui8 IntHashImpl(ui8 key8) noexcept {
    size_t key = key8;

    key += ~(key << 15);
    key ^= (key >> 10);
    key += (key << 3);
    key ^= (key >> 6);
    key += ~(key << 11);
    key ^= (key >> 16);

    return static_cast<ui8>(key);
}

static constexpr ui16 IntHashImpl(ui16 key16) noexcept {
    size_t key = key16;

    key += ~(key << 15);
    key ^= (key >> 10);
    key += (key << 3);
    key ^= (key >> 6);
    key += ~(key << 11);
    key ^= (key >> 16);

    return static_cast<ui16>(key);
}

static constexpr ui32 IntHashImpl(ui32 key) noexcept {
    key += ~(key << 15);
    key ^= (key >> 10);
    key += (key << 3);
    key ^= (key >> 6);
    key += ~(key << 11);
    key ^= (key >> 16);

    return key;
}

static constexpr ui64 IntHashImpl(ui64 key) noexcept {
    key += ~(key << 32);
    key ^= (key >> 22);
    key += ~(key << 13);
    key ^= (key >> 8);
    key += (key << 3);
    key ^= (key >> 15);
    key += ~(key << 27);
    key ^= (key >> 31);

    return key;
}

template <class T>
static constexpr T IntHash(T t) noexcept {
    using TCvt = TFixedWidthUnsignedInt<T>;

    return IntHashImpl((TCvt)(t));
}

/*
 * can handle floats && pointers
 */
template <class T>
static constexpr size_t NumericHash(T t) noexcept {
    if constexpr (std::is_floating_point<T>::value) {
        if (t == T(0)) { // the negative zero is equal to the positive zero, but has different bitwise representation
            t = T(0);    // make sure that the hash will be the same for both kind of zeros
        }
    }
    using TCvt = TFixedWidthUnsignedInt<T>;

    union Y_HIDDEN {
        T t;
        TCvt cvt;
    } u{t};

    return (size_t)IntHash(u.cvt);
}

template <class T>
[[nodiscard]] static constexpr T CombineHashes(T l, T r) noexcept {
    return IntHash(l) ^ r;
}