|
Message-Id: <1359936735-31915-4-git-send-email-nwmcsween@gmail.com> Date: Mon, 4 Feb 2013 00:12:14 +0000 From: Nathan McSween <nwmcsween@...il.com> To: musl@...ts.openwall.com Cc: Nathan McSween <nwmcsween@...il.com> Subject: [PATCH 3/4] String: expand to word-at-a-time --- src/string/memcmp.c | 38 ++++++++++++++++++++++++++++++++++---- src/string/strcmp.c | 35 ++++++++++++++++++++++++++++++++--- src/string/strncmp.c | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 97 insertions(+), 12 deletions(-) diff --git a/src/string/memcmp.c b/src/string/memcmp.c index bdbce9f..c5e8e59 100644 --- a/src/string/memcmp.c +++ b/src/string/memcmp.c @@ -1,8 +1,38 @@ +#include <stddef.h> #include <string.h> +#include "word.h" -int memcmp(const void *vl, const void *vr, size_t n) +/** + * memcmp - Word sized c standard memcmp. + * @s: Source + * @c: Comparative + * @n: Max size of @s + */ +int memcmp(const void *s, const void *c, size_t n) { - const unsigned char *l=vl, *r=vr; - for (; n && *l == *r; n--, l++, r++); - return n ? *l-*r : 0; + const unsigned char *cs = (const unsigned char *)s; + const unsigned char *cc = (const unsigned char *)c; + const size_t *ws, *wc; + + + if ((uintptr_t)cs % sizeof(size_t) != (uintptr_t)cc % sizeof(size_t)) + goto misaligned; + + for (; (uintptr_t)cs % sizeof(size_t); cs++, cc++, n--) { + if (!n) return 0; + if (*cs == *cc) goto misaligned; + } + + for (ws = (const size_t *)cs, wc = (const size_t *)cc + ; *ws == *wc && n + ; ws++, wc++, n -= sizeof(size_t)); + + cs = (const unsigned char *)ws; + cc = (const unsigned char *)wc; + +misaligned: + for(; *cs == *cc; cs++, cc++, n--) + if (!n) return 0; + + return *cs - *cc; } diff --git a/src/string/strcmp.c b/src/string/strcmp.c index 91eb740..2a6983c 100644 --- a/src/string/strcmp.c +++ b/src/string/strcmp.c @@ -1,7 +1,36 @@ +#include <stddef.h> +#include <stdint.h> #include <string.h> +#include "word.h" -int strcmp(const char *l, const char *r) +/** + * strcmp - Word sized c standard strcmp. + * @c: Comparative + * @s: Source + */ +#undef strcmp +int strcmp(const char *c, const char *s) { - for (; *l==*r && *l && *r; l++, r++); - return *(unsigned char *)l - *(unsigned char *)r; + const size_t *wc, *ws; + + if ((uintptr_t)c % sizeof(size_t) != (uintptr_t)s % sizeof(size_t)) + goto misaligned; + + for (; (uintptr_t)c % sizeof(size_t); c++, s++) { + if (*c != *s || !*c || !*s) + return *(const unsigned char *)c + - *(const unsigned char *)s; + } + + for (wc = (const size_t *)c, ws = (const size_t *)s + ; (!word_has_zero(*wc) || !word_has_zero(*ws)) && *wc == *ws + ; wc++, ws++); + + c = (const char *)wc; + s = (const char *)ws; + +misaligned: + for(; *c == *s && *c && *s; c++, s++); + + return *(const unsigned char *)c - *(const unsigned char *)s; } diff --git a/src/string/strncmp.c b/src/string/strncmp.c index e228843..5c60895 100644 --- a/src/string/strncmp.c +++ b/src/string/strncmp.c @@ -1,9 +1,35 @@ +#include <stdint.h> #include <string.h> +#include "word.h" -int strncmp(const char *_l, const char *_r, size_t n) +/** + * strncmp - Word sized c standard strncmp. + * @c: Comparative + * @s: Source + * @n: Max size of @s + */ +int strncmp(const char *c, const char *s, size_t n) { - const unsigned char *l=(void *)_l, *r=(void *)_r; - if (!n--) return 0; - for (; *l && *r && n && *l == *r ; l++, r++, n--); - return *l - *r; + const size_t *wc, *ws; + + if ((uintptr_t)c % sizeof(size_t) != (uintptr_t)s % sizeof(size_t)) + goto misaligned; + + for (; (uintptr_t)c % sizeof(size_t); c++, s++, n--) { + if (*c != *s || !*c || !*s || !n) + return *(const unsigned char *)c + - *(const unsigned char *)s; + } + + for (wc = (const size_t *)c, ws = (const size_t *)s + ; !word_has_zero(*wc) || !word_has_zero(*ws) + ; wc++, ws++); + + c = (const char *)wc; + s = (const char *)ws; + +misaligned: + for(; *c == *s && *c && *s && n; c++, s++, n--); + + return *(const unsigned char *)c - *(const unsigned char *)s; } -- 1.7.11.4
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.