From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/2736 Path: news.gmane.org!not-for-mail From: Nathan McSween Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH 0/4] Refactor and expand string functions. Date: Mon, 04 Feb 2013 20:25:53 -0800 Message-ID: <511089D1.1000803@gmail.com> References: <1359936735-31915-1-git-send-email-nwmcsween@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------010704050305070009060209" X-Trace: ger.gmane.org 1360038396 1068 80.91.229.3 (5 Feb 2013 04:26:36 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 Feb 2013 04:26:36 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-2737-gllmg-musl=m.gmane.org@lists.openwall.com Tue Feb 05 05:26:57 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 1U2a7U-0005W3-HD for gllmg-musl@plane.gmane.org; Tue, 05 Feb 2013 05:26:56 +0100 Original-Received: (qmail 5726 invoked by uid 550); 5 Feb 2013 04:26:37 -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 5718 invoked from network); 5 Feb 2013 04:26:36 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type; bh=+IrwFmiIMGsLmVlY3MPtn2v5OmveY6dnIl2eYZRYIKI=; b=rnbTGRJvS+bIbRSkwMMV8zY+hEn7c4OczH6uSKRxLMOYvHdY+JnUvKBP330TqyvYdZ L5IdYSbbPxGanc2IOcizr/wueCd5oChGoDtx3vmfz0OAd9fPZ1EwuzvipPC/eskbbVw/ m8QyKyGeYR+1dtujqqLdEWzjDSisoENsK8361DI2UB9qlKwyLhD6ZFBPY1G/1UuHqHJd iPW6zY2XvOsTw1/BlpXfPS8GoI2BGSEOdyV9i1u0UAkrWQwVedBukuX6H7Bu6Xvs0HDu yjCxV3+/VMTtQ4g7GL0ExwhpTHEUeuHlGD4Vu2LGARztZYGAvwXyMhoVe6d41oBmjvg0 3I5A== X-Received: by 10.50.208.7 with SMTP id ma7mr10801512igc.26.1360038384667; Mon, 04 Feb 2013 20:26:24 -0800 (PST) User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 In-Reply-To: <1359936735-31915-1-git-send-email-nwmcsween@gmail.com> Xref: news.gmane.org gmane.linux.lib.musl.general:2736 Archived-At: This is a multi-part message in MIME format. --------------010704050305070009060209 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 2/3/2013 4:12 PM, Nathan McSween wrote: > memchr - refactor > memcmp - word-at-a-time > memset - refactor > strcmp - word-at-a-time > strlcpy - refactor and always terminate string > strlen - refactor > strncmp - word-at-a-time > > A simple wc -l on asm lines for changed files gives: > 91 new_memchr.s > 106 musl_memchr.s > 65 new_memcmp.s > 32 musl_memcmp.s > 118 new_memset.s > 121 musl_memset.s > 64 new_strcmp.s > 26 musl_strcmp.s > 98 new_strlcpy.s > 124 musl_strlcpy.s > 55 new_strlen.s > 55 musl_strlen.s > 66 new_strncmp.s > 45 musl_strncmp.s > > Bikeshed over inline documentation welcome. > > Nathan McSween (4): > Internal: Add word.h - word-at-a-time fns / macros > String: refactor to utilize word.h and optimize > String: expand to word-at-a-time > String: refactor to utilize word.h and always terminate string > > src/internal/word.h | 39 ++++++++++++++++++++++++++++++++++++ > src/string/memchr.c | 42 ++++++++++++++++++++++----------------- > src/string/memcmp.c | 38 +++++++++++++++++++++++++++++++---- > src/string/memset.c | 39 +++++++++++++++++++++--------------- > src/string/strcmp.c | 35 +++++++++++++++++++++++++++++--- > src/string/strlcpy.c | 56 ++++++++++++++++++++++++++++++---------------------- > src/string/strlen.c | 29 +++++++++++++++------------ > src/string/strncmp.c | 36 ++++++++++++++++++++++++++++----- > 8 files changed, 231 insertions(+), 83 deletions(-) > create mode 100644 src/internal/word.h > Attached are files of the functions changed. --------------010704050305070009060209 Content-Type: text/plain; charset=windows-1252; name="memchr.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="memchr.c" #include #include #include #include "word.h" /** * memchr - Word sized c standard memchr. * @s: Source * @c: Character * @n: Max size of @s */ void *memchr(const void *s, int c, size_t n) { const unsigned char *cs = (const unsigned char *)s; const size_t *w; c = (unsigned char)c; for (; (uintptr_t)cs % sizeof(size_t); cs++, n--) { if (!n) return NULL; if (*cs == c) return (void *)cs; } for (w = (const size_t *)cs; !word_has_char(*w, c); w++, n--) if (!n) return NULL; for (cs = (const unsigned char *)w; *cs != c; cs++, n--) if (!n) return NULL; return (void *)cs; } --------------010704050305070009060209 Content-Type: text/plain; charset=windows-1252; name="memcmp.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="memcmp.c" #include #include #include "word.h" /** * 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 *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; } --------------010704050305070009060209 Content-Type: text/plain; charset=windows-1252; name="memset.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="memset.c" #include #include #include #include "word.h" /** * memset - Word sized c standard memset. * @d: Destination * @c: Charater to set * @n: Max size to set to @c in @d */ void *memset(void *d, int c, size_t n) { unsigned char *cd = (unsigned char *)d; const size_t wc = WORD_LSB_ONE * (unsigned char)c; size_t *wd; c = (unsigned char)c; for (; (uintptr_t)cd % sizeof(size_t); *cd++ = c, n--) if (!n) return d; for (wd = (size_t *)cd; n >= sizeof(size_t) ; *wd++ = wc, n -= sizeof(size_t)); for (cd = (unsigned char *)wd; n; *cd++ = c, n--); return d; } --------------010704050305070009060209 Content-Type: text/plain; charset=windows-1252; name="strcmp.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="strcmp.c" #include #include #include #include "word.h" /** * strcmp - Word sized c standard strcmp. * @c: Comparative * @s: Source */ #undef strcmp int strcmp(const char *c, const char *s) { 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; } --------------010704050305070009060209 Content-Type: text/plain; charset=windows-1252; name="strlcpy.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="strlcpy.c" #include #include #include #include "word.h" /** * strlcpy - Word sized bsd strlcpy. * @d: Destination * @s: Source * @n: Max @s */ size_t strlcpy(char *d, const char *s, size_t n) { char *z = d; size_t *wd; const size_t *ws; /* A byte for nul */ if (!n--) goto terminate; if ((uintptr_t)d % sizeof(size_t) != (uintptr_t)s % sizeof(size_t)) goto misaligned; for (; (uintptr_t)s % sizeof(size_t); *d++ = *s++, n--) if (!*s || !n) goto terminate; for (wd = (size_t *)d, ws= (const size_t *)s ; !word_has_zero(*ws) && n >= sizeof(size_t) ; *wd = *ws, wd++, ws++, n -= sizeof(size_t)) d = (char *)wd; s = (const char *)ws; misaligned: for (; (*d = *s) && n; d++, s++, n--); terminate: *d = '\0'; return d - z + strlen(s); } --------------010704050305070009060209 Content-Type: text/plain; charset=windows-1252; name="strlen.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="strlen.c" #include #include #include #include "word.h" /** * strlen - Word sized c standard strlen. * @s: Source */ size_t strlen(const char *s) { const char *z = s; const size_t *w; for (; (uintptr_t)s % sizeof(size_t); s++) if (!*s) return s - z; for (w = (const size_t *)s; !word_has_zero(*w); w++); for (s = (const char *)w; *s; s++); return s - z; } --------------010704050305070009060209 Content-Type: text/plain; charset=windows-1252; name="strncmp.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="strncmp.c" #include #include #include "word.h" /** * 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 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; } --------------010704050305070009060209 Content-Type: text/plain; charset=windows-1252; name="word.h" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="word.h" /** * _INTERNAL_WORD_H - various word size functions / macros */ #ifndef _MYOSIN_WORD_H #define _MYOSIN_WORD_H #include #include /** * WORD_LSB_ONE - Set low bit of each byte on arch word size to one. */ #define WORD_LSB_ONE ((size_t)-1 / (unsigned char)-1) /** * WORD_MSB_ONE - Set high bit of each byte on arch word size to one. */ #define WORD_MSB_ONE (WORD_LSB_ONE * ((unsigned char)-1 / 2 + 1)) /** * word_has_zero - Word has a zero character * @w: Word */ static inline char word_has_zero(size_t w) { return !!((w - WORD_LSB_ONE) & (~w & WORD_MSB_ONE)); } /** * word_has_char - Word has a character * @w: Word */ static inline char word_has_char(size_t w, char c) { return !!((w - WORD_LSB_ONE) & ((~w & WORD_MSB_ONE)^(WORD_LSB_ONE * c))); } #endif /* !_INTERNAL_WORD_H */ --------------010704050305070009060209--