1 /* _dl_new_hash for elf symbol lookup
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #ifndef _DL_NEW_HASH_H
20 #define _DL_NEW_HASH_H 1
23 /* For __always_inline and __glibc_unlikely. */
24 #include <sys/cdefs.h>
26 /* The simplest implementation of _dl_new_hash is:
28 _dl_new_hash (const char *s)
31 for (unsigned char c = *s; c != '\0'; c = *++s)
36 We can get better performance by slightly unrolling the loop to
37 pipeline the multiples, which gcc cannot easily do due to
38 dependencies across iterations.
40 As well, as an architecture specific option we add asm statements
41 to explicitly specify order of operations and prevent reassociation
42 of instructions that lengthens the loop carried dependency. This
43 may have no affect as the compiler may have ordered instructions
44 the same way without it but in testing this has not been the case
45 for GCC. Improving GCC to reliably schedule instructions ideally
46 cannot be easily done.
48 Architecture(s) that use the reassociation barriers are:
51 Note it is very unlikely the reassociation barriers would
52 de-optimize performance on any architecture and with an imperfect
53 compiler it may help performance, especially on out-of-order cpus,
54 so it is suggested that the respective maintainers add them.
56 Architecture maintainers are encouraged to benchmark this with
57 __asm_reassociation_barrier defined to __asm__ like it is in x86.
61 #ifndef __asm_reassociation_barrier
62 # define __asm_reassociation_barrier(...)
65 static __always_inline
uint32_t
66 __attribute__ ((unused
))
67 _dl_new_hash (const char *str
)
69 const unsigned char *s
= (const unsigned char *) str
;
70 unsigned int h
= 5381;
75 /* Since hashed string is normally not empty, this is unlikely on the
76 first iteration of the loop. */
77 if (__glibc_unlikely (c0
== 0))
83 /* Ideal computational order is:
88 __asm_reassociation_barrier("" : "+r"(h
) : "r"(c0
));
93 /* Ideal computational order is:
100 __asm_reassociation_barrier("" : "+r"(c1
), "+r"(c0
));
103 __asm_reassociation_barrier("" : "+r"(c1
));
109 #endif /* dl-new-hash.h */