libroot: Add memcmp optimization similar to the strcmp one.
And cleanup the strcmp one a bit for consistency.
This commit is contained in:
@@ -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,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;
|
||||
|
||||
Reference in New Issue
Block a user