Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1345060pxv; Fri, 25 Jun 2021 10:40:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxCgfeQzICyec99rvuIU2HBN6yGuHVXEnK427i3hkRU90/DpXowIMdZR5QZPwzZ6mmSrd0q X-Received: by 2002:a50:8dc6:: with SMTP id s6mr16470970edh.50.1624642849175; Fri, 25 Jun 2021 10:40:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624642849; cv=none; d=google.com; s=arc-20160816; b=dOwMK/VyDNFkVKjFR3S00JeJB+QQTzixAyEDGCD1mGR5iReamVpzzSxX3Ccoqj8qfj YV49HbAYxnFftZDZDPlDYRXx5rpfOj/fDzLp1MBzGvMx2KLJJ2molYv7G24kSYF8CLF1 OMPblF9pA6+KWUSBBTvSZo6ZrYrJJ56MzJphRaIgzYuyQoSS/v8ShII9SnXfwxioL32r Vc2oS+eoWpnO7CnapS6uafLbgO+Jsd4cqlP/gN9RwL4iBRdhZYbMHLzOYHt5bNwaEQ5u nwkRpDj+acJFpZe2DidnCusQeOOco19aNb4U+sOvFFddwFBq5McmWgypA9FU2gNIhLRL RA9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=tQkD3S8PU0hszKT1uHfj/5+AF6ZeccJTp640xlY1fFw=; b=k040BvGzvQ3Ezz3A59DMOLXIv2jZ8aLrFqZjArkRyph30mYrqL3MmYqv56PohXTdDi po57CilHqQr3PdzgPYRaS1FRZfUGnUSNtiSecvvsKWhj3l1By6pW8CRWWW6LmOCp2xgr 6T/By3VRewThUDwjNe94zYIa3C/YWvmelI3ribNK8FD54ZIXHbM1TrBrPiNn87njQupj ZyncekGKFjHIGMfYqVWH/qdCUkYyXKcceEeDmEuh/lT9USjW/daJyMq8Kvd+TUoYcEcv IB/lINFqur5g2MI4MFYGh92NjAxL8VW98jcCZ3c4dtSsFvHg4J1GFHHlNc43oKkX1ZsM LSAQ== 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 yh17si6513304ejb.306.2021.06.25.10.40.24; Fri, 25 Jun 2021 10:40:49 -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 S229723AbhFYRlo convert rfc822-to-8bit (ORCPT + 99 others); Fri, 25 Jun 2021 13:41:44 -0400 Received: from mail-ua1-f46.google.com ([209.85.222.46]:43832 "EHLO mail-ua1-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229531AbhFYRln (ORCPT ); Fri, 25 Jun 2021 13:41:43 -0400 Received: by mail-ua1-f46.google.com with SMTP id f1so3841713uaj.10; Fri, 25 Jun 2021 10:39:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=hMsdHKSeztMKrPHZ8nbG2VqyYL9zlmEgt8jO+8+YC2U=; b=nUT56JRIdUCvJchDRgYdezS6g2Mz2vgennpByZqYJ7owEBc+9VM2PnXG79nAkTBfQ2 p0T1RnMP1ccbTykEwe1EPLyc0/XOdu507SZtCQID7JXToNkGi3+HWj50SL7Ozbo/pD0S CFtgxpWFr8hVEJVxd2c0ZiGgrmw3NhvEo1QRbt0sZL6R4PPtyvcCf5+UNxlcUGYB9MJs WMD8BrK9Z9poKuObRd4od38nxpQOML7y0efn44yAdiire3uG3Ug2wL2g+zvKIKM43cJY 4E+FtBRyTMj1dFW+YnCqCFuFmfjipkyhLRhVKStVHmwcl6qIBvF36nTqaCfhzHCfzHGJ R00Q== X-Gm-Message-State: AOAM531AvKHpb6yfsgknp72vai1RZUnaCddcNiMMCLsKddNnVfl4XEly 1LfprtrRU+ckKRPhjFPaLaGodobHvboAc6C5ZOY= X-Received: by 2002:ab0:3734:: with SMTP id s20mr13225318uag.106.1624642761311; Fri, 25 Jun 2021 10:39:21 -0700 (PDT) MIME-Version: 1.0 References: <20210624224909.6350-1-pali@kernel.org> <20210625143617.12826-1-pali@kernel.org> <20210625143617.12826-8-pali@kernel.org> <20210625153803.u6uesckcqyvvo7dl@pali> <20210625155008.GB16901@1wt.eu> In-Reply-To: <20210625155008.GB16901@1wt.eu> From: Geert Uytterhoeven Date: Fri, 25 Jun 2021 19:39:10 +0200 Message-ID: Subject: Re: [PATCH v2 07/11] math64: New DIV_U64_ROUND_CLOSEST helper To: Willy Tarreau Cc: =?UTF-8?Q?Pali_Roh=C3=A1r?= , Michael Turquette , Stephen Boyd , Rob Herring , Greg Kroah-Hartman , Andrew Lunn , Gregory Clement , Sebastian Hesselbarth , Vladimir Vid , =?UTF-8?B?TWFyZWsgQmVow7pu?= , linux-clk , "open list:SERIAL DRIVERS" , Linux Kernel Mailing List , Linux ARM Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Willy, On Fri, Jun 25, 2021 at 5:50 PM Willy Tarreau wrote: > 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). Fair enough. So we want to add a cast to DIV64_U64_ROUND_CLOSEST() above, too? > 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. Yep. Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds