inchash.h 5.1 KB

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