From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id DD5ECA0C45; Mon, 18 Oct 2021 09:58:39 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 821A240141; Mon, 18 Oct 2021 09:58:38 +0200 (CEST) Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by mails.dpdk.org (Postfix) with ESMTP id AFB334003C for ; Mon, 18 Oct 2021 09:58:36 +0200 (CEST) Received: by mail-wr1-f53.google.com with SMTP id m22so39844935wrb.0 for ; Mon, 18 Oct 2021 00:58:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=8r5UyGNeDyLv7Ti/1yBz7h3lm7l6hWoHG0hVMvlhs/k=; b=K0Jt67/fWt/3vsjnstY0gC8fPI7EL+Kq8kP+jILfCHw+xTbmM2crR2+x4lXFR0yn4Q 3DYr8pSZ+uDXUQsRR5tri7FR53tbLnJ9qbElc4GcrjJtlIX2bnRsETPfH6aaGLhVTdJc f6oHT/g6YZYzt8KnlnUGqfrfOBAEx2hzwA8XFlhrU5xwJY20wMYAPBGbCvyXjKPyVGKM s8SHppow8iyaJzK1JICB3t41cyE40L6X1egrreb63rW67916T36u/wzQ9iEIr60+NAuA l8r7LivEdd2WiswZwh602zG2bWwNyOQUaITRFFxtxQ1rwSG32TebeJWHRhBey0nUlomb aZhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=8r5UyGNeDyLv7Ti/1yBz7h3lm7l6hWoHG0hVMvlhs/k=; b=DBw9spYE0pKez2xFs2xOLJ0Fxh2fCTRPgPWH7zJG/iRKr9SUSD1c9wxZqvfVOgVhKu DWISJut3xFYBHfaE3Zk1i7D+ODQST5AWK6ZwlkYhkzNAY/vRjmKEdGIpVdYoUtIr/HLc hN0NxSMVynKX/120EQZh8BsFhRHFQ1t1Plo+x7ZSioFJ7bEEPIB6VNvVQRuFLG5LqDxM NZnGiiGz28E4OvqsIwY0ukUc9XQRX3XFp7qGpT8i8quVNl5MimOMk5aypjYaQWzVbhm1 Ik+8H1wid9VtEP7z5J8/9TbzVTo8AQD803MrgLPv+ZmEXR+9mfyyg/jMlHHqAZX70Lmp 1kKw== X-Gm-Message-State: AOAM5322bYXvIGwg+rlGX0QqmwP4zYpgi7x8PEjRTiM/W3tRbjGAY1zp FGWbv4WfdkkO91q7GLrIxw5TnA== X-Google-Smtp-Source: ABdhPJyGBgaN8hLZsYy6NCvpVLipiqXUj8MuIF6WlqGRv0lLL2fCc/UlBs7PloHoZHkHc5I2xTrPJw== X-Received: by 2002:adf:9d47:: with SMTP id o7mr34125156wre.50.1634543916424; Mon, 18 Oct 2021 00:58:36 -0700 (PDT) Received: from 6wind.com ([2a01:e0a:5ac:6460:c065:401d:87eb:9b25]) by smtp.gmail.com with ESMTPSA id s8sm1928555wrr.15.2021.10.18.00.58.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Oct 2021 00:58:35 -0700 (PDT) Date: Mon, 18 Oct 2021 09:58:35 +0200 From: Olivier Matz To: Morten =?iso-8859-1?Q?Br=F8rup?= Cc: Georg Sauthoff , Thomas Monjalon , David Marchand , Ferruh Yigit , dev@dpdk.org Message-ID: References: <20211017203718.801998-1-mail@gms.tf> <20211017203718.801998-2-mail@gms.tf> <98CBD80474FA8B44BF855DF32C47DC35D86C46@smartserver.smartshare.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <98CBD80474FA8B44BF855DF32C47DC35D86C46@smartserver.smartshare.dk> Subject: Re: [dpdk-dev] [PATCH v2 1/1] net: fix aliasing issue in checksum computation X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On Mon, Oct 18, 2021 at 09:35:41AM +0200, Morten Brørup wrote: > > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Georg Sauthoff > > Sent: Sunday, 17 October 2021 22.37 > > +Ferruh, as delegate to v1 in Patchwork. > > > > > That means a superfluous cast is removed and aliasing through a uint8_t > > pointer is eliminated. NB: The C standard specifies that a unsigned > > char > > pointer may alias while the C standard doesn't include such requirement > > for uint8_t pointers. > > > > Also simplified the loop since a modern C compiler can speed up (i.e. > > auto-vectorize) it in a similar way. For example, GCC auto-vectorizes > > it > > for Haswell using AVX registers while halving the number of > > instructions > > in the generated code. > > > > Signed-off-by: Georg Sauthoff > > --- > > lib/net/rte_ip.h | 27 ++++++++------------------- > > 1 file changed, 8 insertions(+), 19 deletions(-) > > > > diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h > > index 05948b69b7..1b8c6519a9 100644 > > --- a/lib/net/rte_ip.h > > +++ b/lib/net/rte_ip.h > > @@ -141,29 +141,18 @@ rte_ipv4_hdr_len(const struct rte_ipv4_hdr > > *ipv4_hdr) > > static inline uint32_t > > __rte_raw_cksum(const void *buf, size_t len, uint32_t sum) > > { > > - /* workaround gcc strict-aliasing warning */ > > - uintptr_t ptr = (uintptr_t)buf; > > + /* extend strict-aliasing rules */ > > typedef uint16_t __attribute__((__may_alias__)) u16_p; > > - const u16_p *u16_buf = (const u16_p *)ptr; > > - > > - while (len >= (sizeof(*u16_buf) * 4)) { > > - sum += u16_buf[0]; > > - sum += u16_buf[1]; > > - sum += u16_buf[2]; > > - sum += u16_buf[3]; > > - len -= sizeof(*u16_buf) * 4; > > - u16_buf += 4; > > - } > > - while (len >= sizeof(*u16_buf)) { > > + const u16_p *u16_buf = (const u16_p *)buf; > > + const u16_p *end = u16_buf + len / sizeof(*u16_buf); > > + > > + for (; u16_buf != end; ++u16_buf) > > sum += *u16_buf; > > - len -= sizeof(*u16_buf); > > - u16_buf += 1; > > - } > > > > - /* if length is in odd bytes */ > > - if (len == 1) { > > + /* if length is odd, keeping it byte order independent */ > > + if (unlikely(len % 2)) { > > uint16_t left = 0; > > - *(uint8_t *)&left = *(const uint8_t *)u16_buf; > > + *(unsigned char*)&left = *(const unsigned char *)end; > > sum += left; > > } > > > > -- > > 2.31.1 > > > > Great work documenting your thoughts behind this patch, Georg! I, for one, didn't know about the aliasing difference between uint8_t and unsigned char. :-) > > After taking a good look at v2 and the Godbolt reference to confirm the claimed benefits, there can be no doubts about this patch. +1, thanks for the good documentation > Reviewed-by: Morten Brørup Acked-by: Olivier Matz