struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / cpp / gcc / inchash.h
blobb5c1b7cec5f5e39525db1ba61d61d1f9f5573909
1 /* An incremental hash abstract data type.
2 Copyright (C) 2014-2022 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
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.cc. */
31 hashval_t iterative_hash_host_wide_int (HOST_WIDE_INT, hashval_t);
32 hashval_t iterative_hash_hashval_t (hashval_t, hashval_t);
34 namespace inchash
37 class hash
39 public:
41 /* Start incremential hashing, optionally with SEED. */
42 hash (hashval_t seed = 0)
44 val = seed;
45 bits = 0;
48 /* End incremential hashing and provide the final value. */
49 hashval_t end ()
51 return val;
54 /* Add unsigned value V. */
55 void add_int (unsigned v)
57 val = iterative_hash_hashval_t (v, val);
60 /* Add polynomial value V, treating each element as an unsigned int. */
61 // sdcpp template<unsigned int N, typename T>
62 // sdcpp void add_poly_int (const poly_int_pod<N, T> &v)
63 // sdcpp {
64 // sdcpp for (unsigned int i = 0; i < N; ++i)
65 // sdcpp add_int (v.coeffs[i]);
66 // sdcpp }
68 /* Add HOST_WIDE_INT value V. */
69 void add_hwi (HOST_WIDE_INT v)
71 val = iterative_hash_host_wide_int (v, val);
74 #if 0 // sdcpp
75 /* Add polynomial value V, treating each element as a HOST_WIDE_INT. */
76 template<unsigned int N, typename T>
77 void add_poly_hwi (const poly_int_pod<N, T> &v)
79 for (unsigned int i = 0; i < N; ++i)
80 add_hwi (v.coeffs[i]);
83 /* Add wide_int-based value V. */
84 template<typename T>
85 void add_wide_int (const generic_wide_int<T> &x)
87 add_int (x.get_len ());
88 for (unsigned i = 0; i < x.get_len (); i++)
89 add_hwi (x.sext_elt (i));
91 #endif // sdcpp
93 /* Hash in pointer PTR. */
94 void add_ptr (const void *ptr)
96 add (&ptr, sizeof (ptr));
99 /* Add a memory block DATA with size LEN. */
100 void add (const void *data, size_t len)
102 val = iterative_hash (data, len, val);
105 /* Merge hash value OTHER. */
106 void merge_hash (hashval_t other)
108 val = iterative_hash_hashval_t (other, val);
111 /* Hash in state from other inchash OTHER. */
112 void merge (hash &other)
114 merge_hash (other.val);
117 template<class T> void add_object(T &obj)
119 add (&obj, sizeof(T));
122 /* Support for accumulating boolean flags */
124 void add_flag (bool flag)
126 bits = (bits << 1) | flag;
129 void commit_flag ()
131 add_int (bits);
132 bits = 0;
135 /* Support for commutative hashing. Add A and B in a defined order
136 based on their value. This is useful for hashing commutative
137 expressions, so that A+B and B+A get the same hash. */
139 void add_commutative (hash &a, hash &b)
141 if (a.end() > b.end())
143 merge (b);
144 merge (a);
146 else
148 merge (a);
149 merge (b);
153 private:
154 hashval_t val;
155 unsigned bits;
160 /* Borrowed from hashtab.c iterative_hash implementation. */
161 #define mix(a,b,c) \
163 a -= b; a -= c; a ^= (c>>13); \
164 b -= c; b -= a; b ^= (a<< 8); \
165 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
166 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
167 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
168 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
169 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
170 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
171 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
175 /* Produce good hash value combining VAL and VAL2. */
176 inline
177 hashval_t
178 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
180 /* the golden ratio; an arbitrary value. */
181 hashval_t a = 0x9e3779b9;
183 mix (a, val, val2);
184 return val2;
187 /* Produce good hash value combining VAL and VAL2. */
189 inline
190 hashval_t
191 iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2)
193 if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t))
194 return iterative_hash_hashval_t (val, val2);
195 else
197 hashval_t a = (hashval_t) val;
198 /* Avoid warnings about shifting of more than the width of the type on
199 hosts that won't execute this path. */
200 int zero = 0;
201 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero));
202 mix (a, b, val2);
203 if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t))
205 hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero));
206 hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero));
207 mix (a, b, val2);
209 return val2;
213 #endif