Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp1053792lqg; Sat, 2 Mar 2024 13:14:51 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVOhFMipc9xWWH9814wsBiEK6vtre15Q4mW5rEj5ZIOoI/4mnFsTr1xOfnUPdaw05lf0PyblB8YafrUZa1IsXWSvfV7PR7bnLUyB05pdg== X-Google-Smtp-Source: AGHT+IENiA6veddoEHjoyBt+muxMhfSgdwT/cC5cP73dsWqgvySCd39rS4HaL1qxxd8KWqzSD53k X-Received: by 2002:a0c:efd4:0:b0:690:633e:7ed0 with SMTP id a20-20020a0cefd4000000b00690633e7ed0mr2540721qvt.8.1709414091446; Sat, 02 Mar 2024 13:14:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709414091; cv=pass; d=google.com; s=arc-20160816; b=LKXunVHAXM2XXyJ+6LMSKw0hRRcCYBcvo0KtMAJqn934VEMzwN0MhRHJBnW/QR4Dr6 POWIRlHMLQRiolOnBikOjcpPlY3/ggCBMUWjegsjNCyPrvTISnE25rnYapKAeYzmHiYS L7a44O9228MGFGKVGFhSbOvV5DosbXTZowOXVw4vAM3gpsAfScWDYqlk0FXvS9eIHv2f B80jARN7tiUrWw63tn8kl9lQJIYh42R8z+KzdhFA4dmFjXjkOr/ybMn8/65v/31l9P3q zju12YTgGa5Ksp+4CDeV2kVfbPsJkaVTS+eDVkD58TVRV69zM/veJhySQplX5YTLa8I6 sjgg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=LSmHTC1U1BZnToEb2yjxqrWe+nbp3iaTFfFkGyOQTrU=; fh=YE36z2LY0wHu2Zja3L4BHe42TgXC4L2HAOwhhO0aG+o=; b=q5536aZoC4RodGgQy9BiY5qS0QAcQrX9k30Bo3/Ef3SSLVL8ll6cN8VyfqQu6JrjGa K9trcT3VSpTXr/81L9jcMZIceeGCqy1xHaD9p4YPfyXKcX7p3evtqfh5fSqp0vwe6YTJ SmhyqAXxN3Eg650SNyu8JU+4oKRykbGUAmaEsDPnkUdw37yR/99fYpyVnioUJ0ux23wv 2Z6oYsS+1BiWMUS91zMIjox6QyndrmcECw2dEypSdq/BzVT0/KSDXYypvyCiW3TySHiW xDDGVULplmb6TPFBVciSynsf932RtRXyzEte3cC4mkE5TDYZrdAFAvHayMmEOxNGi6bI 8tNw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=k1kfqMBg; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-89600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89600-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id gv4-20020a056214262400b006900f254febsi6560789qvb.365.2024.03.02.13.14.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Mar 2024 13:14:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-89600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=k1kfqMBg; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-89600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89600-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id EC2291C214BE for ; Sat, 2 Mar 2024 21:14:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B12664120C; Sat, 2 Mar 2024 21:14:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="k1kfqMBg" Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE69D40C09 for ; Sat, 2 Mar 2024 21:14:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709414084; cv=none; b=LKr+gdRfUWL/NL9//FvS/Hjqn3h5xy0XK1IInC7ueNkVdjgalr0+b1dfGQFViNv1uG5k9CD/qt7VNDg8nQx3WPx1ZUfqxW/EhLFnIQXE2twMGHz6nxwEFBMbHOFRFnw4k4to2HJhC7DLg15xRopXlzplzwM0WIRbAj89sAKP5EM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709414084; c=relaxed/simple; bh=cARRLoeHAGL46ZFhIfYilnE6+Rl38Ax3+uFCRY6oWaA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=qhehN3hlzCw6wPFPMnZq//S+MUkccJ6oT0hTjclVpCYM4h7rcjSXw8nRcD6MsY/YdqmlEsyhNjo9vJGhCv1TtAF/dZwIi79h6uU3FIIvGbaptxwZkDvptBQCmiQIwK6o0K7TXuBaZUj32rB6DjB8PQzTHaObZGiBTQ9iqLkqjBg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=k1kfqMBg; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Sender:Reply-To:Content-ID:Content-Description; bh=LSmHTC1U1BZnToEb2yjxqrWe+nbp3iaTFfFkGyOQTrU=; b=k1kfqMBgyxXuRBl8zvvmEI7SIE AvKRFRXZHeRraxo5kn9FLgpNRbhw4peX3XNrn9x5E5v8hFmRCtPMqo2dQUtVBhckt7imMs1R9F1T9 ehZJV7HHGiFZC0e2fjfG7lxaO5fWRARXavZFbzjtjVUDsPAJASLD99jLM9BhIaMU4Ivdy+ftdFpnT ojZGtdqcJF070zpSUTzEdLLhHFmm5DZEczNxWnGsfz0TAV85Nkqa+Af125o71dGyCAqFYGbwnIjbb 5aSoQ07kCL0WQM9mqooiJD+mM55qWSnfKi/GIN7ntqnU/bx+rU2PqGNZv9xVYHVptQ87ddhwUfJeZ YtRszkOg==; Received: from [50.53.50.0] (helo=[192.168.254.15]) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1rgWgq-00000004R13-18BI; Sat, 02 Mar 2024 21:14:40 +0000 Message-ID: Date: Sat, 2 Mar 2024 13:14:37 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mul_u64_u64_div_u64: Increase precision by conditionally swapping a and b Content-Language: en-US To: Marc Kleine-Budde , =?UTF-8?Q?Uwe_Kleine-K=C3=B6nig?= Cc: Andrew Morton , Biju Das , linux-kernel@vger.kernel.org, kernel@pengutronix.de References: <20240302205426.639205-2-u.kleine-koenig@pengutronix.de> <20240302-deed-greyhound-f0c6c831e4c7-mkl@pengutronix.de> From: Randy Dunlap In-Reply-To: <20240302-deed-greyhound-f0c6c831e4c7-mkl@pengutronix.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 3/2/24 13:05, Marc Kleine-Budde wrote: > On 02.03.2024 21:54:27, Uwe Kleine-König wrote: >> As indicated in the added comment, the algorithm works better if b is >> big. As multiplication is commutative, a and b can be swapped. Do this >> If a is bigger than b. >> >> Signed-off-by: Uwe Kleine-König >> --- >> lib/math/div64.c | 17 +++++++++++++++++ >> 1 file changed, 17 insertions(+) >> >> diff --git a/lib/math/div64.c b/lib/math/div64.c >> index 55a81782e271..baf6f8681907 100644 >> --- a/lib/math/div64.c >> +++ b/lib/math/div64.c >> @@ -190,6 +190,23 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c) >> >> /* can a * b overflow ? */ >> if (ilog2(a) + ilog2(b) > 62) { >> + /* >> + * Note that the algorithm after the if block below might loose s/loose/lose/ >> + * some precision and the result is more exact for b > a. So >> + * exchange a and b if a is bigger than b. >> + * >> + * For example with a = 43980465100800, b = 100000000, c = 1000000000 >> + * the below calculation doesn't modify b at all because div == 0 >> + * and then shift becomes 45 + 26 - 62 = 9 and so the result >> + * becomes 4398035251080. However with a and b swapped the exact >> + * result is calculated (i.e. 4398046510080). >> + */ >> + if (a > b) { >> + u64 tmp = a; >> + a = b; >> + b = tmp; > > You can use swap() from linux/minmax.h here. > > Marc > >> + } >> + >> /* >> * (b * a) / c is equal to >> * >> >> base-commit: 1870cdc0e8dee32e3c221704a2977898ba4c10e8 >> -- >> 2.43.0 >> >> >> > -- #Randy