]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/inchash.h
Update copyright years.
[thirdparty/gcc.git] / gcc / inchash.h
CommitLineData
6d8eb96b 1/* An incremental hash abstract data type.
cbe34bb5 2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
6d8eb96b
AK
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef INCHASH_H
21#define INCHASH_H 1
22
6d8eb96b
AK
23
24/* This file implements an incremential hash function ADT, to be used
25 by code that incrementially hashes a lot of unrelated data
26 (not in a single memory block) into a single value. The goal
27 is to make it easy to plug in efficient hash algorithms.
28 Currently it just implements the plain old jhash based
29 incremental hash from gcc's tree.c. */
30
2d44c7de
ML
31hashval_t iterative_hash_host_wide_int (HOST_WIDE_INT, hashval_t);
32hashval_t iterative_hash_hashval_t (hashval_t, hashval_t);
6d8eb96b 33
50de5793
AK
34namespace inchash
35{
36
37class hash
6d8eb96b
AK
38{
39 public:
40
41 /* Start incremential hashing, optionally with SEED. */
50de5793 42 hash (hashval_t seed = 0)
6d8eb96b
AK
43 {
44 val = seed;
45 bits = 0;
46 }
47
48 /* End incremential hashing and provide the final value. */
49 hashval_t end ()
50 {
51 return val;
52 }
53
54 /* Add unsigned value V. */
55 void add_int (unsigned v)
56 {
57 val = iterative_hash_hashval_t (v, val);
58 }
59
60 /* Add HOST_WIDE_INT value V. */
61 void add_wide_int (HOST_WIDE_INT v)
62 {
63 val = iterative_hash_host_wide_int (v, val);
64 }
65
66 /* Hash in pointer PTR. */
2d44c7de 67 void add_ptr (const void *ptr)
6d8eb96b
AK
68 {
69 add (&ptr, sizeof (ptr));
70 }
71
72 /* Add a memory block DATA with size LEN. */
73 void add (const void *data, size_t len)
74 {
75 val = iterative_hash (data, len, val);
76 }
77
78 /* Merge hash value OTHER. */
79 void merge_hash (hashval_t other)
80 {
81 val = iterative_hash_hashval_t (other, val);
82 }
83
84 /* Hash in state from other inchash OTHER. */
50de5793 85 void merge (hash &other)
6d8eb96b
AK
86 {
87 merge_hash (other.val);
88 }
89
50de5793
AK
90 template<class T> void add_object(T &obj)
91 {
92 add (&obj, sizeof(T));
93 }
94
6d8eb96b
AK
95 /* Support for accumulating boolean flags */
96
97 void add_flag (bool flag)
98 {
99 bits = (bits << 1) | flag;
100 }
101
102 void commit_flag ()
103 {
104 add_int (bits);
105 bits = 0;
106 }
107
108 /* Support for commutative hashing. Add A and B in a defined order
109 based on their value. This is useful for hashing commutative
110 expressions, so that A+B and B+A get the same hash. */
111
50de5793 112 void add_commutative (hash &a, hash &b)
6d8eb96b
AK
113 {
114 if (a.end() > b.end())
115 {
2d44c7de 116 merge (b);
6d8eb96b
AK
117 merge (a);
118 }
119 else
120 {
2d44c7de
ML
121 merge (a);
122 merge (b);
6d8eb96b
AK
123 }
124 }
125
126 private:
127 hashval_t val;
128 unsigned bits;
129};
130
50de5793 131}
6d8eb96b 132
2d44c7de
ML
133/* Borrowed from hashtab.c iterative_hash implementation. */
134#define mix(a,b,c) \
135{ \
136 a -= b; a -= c; a ^= (c>>13); \
137 b -= c; b -= a; b ^= (a<< 8); \
138 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
139 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
140 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
141 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
142 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
143 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
144 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
145}
146
147
148/* Produce good hash value combining VAL and VAL2. */
149inline
150hashval_t
151iterative_hash_hashval_t (hashval_t val, hashval_t val2)
152{
153 /* the golden ratio; an arbitrary value. */
154 hashval_t a = 0x9e3779b9;
155
156 mix (a, val, val2);
157 return val2;
158}
159
160/* Produce good hash value combining VAL and VAL2. */
161
162inline
163hashval_t
164iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
165{
166 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
167 return iterative_hash_hashval_t (val, val2);
168 else
169 {
170 hashval_t a = (hashval_t) val;
171 /* Avoid warnings about shifting of more than the width of the type on
172 hosts that won't execute this path. */
173 int zero = 0;
174 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
175 mix (a, b, val2);
176 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
177 {
178 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
179 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
180 mix (a, b, val2);
181 }
182 return val2;
183 }
184}
185
6d8eb96b 186#endif