From patchwork Tue Jan 10 21:00:54 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: 640897 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp2915825pvb; Tue, 10 Jan 2023 13:01:58 -0800 (PST) X-Google-Smtp-Source: AMrXdXtv1aTs49a5evlWkKSmWHSAn3fbcr5Jn7jg2uqgNhGv3TYOM1Zmyntbov5qpKiVFnObKFC8 X-Received: by 2002:a17:906:f84d:b0:7c1:28a7:f79b with SMTP id ks13-20020a170906f84d00b007c128a7f79bmr49916219ejb.59.1673384518264; Tue, 10 Jan 2023 13:01:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673384518; cv=none; d=google.com; s=arc-20160816; b=B33jgXYM7XF/E1Ra7Bj0b3aTZvW1fblGKN2P8+4xaelK+0zUgH6Ufliue5TO67YdbR 5DRfTnHAHK7/im35P2/Jq6eKS4TQkrPMGqgdZlV6cyLq+mHx3eRg1u9yZ2o0kOFVdDVB fsAFKXQOAzIv0cKd6RQfqT0wSRa4SDrbU782hgg4WR4UOeIaluKSLsN47V816jI7O6yP bpzakTCx0A0RXwjRPrxIVaUDTCvkY82aXIAncnK2Bmo4c7eHJizl4xs4kPYTxCc7iS81 0TOkogp+rMuXxkOb6FkZrhKydu67qOTSiWuo5Y8Sy5ISkbdIdqRktRAuu+XhELfrMemY SfvQ== 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=vjdQWCH10pT1CpcvCWLW/vrgN0rDsn2Fj0S0lcT03VEqJxlRHgkmypyViWEvxYbQNF ykafc9cmb+XUr4kP3PCoVmmW2vd083n1hcJMzNajPzUpl1UDZhG+mey6I1ZDdpUVnKr0 na4Dq6k8KnUkJ0LiMCHLFOnlBCB9dNs7Z7ZabKq3Why/6/cgRCwN10YfayovzYmYRH3e qwPLZit15iBjJtgq5t2vV1w4TpzAV5SnAZpl/7xX4soWnrSPQCr8CpKzadPwfexBT9An LijQXgddnsJaYXAQyXnV5bNkIz8wa5R3e7z1djASwd3KB6n309NtIxY99pjN9wl/t7Nj AWLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=EBAK9uLM; 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 qa9-20020a170907868900b007c10a0c6f44si13198664ejc.623.2023.01.10.13.01.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jan 2023 13:01:58 -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=EBAK9uLM; 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 D0DBE385B507 for ; Tue, 10 Jan 2023 21:01:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D0DBE385B507 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673384516; 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=EBAK9uLMGADQCYHon0A/0Tsxu+SjoRhj3ksbBXlmYX4lao7YD4V4fUHwum0EwyGWc Ur64d1n7sd8zZ/VWl64TcU1s983osw4v1eagvOexkjh054JiYozMt6xDlGCwJEyMXS Cvelods5viQnp1q0jiZV0bVxYxLh2t7GEFRC5C7g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by sourceware.org (Postfix) with ESMTPS id 0DEB6385B515 for ; Tue, 10 Jan 2023 21:01:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0DEB6385B515 Received: by mail-oo1-xc32.google.com with SMTP id h3-20020a4aa283000000b004ead187bd6eso3541465ool.5 for ; Tue, 10 Jan 2023 13:01:27 -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=5QVYY+u/7RE7Z0poV1OGaxXYZQeOsHHkdNgGdHGeWZoSNz7tYqHP/GvQLu3lLTVrBi 1m+23vNP4Bp1qtji5ZU0zYXScqibWWarQEMr/2Hjo9EAFfsAJGPBtvbZ6LXUCZvYRPVp w6QMvI+IrJfaGwgsKVVK5F6TDLTgCjdEQ5Xn+nvJ6blh3HuEVcBWmDqO+sAlSh+BBROP jSdtYWau2nEnIxfxsozj2NDdKo5I1nyT35DNhKme40qYqN8PUFZWZ+zxzBKteaWowye7 Iwtqe4gWfdLoMaLB+OPtJiAQtWTIkZmI04s5F/YwLNn3uNsbtE0PE6f6cwgd9ehF20Ab FYAw== X-Gm-Message-State: AFqh2kpC7JDjw7hUbg4ATe1kVGqZcT9F4+FG+Nu3brfWH73Dv9XrcUFC +KitKhuMIps+ZFZiMBR/TUfjM6Xk+HBehamSuH8= X-Received: by 2002:a4a:c386:0:b0:4ae:590:8087 with SMTP id u6-20020a4ac386000000b004ae05908087mr38950484oop.0.1673384485816; Tue, 10 Jan 2023 13:01:25 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:e8a0:dd55:3328:997]) by smtp.gmail.com with ESMTPSA id r5-20020a4a83c5000000b0049ee88e86f9sm6202193oog.10.2023.01.10.13.01.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jan 2023 13:01:25 -0800 (PST) To: libc-alpha@sourceware.org, Noah Goldstein , Richard Henderson Cc: Adhemerval Zanella Netto Subject: [PATCH v6 05/17] string: Improve generic strlen Date: Tue, 10 Jan 2023 18:00:54 -0300 Message-Id: <20230110210106.1457686-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230110210106.1457686-1-adhemerval.zanella@linaro.org> References: <20230110210106.1457686-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 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