Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3120230imm; Thu, 17 May 2018 03:50:35 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrMDZpRejzLMH/QMFswcoMa2yonYYXc/eKWPEEN3JpfgQG/KWfs6DilyvQj/wAsJU82BYJt X-Received: by 2002:a63:8ac4:: with SMTP id y187-v6mr3714256pgd.64.1526554235501; Thu, 17 May 2018 03:50:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526554235; cv=none; d=google.com; s=arc-20160816; b=OIUGOf8NnX6J6LoeYnkLY+w0Vbamr6BU4svHwDN78ROyAYXsaFXsrf6eLwcOwesOPt lChHj6I9yAT5lb2cBXNflzn42tZbn/pNBKZd3/HkuZhR97B0+t0bhig+8WbitPVrUVB4 lQtMR2gzRvIPN0NXFmHINYwvQgsmWn/hdrRu62l3hjE5NMJKAoJc3053ciPfOqb+b027 i2Zauk5+IKgcL5eXCBjV0s4e7XIMYytXJJHs9F1FrYRmc0Oy5Gr7AQdiF1+DxAleBnOA ORWHEDzbC+djHc0uUXAd2KcvNPqAkGRitSutyZKEjN2hrO5wRdIOweE9BTsdygvphoyg 3TEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:cc:to:subject:from:references :in-reply-to:message-id:arc-authentication-results; bh=U86GzXaukqmMdKGhQ2qrQcz2t/1OyENa9k1/i8ZO1+0=; b=csMF/xLq6/qTij9DO/Z4oVTARKon7B44zGgytgUPiaVSLuej82y7ycw46ffTCAdMl+ paHlOXTP2Mn5979YZcMRu3zV3u5Q2/in6Rx/L7pfRQ2W61IzUZXZRAlb1hDwpb3zn+a4 GbjLbtz5Vd61JcSOCmeDT5tcU6V8S6KLj4aIITUYJv3BGKVoU8Nwwkaozhe4ccixPqs7 lYqnH/QupxGdY4AsFH1ozIJfW4WjHfew6R4+oasAikkeAdLvue4LaVT0ObL3mBp8nAo4 0sJT1FGBxegZC4/JyfJsGifnmXiTiu8aK/9h2Fh7lS5liY8gKSaezAsrXTLzhkRWCaUp 9pGQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l187-v6si3607836pgl.485.2018.05.17.03.50.21; Thu, 17 May 2018 03:50:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752081AbeEQKuD (ORCPT + 99 others); Thu, 17 May 2018 06:50:03 -0400 Received: from pegase1.c-s.fr ([93.17.236.30]:6258 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752060AbeEQKuA (ORCPT ); Thu, 17 May 2018 06:50:00 -0400 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 40mp3T6fb7z9v0kC; Thu, 17 May 2018 12:49:57 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id efXsZCLO64KV; Thu, 17 May 2018 12:49:57 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 40mp3T68y9z9v0k0; Thu, 17 May 2018 12:49:57 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 467E88B987; Thu, 17 May 2018 12:49:59 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id Jr8plruDJC2j; Thu, 17 May 2018 12:49:59 +0200 (CEST) Received: from po14934vm.idsi0.si.c-s.fr (po15451.idsi0.si.c-s.fr [172.25.231.2]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 209648B97E; Thu, 17 May 2018 12:49:59 +0200 (CEST) Received: by po14934vm.idsi0.si.c-s.fr (Postfix, from userid 0) id 0A9476F937; Thu, 17 May 2018 12:49:58 +0200 (CEST) Message-Id: <8a6f90d882c8b60e5fa0826cd23dd70a92075659.1526553552.git.christophe.leroy@c-s.fr> In-Reply-To: References: From: Christophe Leroy Subject: [PATCH v2 5/5] powerpc/lib: inline memcmp() for small constant sizes To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Date: Thu, 17 May 2018 12:49:58 +0200 (CEST) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In my 8xx configuration, I get 208 calls to memcmp() Within those 208 calls, about half of them have constant sizes, 46 have a size of 8, 17 have a size of 16, only a few have a size over 16. Other fixed sizes are mostly 4, 6 and 10. This patch inlines calls to memcmp() when size is constant and lower than or equal to 16 In my 8xx configuration, this reduces the number of calls to memcmp() from 208 to 123 The following table shows the number of TB timeticks to perform a constant size memcmp() before and after the patch depending on the size Before After Improvement 01: 7577 5682 25% 02: 41668 5682 86% 03: 51137 13258 74% 04: 45455 5682 87% 05: 58713 13258 77% 06: 58712 13258 77% 07: 68183 20834 70% 08: 56819 15153 73% 09: 70077 28411 60% 10: 70077 28411 60% 11: 79546 35986 55% 12: 68182 28411 58% 13: 81440 35986 55% 14: 81440 39774 51% 15: 94697 43562 54% 16: 79546 37881 52% Signed-off-by: Christophe Leroy --- arch/powerpc/include/asm/string.h | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/arch/powerpc/include/asm/string.h b/arch/powerpc/include/asm/string.h index 35f1aaad9b50..80cf0f9605dd 100644 --- a/arch/powerpc/include/asm/string.h +++ b/arch/powerpc/include/asm/string.h @@ -4,6 +4,8 @@ #ifdef __KERNEL__ +#include + #define __HAVE_ARCH_STRNCPY #define __HAVE_ARCH_STRNCMP #define __HAVE_ARCH_MEMSET @@ -51,10 +53,54 @@ static inline int strncmp(const char *p, const char *q, __kernel_size_t size) return __strncmp(p, q, size); } +static inline int __memcmp1(const void *p, const void *q, int off) +{ + return *(u8*)(p + off) - *(u8*)(q + off); +} + +static inline int __memcmp2(const void *p, const void *q, int off) +{ + return be16_to_cpu(*(u16*)(p + off)) - be16_to_cpu(*(u16*)(q + off)); +} + +static inline int __memcmp4(const void *p, const void *q, int off) +{ + return be32_to_cpu(*(u32*)(p + off)) - be32_to_cpu(*(u32*)(q + off)); +} + +static inline int __memcmp8(const void *p, const void *q, int off) +{ + s64 tmp = be64_to_cpu(*(u64*)(p + off)) - be64_to_cpu(*(u64*)(q + off)); + return tmp >> 32 ? : (int)tmp; +} + +static inline int __memcmp_cst(const void *p,const void *q,__kernel_size_t size) +{ + if (size == 1) + return __memcmp1(p, q, 0); + if (size == 2) + return __memcmp2(p, q, 0); + if (size == 3) + return __memcmp2(p, q, 0) ? : __memcmp1(p, q, 2); + if (size == 4) + return __memcmp4(p, q, 0); + if (size == 5) + return __memcmp4(p, q, 0) ? : __memcmp1(p, q, 4); + if (size == 6) + return __memcmp4(p, q, 0) ? : __memcmp2(p, q, 4); + if (size == 7) + return __memcmp4(p, q, 0) ? : __memcmp2(p, q, 4) ? : __memcmp1(p, q, 6); + return __memcmp8(p, q, 0); +} + static inline int memcmp(const void *p,const void *q,__kernel_size_t size) { if (unlikely(!size)) return 0; + if (__builtin_constant_p(size) && size <= 8) + return __memcmp_cst(p, q, size); + if (__builtin_constant_p(size) && size <= 16) + return __memcmp8(p, q, 0) ? : __memcmp_cst(p + 8, q + 8, size - 8); return __memcmp(p, q, size); } -- 2.13.3