Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1262295pxv; Fri, 25 Jun 2021 08:54:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwO0jQXzcuvl7raroBLUDJ4izfR3Sos2PMwNBYrcvo3r+u9mXDQY/x6VWkTlSeWIh5z5vN5 X-Received: by 2002:a17:906:2d4a:: with SMTP id e10mr9232292eji.87.1624636473460; Fri, 25 Jun 2021 08:54:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624636473; cv=none; d=google.com; s=arc-20160816; b=bJKBGAi/FPnPNQBF1yg1enXt1EizKwl20NdvWG+mGvFGrmXRgTDGOM1V+JgXihsVZQ QQK3K/IGLq+g92Q9Blcz4RYowPCK57fFmB4n1H8NL2BS7LOQjNCpUPBmfICFH0e/hxWZ jWwUr+WOTH2evhELOyw9DRnJba/LRzPOTSf+dntlTKGsle5Zso2TnEUto9Jc0ELebCIr 6LNInWDFqDtHMlrf7fIV/oXVNIJFGxvsASzAZZIjgHXegalSI7L4lbky0ubS22Mtzho4 4nXv5/53lkVus4lUIfN1/UJ0CCmuatrob3nvw4SrLh6Y6f7S5kYMoIcaYL3+yQZHkSZO Bg+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=VSz3fcrFsjwNmNEgH4seU58aLdsK5kRj7u4LeU5bdYQ=; b=XukMGw/pK98WWITh4O/5xoH7sWzB/+5thgOORm/msp025CPVbECc/VpFxbVGwSu3Rv s3+aHhPJFgt8y4h494xXM6QGedpqrh7iPtGggdTJ86ON3yJCHRbKcmKmZn98o0w/Aa0+ pUSzv6e7Ra5FBgxz9BvAlmT0ydZ3Dr1NzxGhuEFOQToKh8DytY8IbrCj+CZIu3CbBAcc TVDwuQyNNBPrnMvG4wHz4FGZzRTAXNKK7hhZAFT/TOqtv7bqzWBVyU9ZA2rIiZR0Zr7Q wyiMfxINjF2rBEiOgfraa5oMeSf8FGdXCfgZMYfk+YPMiTxITzfWWIj+iyT6zCTxH7tB Go6Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id og24si6015721ejc.490.2021.06.25.08.54.09; Fri, 25 Jun 2021 08:54:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229944AbhFYPxH (ORCPT + 99 others); Fri, 25 Jun 2021 11:53:07 -0400 Received: from wtarreau.pck.nerim.net ([62.212.114.60]:57120 "EHLO 1wt.eu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229774AbhFYPxH (ORCPT ); Fri, 25 Jun 2021 11:53:07 -0400 Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 15PFo8Qb017261; Fri, 25 Jun 2021 17:50:08 +0200 Date: Fri, 25 Jun 2021 17:50:08 +0200 From: Willy Tarreau To: Pali =?iso-8859-1?Q?Roh=E1r?= Cc: Geert Uytterhoeven , Michael Turquette , Stephen Boyd , Rob Herring , Greg Kroah-Hartman , Andrew Lunn , Gregory Clement , Sebastian Hesselbarth , Vladimir Vid , Marek =?iso-8859-1?Q?Beh=FAn?= , linux-clk , "open list:SERIAL DRIVERS" , Linux Kernel Mailing List , Linux ARM Subject: Re: [PATCH v2 07/11] math64: New DIV_U64_ROUND_CLOSEST helper Message-ID: <20210625155008.GB16901@1wt.eu> References: <20210624224909.6350-1-pali@kernel.org> <20210625143617.12826-1-pali@kernel.org> <20210625143617.12826-8-pali@kernel.org> <20210625153803.u6uesckcqyvvo7dl@pali> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210625153803.u6uesckcqyvvo7dl@pali> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 25, 2021 at 05:38:03PM +0200, Pali Roh?r wrote: > On Friday 25 June 2021 17:22:31 Geert Uytterhoeven wrote: > > > +/* > > > + * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer > > > + * @dividend: unsigned 64bit dividend > > > + * @divisor: unsigned 32bit divisor > > > + * > > > + * Divide unsigned 64bit dividend by unsigned 32bit divisor > > > + * and round to closest integer. > > > + * > > > + * Return: dividend / divisor rounded to nearest integer > > > + */ > > > +#define DIV_U64_ROUND_CLOSEST(dividend, divisor) \ > > > + ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); }) > > > > Given "dividend" should already be an unsigned 64-bit value, I don't > > think the cast to "u64" is needed. Similar macros in this file also > > don't have the cast. > > It is just to ensure that plus operation between dividend and _tmp is > evaluated in 64-bit context to prevent overflow. Just a case when user > calls this macro with 32-bit dividend param. As it is a macro (and not > inline function) type is not automatically enforced. I agree, a large u32 argument added to _tmp/2 could overflow and remain 32 bits, yielding an incorrect result. The cast is mandatory here (and will either emit no code, or be useful). The only trap I'm seeing is if a negative signed int is passed in dividend, it will be sign-extended and will give a large u64 value. A preliminary u32 cast could avoid this but would break valid u64 arguments, and I'd claim we never know what the user wants if this happens in the first place. Willy