libroot: Add memcmp optimization similar to the strcmp one.

And cleanup the strcmp one a bit for consistency.
This commit is contained in:
Augustin Cavalier
2025-02-24 15:24:25 -05:00
parent 979d83f98b
commit 0b67b191c5
2 changed files with 18 additions and 5 deletions
+14
View File
@@ -4,6 +4,7 @@
*/
#include <stdint.h>
#include <string.h>
@@ -13,6 +14,19 @@ memcmp(const void *_a, const void *_b, size_t count)
const unsigned char *a = (const unsigned char *)_a;
const unsigned char *b = (const unsigned char *)_b;
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) {
a32++;
b32++;
count -= 4;
}
a = (const unsigned char *)a32;
b = (const unsigned char *)b32;
}
while (count-- > 0) {
int cmp = *a++ - *b++;
if (cmp != 0)
+4 -5
View File
@@ -4,9 +4,8 @@
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <SupportDefs.h>
#define LACKS_ZERO_BYTE(value) \
@@ -16,8 +15,8 @@ int
strcmp(char const *a, char const *b)
{
if ((((addr_t)a) & 3) == 0 && (((addr_t)b) & 3) == 0) {
uint32* a32 = (uint32*)a;
uint32* b32 = (uint32*)b;
uint32_t *a32 = (uint32_t *)a;
uint32_t *b32 = (uint32_t *)b;
while (*a32 == *b32 && LACKS_ZERO_BYTE((*a32))) {
a32++;
@@ -27,7 +26,7 @@ strcmp(char const *a, char const *b)
b = (const char *)b32;
}
while (true) {
while (1) {
int cmp = (unsigned char)*a - (unsigned char)*b++;
if (cmp != 0 || *a++ == '\0')
return cmp;