From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id E07CA3856975 for ; Thu, 2 Feb 2023 18:12:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E07CA3856975 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-ot1-x336.google.com with SMTP id p24-20020a056830131800b0068d4b30536aso688454otq.9 for ; Thu, 02 Feb 2023 10:12:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=5avUm1yomjyzJDaEa3GoJBqoNeKtiA3G3u3KIcclhSM=; b=uf6IfMQfsoABm/jGZiSB0d6xLJ0OB19J2HwBy9czQonAHf3oeymsXObmbIRbku7h7C VeesT0/j2JRXehbxJy0XBtZyAcQtYno+twws1YrS+2KQOPPi4iF1YjyKvT6jaf7E9aoy voR2jTvZgBmS0lxzjmG9tpTzY+1jcIAzIdjD1Go8wFDfRyJbQ6gH+YVKvovf8bOImcKP esIH3ydavk6A14qpn7QCkkU1EpyhmD/4Wj5aBcSl2Gcdqlq4lJBvjKed/ff9aCXQsVsr zmT0dxowp9P/HPLnQgI49JV+pPLACvPqULtL1+a46yZlRKSi+5VjULVb+L+pxZ22nLbi tDaw== 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:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5avUm1yomjyzJDaEa3GoJBqoNeKtiA3G3u3KIcclhSM=; b=fBqLNNACVczUQ6dvFRZCSdh7rKw7Sv0nyrTPowaIKpikT/OL+2m2aD69+wlrSXng/b nS33K6WU9UEBwvWLpja/D48Vf1bW03vLKSHNmAhPBO6kmN+HtUIFv+KSIYvVqjC6jJnw fd0a5JJXQXoSuJ11+/NHpFj6Lro7dKnwpNOmmXFCiIpQp31z5UNxK6IBU3edhL1J7RJY M4uSyigMxjcGTrZavoXrtBvPgOfQOcDwktR2NaegX4yue3TQoPbLqtO2humf2GS4afJ3 /5Gop0eN/L4AYKCj5aVTKj0llxKJQgbRJz63MEO/ukV0U2ipxus5lRPCzyET3/A7y88g D0pw== X-Gm-Message-State: AO0yUKXGIMNcY+ViI9KDXywR7JxJuEziA1YAnQwLk87SEodixbItD7Jx OSSaiFAbllt9FzrZc+oWmwmJ+OK0QNoARqK8Qeg= X-Google-Smtp-Source: AK7set9ZFw+U7UtK0XSLbJRPkQ4NbkooJOR6t8I9jJ3kp5UygiU0rLU6Y3E8k+YuEclbZzUpeifesQ== X-Received: by 2002:a05:6830:3b86:b0:68b:e3cf:2967 with SMTP id dm6-20020a0568303b8600b0068be3cf2967mr3308655otb.29.1675361538701; Thu, 02 Feb 2023 10:12:18 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c2:1887:da12:b9d3:2162:a28c]) by smtp.gmail.com with ESMTPSA id ci10-20020a05683063ca00b00684a10970adsm126689otb.16.2023.02.02.10.12.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 10:12:17 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Richard Henderson , Jeff Law , Xi Ruoyao , Noah Goldstein Subject: [PATCH v12 09/31] string: Improve generic stpcpy Date: Thu, 2 Feb 2023 15:11:27 -0300 Message-Id: <20230202181149.2181553-10-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230202181149.2181553-1-adhemerval.zanella@linaro.org> References: <20230202181149.2181553-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,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: It follows the strategy: - Align the destination on word boundary using byte operations. - If source is also word aligned, read a word per time, check for null (using has_zero from string-fzb.h), and write the remaining bytes. - If source is not word aligned, loop by aligning the source, and merging the result of two reads. Similar to aligned case, check for null with has_zero, and write the remaining bytes if null is found. Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu, and powerpc-linux-gnu by removing the arch-specific assembly implementation and disabling multi-arch (it covers both LE and BE for 64 and 32 bits). Reviewed-by: Richard Henderson --- string/stpcpy.c | 92 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 6 deletions(-) diff --git a/string/stpcpy.c b/string/stpcpy.c index 8df5065cfe..dd0fef12ef 100644 --- a/string/stpcpy.c +++ b/string/stpcpy.c @@ -15,12 +15,12 @@ License along with the GNU C Library; if not, see . */ -#ifdef HAVE_CONFIG_H -# include -#endif - #define NO_MEMPCPY_STPCPY_REDIRECT #include +#include +#include +#include +#include #undef __stpcpy #undef stpcpy @@ -29,12 +29,92 @@ # define STPCPY __stpcpy #endif +static __always_inline char * +write_byte_from_word (op_t *dest, op_t word) +{ + char *d = (char *) dest; + for (size_t i = 0; i < OPSIZ; i++, ++d) + { + char c = extractbyte (word, i); + *d = c; + if (c == '\0') + break; + } + return d; +} + +static __always_inline char * +stpcpy_aligned_loop (op_t *restrict dst, const op_t *restrict src) +{ + op_t word; + while (1) + { + word = *src++; + if (has_zero (word)) + break; + *dst++ = word; + } + + return write_byte_from_word (dst, word); +} + +static __always_inline char * +stpcpy_unaligned_loop (op_t *restrict dst, const op_t *restrict src, + uintptr_t ofs) +{ + op_t w2a = *src++; + uintptr_t sh_1 = ofs * CHAR_BIT; + uintptr_t sh_2 = OPSIZ * CHAR_BIT - sh_1; + + op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2); + if (!has_zero (w2)) + { + op_t w2b; + + /* Unaligned loop. The invariant is that W2B, which is "ahead" of W1, + does not contain end-of-string. Therefore it is safe (and necessary) + to read another word from each while we do not have a difference. */ + while (1) + { + w2b = *src++; + w2 = MERGE (w2a, sh_1, w2b, sh_2); + /* Check if there is zero on w2a. */ + if (has_zero (w2)) + goto out; + *dst++ = w2; + if (has_zero (w2b)) + break; + w2a = w2b; + } + + /* Align the final partial of P2. */ + w2 = MERGE (w2b, sh_1, 0, sh_2); + } + +out: + return write_byte_from_word (dst, w2); +} + + /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */ char * STPCPY (char *dest, const char *src) { - size_t len = strlen (src); - return memcpy (dest, src, len + 1) + len; + /* Copy just a few bytes to make DEST aligned. */ + size_t len = (-(uintptr_t) dest) % OPSIZ; + for (; len != 0; len--, ++dest) + { + char c = *src++; + *dest = c; + if (c == '\0') + return dest; + } + + /* DEST is now aligned to op_t, SRC may or may not be. */ + uintptr_t ofs = (uintptr_t) src % OPSIZ; + return ofs == 0 ? stpcpy_aligned_loop ((op_t*) dest, (const op_t *) src) + : stpcpy_unaligned_loop ((op_t*) dest, + (const op_t *) (src - ofs) , ofs); } weak_alias (__stpcpy, stpcpy) libc_hidden_def (__stpcpy) -- 2.34.1