Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp1249498lqg; Sun, 3 Mar 2024 01:25:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVtbu4iV37R7yiTtTO83IScOLzShNuSeMkaBdpkBPGFBXpi9sbIxV9hOPgbhglVjJgnz9S2/VtmumRb9uL1K8hxF+h88MsrKHg5GSGp5A== X-Google-Smtp-Source: AGHT+IGbxNQPyGohEjYsCnQrMOj6Z9gTx+51lGQW7039QaTNJRN7UX9zYu1lwuXSJ4yBt9etLwvf X-Received: by 2002:a05:6870:c693:b0:220:be71:6f40 with SMTP id cv19-20020a056870c69300b00220be716f40mr7741291oab.8.1709457950460; Sun, 03 Mar 2024 01:25:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709457950; cv=pass; d=google.com; s=arc-20160816; b=yA41mi/uhGgasxZIfPUyNhGjZgwLAurRXmo5J3flw1uTbWl2YdK3bkZEMTKnwJKmpj QeEcilEyEV932ow1GuStLo0p6zOIgGhZ15YCGvu22PEkqg7eIgawEuuKAFhdmKMLchCV VF6bw8zQlRn03d/Em8eIFbn3caSWkGt6Kp+Ajq6uq8pBgSMo9vohtz1Ahmxqtp3A5t/x qmLxAQjBvaln0UtxW1NmjP7KyEv06I03g57OzsZE+fBX7ttK4tNLX6zZ7CbRY7K3Gj2w rZ4qC2v0WY8+L8nI7RIeyZSOa/hk8+MUaqhN8zgFRGQIiekY6djhsocu9ZjqZ+nujXnM IUGQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=GLwSfBDy6nZfW7pLqWrRjJ/kBS0wGpiOV6RiM8jnEXY=; fh=ThhFGwRCjDLjCvQ5YviI08S/II7AwIqO/u/fJ0zfB9I=; b=G9V1uBU5U6nRsBVEI3xZPaKZxiiwLstI6LRBLhi8MBEBC5tBwB0yxeh95lzWcKeHqk IIw3qj8FEeZSEE+Hz5gNRcEXIH/zINY7ZPkL6CV3iGMJzlGLPZVsH/KBvk4vKAhpHLWW Vtx+DGHbAfERYTcbPrZlZb/GsS9ZkZFgnq5C+3tRTNSf8O71JEPgiyQ6+RxuyKdfV6tp 18uA1wyN9jogM7XMq/towaHq8dnG/2ikRHGv7EhUCBEA95D9wBaR2LFMXEgr1cvqpE0x riX7zVIrd/P6xlU3e7yXx1u1phqZ4f2fbTLYaqHIwp96uj66h023Md9ZyhG1mSVq2dQc hrgg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=pengutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-89685-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89685-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id y12-20020a62f24c000000b006e59615813bsi6217829pfl.376.2024.03.03.01.25.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Mar 2024 01:25:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-89685-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=pengutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-89685-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89685-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 06E5BB21F12 for ; Sun, 3 Mar 2024 09:24:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 711C9B642; Sun, 3 Mar 2024 09:24:30 +0000 (UTC) Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 B879DAD35 for ; Sun, 3 Mar 2024 09:24:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709457869; cv=none; b=HEWtwJmb1bZsD3LmNB4nmbrbfFtv5MbiUUVPvxmGW+wReSx+qVKSwcRdSgafxLaO3xFyeVtG8iOqeXZrjIn75KddgkozEKNwx+PPvfSaK0/su7sbwKHOy3cbEw6nfcdK0ATx/d3uGj6fRpaaL8Fxd7Zxsy2SY7bZLPxohcQ4tC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709457869; c=relaxed/simple; bh=LlKvoQRPABfKry/b2N2XpVfDUkyfRmoWcEJ2WI/dDZI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=KPb1sDRdUKx7xVbbinMpfjPiz/Wd1vA4XLJrSwe43Xqy1brNnaEYFevVSQXEwY5UQCJNfwuzwX6IS3ojZjmLHBj8O0BcNCJwKeLBoQZN1ny2LIf0q1hKC+dsKqjkWj8fmItGrxyxibkO61SvZJBl29/mINQOEW2C1/T9QveZOBk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rgi50-0007Gy-BM; Sun, 03 Mar 2024 10:24:22 +0100 Received: from [2a0a:edc0:0:900:1d::77] (helo=ptz.office.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rgi4z-0048Ir-28; Sun, 03 Mar 2024 10:24:21 +0100 Received: from ukl by ptz.office.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1rgi4y-00FrWU-39; Sun, 03 Mar 2024 10:24:20 +0100 From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= To: Andrew Morton Cc: Biju Das , linux-kernel@vger.kernel.org, kernel@pengutronix.de Subject: [PATCH v2] mul_u64_u64_div_u64: Increase precision by conditionally swapping a and b Date: Sun, 3 Mar 2024 10:24:09 +0100 Message-ID: <20240303092408.662449-2-u.kleine-koenig@pengutronix.de> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Developer-Signature: v=1; a=openpgp-sha256; l=1840; i=u.kleine-koenig@pengutronix.de; h=from:subject; bh=LlKvoQRPABfKry/b2N2XpVfDUkyfRmoWcEJ2WI/dDZI=; b=owEBbQGS/pANAwAKAY+A+1h9Ev5OAcsmYgBl5EG4uHU+wgSThK8NbSrhs6DrOsbWCbBx4V9+s 36f5N6+XfGJATMEAAEKAB0WIQQ/gaxpOnoeWYmt/tOPgPtYfRL+TgUCZeRBuAAKCRCPgPtYfRL+ TsujB/917hzWZmKaMffR1oaaoL9/J7Qz7ku+4cfOD4wKy+B3GPkeraqhFLV+sYzrcmHKjQ1eUWj uDFU/J0hDHFm5ZMlFxzb2/rltHIJIL3vb5R7kdaWpVN2DZ4ZRq8Jck0gmTL1PrETaxrizdxrvCG 0keL9teMrLm595r0gTBVsNGGTbp0tk3O/sIumbIuPQac466mNuZeDNGE0SJQiYZKUgYRNKZgHAf e/v1QsrUkRVTu6AQtxGqgB0ZDPoWTGl8cReZQfLw6ctZjlx8K0xTOjjo4zb52FmcGXuHgLWc5Gi Abegik2AB5jOZYwghMVPDylW3RvWF8ZrA2uovA9GRo/7R7Qx X-Developer-Key: i=u.kleine-koenig@pengutronix.de; a=openpgp; fpr=0D2511F322BFAB1C1580266BE2DCDD9132669BD6 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org 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 --- Changes since v1: - Make use of swap() (Thanks Marc) - Fix a typo in a code comment (Thanks Randy) - Fix a typo in the commit log (s/If/if/; noticed myself) v1 got a Tested-by from Biju; I didn't add it here as the patch changed. I'm optimistic that this v2 would pass his tests, too, but I don't wanna assume stuff when adding tags. Best regards Uwe lib/math/div64.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/math/div64.c b/lib/math/div64.c index 55a81782e271..191761b1b623 100644 --- a/lib/math/div64.c +++ b/lib/math/div64.c @@ -22,6 +22,7 @@ #include #include #include +#include #include /* Not needed on 64bit architectures */ @@ -190,6 +191,20 @@ 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 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) + swap(a, b); + /* * (b * a) / c is equal to * base-commit: 1870cdc0e8dee32e3c221704a2977898ba4c10e8 -- 2.43.0