]> git.ipfire.org Git - thirdparty/make.git/commitdiff
* src/hash.c (jhash_string): Help the compiler optimize the hash
authorJustine Tunney <jtunney@gmail.com>
Sun, 6 Nov 2022 17:13:21 +0000 (12:13 -0500)
committerPaul Smith <psmith@gnu.org>
Sun, 13 Nov 2022 15:38:50 +0000 (10:38 -0500)
Invoke memcpy() with a constant length, where possible.

Copyright-paperwork-exempt: yes

src/hash.c

index 5d7ea81b2840f581c86fbc24d8a51997d44b53a7..d1652f84b4e73c8026af4be18fa7433d6d73e4d1 100644 (file)
@@ -361,7 +361,7 @@ round_up_2 (unsigned long n)
 #define sum_get_unaligned_32(r, p)              \
   do {                                          \
     unsigned int val;                           \
-    memcpy(&val, (p), 4);                       \
+    memcpy (&val, (p), 4);                      \
     r += val;                                   \
   } while(0);
 
@@ -413,13 +413,16 @@ jhash(unsigned const char *k, int length)
 #define UINTSZ sizeof (unsigned int)
 
 #ifdef WORDS_BIGENDIAN
-/* The ifs are ordered from the first byte in memory to the last.  */
+/* The ifs are ordered from the first byte in memory to the last.
+   Help the compiler optimize by using static memcpy length.  */
 #define sum_up_to_nul(r, p, plen, flag)   \
   do {                                    \
     unsigned int val = 0;                 \
     size_t pn = (plen);                   \
-    size_t n = pn < UINTSZ ? pn : UINTSZ; \
-    memcpy (&val, (p), n);                \
+    if (pn >= UINTSZ)                     \
+      memcpy (&val, (p), UINTSZ);         \
+    else                                  \
+      memcpy (&val, (p), pn);             \
     if ((val & 0xFF000000) == 0)          \
       flag = 1;                           \
     else if ((val & 0xFF0000) == 0)       \
@@ -432,13 +435,16 @@ jhash(unsigned const char *k, int length)
 #else
 /* First detect the presence of zeroes.  If there is none, we can
    sum the 4 bytes directly.  Otherwise, the ifs are ordered as in the
-   big endian case, from the first byte in memory to the last.  */
+   big endian case, from the first byte in memory to the last.
+   Help the compiler optimize by using static memcpy length.  */
 #define sum_up_to_nul(r, p, plen, flag)              \
   do {                                               \
     unsigned int val = 0;                            \
     size_t pn = (plen);                              \
-    size_t n = pn < UINTSZ ? pn : UINTSZ;            \
-    memcpy (&val, (p), n);                           \
+    if (pn >= UINTSZ)                                \
+      memcpy (&val, (p), UINTSZ);                    \
+    else                                             \
+      memcpy (&val, (p), pn);                        \
     flag = ((val - 0x01010101) & ~val) & 0x80808080; \
     if (!flag)                                       \
       r += val;                                      \