From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12526 invoked by alias); 20 Dec 2016 12:50:28 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 12514 invoked by uid 89); 20 Dec 2016 12:50:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=2966 X-HELO: mail-yw0-f172.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=ZznC2Bj3dEAVLLu4T2Nt5CSeQouakidp6KUCtkJ9lNY=; b=A183s+1/YKRPcH/HIgCVhOon9Z1xsK+98QKkCGDQlNsl/M4ZqvYbGSHzrJlZixgPvv KvbZIl7mVZbFpmN3JKi8/A3FUEXNfMo7EeqEYbeUCGAs1L7fnHqVrSNzklav086R8zhq jqfm0chjCMrB55rCr9L4bLZV8qE4GHwt2AmsVXiF2giPFQAyUVjlbhlHhxBxp4EIMleZ 26z4V0MbJZevC297IYXmeecua8ulQxjvbYJ2RSxlL5UKCurlqrirFzyOYtGUcE5kbBmL k7Pgi3XssV0pcSLPwkFsAZ7wWF/0t5jqKhx6e8fbf83CBMIego/5qJ3wTs1Wv1hkKQmW J3tA== X-Gm-Message-State: AKaTC03UR2opF762WCxkNKwyMfqs0VslAy7aqSUSxYkox5K9ZSrYCCv0wE4jzIBN30A6YAXr X-Received: by 10.129.43.133 with SMTP id r127mr15414977ywr.213.1482238215780; Tue, 20 Dec 2016 04:50:15 -0800 (PST) Subject: Re: [PATCH 05/11] Improve generic strrchr To: libc-alpha@sourceware.org References: <20161217065729.28561-1-rth@twiddle.net> <20161217065729.28561-6-rth@twiddle.net> From: Adhemerval Zanella Message-ID: Date: Tue, 20 Dec 2016 12:50:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 In-Reply-To: <20161217065729.28561-6-rth@twiddle.net> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2016-12/txt/msg00760.txt.bz2 As for strchr and and strnlen, I think default strrchr can be changed use already optimized symbols: -- #ifndef STRRCHR # define STRRCHR strrchr #endif /* Find the last occurrence of C in S. */ char * STRRCHR (const char *s, int int_c) { return __memrchr (s, int_c, strlen (s) + 1); } -- On 17/12/2016 04:57, Richard Henderson wrote: > * string/strrchr.c: Use haszero.h. > --- > string/strrchr.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++---------- > 1 file changed, 56 insertions(+), 11 deletions(-) > > diff --git a/string/strrchr.c b/string/strrchr.c > index a07457e..bea7d76 100644 > --- a/string/strrchr.c > +++ b/string/strrchr.c > @@ -16,6 +16,10 @@ > . */ > > #include > +#include > +#include > +#include > +#include > > #undef strrchr > > @@ -25,25 +29,66 @@ > > /* Find the last occurrence of C in S. */ > char * > -STRRCHR (const char *s, int c) > +STRRCHR (const char *s, int int_c) > { > - const char *found, *p; > + const unsigned char *found_c = NULL, *ptr_c; > + const unsigned long int *found_w = NULL, *ptr_w; > + unsigned long int longword, repeated_c; > + uintptr_t i, align; > + unsigned char c; > > - c = (unsigned char) c; > + c = (unsigned char) int_c; > + ptr_c = (const unsigned char *) s; > > - /* Since strchr is fast, we use it rather than the obvious loop. */ > + /* Handle the first few characters by reading one character at a time. > + Do this until CHAR_PTR is aligned on a longword boundary. */ > + align = -(uintptr_t)ptr_c % sizeof(longword); > + for (i = 0; i < align; ++i, ++ptr_c) > + { > + unsigned char this_c = *ptr_c; > + if (this_c == c) > + found_c = ptr_c; > + if (this_c == '\0') > + goto done; > + } > + > + /* Set up a longword, each of whose bytes is C. */ > + repeated_c = (-1ul / 0xff) * c; > + > + /* Search words for C. At this point, merely record the last word > + that contained the character. Stop when we find EOS. */ > + ptr_w = (const unsigned long int *) ptr_c; > + while (1) > + { > + longword = *ptr_w; > + if (haszero (longword)) > + break; > + if (haszero (longword ^ repeated_c)) > + found_w = ptr_w; > + ptr_w++; > + } > > - if (c == '\0') > - return strchr (s, '\0'); > + /* Check to see if we've got C in the last longword. */ > + i = whichzero2 (longword, longword ^ repeated_c); > + if (extractbyte (longword, i) == c) > + found_w = ptr_w; > > - found = NULL; > - while ((p = strchr (s, c)) != NULL) > + /* If we found a word containing C, go back and search it byte by byte. */ > + if (found_w) > { > - found = p; > - s = p + 1; > + ptr_c = (const unsigned char *) found_w; > + for (i = 0; i < sizeof(longword); ++i, ++ptr_c) > + { > + unsigned char this_c = *ptr_c; > + if (this_c == c) > + found_c = ptr_c; > + if (this_c == '\0') > + break; > + } > } > > - return (char *) found; > + done: > + return (char *) found_c; > } > > #ifdef weak_alias >