From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E36C2C25B47 for ; Tue, 24 Oct 2023 16:27:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234630AbjJXQ1E convert rfc822-to-8bit (ORCPT ); Tue, 24 Oct 2023 12:27:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229544AbjJXQ1D (ORCPT ); Tue, 24 Oct 2023 12:27:03 -0400 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8AF993 for ; Tue, 24 Oct 2023 09:27:00 -0700 (PDT) Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-159-loZMyKNWM46MLirKavY2uw-1; Tue, 24 Oct 2023 17:26:56 +0100 X-MC-Unique: loZMyKNWM46MLirKavY2uw-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Tue, 24 Oct 2023 17:26:55 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Tue, 24 Oct 2023 17:26:55 +0100 From: David Laight To: 'Sebastian Reichel' , Andy Shevchenko , Michael Turquette , Stephen Boyd , "linux-clk@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Vasily Gorbik , Niklas Schnelle , Linus Torvalds , "kernel@collabora.com" Subject: RE: [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW Thread-Topic: [PATCH v4 1/3] math.h: add DIV_ROUND_UP_NO_OVERFLOW Thread-Index: AQHaBpXnM+Cp9N+qfkK9yv140Cm+RbBZHxkA Date: Tue, 24 Oct 2023 16:26:55 +0000 Message-ID: <488098703d6947ee8d7b19312c7097a6@AcuMS.aculab.com> References: <20231024161931.78567-1-sebastian.reichel@collabora.com> <20231024161931.78567-2-sebastian.reichel@collabora.com> In-Reply-To: <20231024161931.78567-2-sebastian.reichel@collabora.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org From: Sebastian Reichel > Sent: 24 October 2023 17:18 > > Add a new DIV_ROUND_UP helper, which cannot overflow when > big numbers are being used. For non-zero you can use (n - 1)/d + 1 instead of (n + d - 1)/d So maybe add: #define DIV_ROUND_UP_NON_ZERO(n, d) (((n) - 1)/(d) + 1) Saves the compiler having to get the remainder (if not generated by a divide instruction. David > > Signed-off-by: Sebastian Reichel > --- > include/linux/math.h | 11 +++++++++++ > 1 file changed, 11 insertions(+) > > diff --git a/include/linux/math.h b/include/linux/math.h > index dd4152711de7..f80bfb375ab9 100644 > --- a/include/linux/math.h > +++ b/include/linux/math.h > @@ -36,6 +36,17 @@ > > #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP > > +/** > + * DIV_ROUND_UP_NO_OVERFLOW - divide two numbers and always round up > + * @n: numerator / dividend > + * @d: denominator / divisor > + * > + * This functions does the same as DIV_ROUND_UP, but internally uses a > + * division and a modulo operation instead of math tricks. This way it > + * avoids overflowing when handling big numbers. > + */ > +#define DIV_ROUND_UP_NO_OVERFLOW(n, d) (((n) / (d)) + !!((n) % (d))) > + > #define DIV_ROUND_DOWN_ULL(ll, d) \ > ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) > > -- > 2.42.0 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)