mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Nathan McSween <nwmcsween@gmail.com>
To: musl@lists.openwall.com
Cc: Nathan McSween <nwmcsween@gmail.com>
Subject: [PATCH 3/4] String: expand to word-at-a-time
Date: Mon,  4 Feb 2013 00:12:14 +0000	[thread overview]
Message-ID: <1359936735-31915-4-git-send-email-nwmcsween@gmail.com> (raw)
In-Reply-To: <1359936735-31915-1-git-send-email-nwmcsween@gmail.com>

---
 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



  parent reply	other threads:[~2013-02-04  0:12 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-04  0:12 [PATCH 0/4] Refactor and expand string functions Nathan McSween
2013-02-04  0:12 ` [PATCH 1/4] Internal: Add word.h - word-at-a-time fns / macros Nathan McSween
2013-02-04  0:12 ` [PATCH 2/4] String: refactor to utilize word.h and optimize Nathan McSween
2013-02-04  0:12 ` Nathan McSween [this message]
2013-02-04  2:24   ` [PATCH 3/4] String: expand to word-at-a-time Isaac Dunham
2013-02-04  2:55     ` nwmcsween
2013-02-04  0:12 ` [PATCH 4/4] String: refactor to utilize word.h and always terminate string Nathan McSween
2013-02-05  4:25 ` [PATCH 0/4] Refactor and expand string functions Nathan McSween
2013-02-05 11:19   ` Szabolcs Nagy
2013-02-05 14:05     ` Rich Felker
2013-02-05 15:05       ` Szabolcs Nagy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1359936735-31915-4-git-send-email-nwmcsween@gmail.com \
    --to=nwmcsween@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).