From patchwork Wed Jan 10 12:47:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 124090 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp5236498qgn; Wed, 10 Jan 2018 04:50:58 -0800 (PST) X-Google-Smtp-Source: ACJfBovFiwk645wxSkvVKukwlAWj3Z1z1f9dikNVMhg2GPXgZlqfyJyj3aQNqoxozlZM4Rlo+mGc X-Received: by 10.84.218.69 with SMTP id f5mr6572831plm.431.1515588658857; Wed, 10 Jan 2018 04:50:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515588658; cv=none; d=google.com; s=arc-20160816; b=T7FvcLu6a2RwCfP2WP7t9lbhhzj9s/kCegx3WJlrfy2KizMluzxThIr0j5o/1droYA p2oSDqjSgHj1fSu7B2uCyGSJ+AYL1xBrBqAT9noAxqAT3qIH5E9f/pZIaGR+dKgOy/Ig MZVUk7JJfCs2YPY6GGxF/YJVv7FQWCMmqJn025MRWMKH40rspk43ly+gy1IXEsAY+lw4 XdQVQ4N7a2k62ls3e9CSG67kLIK5PZsRd1a+jWfqiSM4NgMOvH6/o9SQMijahgLP3t8O 4fJAvPvut1FHfLh7FcWYcmg6OMZHUx/6u2t1HPYrgpOA4IFXzTVPay+lRE7a6e+XPQzf /gKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :delivered-to:sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=ZAGQXyp3oa0jH6OpVTFTW783F01OZriu/gm0F37XlL0=; b=flq42qAfBEuHr6/bPc+lJu+L3HPAI4zMTUxytfgLmsBOESSL7MauvdnfSvPg8rsqC7 Cy3AiVuwaLx1sbZjOOJ+7PoiqOKgtcZo+Jdh2ldMvvqiXIoS/ckG5059Xr2yS0LEDV5p 4jxiV9LFUopVH2qy44UuKm0GmbpVZ9jQlz4ha6czmQP0cPN/+8YN+YLmw06+6265jixz tFip73kaG/5hPivzb+oOpY7jGbikk3mMEKq3h07nypN3iC5NJcDKzZ7lvTdTsLF2OCIJ maj5AAqgEpZiAxD92CGamRbhfT2oh5qhyCO/TYn+86MCPQNB4GfmVmNruWcGNFWh9oiN q/2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=tRLc59+e; spf=pass (google.com: domain of libc-alpha-return-89012-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89012-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id j189si5724241pgc.294.2018.01.10.04.50.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Jan 2018 04:50:58 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-89012-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=tRLc59+e; spf=pass (google.com: domain of libc-alpha-return-89012-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89012-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=VA0ZLHngqyP7/JJvWR1jFVBj0GLd58m 4INSWQV+r2PBiTTdPmPEUrjZnY72SyBfgolfRH/TUQzyXonEIUVv6Q5JAZp/EGQo jRxQvEfSgx2Ue2pnvrWykoPuXf4d4VzH7e9JADE89+n5DojQWNuzGzuD+ZM47BHm 8eKy5RM+c6ck= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=4sN2xALPEoodkH/Ch3h/E4wONto=; b=tRLc5 9+egLY/Cg2y6a9gTFndKVsqPkPcsYP+OkCE/qTGyOSrvgsNc9TctI2klFaM4c1u8 gW42HeHqufGQZ1T1Qee57twP5Q84eRU0uSwjlIFUpPazd8iAAo4E2bkTyqtG+gS6 XJzozK1V+bb8ZQq0/ksivEoRQaBtrQgAPDoa+s= Received: (qmail 130719 invoked by alias); 10 Jan 2018 12:48:45 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 121697 invoked by uid 89); 10 Jan 2018 12:48:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=j1 X-HELO: mail-qk0-f195.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZAGQXyp3oa0jH6OpVTFTW783F01OZriu/gm0F37XlL0=; b=GA6VbXPLY23hRNijI9SQE/ukqN0bVx9fOUlK6/q0MsIsHdLXn0tBkNmmdqiIM8PGEr oQFickl7m2/ljFqzxRBaSCNeoa42C7VS+2zVO4zER5/NfVw/HcA1Z4fES5q2S9q4NY+x nZtku9up8AXYAY9WocUSdKV5BSX/co2LKMPdF+PX7yDsUtLSslv5/FaQffbOAVDaPgBL JrnnRxQFxI/noreaZGv86ZmZrSm2Vmt7+Pvqnf5h9luqSUMWY4KBz0U5zb+0mqMgdxDZ pnMcMU4raODuCf0RL+UX4AobTW+ghObzRqoPQVaDKDtJBjXVTHL8a34oxDs9FgMLJ2LB 43Hw== X-Gm-Message-State: AKGB3mL5cJn9o4p1jS138JaHINXwNRUClIUb7AVlDKw8seMObIPUO+kl bgEFpYLKDtS8oH/nWMLt3RZHE0reufE= X-Received: by 10.55.106.195 with SMTP id f186mr24382195qkc.53.1515588498463; Wed, 10 Jan 2018 04:48:18 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Richard Henderson Subject: [PATCH v3 06/18] string: Improve generic memchr Date: Wed, 10 Jan 2018 10:47:50 -0200 Message-Id: <1515588482-15744-7-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1515588482-15744-1-git-send-email-adhemerval.zanella@linaro.org> References: <1515588482-15744-1-git-send-email-adhemerval.zanella@linaro.org> From: Richard Henderson New algorithm have the following key differences: - Reads first word unaligned and use string-maskoff function to remove unwanted data. This strategy follow assemble optimized ones for aarch64, powerpc and tile. - Use string-fz{b,i} and string-opthr functions. Checked on x86_64-linux-gnu, i686-linux-gnu, sparc64-linux-gnu, and sparcv9-linux-gnu by removing the arch-specific assembly implementation and disabling multi-arch (it covers both LE and BE for 64 and 32 bits). [BZ #5806] * string/memchr.c: Use string-fzb.h, string-fzi.h, string-opthr.h. --- string/memchr.c | 157 +++++++++++++++----------------------------------------- 1 file changed, 40 insertions(+), 117 deletions(-) -- 2.7.4 diff --git a/string/memchr.c b/string/memchr.c index c4e21b8..ae3fd93 100644 --- a/string/memchr.c +++ b/string/memchr.c @@ -20,24 +20,16 @@ License along with the GNU C Library; if not, see . */ -#ifndef _LIBC -# include -#endif - #include - #include +#include +#include +#include +#include +#include +#include -#include - -#undef __memchr -#ifdef _LIBC -# undef memchr -#endif - -#ifndef weak_alias -# define __memchr memchr -#endif +#undef memchr #ifndef MEMCHR # define MEMCHR __memchr @@ -47,116 +39,47 @@ void * MEMCHR (void const *s, int c_in, size_t n) { - /* On 32-bit hardware, choosing longword to be a 32-bit unsigned - long instead of a 64-bit uintmax_t tends to give better - performance. On 64-bit hardware, unsigned long is generally 64 - bits already. Change this typedef to experiment with - performance. */ - typedef unsigned long int longword; - - const unsigned char *char_ptr; - const longword *longword_ptr; - longword repeated_one; - longword repeated_c; - unsigned char c; - - c = (unsigned char) c_in; - - /* Handle the first few bytes by reading one byte at a time. - Do this until CHAR_PTR is aligned on a longword boundary. */ - for (char_ptr = (const unsigned char *) s; - n > 0 && (size_t) char_ptr % sizeof (longword) != 0; - --n, ++char_ptr) - if (*char_ptr == c) - return (void *) char_ptr; - - longword_ptr = (const longword *) char_ptr; - - /* All these elucidatory comments refer to 4-byte longwords, - but the theory applies equally well to any size longwords. */ - - /* Compute auxiliary longword values: - repeated_one is a value which has a 1 in every byte. - repeated_c has c in every byte. */ - repeated_one = 0x01010101; - repeated_c = c | (c << 8); - repeated_c |= repeated_c << 16; - if (0xffffffffU < (longword) -1) - { - repeated_one |= repeated_one << 31 << 1; - repeated_c |= repeated_c << 31 << 1; - if (8 < sizeof (longword)) - { - size_t i; - - for (i = 64; i < sizeof (longword) * 8; i *= 2) - { - repeated_one |= repeated_one << i; - repeated_c |= repeated_c << i; - } - } - } + const op_t *word_ptr, *lword; + op_t repeated_c, before_mask, word; + const char *lbyte; + char *ret; + uintptr_t s_int; - /* Instead of the traditional loop which tests each byte, we will test a - longword at a time. The tricky part is testing if *any of the four* - bytes in the longword in question are equal to c. We first use an xor - with repeated_c. This reduces the task to testing whether *any of the - four* bytes in longword1 is zero. - - We compute tmp = - ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7). - That is, we perform the following operations: - 1. Subtract repeated_one. - 2. & ~longword1. - 3. & a mask consisting of 0x80 in every byte. - Consider what happens in each byte: - - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff, - and step 3 transforms it into 0x80. A carry can also be propagated - to more significant bytes. - - If a byte of longword1 is nonzero, let its lowest 1 bit be at - position k (0 <= k <= 7); so the lowest k bits are 0. After step 1, - the byte ends in a single bit of value 0 and k bits of value 1. - After step 2, the result is just k bits of value 1: 2^k - 1. After - step 3, the result is 0. And no carry is produced. - So, if longword1 has only non-zero bytes, tmp is zero. - Whereas if longword1 has a zero byte, call j the position of the least - significant zero byte. Then the result has a zero at positions 0, ..., - j-1 and a 0x80 at position j. We cannot predict the result at the more - significant bytes (positions j+1..3), but it does not matter since we - already have a non-zero bit at position 8*j+7. - - So, the test whether any byte in longword1 is zero is equivalent to - testing whether tmp is nonzero. */ - - while (n >= sizeof (longword)) - { - longword longword1 = *longword_ptr ^ repeated_c; - if ((((longword1 - repeated_one) & ~longword1) - & (repeated_one << 7)) != 0) - break; - longword_ptr++; - n -= sizeof (longword); - } + if (__glibc_unlikely (n == 0)) + return NULL; + + s_int = (uintptr_t) s; + word_ptr = (const op_t*) (s_int & -sizeof (op_t)); - char_ptr = (const unsigned char *) longword_ptr; + /* Set up a word, each of whose bytes is C. */ + repeated_c = repeat_bytes (c_in); + before_mask = create_mask (s_int); - /* At this point, we know that either n < sizeof (longword), or one of the - sizeof (longword) bytes starting at char_ptr is == c. On little-endian - machines, we could determine the first such byte without any further - memory accesses, just by looking at the tmp result from the last loop - iteration. But this does not work on big-endian machines. Choose code - that works in both cases. */ + /* Compute the address of the last byte taking in consideration possible + overflow. */ + uintptr_t lbyte_int = s_int + n - 1; + lbyte_int |= -(lbyte_int < s_int); + lbyte = (const char *) lbyte_int; - for (; n > 0; --n, ++char_ptr) + /* Compute the address of the word containing the last byte. */ + lword = (const op_t *) ((uintptr_t) lbyte & -sizeof (op_t)); + + /* Read the first word, but munge it so that bytes before the array + will not match goal. */ + word = (*word_ptr | before_mask) ^ (repeated_c & before_mask); + + while (has_eq (word, repeated_c) == 0) { - if (*char_ptr == c) - return (void *) char_ptr; + if (word_ptr == lword) + return NULL; + word = *++word_ptr; } - return NULL; + /* We found a match, but it might be in a byte past the end + of the array. */ + ret = (char *) word_ptr + index_first_eq (word, repeated_c); + return (ret <= lbyte) ? ret : NULL; } -#ifdef weak_alias weak_alias (__memchr, memchr) -#endif libc_hidden_builtin_def (memchr)