string: Hook up the default implementation on test-strcmp
[glibc.git] / nss / nss_hash.c
blobe16c85064f29ca9630efe8a79b7a6b01f3bdde39
1 /* Copyright (c) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <nss.h>
20 /* This is from libc/db/hash/hash_func.c, hash3 is static there */
22 * This is INCREDIBLY ugly, but fast. We break the string up into 4 byte
23 * units. On the first time through the loop we get the "leftover bytes"
24 * (len % 4). On every other iteration, we perform a 4x unrolled version
25 * HASHC. Further unrolling does not appear to help.
27 * OZ's original sdbm hash
29 uint32_t
30 __nss_hash (const void *keyarg, size_t len)
32 enum
34 HASH_CONST_P0 = 1, /* (uint32_t)(65599 ^ 0). */
35 HASH_CONST_P1 = 65599, /* (uint32_t)(65599 ^ 1). */
36 HASH_CONST_P2 = 8261505, /* (uint32_t)(65599 ^ 2). */
37 HASH_CONST_P3 = 780587199, /* (uint32_t)(65599 ^ 3). */
38 HASH_CONST_P4 = 1139564289 /* (uint32_t)(65599 ^ 4). */
41 const unsigned char *key;
42 uint32_t h;
44 #define HASHC h = *key++ + HASH_CONST_P1 * h
46 h = 0;
47 key = keyarg;
48 if (len > 0)
50 switch ((len & (4 - 1)))
52 case 0:
53 /* h starts out as zero so no need to include the multiply. */
54 h = *key++;
55 /* FALLTHROUGH */
56 case 3:
57 HASHC;
58 /* FALLTHROUGH */
59 case 2:
60 HASHC;
61 /* FALLTHROUGH */
62 case 1:
63 HASHC;
64 /* FALLTHROUGH */
67 uint32_t c0, c1, c2, c3;
68 for (--len; len >= 4; len -= 4)
70 c0 = (unsigned char) *(key + 0);
71 c1 = (unsigned char) *(key + 1);
72 c2 = (unsigned char) *(key + 2);
73 c3 = (unsigned char) *(key + 3);
74 h = HASH_CONST_P4 * h + HASH_CONST_P3 * c0 + HASH_CONST_P2 * c1
75 + HASH_CONST_P1 * c2 + HASH_CONST_P0 * c3;
77 key += 4;
80 return h;
84 libc_hidden_def (__nss_hash)