1 1.1 mrg /* An expandable hash tables datatype. 2 1.7 mrg Copyright (C) 1999-2022 Free Software Foundation, Inc. 3 1.1 mrg Contributed by Vladimir Makarov <vmakarov (at) cygnus.com>. 4 1.1 mrg 5 1.3 mrg This file is part of the GNU Offloading and Multi Processing Library 6 1.3 mrg (libgomp). 7 1.3 mrg 8 1.3 mrg Libgomp is free software; you can redistribute it and/or modify it 9 1.3 mrg under the terms of the GNU General Public License as published by 10 1.3 mrg the Free Software Foundation; either version 3, or (at your option) 11 1.3 mrg any later version. 12 1.3 mrg 13 1.3 mrg Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 14 1.3 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 1.3 mrg FOR A PARTICULAR PURPOSE. See the GNU General Public License for 16 1.3 mrg more details. 17 1.3 mrg 18 1.3 mrg Under Section 7 of GPL version 3, you are granted additional 19 1.3 mrg permissions described in the GCC Runtime Library Exception, version 20 1.3 mrg 3.1, as published by the Free Software Foundation. 21 1.3 mrg 22 1.3 mrg You should have received a copy of the GNU General Public License and 23 1.3 mrg a copy of the GCC Runtime Library Exception along with this program; 24 1.3 mrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25 1.3 mrg <http://www.gnu.org/licenses/>. */ 26 1.1 mrg 27 1.1 mrg /* The hash table code copied from include/hashtab.[hc] and adjusted, 28 1.1 mrg so that the hash table entries are in the flexible array at the end 29 1.1 mrg of the control structure, no callbacks are used and the elements in the 30 1.1 mrg table are of the hash_entry_type type. 31 1.1 mrg Before including this file, define hash_entry_type type and 32 1.1 mrg htab_alloc and htab_free functions. After including it, define 33 1.1 mrg htab_hash and htab_eq inline functions. */ 34 1.1 mrg 35 1.1 mrg /* This package implements basic hash table functionality. It is possible 36 1.1 mrg to search for an entry, create an entry and destroy an entry. 37 1.1 mrg 38 1.1 mrg Elements in the table are generic pointers. 39 1.1 mrg 40 1.1 mrg The size of the table is not fixed; if the occupancy of the table 41 1.1 mrg grows too high the hash table will be expanded. 42 1.1 mrg 43 1.1 mrg The abstract data implementation is based on generalized Algorithm D 44 1.1 mrg from Knuth's book "The art of computer programming". Hash table is 45 1.1 mrg expanded by creation of new hash table and transferring elements from 46 1.1 mrg the old table to the new table. */ 47 1.1 mrg 48 1.1 mrg /* The type for a hash code. */ 49 1.1 mrg typedef unsigned int hashval_t; 50 1.1 mrg 51 1.1 mrg static inline hashval_t htab_hash (hash_entry_type); 52 1.1 mrg static inline bool htab_eq (hash_entry_type, hash_entry_type); 53 1.1 mrg 54 1.1 mrg /* This macro defines reserved value for empty table entry. */ 55 1.1 mrg 56 1.1 mrg #define HTAB_EMPTY_ENTRY ((hash_entry_type) 0) 57 1.1 mrg 58 1.1 mrg /* This macro defines reserved value for table entry which contained 59 1.1 mrg a deleted element. */ 60 1.1 mrg 61 1.1 mrg #define HTAB_DELETED_ENTRY ((hash_entry_type) 1) 62 1.1 mrg 63 1.1 mrg /* Hash tables are of the following type. The structure 64 1.1 mrg (implementation) of this type is not needed for using the hash 65 1.1 mrg tables. All work with hash table should be executed only through 66 1.1 mrg functions mentioned below. The size of this structure is subject to 67 1.1 mrg change. */ 68 1.1 mrg 69 1.1 mrg struct htab { 70 1.1 mrg /* Current size (in entries) of the hash table. */ 71 1.1 mrg size_t size; 72 1.1 mrg 73 1.1 mrg /* Current number of elements including also deleted elements. */ 74 1.1 mrg size_t n_elements; 75 1.1 mrg 76 1.1 mrg /* Current number of deleted elements in the table. */ 77 1.1 mrg size_t n_deleted; 78 1.1 mrg 79 1.1 mrg /* Current size (in entries) of the hash table, as an index into the 80 1.1 mrg table of primes. */ 81 1.1 mrg unsigned int size_prime_index; 82 1.1 mrg 83 1.1 mrg /* Table itself. */ 84 1.1 mrg hash_entry_type entries[]; 85 1.1 mrg }; 86 1.1 mrg 87 1.1 mrg typedef struct htab *htab_t; 88 1.1 mrg 89 1.1 mrg /* An enum saying whether we insert into the hash table or not. */ 90 1.1 mrg enum insert_option {NO_INSERT, INSERT}; 91 1.1 mrg 92 1.1 mrg /* Table of primes and multiplicative inverses. 93 1.1 mrg 94 1.1 mrg Note that these are not minimally reduced inverses. Unlike when generating 95 1.1 mrg code to divide by a constant, we want to be able to use the same algorithm 96 1.1 mrg all the time. All of these inverses (are implied to) have bit 32 set. 97 1.1 mrg 98 1.1 mrg For the record, the function that computed the table is in 99 1.1 mrg libiberty/hashtab.c. */ 100 1.1 mrg 101 1.1 mrg struct prime_ent 102 1.1 mrg { 103 1.1 mrg hashval_t prime; 104 1.1 mrg hashval_t inv; 105 1.1 mrg hashval_t inv_m2; /* inverse of prime-2 */ 106 1.1 mrg hashval_t shift; 107 1.1 mrg }; 108 1.1 mrg 109 1.1 mrg static struct prime_ent const prime_tab[] = { 110 1.1 mrg { 7, 0x24924925, 0x9999999b, 2 }, 111 1.1 mrg { 13, 0x3b13b13c, 0x745d1747, 3 }, 112 1.1 mrg { 31, 0x08421085, 0x1a7b9612, 4 }, 113 1.1 mrg { 61, 0x0c9714fc, 0x15b1e5f8, 5 }, 114 1.1 mrg { 127, 0x02040811, 0x0624dd30, 6 }, 115 1.1 mrg { 251, 0x05197f7e, 0x073260a5, 7 }, 116 1.1 mrg { 509, 0x01824366, 0x02864fc8, 8 }, 117 1.1 mrg { 1021, 0x00c0906d, 0x014191f7, 9 }, 118 1.1 mrg { 2039, 0x0121456f, 0x0161e69e, 10 }, 119 1.1 mrg { 4093, 0x00300902, 0x00501908, 11 }, 120 1.1 mrg { 8191, 0x00080041, 0x00180241, 12 }, 121 1.1 mrg { 16381, 0x000c0091, 0x00140191, 13 }, 122 1.1 mrg { 32749, 0x002605a5, 0x002a06e6, 14 }, 123 1.1 mrg { 65521, 0x000f00e2, 0x00110122, 15 }, 124 1.1 mrg { 131071, 0x00008001, 0x00018003, 16 }, 125 1.1 mrg { 262139, 0x00014002, 0x0001c004, 17 }, 126 1.1 mrg { 524287, 0x00002001, 0x00006001, 18 }, 127 1.1 mrg { 1048573, 0x00003001, 0x00005001, 19 }, 128 1.1 mrg { 2097143, 0x00004801, 0x00005801, 20 }, 129 1.1 mrg { 4194301, 0x00000c01, 0x00001401, 21 }, 130 1.1 mrg { 8388593, 0x00001e01, 0x00002201, 22 }, 131 1.1 mrg { 16777213, 0x00000301, 0x00000501, 23 }, 132 1.1 mrg { 33554393, 0x00001381, 0x00001481, 24 }, 133 1.1 mrg { 67108859, 0x00000141, 0x000001c1, 25 }, 134 1.1 mrg { 134217689, 0x000004e1, 0x00000521, 26 }, 135 1.1 mrg { 268435399, 0x00000391, 0x000003b1, 27 }, 136 1.1 mrg { 536870909, 0x00000019, 0x00000029, 28 }, 137 1.1 mrg { 1073741789, 0x0000008d, 0x00000095, 29 }, 138 1.1 mrg { 2147483647, 0x00000003, 0x00000007, 30 }, 139 1.1 mrg /* Avoid "decimal constant so large it is unsigned" for 4294967291. */ 140 1.1 mrg { 0xfffffffb, 0x00000006, 0x00000008, 31 } 141 1.1 mrg }; 142 1.1 mrg 143 1.1 mrg /* The following function returns an index into the above table of the 144 1.1 mrg nearest prime number which is greater than N, and near a power of two. */ 145 1.1 mrg 146 1.1 mrg static unsigned int 147 1.1 mrg higher_prime_index (unsigned long n) 148 1.1 mrg { 149 1.1 mrg unsigned int low = 0; 150 1.1 mrg unsigned int high = sizeof(prime_tab) / sizeof(prime_tab[0]); 151 1.1 mrg 152 1.1 mrg while (low != high) 153 1.1 mrg { 154 1.1 mrg unsigned int mid = low + (high - low) / 2; 155 1.1 mrg if (n > prime_tab[mid].prime) 156 1.1 mrg low = mid + 1; 157 1.1 mrg else 158 1.1 mrg high = mid; 159 1.1 mrg } 160 1.1 mrg 161 1.1 mrg /* If we've run out of primes, abort. */ 162 1.1 mrg if (n > prime_tab[low].prime) 163 1.1 mrg abort (); 164 1.1 mrg 165 1.1 mrg return low; 166 1.1 mrg } 167 1.1 mrg 168 1.1 mrg /* Return the current size of given hash table. */ 169 1.1 mrg 170 1.1 mrg static inline size_t 171 1.1 mrg htab_size (htab_t htab) 172 1.1 mrg { 173 1.1 mrg return htab->size; 174 1.1 mrg } 175 1.1 mrg 176 1.1 mrg /* Return the current number of elements in given hash table. */ 177 1.1 mrg 178 1.1 mrg static inline size_t 179 1.1 mrg htab_elements (htab_t htab) 180 1.1 mrg { 181 1.1 mrg return htab->n_elements - htab->n_deleted; 182 1.1 mrg } 183 1.1 mrg 184 1.1 mrg /* Return X % Y. */ 185 1.1 mrg 186 1.1 mrg static inline hashval_t 187 1.1 mrg htab_mod_1 (hashval_t x, hashval_t y, hashval_t inv, int shift) 188 1.1 mrg { 189 1.1 mrg /* The multiplicative inverses computed above are for 32-bit types, and 190 1.1 mrg requires that we be able to compute a highpart multiply. */ 191 1.1 mrg if (sizeof (hashval_t) * __CHAR_BIT__ <= 32) 192 1.1 mrg { 193 1.1 mrg hashval_t t1, t2, t3, t4, q, r; 194 1.1 mrg 195 1.1 mrg t1 = ((unsigned long long)x * inv) >> 32; 196 1.1 mrg t2 = x - t1; 197 1.1 mrg t3 = t2 >> 1; 198 1.1 mrg t4 = t1 + t3; 199 1.1 mrg q = t4 >> shift; 200 1.1 mrg r = x - (q * y); 201 1.1 mrg 202 1.1 mrg return r; 203 1.1 mrg } 204 1.1 mrg 205 1.1 mrg /* Otherwise just use the native division routines. */ 206 1.1 mrg return x % y; 207 1.1 mrg } 208 1.1 mrg 209 1.1 mrg /* Compute the primary hash for HASH given HTAB's current size. */ 210 1.1 mrg 211 1.1 mrg static inline hashval_t 212 1.1 mrg htab_mod (hashval_t hash, htab_t htab) 213 1.1 mrg { 214 1.1 mrg const struct prime_ent *p = &prime_tab[htab->size_prime_index]; 215 1.1 mrg return htab_mod_1 (hash, p->prime, p->inv, p->shift); 216 1.1 mrg } 217 1.1 mrg 218 1.1 mrg /* Compute the secondary hash for HASH given HTAB's current size. */ 219 1.1 mrg 220 1.1 mrg static inline hashval_t 221 1.1 mrg htab_mod_m2 (hashval_t hash, htab_t htab) 222 1.1 mrg { 223 1.1 mrg const struct prime_ent *p = &prime_tab[htab->size_prime_index]; 224 1.1 mrg return 1 + htab_mod_1 (hash, p->prime - 2, p->inv_m2, p->shift); 225 1.1 mrg } 226 1.1 mrg 227 1.7 mrg static inline htab_t 228 1.7 mrg htab_clear (htab_t htab) 229 1.7 mrg { 230 1.7 mrg htab->n_elements = 0; 231 1.7 mrg htab->n_deleted = 0; 232 1.7 mrg memset (htab->entries, 0, htab->size * sizeof (hash_entry_type)); 233 1.7 mrg return htab; 234 1.7 mrg } 235 1.7 mrg 236 1.1 mrg /* Create hash table of size SIZE. */ 237 1.1 mrg 238 1.1 mrg static htab_t 239 1.1 mrg htab_create (size_t size) 240 1.1 mrg { 241 1.1 mrg htab_t result; 242 1.1 mrg unsigned int size_prime_index; 243 1.1 mrg 244 1.1 mrg size_prime_index = higher_prime_index (size); 245 1.1 mrg size = prime_tab[size_prime_index].prime; 246 1.1 mrg 247 1.1 mrg result = (htab_t) htab_alloc (sizeof (struct htab) 248 1.1 mrg + size * sizeof (hash_entry_type)); 249 1.1 mrg result->size = size; 250 1.1 mrg result->size_prime_index = size_prime_index; 251 1.7 mrg return htab_clear (result); 252 1.1 mrg } 253 1.1 mrg 254 1.1 mrg /* Similar to htab_find_slot, but without several unwanted side effects: 255 1.1 mrg - Does not call htab_eq when it finds an existing entry. 256 1.1 mrg - Does not change the count of elements in the hash table. 257 1.1 mrg This function also assumes there are no deleted entries in the table. 258 1.1 mrg HASH is the hash value for the element to be inserted. */ 259 1.1 mrg 260 1.1 mrg static hash_entry_type * 261 1.1 mrg find_empty_slot_for_expand (htab_t htab, hashval_t hash) 262 1.1 mrg { 263 1.1 mrg hashval_t index = htab_mod (hash, htab); 264 1.1 mrg size_t size = htab_size (htab); 265 1.1 mrg hash_entry_type *slot = htab->entries + index; 266 1.1 mrg hashval_t hash2; 267 1.1 mrg 268 1.1 mrg if (*slot == HTAB_EMPTY_ENTRY) 269 1.1 mrg return slot; 270 1.1 mrg else if (*slot == HTAB_DELETED_ENTRY) 271 1.1 mrg abort (); 272 1.1 mrg 273 1.1 mrg hash2 = htab_mod_m2 (hash, htab); 274 1.1 mrg for (;;) 275 1.1 mrg { 276 1.1 mrg index += hash2; 277 1.1 mrg if (index >= size) 278 1.1 mrg index -= size; 279 1.1 mrg 280 1.1 mrg slot = htab->entries + index; 281 1.1 mrg if (*slot == HTAB_EMPTY_ENTRY) 282 1.1 mrg return slot; 283 1.1 mrg else if (*slot == HTAB_DELETED_ENTRY) 284 1.1 mrg abort (); 285 1.1 mrg } 286 1.1 mrg } 287 1.1 mrg 288 1.1 mrg /* The following function changes size of memory allocated for the 289 1.1 mrg entries and repeatedly inserts the table elements. The occupancy 290 1.1 mrg of the table after the call will be about 50%. Naturally the hash 291 1.1 mrg table must already exist. Remember also that the place of the 292 1.1 mrg table entries is changed. */ 293 1.1 mrg 294 1.1 mrg static htab_t 295 1.1 mrg htab_expand (htab_t htab) 296 1.1 mrg { 297 1.1 mrg htab_t nhtab; 298 1.1 mrg hash_entry_type *olimit; 299 1.1 mrg hash_entry_type *p; 300 1.1 mrg size_t osize, elts; 301 1.1 mrg 302 1.1 mrg osize = htab->size; 303 1.1 mrg olimit = htab->entries + osize; 304 1.1 mrg elts = htab_elements (htab); 305 1.1 mrg 306 1.1 mrg /* Resize only when table after removal of unused elements is either 307 1.1 mrg too full or too empty. */ 308 1.1 mrg if (elts * 2 > osize || (elts * 8 < osize && osize > 32)) 309 1.1 mrg nhtab = htab_create (elts * 2); 310 1.1 mrg else 311 1.1 mrg nhtab = htab_create (osize - 1); 312 1.1 mrg nhtab->n_elements = htab->n_elements - htab->n_deleted; 313 1.1 mrg 314 1.1 mrg p = htab->entries; 315 1.1 mrg do 316 1.1 mrg { 317 1.1 mrg hash_entry_type x = *p; 318 1.1 mrg 319 1.1 mrg if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY) 320 1.1 mrg *find_empty_slot_for_expand (nhtab, htab_hash (x)) = x; 321 1.1 mrg 322 1.1 mrg p++; 323 1.1 mrg } 324 1.1 mrg while (p < olimit); 325 1.1 mrg 326 1.1 mrg htab_free (htab); 327 1.1 mrg return nhtab; 328 1.1 mrg } 329 1.1 mrg 330 1.1 mrg /* This function searches for a hash table entry equal to the given 331 1.1 mrg element. It cannot be used to insert or delete an element. */ 332 1.1 mrg 333 1.1 mrg static hash_entry_type 334 1.1 mrg htab_find (htab_t htab, const hash_entry_type element) 335 1.1 mrg { 336 1.1 mrg hashval_t index, hash2, hash = htab_hash (element); 337 1.1 mrg size_t size; 338 1.1 mrg hash_entry_type entry; 339 1.1 mrg 340 1.1 mrg size = htab_size (htab); 341 1.1 mrg index = htab_mod (hash, htab); 342 1.1 mrg 343 1.1 mrg entry = htab->entries[index]; 344 1.1 mrg if (entry == HTAB_EMPTY_ENTRY 345 1.1 mrg || (entry != HTAB_DELETED_ENTRY && htab_eq (entry, element))) 346 1.1 mrg return entry; 347 1.1 mrg 348 1.1 mrg hash2 = htab_mod_m2 (hash, htab); 349 1.1 mrg for (;;) 350 1.1 mrg { 351 1.1 mrg index += hash2; 352 1.1 mrg if (index >= size) 353 1.1 mrg index -= size; 354 1.1 mrg 355 1.1 mrg entry = htab->entries[index]; 356 1.1 mrg if (entry == HTAB_EMPTY_ENTRY 357 1.1 mrg || (entry != HTAB_DELETED_ENTRY && htab_eq (entry, element))) 358 1.1 mrg return entry; 359 1.1 mrg } 360 1.1 mrg } 361 1.1 mrg 362 1.1 mrg /* This function searches for a hash table slot containing an entry 363 1.1 mrg equal to the given element. To delete an entry, call this with 364 1.1 mrg insert=NO_INSERT, then call htab_clear_slot on the slot returned 365 1.1 mrg (possibly after doing some checks). To insert an entry, call this 366 1.1 mrg with insert=INSERT, then write the value you want into the returned 367 1.1 mrg slot. */ 368 1.1 mrg 369 1.1 mrg static hash_entry_type * 370 1.1 mrg htab_find_slot (htab_t *htabp, const hash_entry_type element, 371 1.1 mrg enum insert_option insert) 372 1.1 mrg { 373 1.1 mrg hash_entry_type *first_deleted_slot; 374 1.1 mrg hashval_t index, hash2, hash = htab_hash (element); 375 1.1 mrg size_t size; 376 1.1 mrg hash_entry_type entry; 377 1.1 mrg htab_t htab = *htabp; 378 1.1 mrg 379 1.1 mrg size = htab_size (htab); 380 1.1 mrg if (insert == INSERT && size * 3 <= htab->n_elements * 4) 381 1.1 mrg { 382 1.1 mrg htab = *htabp = htab_expand (htab); 383 1.1 mrg size = htab_size (htab); 384 1.1 mrg } 385 1.1 mrg 386 1.1 mrg index = htab_mod (hash, htab); 387 1.1 mrg 388 1.1 mrg first_deleted_slot = NULL; 389 1.1 mrg 390 1.1 mrg entry = htab->entries[index]; 391 1.1 mrg if (entry == HTAB_EMPTY_ENTRY) 392 1.1 mrg goto empty_entry; 393 1.1 mrg else if (entry == HTAB_DELETED_ENTRY) 394 1.1 mrg first_deleted_slot = &htab->entries[index]; 395 1.1 mrg else if (htab_eq (entry, element)) 396 1.1 mrg return &htab->entries[index]; 397 1.1 mrg 398 1.1 mrg hash2 = htab_mod_m2 (hash, htab); 399 1.1 mrg for (;;) 400 1.1 mrg { 401 1.1 mrg index += hash2; 402 1.1 mrg if (index >= size) 403 1.1 mrg index -= size; 404 1.1 mrg 405 1.1 mrg entry = htab->entries[index]; 406 1.1 mrg if (entry == HTAB_EMPTY_ENTRY) 407 1.1 mrg goto empty_entry; 408 1.1 mrg else if (entry == HTAB_DELETED_ENTRY) 409 1.1 mrg { 410 1.1 mrg if (!first_deleted_slot) 411 1.1 mrg first_deleted_slot = &htab->entries[index]; 412 1.1 mrg } 413 1.1 mrg else if (htab_eq (entry, element)) 414 1.1 mrg return &htab->entries[index]; 415 1.1 mrg } 416 1.1 mrg 417 1.1 mrg empty_entry: 418 1.1 mrg if (insert == NO_INSERT) 419 1.1 mrg return NULL; 420 1.1 mrg 421 1.1 mrg if (first_deleted_slot) 422 1.1 mrg { 423 1.1 mrg htab->n_deleted--; 424 1.1 mrg *first_deleted_slot = HTAB_EMPTY_ENTRY; 425 1.1 mrg return first_deleted_slot; 426 1.1 mrg } 427 1.1 mrg 428 1.1 mrg htab->n_elements++; 429 1.1 mrg return &htab->entries[index]; 430 1.1 mrg } 431 1.1 mrg 432 1.1 mrg /* This function clears a specified slot in a hash table. It is 433 1.1 mrg useful when you've already done the lookup and don't want to do it 434 1.1 mrg again. */ 435 1.1 mrg 436 1.1 mrg static inline void 437 1.1 mrg htab_clear_slot (htab_t htab, hash_entry_type *slot) 438 1.1 mrg { 439 1.1 mrg if (slot < htab->entries || slot >= htab->entries + htab_size (htab) 440 1.1 mrg || *slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY) 441 1.1 mrg abort (); 442 1.1 mrg 443 1.1 mrg *slot = HTAB_DELETED_ENTRY; 444 1.1 mrg htab->n_deleted++; 445 1.1 mrg } 446 1.1 mrg 447 1.1 mrg /* Returns a hash code for pointer P. Simplified version of evahash */ 448 1.1 mrg 449 1.1 mrg static inline hashval_t 450 1.1 mrg hash_pointer (const void *p) 451 1.1 mrg { 452 1.1 mrg uintptr_t v = (uintptr_t) p; 453 1.1 mrg if (sizeof (v) > sizeof (hashval_t)) 454 1.1 mrg v ^= v >> (sizeof (uintptr_t) / 2 * __CHAR_BIT__); 455 1.1 mrg return v; 456 1.1 mrg } 457