libroot/string: Apply the same optimization to strncmp as in strcmp.

This commit is contained in:
Augustin Cavalier
2025-03-05 10:45:55 -05:00
parent 3096860403
commit 5d46b254d4
+17
View File
@@ -4,12 +4,29 @@
*/
#include <stdint.h>
#include <string.h>
#define LACKS_ZERO_BYTE(value) \
(((value - 0x01010101) & ~value & 0x80808080) == 0)
int
strncmp(char const *a, char const *b, size_t count)
{
if ((((addr_t)a) & 3) == 0 && (((addr_t)b) & 3) == 0) {
uint32_t *a32 = (uint32_t *)a;
uint32_t *b32 = (uint32_t *)b;
while (count >= 4 && *a32 == *b32 && LACKS_ZERO_BYTE((*a32))) {
a32++;
b32++;
count -= 4;
}
a = (const char *)a32;
b = (const char *)b32;
}
while (count-- > 0) {
int cmp = (unsigned char)*a - (unsigned char)*b++;
if (cmp != 0 || *a++ == '\0')