mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: Re: Re: [PATCH 0/4] Refactor and expand string functions.
Date: Tue, 5 Feb 2013 12:19:10 +0100	[thread overview]
Message-ID: <20130205111910.GQ6181@port70.net> (raw)
In-Reply-To: <511089D1.1000803@gmail.com>

* Nathan McSween <nwmcsween@gmail.com> [2013-02-04 20:25:53 -0800]:
> /**
>  * 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--)
> 		i

did you test this code?

w++ but n-- does not seem right

> #define WORD_LSB_ONE ((size_t)-1 / (unsigned char)-1)
> #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

the code is mostly ok: don't use "char c" argument, use
unsigned char or int, same for return value

but i don't like the style of this

maybe it's just me but the WORD_LSB_ONE is a long name
that does not help me understand what it is, it could
be W1 vs W128 and the same amount of explanation would
be embedded in the name (but much shorter and thus easier
to recognize, things fit on one line etc, it assumes 8bit
char though)

the comments are bad (lot of text with no content)
and the code actually does magic that may need explanation
it's rather frustrating when you try to understand what's
going on and have to read lot of useless comments..

i dont see how word_has_char works, the name suggests that
it tests if w has c in it, but that's not what it does
(and the comment is actively misleading: only shows one arg,
which demonstrates why i dislike these kinds of comments,
thye cause more harm than good)


  reply	other threads:[~2013-02-05 11:19 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-04  0:12 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 ` [PATCH 3/4] String: expand to word-at-a-time Nathan McSween
2013-02-04  2:24   ` 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 [this message]
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=20130205111910.GQ6181@port70.net \
    --to=nsz@port70.net \
    --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).