From patchwork Sat Apr 15 11:23:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 67779 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1BD223856DE7 for ; Sat, 15 Apr 2023 11:25:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BD223856DE7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681557915; bh=yw7BZMhKhW7NZ+3bKPE0YKL64b9ymn6DQttBCzVojKs=; 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=AT+7KDxkLAJ6p2B5VDTHt6KUEfjHV75WjcK7FQ9qTOstDNUSKPr1oTl+h6L4OVBjB xWvdUmChLUve5ZlXnBRZ5sPPic0oL+UH2u55PlbzA2HRzJdGr9Q4QuxVed3rzncTWR br8dFOt5mr7+9JfyBV0JxmN1zaNxQ0Bx8t/cB03Q= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from xry111.site (xry111.site [IPv6:2001:470:683e::1]) by sourceware.org (Postfix) with ESMTPS id EE8C23857024 for ; Sat, 15 Apr 2023 11:24:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE8C23857024 Received: from stargazer.. (unknown [113.140.11.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id D4E9666120; Sat, 15 Apr 2023 07:23:59 -0400 (EDT) To: libc-alpha@sourceware.org Cc: caiyinyu , Wang Xuerui , Adhemerval Zanella Netto , Xi Ruoyao Subject: [PATCH 4/5] LoongArch: Multiarch stpcpy for unaligned access Date: Sat, 15 Apr 2023 19:23:39 +0800 Message-Id: <20230415112340.38431-5-xry111@xry111.site> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230415112340.38431-1-xry111@xry111.site> References: <20230415112340.38431-1-xry111@xry111.site> MIME-Version: 1.0 X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, LIKELY_SPAM_FROM, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_PASS, 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: Xi Ruoyao via Libc-alpha From: Xi Ruoyao Reply-To: Xi Ruoyao Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" When the CPU supports unaligned access, we can align the src pointer first (to avoid a segmentation fault if a small source string is located at the end of a page), then just copy the string pretending both src and dest are aligned. --- sysdeps/loongarch/multiarch/Makefile | 5 +++ sysdeps/loongarch/multiarch/stpcpy-generic.c | 25 ++++++++++++ sysdeps/loongarch/multiarch/stpcpy-ual.c | 43 ++++++++++++++++++++ sysdeps/loongarch/multiarch/stpcpy.c | 37 +++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 sysdeps/loongarch/multiarch/Makefile create mode 100644 sysdeps/loongarch/multiarch/stpcpy-generic.c create mode 100644 sysdeps/loongarch/multiarch/stpcpy-ual.c create mode 100644 sysdeps/loongarch/multiarch/stpcpy.c diff --git a/sysdeps/loongarch/multiarch/Makefile b/sysdeps/loongarch/multiarch/Makefile new file mode 100644 index 0000000000..958752bcbd --- /dev/null +++ b/sysdeps/loongarch/multiarch/Makefile @@ -0,0 +1,5 @@ +ifeq ($(subdir),string) +sysdep_routines += stpcpy-generic stpcpy-ual + +CFLAGS-stpcpy-ual.c += -mno-strict-align +endif diff --git a/sysdeps/loongarch/multiarch/stpcpy-generic.c b/sysdeps/loongarch/multiarch/stpcpy-generic.c new file mode 100644 index 0000000000..487388372f --- /dev/null +++ b/sysdeps/loongarch/multiarch/stpcpy-generic.c @@ -0,0 +1,25 @@ +/* Multiarch stpcpy for LoongArch. Generic version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +extern __typeof (stpcpy) __stpcpy_generic attribute_hidden; + +#define STPCPY __stpcpy_generic + +#include diff --git a/sysdeps/loongarch/multiarch/stpcpy-ual.c b/sysdeps/loongarch/multiarch/stpcpy-ual.c new file mode 100644 index 0000000000..9cd13e7fef --- /dev/null +++ b/sysdeps/loongarch/multiarch/stpcpy-ual.c @@ -0,0 +1,43 @@ +/* Multiarch stpcpy for LoongArch. Unaligned access version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +static __typeof (stpcpy) __stpcpy_unused __attribute__ ((unused)); +extern __typeof (stpcpy) __stpcpy_ual attribute_hidden; + +#define STPCPY __stpcpy_unused + +#include + +char * +__stpcpy_ual (char *dest, const char *src) +{ + /* Copy just a few bytes to make SRC aligned. It's needed not to trigger + a segmentation fault when a short string is at the end of a segment. */ + size_t len = (-(uintptr_t) src) % OPSIZ; + for (; len != 0; len--, ++dest) + { + char c = *src++; + *dest = c; + if (c == '\0') + return dest; + } + + return stpcpy_aligned_loop ((op_t *) dest, (const op_t *) src); +} diff --git a/sysdeps/loongarch/multiarch/stpcpy.c b/sysdeps/loongarch/multiarch/stpcpy.c new file mode 100644 index 0000000000..58bfb1c89d --- /dev/null +++ b/sysdeps/loongarch/multiarch/stpcpy.c @@ -0,0 +1,37 @@ +/* Multiple versions of stpcpy. LoongArch version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#if defined SHARED && IS_IN (libc) +# define __NO_STRING_INLINES +# define NO_MEMPCPY_STPCPY_REDIRECT +# include + +extern __typeof (__stpcpy) __stpcpy_generic attribute_hidden; +extern __typeof (__stpcpy) __stpcpy_ual attribute_hidden; + +# include +# define INIT_ARCH() + +libc_ifunc_hidden (__stpcpy, __stpcpy, + LOONGARCH_HAVE_UAL ? __stpcpy_ual : __stpcpy_generic); +weak_alias (__stpcpy, stpcpy) +libc_hidden_def (__stpcpy) +libc_hidden_def (stpcpy) +#else +# include +#endif