mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Jonas Wagner <jonas.wagner@epfl.ch>
To: musl@lists.openwall.com
Subject: Fwd: Alignment check in strlen
Date: Wed, 20 Feb 2013 19:05:28 +0100	[thread overview]
Message-ID: <CAMRJFfL3ZnFdiW9_JS2p3mpvSLp_28zwmZDu3GbmNo+jkfdqsA@mail.gmail.com> (raw)
In-Reply-To: <CAMRJFfKkghLFLpxg4C12XQmNiWZpZbZGVYWwup5dXmZ7Ty=W-g@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 517 bytes --]

Dear all,

attached is a patch that makes the alignment check in strlen more
consistent with the one in memcpy, and hopefully faster.

In src/string/memcpy.c, I find the following:

#define ALIGN (sizeof(size_t)-1)
if (((uintptr_t)d & ALIGN) != ((uintptr_t)s & ALIGN))
  goto misaligned;

In src/string/strlen.c, a different check was used instead:

#define ALIGN (sizeof(size_t))
for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;

I do not think there is any particular reason for this difference.

Best,
Jonas

[-- Attachment #1.2: Type: text/html, Size: 1166 bytes --]

[-- Attachment #2: 0001-strlen-use-bitwise-AND-for-alignment-test-instead-of.patch --]
[-- Type: application/octet-stream, Size: 1018 bytes --]

From d2b8aff9b799a2402e7f7769bdfa511cc246768b Mon Sep 17 00:00:00 2001
From: Jonas Wagner <jonas.wagner@epfl.ch>
Date: Wed, 20 Feb 2013 18:50:01 +0100
Subject: [PATCH] strlen: use bitwise AND for alignment test, instead of %.

---
 src/string/strlen.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/string/strlen.c b/src/string/strlen.c
index d6f8631..92784ea 100644
--- a/src/string/strlen.c
+++ b/src/string/strlen.c
@@ -3,7 +3,7 @@
 #include <stdint.h>
 #include <limits.h>
 
-#define ALIGN (sizeof(size_t))
+#define ALIGN (sizeof(size_t)-1)
 #define ONES ((size_t)-1/UCHAR_MAX)
 #define HIGHS (ONES * (UCHAR_MAX/2+1))
 #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
@@ -12,7 +12,7 @@ size_t strlen(const char *s)
 {
 	const char *a = s;
 	const size_t *w;
-	for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;
+	for (; (uintptr_t)s & ALIGN; s++) if (!*s) return s-a;
 	for (w = (const void *)s; !HASZERO(*w); w++);
 	for (s = (const void *)w; *s; s++);
 	return s-a;
-- 
1.7.10.4


       reply	other threads:[~2013-02-20 18:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAMRJFfKkghLFLpxg4C12XQmNiWZpZbZGVYWwup5dXmZ7Ty=W-g@mail.gmail.com>
2013-02-20 18:05 ` Jonas Wagner [this message]
2013-02-20 18:22   ` 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=CAMRJFfL3ZnFdiW9_JS2p3mpvSLp_28zwmZDu3GbmNo+jkfdqsA@mail.gmail.com \
    --to=jonas.wagner@epfl.ch \
    --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).