From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 96EA920248 for ; Wed, 13 Mar 2019 14:03:40 +0000 (UTC) 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=m9MyNq+OGQjNjnzUTL0cQg2C9pA6G10QKjcTgvVXAwluwrXC/tZK9 trSHdNOwZxnpKDG6AaXoRxkC+ZuQWZGU/ZjVkj09EXlW3H5isPjljZgf7JTyZpqf 85b0JEpEB6jcCUtUoPxeQiIIp0R+TsrZip/uFRejHschUNs2gF3GNM= 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=fPb+FDSoHDBRLhezI6Kx/edtfvnK FL4rqK5flksXRVDJylhT3xwWohmLDoOqKO/CYh6PVO7mM9DJjCUNGdui/4d40VLM j6/nWyRrb8E2y0e2hcPHnKoO3NhVYfd+XGF5FX0IB55uO4iG32ewNsHk48dmgEa9 v6reCv0zdHkqZWo= 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 Received: (qmail 51298 invoked by uid 89); 13 Mar 2019 14:03:31 -0000 Authentication-Results: sourceware.org; auth=none 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== 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 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) -- 2.17.1