From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x2c.google.com (mail-oa1-x2c.google.com [IPv6:2001:4860:4864:20::2c]) by sourceware.org (Postfix) with ESMTPS id DE8383858D37 for ; Mon, 9 Jan 2023 20:51:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE8383858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-15027746720so10018909fac.13 for ; Mon, 09 Jan 2023 12:51:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=FW4WlYhpnwSHTmo/bl3fuZsMYO/tvh2dKvxTC4kMCCk=; b=P6mH38OP7kZ12Ph3LUK7rMTS+z6ynTMMMBpoTI9j0tumX1Uf5pE7znpISo6uAGiqvV 8MsLT7Aq5OiMyDrOnCXKyc26bltNeP4WErnZKPOOyCdbwtQ5loWNYWklrZJoBunY9jTc wzoEGGoA+y2jbHSWtZoxUdWI0YJ12gUl/4B+necJ7nASpVoQ8YCorshRU/Y+6PAvLYvj QqcvNXcbjTzAcVPhhXVtdvFTNP1krji7rICUImHoT2mGkVuiFdBt5l5BZVa2SQGKG4o6 YWkIzmZQam1PC71gZMfXzRWzyFO/XfaUdRDGKSR9PVzzGRcDLxr/salaETF5oKcFuE6h SaDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=FW4WlYhpnwSHTmo/bl3fuZsMYO/tvh2dKvxTC4kMCCk=; b=PNyCa3Z1pSpHdsnpmiUDHKI9Wbmj26BAOMTqgcnCs6TZUNZlquBuHr5KoUL7F15ox8 7jOMmnP59cwaYepszHTfBk6Ix2kmjl7NH+3VDOMt8Wt+sK0RZ/OK9jHTSnS6f6Ld/JeS VlN4G4MED0R6AO1CcUATzTCOcp2GBXiiTL8W0BRvvTP/zqXtxRJdOitntmvubHNnChmM 8jm8TajfMBxgDcbCSiuI/YTnsq3fFOlZmMRmFoAwGEGOJF4qEZV4/TJJ0fil/J/3dbKX 1knqUfRzXSmMDJ4hUnuNWjwmgT56/1G27P3BVsnxH/Rq4dwBdXttlFrqEaSJnjKcPLiD ITHg== X-Gm-Message-State: AFqh2kpHD2bZOErb3luOs5L8aF1eIrnUfP+XC2tokcJahHacl1K+VkHz 5AqONymvY1SHX2PeEKAZ1CZCqA== X-Google-Smtp-Source: AMrXdXtezqX3CUuLmOzn1T3DJ2VcVRKrbu06Q2USzcPVy4wFMu5LSMbn9PW/oJFm3bCWB9Q1/vTWaA== X-Received: by 2002:a05:6870:a913:b0:144:1010:d55a with SMTP id eq19-20020a056870a91300b001441010d55amr32104235oab.26.1673297492988; Mon, 09 Jan 2023 12:51:32 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c0:a93a:8d00:c4d9:6d86:9f2b? ([2804:1b3:a7c0:a93a:8d00:c4d9:6d86:9f2b]) by smtp.gmail.com with ESMTPSA id h24-20020a056870171800b0014f9cc82421sm4782530oae.33.2023.01.09.12.51.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Jan 2023 12:51:32 -0800 (PST) Message-ID: <6e926487-5fff-5c67-6c86-6cc38a126bf8@linaro.org> Date: Mon, 9 Jan 2023 17:51:29 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH v5 10/17] string: Improve generic memchr Content-Language: en-US To: Noah Goldstein Cc: libc-alpha@sourceware.org, Richard Henderson References: <20220919195920.956393-1-adhemerval.zanella@linaro.org> <20220919195920.956393-11-adhemerval.zanella@linaro.org> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 05/01/23 20:49, Noah Goldstein wrote: > On Mon, Sep 19, 2022 at 1:05 PM Adhemerval Zanella via Libc-alpha > wrote: >> >> New algorithm have the following key differences: >> >> - Reads first word unaligned and use string-maskoff function to >> remove unwanted data. This strategy follow arch-specific >> optimization used on aarch64 and powerpc. >> >> - Use string-fz{b,i} and string-opthr functions. >> >> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu, >> and powerpc64-linux-gnu by removing the arch-specific assembly >> implementation and disabling multi-arch (it covers both LE and BE >> for 64 and 32 bits). >> >> Co-authored-by: Richard Henderson >> --- >> string/memchr.c | 168 +++++------------- >> .../powerpc32/power4/multiarch/memchr-ppc32.c | 14 +- >> .../powerpc64/multiarch/memchr-ppc64.c | 9 +- >> 3 files changed, 48 insertions(+), 143 deletions(-) >> >> diff --git a/string/memchr.c b/string/memchr.c >> index 422bcd0cd6..08d518b02d 100644 >> --- a/string/memchr.c >> +++ b/string/memchr.c >> @@ -1,10 +1,6 @@ >> -/* Copyright (C) 1991-2022 Free Software Foundation, Inc. >> +/* Scan memory for a character. Generic version >> + Copyright (C) 1991-2022 Free Software Foundation, Inc. >> This file is part of the GNU C Library. >> - Based on strlen implementation by Torbjorn Granlund (tege@sics.se), >> - with help from Dan Sahlin (dan@sics.se) and >> - commentary by Jim Blandy (jimb@ai.mit.edu); >> - adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu), >> - and implemented by Roland McGrath (roland@ai.mit.edu). >> >> The GNU C Library is free software; you can redistribute it and/or >> modify it under the terms of the GNU Lesser General Public >> @@ -20,143 +16,65 @@ >> License along with the GNU C Library; if not, see >> . */ >> >> -#ifndef _LIBC >> -# include >> -#endif >> - >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> #include >> >> -#include >> +#undef memchr >> >> -#include >> - >> -#undef __memchr >> -#ifdef _LIBC >> -# undef memchr >> +#ifdef MEMCHR >> +# define __memchr MEMCHR >> #endif >> >> -#ifndef weak_alias >> -# define __memchr memchr >> -#endif >> - >> -#ifndef MEMCHR >> -# define MEMCHR __memchr >> -#endif >> +static inline const char * >> +sadd (uintptr_t x, uintptr_t y) >> +{ >> + uintptr_t ret = INT_ADD_OVERFLOW (x, y) ? (uintptr_t)-1 : x + y; >> + return (const char *)ret; >> +} >> >> /* Search no more than N bytes of S for C. */ >> void * >> -MEMCHR (void const *s, int c_in, size_t n) >> +__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; >> + if (__glibc_unlikely (n == 0)) >> + return NULL; >> >> - const unsigned char *char_ptr; >> - const longword *longword_ptr; >> - longword repeated_one; >> - longword repeated_c; >> - unsigned char c; >> + uintptr_t s_int = (uintptr_t) s; >> >> - c = (unsigned char) c_in; >> + /* Set up a word, each of whose bytes is C. */ >> + op_t repeated_c = repeat_bytes (c_in); >> + op_t before_mask = create_mask (s_int); >> >> - /* 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; >> + /* Compute the address of the last byte taking in consideration possible >> + overflow. */ >> + const char *lbyte = sadd (s_int, n - 1); >> >> - longword_ptr = (const longword *) char_ptr; >> + /* Compute the address of the word containing the last byte. */ >> + const op_t *lword = word_containing (lbyte); >> >> - /* All these elucidatory comments refer to 4-byte longwords, >> - but the theory applies equally well to any size longwords. */ >> + /* Read the first word, but munge it so that bytes before the array >> + will not match goal. */ >> + const op_t *word_ptr = word_containing (s); >> + op_t word = (*word_ptr | before_mask) ^ (repeated_c & before_mask); >> >> - /* 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) >> + while (has_eq (word, repeated_c) == 0) >> { >> - 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; >> - } >> - } >> + if (word_ptr == lword) >> + return NULL; > Inuitively making lword, lword - 1 so that normal returns don't need the extra > null check would be faster. Hum, I did not follow; could you explain it with more details what you mean here?