Message ID | 20231027-optimize_checksum-v8-1-feb7101d128d@rivosinc.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp925752vqb; Fri, 27 Oct 2023 15:44:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHUdpabft/S9h7oczXygrz0aOvQkhl0YbW0+l3APsJ6d4soL9O0sqhklx1dqKGoLlOAwn1f X-Received: by 2002:a05:6871:a4c9:b0:1eb:4805:30d9 with SMTP id wb9-20020a056871a4c900b001eb480530d9mr4075061oab.49.1698446666314; Fri, 27 Oct 2023 15:44:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698446666; cv=none; d=google.com; s=arc-20160816; b=XZLcas3tZpDK9tUwVFrPL+aWYB9LOGdGK+ltuM6Kas1bkUqYE4frIvJ/TgGulEQ/bd iHIhlWJy+OvQfVNF18e8mTph8ahoxlU50Lj+0ZUpUk/tcke0pqHqvJeZ7tDss/xoz8O4 tHnfXSu13UTVlmnuYI4A21CXAW7bjgX85Sc1fGjn9YdOkgvZoIFNjJynzDV47ZIKccDG fRfrU2XJHtlZYPCxBU/yV3Jkg4T5JC9tF3UAz4jqrHwZTUevTT8XmTAXeyNdfPMjsUkR YvnCOTuaeeBqNxg2biPukZ31lI13wSuYHNfK3GIiac3SW14eMLeA5P6Cw+rI8H515qU7 wirg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=55gQAkGJdgEhOiqM5Y2FlQbtEmx2dMpuljRL7f6iQqU=; fh=WrYWo2NWGfDp1Q0L0IXsGjg4kDrSbEOOvn62+ocLtXY=; b=Q+3CHxuS4qlxCCnk7PBbssVsEi6RHENVYtGRoclPebTEBbr8YgwZYK/HD+FdMZi6gw 3QH79PXVEh5UUXDN/Sd9//UQ1//zMGI4DspYiPHtPqrtn9FidhfoMDAnckmfqLGkXgKr YAMglZtQE/bbIAInL/0zsf/WEAewRFQSMzu+8JCwy1syS2a396hUuuyBGH2+3wx+rj06 NgquxDQ95eCfn5ZBov7hlp8IK3pPwInxjHtC7co7w3/6HBJpJlHE14KGzscqYlQWfWQ7 4AK2VzKgEQzmz+EF9Ssy3Pds564f8uk9Fj6EcW/Cx/jMZjJWTvMXrXluIKbdiJrL0CDK SO3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ZfYCxxiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id n70-20020a252749000000b00da04d3c074fsi3765974ybn.648.2023.10.27.15.44.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 15:44:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ZfYCxxiL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id DF18A816F04B; Fri, 27 Oct 2023 15:44:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346678AbjJ0WoF (ORCPT <rfc822;yuanzuo1009@gmail.com> + 27 others); Fri, 27 Oct 2023 18:44:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235085AbjJ0WoD (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 27 Oct 2023 18:44:03 -0400 Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 720C3E1 for <linux-kernel@vger.kernel.org>; Fri, 27 Oct 2023 15:44:01 -0700 (PDT) Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-1e5bc692721so1530281fac.0 for <linux-kernel@vger.kernel.org>; Fri, 27 Oct 2023 15:44:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698446640; x=1699051440; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=55gQAkGJdgEhOiqM5Y2FlQbtEmx2dMpuljRL7f6iQqU=; b=ZfYCxxiLDIHJP8Ta07zA+zY53qZvOfdU5StWtIID+eenTaPlylAzMWDVxfW8xftFup sYpRa8vL9iMW1YtRqHJc8WAr8et10TJ7tHrs2OzIEGkR3tnUUiGc3a2wdgm1qzO+BZXP Px8IifzLE1ODU8uSeHsbTnunBbMGfDgDRzGdTkIIcovQEfUCkFiUP6ldoWze+P1mqdZu 3KW61SF2vR74FHVYawdQJchJqM86XSOq6wauEMvohCJz1Izza9KeavZVgvkRWzpCaGTS csAM9Y3x6Fwli3vHvUdIQCSlxcj8b4d6FNuhowdTisfFaNhFc4kPGikZLjxXFJhF/LAa altA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698446640; x=1699051440; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=55gQAkGJdgEhOiqM5Y2FlQbtEmx2dMpuljRL7f6iQqU=; b=qjyH7R8cIDcp6Bu0YGrR4lVJRoM3EK6d/qMTsIMxfZVFt48NAkyanaqkbgm5ydPRCN hg2v/E0ClgkV2uUI5pIn3eJ2vyX5Rz/0gQScVVvBFA2+HzoRRgV+vNeLhEWzYp5FJLxf S+E9/jJTZyxEsMr9An6IJM7SuMgADzGMFiqFNGafSrvoFQnns6xDvjXjWbnqXcDsvVR6 rq4H+rqVj6v2MSitvM4BEt8oJwox0rExJLizAerAQP5axzCGSlXz7sRTqGvOkbMCOGV2 C/9hy4AIbWRI2D0/sxqZUBSm/eFYiMc50t0M+WV5Rw6/JoomkQh594wiAJRpjw5LQo39 JppA== X-Gm-Message-State: AOJu0YygId1aAGbHX8VQQkZzC/V680vSO+pszOOgfrODzQ0o335Opi8m 7wMVii3hO1OzLa0fmWBy1ZfBCkQJgTTsKrGKdOU= X-Received: by 2002:a05:6870:ad0c:b0:1e9:c252:4853 with SMTP id nt12-20020a056870ad0c00b001e9c2524853mr5049857oab.5.1698446640404; Fri, 27 Oct 2023 15:44:00 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t15-20020a9d748f000000b006c61c098d38sm448564otk.21.2023.10.27.15.43.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 15:43:59 -0700 (PDT) From: Charlie Jenkins <charlie@rivosinc.com> Date: Fri, 27 Oct 2023 15:43:51 -0700 Subject: [PATCH v8 1/5] asm-generic: Improve csum_fold MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231027-optimize_checksum-v8-1-feb7101d128d@rivosinc.com> References: <20231027-optimize_checksum-v8-0-feb7101d128d@rivosinc.com> In-Reply-To: <20231027-optimize_checksum-v8-0-feb7101d128d@rivosinc.com> To: Charlie Jenkins <charlie@rivosinc.com>, Palmer Dabbelt <palmer@dabbelt.com>, Conor Dooley <conor@kernel.org>, Samuel Holland <samuel.holland@sifive.com>, David Laight <David.Laight@aculab.com>, Xiao Wang <xiao.w.wang@intel.com>, Evan Green <evan@rivosinc.com>, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Paul Walmsley <paul.walmsley@sifive.com>, Albert Ou <aou@eecs.berkeley.edu>, Arnd Bergmann <arnd@arndb.de>, David Laight <david.laight@aculab.com> X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 27 Oct 2023 15:44:23 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780950411809296539 X-GMAIL-MSGID: 1780950411809296539 |
Series | riscv: Add fine-tuned checksum functions | |
Commit Message
Charlie Jenkins
Oct. 27, 2023, 10:43 p.m. UTC
This csum_fold implementation introduced into arch/arc by Vineet Gupta is better than the default implementation on at least arc, x86, and riscv. Using GCC trunk and compiling non-inlined version, this implementation has 41.6667%, 25% fewer instructions on riscv64, x86-64 respectively with -O3 optimization. Most implmentations override this default in asm, but this should be more performant than all of those other implementations except for arm which has barrel shifting and sparc32 which has a carry flag. Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Reviewed-by: David Laight <david.laight@aculab.com> --- include/asm-generic/checksum.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Comments
On Fri, Oct 27, 2023 at 03:43:51PM -0700, Charlie Jenkins wrote: > /* > * computes the checksum of a memory block at buff, length len, > * and adds in "sum" (32-bit) > @@ -31,9 +33,7 @@ extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl); > static inline __sum16 csum_fold(__wsum csum) > { > u32 sum = (__force u32)csum; > - sum = (sum & 0xffff) + (sum >> 16); > - sum = (sum & 0xffff) + (sum >> 16); > - return (__force __sum16)~sum; > + return (__force __sum16)((~sum - ror32(sum, 16)) >> 16); > } Will (~(sum + ror32(sum, 16))>>16 produce worse code than that? Because at least with recent gcc this will generate the exact thing you get from arm inline asm...
On Sat, Oct 28, 2023 at 12:10:36AM +0100, Al Viro wrote: > On Fri, Oct 27, 2023 at 03:43:51PM -0700, Charlie Jenkins wrote: > > /* > > * computes the checksum of a memory block at buff, length len, > > * and adds in "sum" (32-bit) > > @@ -31,9 +33,7 @@ extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl); > > static inline __sum16 csum_fold(__wsum csum) > > { > > u32 sum = (__force u32)csum; > > - sum = (sum & 0xffff) + (sum >> 16); > > - sum = (sum & 0xffff) + (sum >> 16); > > - return (__force __sum16)~sum; > > + return (__force __sum16)((~sum - ror32(sum, 16)) >> 16); > > } > > Will (~(sum + ror32(sum, 16))>>16 produce worse code than that? > Because at least with recent gcc this will generate the exact thing > you get from arm inline asm... Yes that will produce worse code because an out-of-order processor will be able to leverage that ~sum and ror32(sum, 16) can be computed independently of each other. There are more strict data dependencies in (~(sum + ror32(sum, 16))>>16. - Charlie
diff --git a/include/asm-generic/checksum.h b/include/asm-generic/checksum.h index 43e18db89c14..ad928cce268b 100644 --- a/include/asm-generic/checksum.h +++ b/include/asm-generic/checksum.h @@ -2,6 +2,8 @@ #ifndef __ASM_GENERIC_CHECKSUM_H #define __ASM_GENERIC_CHECKSUM_H +#include <linux/bitops.h> + /* * computes the checksum of a memory block at buff, length len, * and adds in "sum" (32-bit) @@ -31,9 +33,7 @@ extern __sum16 ip_fast_csum(const void *iph, unsigned int ihl); static inline __sum16 csum_fold(__wsum csum) { u32 sum = (__force u32)csum; - sum = (sum & 0xffff) + (sum >> 16); - sum = (sum & 0xffff) + (sum >> 16); - return (__force __sum16)~sum; + return (__force __sum16)((~sum - ror32(sum, 16)) >> 16); } #endif