1 /* An incremental hash abstract data type.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
25 /* This file implements an incremential hash function ADT, to be used
26 by code that incrementially hashes a lot of unrelated data
27 (not in a single memory block) into a single value. The goal
28 is to make it easy to plug in efficient hash algorithms.
29 Currently it just implements the plain old jhash based
30 incremental hash from gcc's tree.c. */
32 hashval_t
iterative_hash_host_wide_int (HOST_WIDE_INT
, hashval_t
);
33 hashval_t
iterative_hash_hashval_t (hashval_t
, hashval_t
);
42 /* Start incremential hashing, optionally with SEED. */
43 hash (hashval_t seed
= 0)
49 /* End incremential hashing and provide the final value. */
55 /* Add unsigned value V. */
56 void add_int (unsigned v
)
58 val
= iterative_hash_hashval_t (v
, val
);
61 /* Add HOST_WIDE_INT value V. */
62 void add_wide_int (HOST_WIDE_INT v
)
64 val
= iterative_hash_host_wide_int (v
, val
);
67 /* Hash in pointer PTR. */
68 void add_ptr (const void *ptr
)
70 add (&ptr
, sizeof (ptr
));
73 /* Add a memory block DATA with size LEN. */
74 void add (const void *data
, size_t len
)
76 val
= iterative_hash (data
, len
, val
);
79 /* Merge hash value OTHER. */
80 void merge_hash (hashval_t other
)
82 val
= iterative_hash_hashval_t (other
, val
);
85 /* Hash in state from other inchash OTHER. */
86 void merge (hash
&other
)
88 merge_hash (other
.val
);
91 template<class T
> void add_object(T
&obj
)
93 add (&obj
, sizeof(T
));
96 /* Support for accumulating boolean flags */
98 void add_flag (bool flag
)
100 bits
= (bits
<< 1) | flag
;
109 /* Support for commutative hashing. Add A and B in a defined order
110 based on their value. This is useful for hashing commutative
111 expressions, so that A+B and B+A get the same hash. */
113 void add_commutative (hash
&a
, hash
&b
)
115 if (a
.end() > b
.end())
134 /* Borrowed from hashtab.c iterative_hash implementation. */
137 a -= b; a -= c; a ^= (c>>13); \
138 b -= c; b -= a; b ^= (a<< 8); \
139 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
140 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
141 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
142 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
143 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
144 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
145 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
149 /* Produce good hash value combining VAL and VAL2. */
152 iterative_hash_hashval_t (hashval_t val
, hashval_t val2
)
154 /* the golden ratio; an arbitrary value. */
155 hashval_t a
= 0x9e3779b9;
161 /* Produce good hash value combining VAL and VAL2. */
165 iterative_hash_host_wide_int (HOST_WIDE_INT val
, hashval_t val2
)
167 if (sizeof (HOST_WIDE_INT
) == sizeof (hashval_t
))
168 return iterative_hash_hashval_t (val
, val2
);
171 hashval_t a
= (hashval_t
) val
;
172 /* Avoid warnings about shifting of more than the width of the type on
173 hosts that won't execute this path. */
175 hashval_t b
= (hashval_t
) (val
>> (sizeof (hashval_t
) * 8 + zero
));
177 if (sizeof (HOST_WIDE_INT
) > 2 * sizeof (hashval_t
))
179 hashval_t a
= (hashval_t
) (val
>> (sizeof (hashval_t
) * 16 + zero
));
180 hashval_t b
= (hashval_t
) (val
>> (sizeof (hashval_t
) * 24 + zero
));