Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 : #include "rust-system.h"
19 :
20 : #ifndef RUST_FNV_HASH_H
21 : #define RUST_FNV_HASH_H
22 :
23 : namespace Rust {
24 : namespace Hash {
25 :
26 : const uint64_t offset128Lower = 0x62b821756295c58d;
27 : const uint64_t offset128Higher = 0x6c62272e07bb0142;
28 : const uint64_t prime128Lower = 0x13b;
29 : const uint64_t prime128Shift = 24;
30 :
31 : // ported from https://github.com/golang/go/blob/master/src/hash/fnv/fnv.go
32 : class FNV128
33 : {
34 : public:
35 38645 : FNV128 () { reset (); }
36 :
37 38645 : void reset ()
38 : {
39 38645 : buf[0] = offset128Higher;
40 38645 : buf[1] = offset128Lower;
41 : }
42 :
43 38645 : void write (const unsigned char *in, size_t len)
44 : {
45 3092949 : for (size_t i = 0; i < len; i++)
46 : {
47 3054304 : unsigned char c = in[i];
48 :
49 : // https://stackoverflow.com/questions/28868367/getting-the-high-part-of-64-bit-integer-multiplication
50 : // modified since a is sub-32-bit
51 3054304 : uint64_t a = prime128Lower;
52 3054304 : uint64_t b = buf[1];
53 :
54 3054304 : uint64_t b_lo = (uint32_t) b;
55 3054304 : uint64_t b_hi = b >> 32;
56 :
57 3054304 : uint64_t a_x_b_mid = a * b_hi;
58 3054304 : uint64_t a_x_b_lo = a * b_lo;
59 :
60 3054304 : uint64_t carry_bit
61 3054304 : = ((uint64_t) (uint32_t) a_x_b_mid + (a_x_b_lo >> 32)) >> 32;
62 :
63 3054304 : uint64_t multhi = (a_x_b_mid >> 32) + carry_bit;
64 :
65 3054304 : uint64_t s0 = multhi; // high
66 3054304 : uint64_t s1 = prime128Lower * buf[1]; // low
67 :
68 3054304 : s0 += (buf[1] << prime128Shift) + prime128Lower * buf[0];
69 :
70 : // Update the values
71 3054304 : buf[1] = s1;
72 3054304 : buf[0] = s0;
73 3054304 : buf[1] ^= (uint64_t) c;
74 : }
75 38645 : }
76 :
77 38645 : void sum (uint64_t *hi, uint64_t *lo) const
78 : {
79 38645 : *hi = buf[0];
80 38645 : *lo = buf[1];
81 : }
82 :
83 : private:
84 : uint64_t buf[2];
85 : };
86 :
87 : } // namespace Hash
88 : } // namespace Rust
89 :
90 : #endif // RUST_FNV_HASH_H
|