From patchwork Wed Jan 11 20:45:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 641264 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp3458102pvb; Wed, 11 Jan 2023 12:47:06 -0800 (PST) X-Google-Smtp-Source: AMrXdXuirLWQ9u2E51Bfyml/sTuVVHRRelVu+uthbcewCvtUJl0pn5xILQNpKl9rQr7Mze4G/ct5 X-Received: by 2002:a05:6402:4441:b0:47f:6531:dee9 with SMTP id o1-20020a056402444100b0047f6531dee9mr60065426edb.20.1673470026721; Wed, 11 Jan 2023 12:47:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673470026; cv=none; d=google.com; s=arc-20160816; b=MYRdNT8rJdqx/rtxRAYcxhQPo+QCJC0uVQ/wA2ojhFTm2sUTfBYmIN0wdUn/63efTJ y1jqz2X2aj7zwAX6Cx4bt7S+BrSDtyzWXSYXimZwxAkD4WVI/qkpwdaC4SyVbK1Koi0n Kgjv8opDtb7OAqsSqdQPWJUcr5heYUWxlPmrZ9Lblpmh50msQBXBMZo5Ilir0Uj1xAgZ ZnFJjJkzk2QXpfrwASQi70K++JkMLydKFSNf5rCCldyxIze38VkoPmucU8iEYhwDGafi sEUTqz/f9+J2SyvRzHca3h3JCSoViwCSetMAFYQdYvHXJChtOD28RrpH6XGwvcA4kqCc c0pA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=k8tqbHLdBSZ3p68T8t3IP3XZ3ntWWpTLKxx6X1fIzvo=; b=K8hPS6LQJXI62HpaXy7Y8oQyWsrlzgNPYWwyAV1lO0jr6vHQpuzxhAreuGHZHJgUr+ JW/hFXd5Ngtf3Jeiu7mWGXf6uQULCoakBGiOG5oWfTL+v8a8MrtKYTXriGsn2WmHMHnr SFxE0WPBjIlzqPOquFx2LMTyvtLfibJudCgXSH0uhWnMIlcQ9fK8DjATtp/O24M/U6t1 8GstfFariVGj1GtagNv4ZrVT+6U/gaDCaa8Zq2O6I5lsidz51V0jN7jSJjb19HSf+3K3 khroIIac0v7hcizXvkm62mts59cH8T8qs7kPFpLpaaIui1tfZWkWxcnDurD9ziRSKhVW 3mrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=cAKRSWtq; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Return-Path: Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w13-20020a05640234cd00b0048ddbdac278si18260234edc.32.2023.01.11.12.47.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jan 2023 12:47:06 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=cAKRSWtq; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B3360384F037 for ; Wed, 11 Jan 2023 20:47:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B3360384F037 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673470024; bh=k8tqbHLdBSZ3p68T8t3IP3XZ3ntWWpTLKxx6X1fIzvo=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=cAKRSWtqxXElLZIdxR09/j6YFd7EKpv0sJlH3BjW3I5+eswAAmtP6XNFPr2acKBLY 8lzftCThZxZdXoE5Ncgy3jw/Iwl1LqBklkW/Bvc4zM2Twn3a5VrvF4/OuOSeABAhkv 3qClDk1dnfpJSqueUBvOZ7AEPezQkpRCSb1o8YFo= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x36.google.com (mail-oa1-x36.google.com [IPv6:2001:4860:4864:20::36]) by sourceware.org (Postfix) with ESMTPS id 387C6385B530 for ; Wed, 11 Jan 2023 20:46:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 387C6385B530 Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-1322d768ba7so16822121fac.5 for ; Wed, 11 Jan 2023 12:46:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=k8tqbHLdBSZ3p68T8t3IP3XZ3ntWWpTLKxx6X1fIzvo=; b=DEuGg9QcOeWS2VvGhgmi4FzxSor2I9P3We4Sfvu9a17g9V9KOr88wvAYoGFNaovZSl 3mD2m5BFA13AsorVu4H2ZF3sqmGOLHgvIY7bXTDf2FcgnCmvN9msHml1EN4mqiFPj2yI x2GpPseVnrcKFdHKgtVwBx5Tj1Q3lEHMTcAMGJCICg1GKWJhd35arRaoTl3YiFFmBCSl lhxGt9u/e3kjZcTZw7ARa83TZgmCkh+06GaPtI7Bf+CINC3hUAkFBWLPw7fJN+S4u7U1 TfEwfvQhKKnciEg3koOj/INSOlyIwMqIIkXDU4yNDXfm3eDMrlQ9Fvs10U+4wGFCjoIt ZDeQ== X-Gm-Message-State: AFqh2kp1mveUFaamvHnTKqWsVyPwdTTC2RPFxmFuBG/fmRnc2UyWnPUE PRAFsGHX68opYHZ8j1GVHc6Y8CXZ5do+FrCrFk8= X-Received: by 2002:a05:6870:ac21:b0:142:1837:9008 with SMTP id kw33-20020a056870ac2100b0014218379008mr43600185oab.39.1673469974828; Wed, 11 Jan 2023 12:46:14 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:a504:f3f6:dd7b:801]) by smtp.gmail.com with ESMTPSA id kw18-20020a056870ac1200b0014c8b5d54b2sm7990274oab.20.2023.01.11.12.46.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jan 2023 12:46:13 -0800 (PST) To: libc-alpha@sourceware.org, Noah Goldstein , Richard Henderson Cc: Adhemerval Zanella Netto Subject: [PATCH v7 05/17] string: Improve generic strlen Date: Wed, 11 Jan 2023 17:45:46 -0300 Message-Id: <20230111204558.2402155-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230111204558.2402155-1-adhemerval.zanella@linaro.org> References: <20230111204558.2402155-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org Sender: "Libc-alpha" From: Adhemerval Zanella Netto New algorithm have the following key differences: - Reads first word unaligned and use string-maskoff functions to remove unwanted data. This strategy follow arch-specific optimization used on powerpc, sparc, and SH. - Use of has_zero and index_first_zero parametrized functions. Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu, and powercp64-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/strlen.c | 90 +++++++++-------------------------------- sysdeps/s390/strlen-c.c | 10 +++-- 2 files changed, 26 insertions(+), 74 deletions(-) diff --git a/string/strlen.c b/string/strlen.c index ee1aae0fff..a69f3343ef 100644 --- a/string/strlen.c +++ b/string/strlen.c @@ -17,84 +17,34 @@ #include #include - -#undef strlen - -#ifndef STRLEN -# define STRLEN strlen +#include +#include +#include +#include +#include + +#ifdef STRLEN +# define __strlen STRLEN #endif /* Return the length of the null-terminated string STR. Scan for the null terminator quickly by testing four bytes at a time. */ size_t -STRLEN (const char *str) +__strlen (const char *str) { - const char *char_ptr; - const unsigned long int *longword_ptr; - unsigned long int longword, himagic, lomagic; - - /* Handle the first few characters by reading one character at a time. - Do this until CHAR_PTR is aligned on a longword boundary. */ - for (char_ptr = str; ((unsigned long int) char_ptr - & (sizeof (longword) - 1)) != 0; - ++char_ptr) - if (*char_ptr == '\0') - return char_ptr - str; - - /* All these elucidatory comments refer to 4-byte longwords, - but the theory applies equally well to 8-byte longwords. */ + /* Align pointer to sizeof op_t. */ + const uintptr_t s_int = (uintptr_t) str; + const op_t *word_ptr = word_containing (str); - longword_ptr = (unsigned long int *) char_ptr; + /* Read and MASK the first word. */ + op_t word = *word_ptr | create_mask (s_int); - /* Computing (longword - lomagic) sets the high bit of any corresponding - byte that is either zero or greater than 0x80. The latter case can be - filtered out by computing (~longword & himagic). The final result - will always be non-zero if one of the bytes of longword is zero. */ - himagic = 0x80808080L; - lomagic = 0x01010101L; - if (sizeof (longword) > 4) - { - /* 64-bit version of the magic. */ - /* Do the shift in two steps to avoid a warning if long has 32 bits. */ - himagic = ((himagic << 16) << 16) | himagic; - lomagic = ((lomagic << 16) << 16) | lomagic; - } - if (sizeof (longword) > 8) - abort (); + while (! has_zero (word)) + word = *++word_ptr; - /* Instead of the traditional loop which tests each character, - 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 zero. */ - for (;;) - { - longword = *longword_ptr++; - - if (((longword - lomagic) & ~longword & himagic) != 0) - { - /* Which of the bytes was the zero? */ - - const char *cp = (const char *) (longword_ptr - 1); - - if (cp[0] == 0) - return cp - str; - if (cp[1] == 0) - return cp - str + 1; - if (cp[2] == 0) - return cp - str + 2; - if (cp[3] == 0) - return cp - str + 3; - if (sizeof (longword) > 4) - { - if (cp[4] == 0) - return cp - str + 4; - if (cp[5] == 0) - return cp - str + 5; - if (cp[6] == 0) - return cp - str + 6; - if (cp[7] == 0) - return cp - str + 7; - } - } - } + return ((const char *) word_ptr) + index_first_zero (word) - str; } +#ifndef STRLEN +weak_alias (__strlen, strlen) libc_hidden_builtin_def (strlen) +#endif diff --git a/sysdeps/s390/strlen-c.c b/sysdeps/s390/strlen-c.c index b829ef2452..0a33a6f8e5 100644 --- a/sysdeps/s390/strlen-c.c +++ b/sysdeps/s390/strlen-c.c @@ -21,12 +21,14 @@ #if HAVE_STRLEN_C # if HAVE_STRLEN_IFUNC # define STRLEN STRLEN_C +# endif + +# include + +# if HAVE_STRLEN_IFUNC # if defined SHARED && IS_IN (libc) -# undef libc_hidden_builtin_def -# define libc_hidden_builtin_def(name) \ - __hidden_ver1 (__strlen_c, __GI_strlen, __strlen_c); +__hidden_ver1 (__strlen_c, __GI_strlen, __strlen_c); # endif # endif -# include #endif