From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/2730 Path: news.gmane.org!not-for-mail From: Nathan McSween Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH 3/4] String: expand to word-at-a-time Date: Mon, 4 Feb 2013 00:12:14 +0000 Message-ID: <1359936735-31915-4-git-send-email-nwmcsween@gmail.com> References: <1359936735-31915-1-git-send-email-nwmcsween@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1359936775 26596 80.91.229.3 (4 Feb 2013 00:12:55 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 4 Feb 2013 00:12:55 +0000 (UTC) Cc: Nathan McSween To: musl@lists.openwall.com Original-X-From: musl-return-2733-gllmg-musl=m.gmane.org@lists.openwall.com Mon Feb 04 01:13:15 2013 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1U29gP-000729-H6 for gllmg-musl@plane.gmane.org; Mon, 04 Feb 2013 01:13:13 +0100 Original-Received: (qmail 32761 invoked by uid 550); 4 Feb 2013 00:12:55 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 32753 invoked from network); 4 Feb 2013 00:12:54 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=c9l+6dNe1IrpVsEciu9yuXDEI5Dv92WmMou7iJfP15w=; b=WyyYYdA9OA255NfGL7NZ0cvlfNin5+L9j/15YrgBDRsoR5VFQiGQ62ghYnM+WMdvn2 nvvcwfhXt5egcWQvHcPqS0W/z8Y/jgnVJZANnMkAy2Wui+XE0ROUepwXye3frRPyCs3m xwdIifrRgUvslP17XtZj8436Awetkw1hZgnTWyxU1gTw/iSY+PDWGhqFmq90M2szq8gd XYxbEluV7gWLXV50Wvp3VGQRMoBvu0Lt1qlEn1aJQfdxc0zZucETjHRlCWP/ML8NEM9a RtaaDMZAO7xT1iZ/E4U8BSn85BrlDvPBHtn9NhcQDKfvR2PpMHTF8/XRSr4pk0wgasB6 Evlg== X-Received: by 10.68.189.35 with SMTP id gf3mr50157556pbc.69.1359936762885; Sun, 03 Feb 2013 16:12:42 -0800 (PST) X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1359936735-31915-1-git-send-email-nwmcsween@gmail.com> Xref: news.gmane.org gmane.linux.lib.musl.general:2730 Archived-At: --- 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 #include +#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 +#include #include +#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 #include +#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