PR libstdc++/66354
[official-gcc.git] / gcc / inchash.h
blob4bb1272c1a9eea78c7fdbc925b23e2e98b62d1c6
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
9 version.
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
14 for more details.
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/>. */
20 #ifndef INCHASH_H
21 #define INCHASH_H 1
23 #ifdef GENERATOR_FILE
24 #include "bconfig.h"
25 #else
26 #include "config.h"
27 #endif
28 #include "system.h"
29 #include "coretypes.h"
30 #include "hashtab.h"
32 /* This file implements an incremential hash function ADT, to be used
33 by code that incrementially hashes a lot of unrelated data
34 (not in a single memory block) into a single value. The goal
35 is to make it easy to plug in efficient hash algorithms.
36 Currently it just implements the plain old jhash based
37 incremental hash from gcc's tree.c. */
39 hashval_t iterative_hash_host_wide_int (HOST_WIDE_INT, hashval_t);
40 hashval_t iterative_hash_hashval_t (hashval_t, hashval_t);
42 namespace inchash
45 class hash
47 public:
49 /* Start incremential hashing, optionally with SEED. */
50 hash (hashval_t seed = 0)
52 val = seed;
53 bits = 0;
56 /* End incremential hashing and provide the final value. */
57 hashval_t end ()
59 return val;
62 /* Add unsigned value V. */
63 void add_int (unsigned v)
65 val = iterative_hash_hashval_t (v, val);
68 /* Add HOST_WIDE_INT value V. */
69 void add_wide_int (HOST_WIDE_INT v)
71 val = iterative_hash_host_wide_int (v, val);
74 /* Hash in pointer PTR. */
75 void add_ptr (const void *ptr)
77 add (&ptr, sizeof (ptr));
80 /* Add a memory block DATA with size LEN. */
81 void add (const void *data, size_t len)
83 val = iterative_hash (data, len, val);
86 /* Merge hash value OTHER. */
87 void merge_hash (hashval_t other)
89 val = iterative_hash_hashval_t (other, val);
92 /* Hash in state from other inchash OTHER. */
93 void merge (hash &other)
95 merge_hash (other.val);
98 template<class T> void add_object(T &obj)
100 add (&obj, sizeof(T));
103 /* Support for accumulating boolean flags */
105 void add_flag (bool flag)
107 bits = (bits << 1) | flag;
110 void commit_flag ()
112 add_int (bits);
113 bits = 0;
116 /* Support for commutative hashing. Add A and B in a defined order
117 based on their value. This is useful for hashing commutative
118 expressions, so that A+B and B+A get the same hash. */
120 void add_commutative (hash &a, hash &b)
122 if (a.end() > b.end())
124 merge (b);
125 merge (a);
127 else
129 merge (a);
130 merge (b);
134 private:
135 hashval_t val;
136 unsigned bits;
141 /* Borrowed from hashtab.c iterative_hash implementation. */
142 #define mix(a,b,c) \
144 a -= b; a -= c; a ^= (c>>13); \
145 b -= c; b -= a; b ^= (a<< 8); \
146 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
147 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
148 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
149 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
150 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
151 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
152 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
156 /* Produce good hash value combining VAL and VAL2. */
157 inline
158 hashval_t
159 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
161 /* the golden ratio; an arbitrary value. */
162 hashval_t a = 0x9e3779b9;
164 mix (a, val, val2);
165 return val2;
168 /* Produce good hash value combining VAL and VAL2. */
170 inline
171 hashval_t
172 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
174 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
175 return iterative_hash_hashval_t (val, val2);
176 else
178 hashval_t a = (hashval_t) val;
179 /* Avoid warnings about shifting of more than the width of the type on
180 hosts that won't execute this path. */
181 int zero = 0;
182 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
183 mix (a, b, val2);
184 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
186 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
187 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
188 mix (a, b, val2);
190 return val2;
194 #endif