From patchwork Wed Mar 13 14:03:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 160226 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp14356546jad; Wed, 13 Mar 2019 07:03:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqysTBRfHMWmkCPb4nSyTkvJIrhaNqy8CN1axBCwJh6yp9BN1CjT5yeFPSGRguwBP0MINXHi X-Received: by 2002:a17:902:8a8b:: with SMTP id p11mr9764345plo.162.1552485827242; Wed, 13 Mar 2019 07:03:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552485827; cv=none; d=google.com; s=arc-20160816; b=aLIfTmRMIL1Cozwz8IqUnVU4p/Z2yh/VeO2IYdgPB6c2sEp2pJsm96Xvez8+6HZFnz rRIkZ2Dk8w+dj4qayfiU+8LNsoMp5xfqPT+ibrBaEyoT94oW6ez01aRfSb2LADjmSzUs P8cuhidD4TIRb2jyPIRqp7W04gisZBUlTpRzlEF3zEw3HT32bDzOnWlwe90WfNhqyPjg zmHDr60fx9JmbJGijXMk2YSLtZVvo43OSNiLJUjW6FA/5Qj0tmgFqgh7CgagGvDyFQTO ezt9YSCoBAzZExJcwrC7WyPO+v99qJCp5MRg4AUXRxZg2LOqB+d27yMRe6evhUq7QPYA qvJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature; bh=4HGcriFPU19zsB9uj/BGBhFP6+SX/hPLwXKmIgtVk9M=; b=ITVN8MotFDeXJ6b+CkVo/K1iw3tk734DI5od203eg2HGufqsePtChXWGkxfbY8OuWf iKy/mIY+i9Jg8E3x4RJiCaw1ezMJUmi+rfFFotQZK9Ftp3VkKyj7CFXEpvb5HlB3zTIu FZ0bcoqPBB6Wv9jsNvjwmUzKddG+sgkDFA8X4IpBod5xF2daZuthMRa025qNYBbykM1/ lWJklVxEQNAP7ISbV6q14Emt/qqg13ULwYowSEkQXYXCNStaB88KdXjhdx0PwpE2drL/ O6lXME6XXBFb44Q7o9PNJX4Nt1/qSvREh3+DfvPjAi4j0eP+H0rXmOCFfdfTDXueYAx0 +RZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=r9P9MmSP; dkim=pass header.i=@linaro.org header.s=google header.b=WKh0mVqj; spf=pass (google.com: domain of libc-alpha-return-100599-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-100599-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 186si11029800pfe.262.2019.03.13.07.03.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Mar 2019 07:03:47 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-100599-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=r9P9MmSP; dkim=pass header.i=@linaro.org header.s=google header.b=WKh0mVqj; spf=pass (google.com: domain of libc-alpha-return-100599-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="libc-alpha-return-100599-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; q=dns; s= default; b=WNjmBa7tHJEpS0Pwct/k7RtXTHdtIWJSXKygZNc7N3Oa4e5Q9wkkK xA2V76Op4TMQvZDMX0VZY2NbDsJgH8m+Xl7Zx91jw596PCj26AYMTSslChq4Sq3Q IHy0dnv2zdnfS39gKCZ7mYWKC31su4gZgPol8YrIWj1V1kD2pt20wg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; s=default; bh=KyMmJZysZUSi+34sVLTUmJh5Lns=; b=r9P9MmSPx9697Nv0aaIFmUSMvmfV 7DLP8XjP6s23uiAJJpX/yh0KOmI7YVqhmBwALjB0BEmJ6oo46BBg2rBQRhv+WuNC NZicMwBy832kYSyfGxOVO8QSsIO9Xxu7HyjqMR4DJNr55j3Zxsoni5a+Tv/M2i+z AkPAR3DJ+pDqm18= Received: (qmail 51441 invoked by alias); 13 Mar 2019 14:03:32 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 51298 invoked by uid 89); 13 Mar 2019 14:03:31 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-14.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=sk:unroll_, unrolled, distributed X-HELO: mail-vs1-f65.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=4HGcriFPU19zsB9uj/BGBhFP6+SX/hPLwXKmIgtVk9M=; b=WKh0mVqjY4hMID8vPXuiwa1OMVtwHiilAImcl+yVGHQb/5lshsqR0Sl9JcB8fGYlWz /G6irZSREplND7vuM+RVgjrDC7x0n02Ut6K4X1TZk+4BV3ZXhYpCsdxwhUoecuuKCvFa 9TRbxKwPtuwjFbCABtITBeE8Gp9njl51k2JooHrJwCD7XQ15BXG6zD8r7QSrdf/pU1jj 6lOivxBWcY/H6H0kjgZCN6Yg8DjkWI/f9C99FwwW//wBRF902H4JGBzzN1viFLe3IhkT MalqtPdf/wZ+Nbkbho7hIIK7gaSx4xWjS7iQEQcahPSSlxyjYkfDSfjP1bzdIUmRE+n5 8kJQ== Return-Path: From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: "Gabriel F . T . Gomes" Subject: [PATCH v2 1/6] wcsmbs: Add wcscpy loop unroll option Date: Wed, 13 Mar 2019 11:03:12 -0300 Message-Id: <20190313140317.8894-1-adhemerval.zanella@linaro.org> This allows an architecture the use the old generic implementation and also set explicit loop unrolling. Checked on aarch64-linux-gnu. * include/loop_unroll.h: New file. * wcsmbs/wcscpy (__wcscpy): Add option to use loop unrolling besides generic implementation. --- include/loop_unroll.h | 78 +++++++++++++++++++++++++++++++++++++++++++ wcsmbs/wcscpy.c | 19 +++++++++++ 2 files changed, 97 insertions(+) create mode 100644 include/loop_unroll.h -- 2.17.1 Reviewed-by: Gabriel F. T. Gomes diff --git a/include/loop_unroll.h b/include/loop_unroll.h new file mode 100644 index 0000000000..f40d375257 --- /dev/null +++ b/include/loop_unroll.h @@ -0,0 +1,78 @@ +/* Macro for explicit loop unrolling. + Copyright (C) 2019 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 + . */ + +#ifndef _LOOP_UNROLL_H +#define _LOOP_UNROLL_H + +/* Loop unroll macro to be used for explicity force loop unrolling with a + configurable number or iterations. The idea is to make the loop unrolling + independent of whether compiler is able to unrolling through specific + optimizations options (-funroll-loops or -funroll-all-loops). + + For instance, to implement strcpy with SRC being the source input and + DEST the destination buffer, it is expected the macro to be used in this + way: + + #define ITERATION(index) \ + ({ char c = *str++; *dest++ = c; c != '\0' }) + + while (1) + UNROLL_REPEAT (4, ITERATION) + + The loop will be manually unrolled 4 times. Another option is to do + the index update after the tests: + + #define ITERATION(index) \ + ({ char c = *(str + index); *(dest + index) = c; c != '\0' }) + #define UPDATE(n) \ + str += n; dst += n + + while (1) + UNROLL_REPEAT_UPDATE (4, ITERATION, UPDATE) + + The loop will be manually unrolled 4 times and the SRC and DEST pointers + will be update only after last iteration. + + Currently both macros unrolls the loop 8 times at maximum. */ + +#define UNROLL_REPEAT_1(X) if (!X(0)) break; +#define UNROLL_REPEAT_2(X) UNROLL_REPEAT_1(X) if (!X(1)) break; +#define UNROLL_REPEAT_3(X) UNROLL_REPEAT_2(X) if (!X(2)) break; +#define UNROLL_REPEAT_4(X) UNROLL_REPEAT_3(X) if (!X(3)) break; +#define UNROLL_REPEAT_5(X) UNROLL_REPEAT_4(X) if (!X(4)) break; +#define UNROLL_REPEAT_6(X) UNROLL_REPEAT_5(X) if (!X(5)) break; +#define UNROLL_REPEAT_7(X) UNROLL_REPEAT_6(X) if (!X(6)) break; +#define UNROLL_REPEAT_8(X) UNROLL_REPEAT_7(X) if (!X(7)) break; + +#define UNROLL_EXPAND(...) __VA_ARGS__ + +#define UNROLL_REPEAT__(N, X) UNROLL_EXPAND(UNROLL_REPEAT_ ## N) (X) +#define UNROLL_REPEAT_(N, X) UNROLL_REPEAT__ (N, X) + +#define UNROLL_REPEAT(N, X) \ + (void) ({ \ + UNROLL_REPEAT_(UNROLL_EXPAND(N), X); \ + }) + +#define UNROLL_REPEAT_UPDATE(N, X, U) \ + (void) ({ \ + UNROLL_REPEAT_ (UNROLL_EXPAND(N), X); \ + UPDATE (N); \ + }) + +#endif diff --git a/wcsmbs/wcscpy.c b/wcsmbs/wcscpy.c index 6fb2969513..8e8719744e 100644 --- a/wcsmbs/wcscpy.c +++ b/wcsmbs/wcscpy.c @@ -17,6 +17,7 @@ . */ #include +#include #ifdef WCSCPY @@ -27,7 +28,25 @@ wchar_t * __wcscpy (wchar_t *dest, const wchar_t *src) { +#ifndef UNROLL_NTIMES return __wmemcpy (dest, src, __wcslen (src) + 1); +#else + /* Some architectures might have costly tail function call (powerpc + for instance) where wmemcpy call overhead for smalls sizes might + be costly than just unroll the main loop. */ + wchar_t *wcp = dest; + +#define ITERATION(index) \ + ({ \ + wchar_t c = *src++; \ + *wcp++ = c; \ + c != L'\0'; \ + }) + + while (1) + UNROLL_REPEAT(UNROLL_NTIMES, ITERATION); + return dest; +#endif } #ifndef WCSCPY weak_alias (__wcscpy, wcscpy)