From nobody Sun Dec 14 06:15:38 2025 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9DD61DD877 for ; Sun, 18 May 2025 13:39:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747575542; cv=none; b=Vmh9NQPovFYV6H/bfKZBrBwnK9Fnxx/dch89jf/n16HX/Yv90jNjRe+W4zw/enlsBbvsKvUmAZPCFwAI9URU2dBRqbk6kVRl6RgWT+rFe6iBxMhSYoqomvODj2WADp98rA8SRTNaeVdkv5XB6KG5IL/1lrZhX7Fu9oDS4SJK5EA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747575542; c=relaxed/simple; bh=QJ3yOJOT1zzdfKoKBAGKSk8xnN0U7WXtl4zPnr/y+8A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Am/v5+Har6/ZzGQmFQFU3YZ/2R6/mZyfywRARGme/3zNahyIGCV/8ocXhU5i0HsB0rtJ76AyecEhnW70k7ugdHQlmNCt7F2uxGFS/nPKAHJPtU378apYBlPOdbGj5s3VMhNaP2IEWvjs/tOpVv2RV8ZtX+3BceE8k1QR9WtmwfA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Rv5rRnLg; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Rv5rRnLg" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-3a064a3e143so1817563f8f.3 for ; Sun, 18 May 2025 06:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747575539; x=1748180339; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=z1+kNrtA3xdQK8Wggj2tCmDdplh3SmM+EQ04B7r6wEw=; b=Rv5rRnLgtGHYLf0QykAhcGGsR/If7r8AXKbcjth6mlu8cJ1bdn7zh1OwlN+XC51rmE 0outFyOxvwEHqA9t5B+TSr60x0Zrn9a8IsMj2q5vBXx/OZd1R/sNhWlRzXo4hglA9J1a THJ6BL5BLX/NCPO3lHHJe36rywjpnL1ZnWNbtIUUQBQvT7KivT8XaVAwFR3nDdeSGukZ X7UoBRF2ynDc/hmoVV95SydixdNMsY7KTPOTroYN9Xk2AjGtpWkytWSBKf47ZlA/unVY gtuYkaNrmh/qnYEk8eUToSlcl3/oypDtXNYCW/NvZXGVVVyzCqLaC07jPGJOoadl3M7z SUfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747575539; x=1748180339; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z1+kNrtA3xdQK8Wggj2tCmDdplh3SmM+EQ04B7r6wEw=; b=fwlSiBHMzJqZmKhXMoNVNQNQAGlp/iWRghVlcibg1vscKf10nXfdj7TfC59bjM9+xw PnOCMSayeRT//IpDBffHpyroFl2EpdA/999oTfP812VKpN9kNT9xO1gTuSk4CsCwOf8h QwzK1RN+0vpZnIFRLnSbfqflZ4kcR644tBZUSy5/z1rxmP8Wfr/A8jwqazd/ZM0bjO+j zsf7t/myCznxoxXFQ0JEaqQO88qBbvEqABGFnqeL/p62Afut0xKz+u2UwYYxrYzBr0L5 2ViV9h5W4Vo28GFcoYjS5bTd3R76uPL/nMhLxt2WaTJwRgAymPs4BbQNvsQ06CnCpAOc dn4A== X-Forwarded-Encrypted: i=1; AJvYcCXutYRB3dVSWHkvPQ2awttv/RgzLcDe0P5MyWMakjAYWHLqBMBsXFa2d9lR8Z6YpTsNkqW2NQr2esj11GI=@vger.kernel.org X-Gm-Message-State: AOJu0YxNWsRj7ptY9K/S3L9Rno/3TGwqWqLQ97kRpVZJwv//tNZRTkK8 L3551qvZRUuxOzXLlq+oePFrdNYoUwxFKiWvOG4th8+bSMn+FHu2vOzq X-Gm-Gg: ASbGncvQbEKSuorjkO1EMbFOaiN/GiCwODqnIfV1dewi+qhDJDq8qKWrSW/4nZpFKgt ETSd2eAHZlsdIi7jKGWPwoIPBdHPa6e/XCu1Pt7REFKXhHRKKDsv9G5OBSiV9qz/+DqhhccLo/A uO4yBFeQcPg7feMd97B5TI71+sWz7HLlwj6xOviZvAxhg3yp9+sfPqzQjMoM3qB6CG4bYQCdzAP ebfOkRropCSpS8qpDEOUyXi8acqoVdxkc/XijD9UPRPjJRTzFcRkx4vRK7+HoW7XfFdG3qQAl5a CCjeOXNgm5H0Mm+JYaYf1XBoQuci2NUeUxsBXHopgELgbgXd35n7fLzCnMxVouA3vWMiuAoQnn0 nnkjzuY6vGZZxh4Mn0xRpN13WjdYvqbbx X-Google-Smtp-Source: AGHT+IFL6CE2YZvlcQRHkSkyRhGvvKJFWqxyMyhnBqiAsvx8o2d0Ljtcr7D+xItujCwIYAwDFnoQzQ== X-Received: by 2002:a05:6000:186d:b0:3a3:6b0c:a8a3 with SMTP id ffacd0b85a97d-3a36b0ca9b1mr1818031f8f.17.1747575538877; Sun, 18 May 2025 06:38:58 -0700 (PDT) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a35ca5a7d6sm9429508f8f.30.2025.05.18.06.38.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 May 2025 06:38:58 -0700 (PDT) From: David Laight To: Andrew Morton , linux-kernel@vger.kernel.org Cc: David Laight , u.kleine-koenig@baylibre.com, Nicolas Pitre , Oleg Nesterov , Peter Zijlstra , Biju Das Subject: [PATCH v2 next 1/4] lib: mul_u64_u64_div_u64() rename parameter 'c' to 'd' Date: Sun, 18 May 2025 14:38:45 +0100 Message-Id: <20250518133848.5811-2-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250518133848.5811-1-david.laight.linux@gmail.com> References: <20250518133848.5811-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Change to prototype from mul_u64_u64_div_u64(u64 a, u64 b, u64 c) to mul_u64_u64_div_u64(u64 a, u64 b, u64 d). Using 'd' for 'divisor' makes more sense. Am upcoming change adds a 'c' parameter to calculate (a * b + c)/d. Signed-off-by: David Laight Reviewed-by: Nicolas Pitre --- Note v1 of the patchset had mul_u64_add_u64_div_u64(a, b, add, c). lib/math/div64.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/math/div64.c b/lib/math/div64.c index 5faa29208bdb..a5c966a36836 100644 --- a/lib/math/div64.c +++ b/lib/math/div64.c @@ -184,10 +184,10 @@ u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *= remainder) EXPORT_SYMBOL(iter_div_u64_rem); =20 #ifndef mul_u64_u64_div_u64 -u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c) +u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d) { if (ilog2(a) + ilog2(b) <=3D 62) - return div64_u64(a * b, c); + return div64_u64(a * b, d); =20 #if defined(__SIZEOF_INT128__) =20 @@ -212,36 +212,36 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c) =20 #endif =20 - /* make sure c is not zero, trigger exception otherwise */ + /* make sure d is not zero, trigger exception otherwise */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdiv-by-zero" - if (unlikely(c =3D=3D 0)) + if (unlikely(d =3D=3D 0)) return 1/0; #pragma GCC diagnostic pop =20 - int shift =3D __builtin_ctzll(c); + int shift =3D __builtin_ctzll(d); =20 /* try reducing the fraction in case the dividend becomes <=3D 64 bits */ if ((n_hi >> shift) =3D=3D 0) { u64 n =3D shift ? (n_lo >> shift) | (n_hi << (64 - shift)) : n_lo; =20 - return div64_u64(n, c >> shift); + return div64_u64(n, d >> shift); /* * The remainder value if needed would be: - * res =3D div64_u64_rem(n, c >> shift, &rem); + * res =3D div64_u64_rem(n, d >> shift, &rem); * rem =3D (rem << shift) + (n_lo - (n << shift)); */ } =20 - if (n_hi >=3D c) { + if (n_hi >=3D d) { /* overflow: result is unrepresentable in a u64 */ return -1; } =20 /* Do the full 128 by 64 bits division */ =20 - shift =3D __builtin_clzll(c); - c <<=3D shift; + shift =3D __builtin_clzll(d); + d <<=3D shift; =20 int p =3D 64 + shift; u64 res =3D 0; @@ -256,8 +256,8 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c) n_hi <<=3D shift; n_hi |=3D n_lo >> (64 - shift); n_lo <<=3D shift; - if (carry || (n_hi >=3D c)) { - n_hi -=3D c; + if (carry || (n_hi >=3D d)) { + n_hi -=3D d; res |=3D 1ULL << p; } } while (n_hi); --=20 2.39.5 From nobody Sun Dec 14 06:15:38 2025 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4AFCD21171B for ; Sun, 18 May 2025 13:39:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747575543; cv=none; b=VtvKxAtcPm7lOITZn+PwEOE4rDDJi7HVcRacmvx49r+16f1yXWLPikIGu5/+CK6/QnzNBHtD8zskzW7R6aiYuClgcMPT5hI3OxbZeKoeEG9yWybrv0RzuiqaqRSO4yRKZKXhZzOf87JBfEPmF/qcLQjdtuT8KHBpmwHPNdV1TIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747575543; c=relaxed/simple; bh=Qt2xjNPt5+Dy+URK8MvzmyqOVnwPZrjCeq5xMgAe/cM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mjWi1FOutqw6I6pYkbJuD/bNqfUjuPsvcLx6XGFxps/qG0MyGE1Fgrx7bZbYstq1v+RRAgG7EcbujpciRjrXBVekdh0sBzVF8fNd4qGO6feym0hnFAgroAT3w2NNHqnL1VQ1ImIHIdL1/opmtGaLPjXbkntUQjIcCN9/bnSXK7I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UEcmxL4C; arc=none smtp.client-ip=209.85.128.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UEcmxL4C" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-445b11306abso2412575e9.3 for ; Sun, 18 May 2025 06:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747575539; x=1748180339; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/Ulhux40xZIUdH8FPaZF/btHZ6IAcrvTToW7pK3BG2g=; b=UEcmxL4C0TXrRknTNKNI+WdjYj4e2BJqVDhDcmFM86AJCxC5hbch3WFG9pOBMlRqDv MJbtpsbJRjovb6Hf2zEzIqA4aS2a/vAr7Y3xckDeIa7upYopmfynwCVdxXkFZb6lFXIk caBxKZO9QUe9EMWs27sK/hDcJ6EmGIV8hEzRSXM9AZVnJB3OOTkcr17bF+lMnyQqbSLO UlV0yhbPmH7c0dJj1iisxhWA4Vs70rI5Wr6M/PJJ3iAapm3YsaeQuOPKe2jgFuuYlbHq OEy2/08rPPzu8gfkOjXQV2h8K3Gc2cSHJm+7PFM7WgWHPAJmHSH0KSIwRX1vTWVWcxGa awlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747575539; x=1748180339; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/Ulhux40xZIUdH8FPaZF/btHZ6IAcrvTToW7pK3BG2g=; b=kV0cCASaKSkeDlN57u1mSO/NdTxD7jZxbWo8KZrvTYdM1IHFEVWMKggJYJnoUFOCil 2qgKoTUCXYa/92YceNwHl3cNfD8nYCM6utAVgiR1H0bs6Iib47892YZoJ0773zcH8Xx7 GQ6Rpwqwkk/FFhiCXt1v+yEIgjIIJAJfaQznKaR7WBLpuzFX4RvlHrdsUWV4EfhSIwcP spvgJlQhsjsR49P4JwC0b8PRScPJkXbtesNmVeEdp2F+fv0zvPbGl/tG18BswcoxisjG MWc+EdEttfZocuRXQiWrX87ERRqDUqqMKz8qdcJY1kCceYYFADb+WvJC/2Or7ZDWHGXC 00mg== X-Forwarded-Encrypted: i=1; AJvYcCVT7C9qhEFaRaQdMDOpyjELl9NQSOm8gi+OyLsSSjAV/lVa3xiYlZc/RxiBYyCegNt9SK8QtM8H6wpwtu8=@vger.kernel.org X-Gm-Message-State: AOJu0YyCphhsSfxXWZ03MrMx4l3rROF3FYXc9Xi8+lUpIa4yWxVfklOy 4f6d/3ohSFjND9r5JUbXlI6EsczQRBEqQaEMtRo2cJc8fAnoHYDviQqo X-Gm-Gg: ASbGncvHJWJ7afM9uNneOAlMGq4ZACQOjwz1cyF9DCqAOhyEOM3tAK2OBst7MpD4Vok 81AjBZ2S6CGM2YFGNu0WNukP5ujeta5EoqNvov98EBpiqxCFEXiRs4imCAwSPV3VRWaZFijcYlE Tk013u6OGYENOu15HpUzGATWtV4EbYdsuylTDyhZu+9MI+qbOxxMLbE3ovaDoIxAFx5Go1JSPjT fBC3gBCEb8kCyBxhEGL+P8MqvBwQ/Gvasjtu9dMrZGU35QwNflhZByikfwdnEt72h8Objvs/SmB vueE7ZPD012a6lHoCzN03xUgpesbwsazfNtBgUqWIpfwcaZJSBYbIbKo8a003p5RHmsWHA64PkG O6SKLCiJPwbDgmzHe7oDdtTobyabapp9M X-Google-Smtp-Source: AGHT+IEQLoZh71Y1B+UMg2W/nbn4Hld5yAILnTVv5RILKdvYlsVwHqes4M3l+Z43PlbP4mVOsgnsRw== X-Received: by 2002:a05:600c:3588:b0:43d:a90:9f1 with SMTP id 5b1f17b1804b1-442fefdad6bmr72812255e9.6.1747575539296; Sun, 18 May 2025 06:38:59 -0700 (PDT) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a35ca5a7d6sm9429508f8f.30.2025.05.18.06.38.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 May 2025 06:38:59 -0700 (PDT) From: David Laight To: Andrew Morton , linux-kernel@vger.kernel.org Cc: David Laight , u.kleine-koenig@baylibre.com, Nicolas Pitre , Oleg Nesterov , Peter Zijlstra , Biju Das Subject: [PATCH v2 next 2/4] lib: mul_u64_u64_div_u64() Use BUG_ON() for divide by zero Date: Sun, 18 May 2025 14:38:46 +0100 Message-Id: <20250518133848.5811-3-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250518133848.5811-1-david.laight.linux@gmail.com> References: <20250518133848.5811-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Do an explicit BUG_ON(!divisor) instead of hoping the 'undefined behaviour' the compiler generated for a compile-time 1/0 is in any way useful. It may be better to define the function to return ~(u64)0 for divide by zero. Signed-off-by: David Laight --- A new change for v2 of the patchset. Whereas gcc inserts (IIRC) 'ud2' clang is likely to let the code continue and generate 'random' results for any 'undefined bahaviour'. lib/math/div64.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/math/div64.c b/lib/math/div64.c index a5c966a36836..c426fa0660bc 100644 --- a/lib/math/div64.c +++ b/lib/math/div64.c @@ -186,6 +186,9 @@ EXPORT_SYMBOL(iter_div_u64_rem); #ifndef mul_u64_u64_div_u64 u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d) { + /* Trigger exception if divisor is zero */ + BUG_ON(!d); + if (ilog2(a) + ilog2(b) <=3D 62) return div64_u64(a * b, d); =20 @@ -212,13 +215,6 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d) =20 #endif =20 - /* make sure d is not zero, trigger exception otherwise */ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdiv-by-zero" - if (unlikely(d =3D=3D 0)) - return 1/0; -#pragma GCC diagnostic pop - int shift =3D __builtin_ctzll(d); =20 /* try reducing the fraction in case the dividend becomes <=3D 64 bits */ --=20 2.39.5 From nobody Sun Dec 14 06:15:38 2025 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A20B7219311 for ; Sun, 18 May 2025 13:39:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747575543; cv=none; b=iK8ErA98SDAtgq/GHfCAbvWp3LG9U0/1Re6JZdAeNdCbYqNwe6nButN9qpDDLhkdRDopOOeIJEOK0D/PlSXmnwmU0SZVy5hjOKjekWC7PnGKq4mXdlvdxT1kO0VK4TwQujzoCi0gjTab0pupIgEOC2rlwXFPOBcfwZ7wOKK+6h0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747575543; c=relaxed/simple; bh=VcTY3ATs49wdmFeijGtBSwT2eDFNAmdiz8oLBVsqvsw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=c7ZCjXM/Z1QbkGuB8u9iKjPg2TmASMaKRXGw6yMh2nTQGUFNGetX10yXExF8kLZRAxjv5GDdP4UNGQDvE/arPBJiKHuDX9GULzpOhTy+3O4KtID9T+8vdmvwTyhucqh6J1kyolc1tLbt9YzrKvDycf22raGyvaVlE5Nti2o8mT8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=H5U001J9; arc=none smtp.client-ip=209.85.221.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="H5U001J9" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-3a36f26584bso71676f8f.0 for ; Sun, 18 May 2025 06:39:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747575540; x=1748180340; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ff4zjuq6YR8BU4ZJhNeIt/UngqbNKAR/HUeaqnUWpSs=; b=H5U001J9A568LpowWhGb/JSKz7pUtovTDRQQQezXseh9H0cwO+O4T+igzUvJltKICs iz8mgm9eY4voHkAC8Zcfhyf+YLWuAtMRsMuMGZ4EMBSZsAuUXRat58JUg6oGiUIiGRjc YepE2SK/QOyZdvB8DfvtRaY+YXLSFcqr1v6LGJEoYxmnyYoLX5+NLt8dctjSpMngD7Nw dLxlLmAIzOOd7jKeFstG2ILwYJv0QYNVGzybXE46uT3djm1Y6ZkBPPvfvUxhwUZnO7e2 ANRnjgsoafgo59ZDLLk1T0LqIV0h7PrmmpSN7SIZK/oLhIdyC9OTXgUlA4vCdA5jPVw/ aBLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747575540; x=1748180340; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ff4zjuq6YR8BU4ZJhNeIt/UngqbNKAR/HUeaqnUWpSs=; b=m3qk8y5/y+QfbOb/ChkMLgJZflLFPQ3CpN2zQ3KnUAX7v3l6KLGga6SK5ZuuI/7zOS R/ngqP8CeTR3vEzwiJKfITrWxgeDC4SNBOCLjS6DomXPVAWbRGM/f5r7YnxD1uwo4Ir1 erUKfAqyKLCLuP+sZYBSvRodEP21fvrZl2v6C4XYtes7Wk3BlxsXbLwGo3daSY1Ppgsj 3UvWsXEmNrfiGtQpF61arLo+Jd0JcHsr2aNCZH/YAIhsJKbVBiyAdCH4q6yUqR6orOXO aMlbNLJ4FcZxDq/GLJtr0z4lVCn8/oqBuoLo12izzQNFq7BTaOwBUSQut1uNvipXvRA3 Cv5g== X-Forwarded-Encrypted: i=1; AJvYcCXf0mQcB9xlfkyN8kV1wHdGM3tIKMwbf1DfJfL6Gl6P8AdAt1RFzn4OxIt5sZVef7Sqgxj0xxAWefv0Egs=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6uF8/jbWXfZXsmCDvJ388CqARcSOLR+Z+6e9DH32GCRrA2f+i pLexpebczSZmFISNYgfFDShF4plckBrtGfbAJYF8iFAPV0N0s8Kc6rw3 X-Gm-Gg: ASbGnctG1MULSsDwp/cN9ewrK1e4uJhQpnYQffH7dDsovnPsXuqmYs9/+6F/RA7roPs lwjk8X8chv4bnYFjKsey6cHf3JoBZTyi9UNiYgtxxjdCRZ6HpJDcwtNp2FaX2bue7DF/ajfH+oW ZaS/7PpZE8QK0+GZ9iSwinzJbzBRrmQyi8/K9ywsy6YmqiH1EGVElOvOfFt2JB8coY2eSonwxni znKdpvLc4N7jtu4YN0Sqi96N7Kvm7cqm5ADUSD09x/aJV42/8FMPiK+n8RHCWaUCyt2zg190zuK EaXqag+XXkzNxgVi9Aaz7avWJCj5hmKrPs0uQQ3UnRmdV/bkSqRlaZiEdJ3CnXy7p+lP03Eu9Pe FP8pUkAbEqNnUOn6w2iY6eFno/abjZEx5 X-Google-Smtp-Source: AGHT+IE6HIuFs5i40XK9rYYMZBxnHklain7m6LQ4pUyx98ocCo1SNY0WkHtjlOyWTrjT3YtpA8WvaA== X-Received: by 2002:a05:6000:2ae:b0:3a1:f655:c5b2 with SMTP id ffacd0b85a97d-3a3600da099mr7842194f8f.39.1747575539699; Sun, 18 May 2025 06:38:59 -0700 (PDT) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a35ca5a7d6sm9429508f8f.30.2025.05.18.06.38.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 May 2025 06:38:59 -0700 (PDT) From: David Laight To: Andrew Morton , linux-kernel@vger.kernel.org Cc: David Laight , u.kleine-koenig@baylibre.com, Nicolas Pitre , Oleg Nesterov , Peter Zijlstra , Biju Das Subject: [PATCH v2 next 3/4] lib: Add mul_u64_add_u64_div_u64() and mul_u64_u64_div_u64_roundup() Date: Sun, 18 May 2025 14:38:47 +0100 Message-Id: <20250518133848.5811-4-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250518133848.5811-1-david.laight.linux@gmail.com> References: <20250518133848.5811-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The existing mul_u64_u64_div_u64() rounds down, a 'rounding up' variant needs 'divisor - 1' adding in between the multiply and divide so cannot easily be done by a caller. Add mul_u64_add_u64_div_u64(a, b, c, d) that calculates (a * b + c)/d and implement the 'round down' and 'round up' using it. Update the x86-64 asm to optimise for 'c' being a constant zero. For architectures that support u128 check for a 64bit product after the multiply (will be cheap). Leave in the early check for other architectures (mostly 32bit) when 'c' is zero to avoid the multi-part multiply. Note that the cost of the 128bit divide will dwarf the rest of the code. This function is very slow on everything except x86-64 (very very slow on 32bit). Add kerndoc definitions for all three functions. Signed-off-by: David Laight --- Changes for v2 (formally patch 1/3): - Reinstate the early call to div64_u64() on 32bit when 'c' is zero. Although I'm not convinced the path is common enough to be worth the two ilog2() calls. arch/x86/include/asm/div64.h | 19 ++++++++++----- include/linux/math64.h | 45 +++++++++++++++++++++++++++++++++++- lib/math/div64.c | 21 ++++++++++------- 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/div64.h b/arch/x86/include/asm/div64.h index 9931e4c7d73f..7a0a916a2d7d 100644 --- a/arch/x86/include/asm/div64.h +++ b/arch/x86/include/asm/div64.h @@ -84,21 +84,28 @@ static inline u64 mul_u32_u32(u32 a, u32 b) * Will generate an #DE when the result doesn't fit u64, could fix with an * __ex_table[] entry when it becomes an issue. */ -static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div) +static inline u64 mul_u64_add_u64_div_u64(u64 a, u64 mul, u64 add, u64 div) { u64 q; =20 - asm ("mulq %2; divq %3" : "=3Da" (q) - : "a" (a), "rm" (mul), "rm" (div) - : "rdx"); + if (statically_true(!add)) { + asm ("mulq %2; divq %3" : "=3Da" (q) + : "a" (a), "rm" (mul), "rm" (div) + : "rdx"); + } else { + asm ("mulq %2; addq %3, %%rax; adcq $0, %%rdx; divq %4" + : "=3Da" (q) + : "a" (a), "rm" (mul), "rm" (add), "rm" (div) + : "rdx"); + } =20 return q; } -#define mul_u64_u64_div_u64 mul_u64_u64_div_u64 +#define mul_u64_add_u64_div_u64 mul_u64_add_u64_div_u64 =20 static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 div) { - return mul_u64_u64_div_u64(a, mul, div); + return mul_u64_add_u64_div_u64(a, mul, 0, div); } #define mul_u64_u32_div mul_u64_u32_div =20 diff --git a/include/linux/math64.h b/include/linux/math64.h index 6aaccc1626ab..e1c2e3642cec 100644 --- a/include/linux/math64.h +++ b/include/linux/math64.h @@ -282,7 +282,50 @@ static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 = divisor) } #endif /* mul_u64_u32_div */ =20 -u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div); +/** + * mul_u64_add_u64_div_u64 - unsigned 64bit multiply, add, and divide + * @a: first unsigned 64bit multiplicand + * @b: second unsigned 64bit multiplicand + * @c: unsigned 64bit addend + * @d: unsigned 64bit divisor + * + * Multiply two 64bit values together to generate a 128bit product + * add a third value and then divide by a fourth. + * May BUG()/trap if @d is zero or the quotient exceeds 64 bits. + * + * Return: (@a * @b + @c) / @d + */ +u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d); + +/** + * mul_u64_u64_div_u64 - unsigned 64bit multiply and divide + * @a: first unsigned 64bit multiplicand + * @b: second unsigned 64bit multiplicand + * @d: unsigned 64bit divisor + * + * Multiply two 64bit values together to generate a 128bit product + * and then divide by a third value. + * May BUG()/trap if @d is zero or the quotient exceeds 64 bits. + * + * Return: @a * @b / @d + */ +#define mul_u64_u64_div_u64(a, b, d) mul_u64_add_u64_div_u64(a, b, 0, d) + +/** + * mul_u64_u64_div_u64_roundup - unsigned 64bit multiply and divide rounde= d up + * @a: first unsigned 64bit multiplicand + * @b: second unsigned 64bit multiplicand + * @d: unsigned 64bit divisor + * + * Multiply two 64bit values together to generate a 128bit product + * and then divide and round up. + * May BUG()/trap if @d is zero or the quotient exceeds 64 bits. + * + * Return: (@a * @b + @d - 1) / @d + */ +#define mul_u64_u64_div_u64_roundup(a, b, d) \ + ({ u64 _tmp =3D (d); mul_u64_add_u64_div_u64(a, b, _tmp - 1, _tmp); }) + =20 /** * DIV64_U64_ROUND_UP - unsigned 64bit divide with 64bit divisor rounded up diff --git a/lib/math/div64.c b/lib/math/div64.c index c426fa0660bc..66bfb6159f02 100644 --- a/lib/math/div64.c +++ b/lib/math/div64.c @@ -183,29 +183,31 @@ u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *= remainder) } EXPORT_SYMBOL(iter_div_u64_rem); =20 -#ifndef mul_u64_u64_div_u64 -u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d) +#ifndef mul_u64_add_u64_div_u64 +u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d) { /* Trigger exception if divisor is zero */ BUG_ON(!d); =20 - if (ilog2(a) + ilog2(b) <=3D 62) - return div64_u64(a * b, d); - #if defined(__SIZEOF_INT128__) =20 /* native 64x64=3D128 bits multiplication */ - u128 prod =3D (u128)a * b; + u128 prod =3D (u128)a * b + c; u64 n_lo =3D prod, n_hi =3D prod >> 64; =20 #else =20 + if (!c && ilog2(a) + ilog2(b) <=3D 62) + return div64_u64(a * b, d); + /* perform a 64x64=3D128 bits multiplication manually */ u32 a_lo =3D a, a_hi =3D a >> 32, b_lo =3D b, b_hi =3D b >> 32; u64 x, y, z; =20 - x =3D (u64)a_lo * b_lo; + /* Since (x-1)(x-1) + 2(x-1) =3D=3D x.x - 1 two u32 can be added to a u64= */ + x =3D (u64)a_lo * b_lo + (u32)c; y =3D (u64)a_lo * b_hi + (u32)(x >> 32); + y +=3D (u32)(c >> 32); z =3D (u64)a_hi * b_hi + (u32)(y >> 32); y =3D (u64)a_hi * b_lo + (u32)y; z +=3D (u32)(y >> 32); @@ -215,6 +217,9 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d) =20 #endif =20 + if (!n_hi) + return div64_u64(n_lo, d); + int shift =3D __builtin_ctzll(d); =20 /* try reducing the fraction in case the dividend becomes <=3D 64 bits */ @@ -261,5 +266,5 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d) =20 return res; } -EXPORT_SYMBOL(mul_u64_u64_div_u64); +EXPORT_SYMBOL(mul_u64_add_u64_div_u64); #endif --=20 2.39.5 From nobody Sun Dec 14 06:15:38 2025 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0976721931E for ; Sun, 18 May 2025 13:39:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747575544; cv=none; b=JrHAb3Te50QnNk7UBLv76ZWeQekyw7Q02UEu52cjspxEL2R55Tw3KXqAd8bwhk9nLpVFLB/69lw4Bmswga8k+o4DcU4eze/BN1kCSQYuotWspFQlefKpwmmycmOwtK5l3EI5qpBMcDR8O5dsppi5Sh3vcivsqU89aBYb0WL/PzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747575544; c=relaxed/simple; bh=ahVKsryo5fJK8cRYJYD2qDeTO3r6r+FuEN+QGZOPkCw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=to3LK0PGOUj5cnZqELg0uFKagev8CPkuLApvfcr+3jJvxtBsJEkNNvo6B5/4qAeDINl7CQthL/hxkL0FoX5uPwOf5Tmae8OtcIAWL71lig0JmWBhtENGibpAdI1ULx8lRIYoNtnkTqiMMno073ylvDUUB8j+zZXqdsVYPX9uEv4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DSEtVG9y; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DSEtVG9y" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-442d146a1aaso32600805e9.1 for ; Sun, 18 May 2025 06:39:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747575540; x=1748180340; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=O5l+AFdahfaamX5xz3SGr3TGKeKCr101AWT1rgabtrA=; b=DSEtVG9ysQBbLL7deOH43sG84yZjn1opwikgSeZ0Zk2iLupgLZPeXvp9QW/NPLsiET iKvbSFYXvdjxdgGKiO00mPCws9DDbt8NulCMeerk33VfAdKPmHH3qPiqX+N1WA8vOezg bfvKqiTYELWMkPJ5ecEHbhjoPnWm1Iz/9UYgXrgzu4Cww3Ozb1VNTj2xBmNWpF3SQZoI JrfGmflgBpupjSXYZytgpQcT5WVEdCTBrJhrC4MLarJwb8+y/KWO0kfoTYhvd1rWggfC 2fy/xH8HEjEOali0fwbrj0VunVMDE0oOcPAxfP6F5MWSnk32h5k+J1uaprh/XSekdQdF U8kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747575540; x=1748180340; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O5l+AFdahfaamX5xz3SGr3TGKeKCr101AWT1rgabtrA=; b=BtS4urpYgPfbIrnEn+BD87Q1AXuFu/htprS8dhsfd9NEFTd3/Rfu2QsvVIcDKtgFop dtrvjrLJyrvO936JsSCk2ut1Rll0jlmn35Unsow8uEyNUVfYCBoLJlZ5TjjHimKscajE jwccMTS7txEWu86e6leHROoDCoWrTqioPVw4SeqRj9b1HCYvF8oocEp4LgRrlmO7Wddb GMuKne9bGna/SmBhFWBg2g5pwfv6tkWHqy6dRG7OB/Co06mv1+IeezqLsa8StJ9kB590 jFJjnlJZHCLOBCueLnjHvSVdnIzgVIEAr9XTkdlr0ph46PzXMYrpnwg3KaMzWCdq3PaM 0GVA== X-Forwarded-Encrypted: i=1; AJvYcCUgUFxWxqNnV1tl9bIVUGWWyfZ+zdAScTiFNqO1+IkZkiVW71te3Hn+qITOalXEYUm3aqMx+khCYQQa+7Y=@vger.kernel.org X-Gm-Message-State: AOJu0YzGXAoXAHXoAJ4bknV7a83YF83809IRWu4OZKZYl6LRclc9+oRL lnTIEMsoIgn6XwOudrwFs8v4lh0Jgb7Dop0d+e8Z2whKXKg4jY/34xH5 X-Gm-Gg: ASbGnctRrtDfH4HZ5TG57h0VcJGGb8G3egs+YP+NNyPNz9oW4kFacwV9UIzRVQSfXHc VQF4Npv5z84GobVXPrPucGlZSziaEhgeS9tft3TdrbYWSnK25mTiQdOjyI+pNAEm1tTN9QXRy3g f4bmJRTecDG1SjDuDAbrT1zopHaILx7C0aVM9e5tO3dK5POVTHiKhuSycGLcPyUpJRwW1jBl7CA n+pFLRYemBPf69l5ELrRWLlesaeHYYGv24ZmwglQK4rIDLqNtBLE8vkyS1rVJzGSX4D87B+1iOL slls55M/2+7fH0o2r1Wb3vgJheOdPi0RT/UZdlzVa6NEwd0zS7lrvdDhtlHG0SCJ5EZ4pnIP4I4 fbDldgpHg9qJjr0zavbsKuqz89v9U4z45 X-Google-Smtp-Source: AGHT+IFf9N/0a43DmFdhhNj8e/1jd5AX2a/Ky8wnQHanBiEgEevHV5wkIKRhR0G0cdqeUwpBPtB19g== X-Received: by 2002:a05:600d:1c:b0:442:ffb1:b58 with SMTP id 5b1f17b1804b1-442ffb10be7mr71723615e9.12.1747575540144; Sun, 18 May 2025 06:39:00 -0700 (PDT) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a35ca5a7d6sm9429508f8f.30.2025.05.18.06.38.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 May 2025 06:38:59 -0700 (PDT) From: David Laight To: Andrew Morton , linux-kernel@vger.kernel.org Cc: David Laight , u.kleine-koenig@baylibre.com, Nicolas Pitre , Oleg Nesterov , Peter Zijlstra , Biju Das Subject: [PATCH v2 next 4/4] lib: Add tests for mul_u64_u64_div_u64_roundup() Date: Sun, 18 May 2025 14:38:48 +0100 Message-Id: <20250518133848.5811-5-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250518133848.5811-1-david.laight.linux@gmail.com> References: <20250518133848.5811-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replicate the existing mul_u64_u64_div_u64() test cases with round up. Update the shell script that verifies the table, remove the comment markers so that it can be directly pasted into a shell. It any tests fail then fail the module load with -EINVAL. Signed-off-by: David Laight Reviewed-by: Nicolas Pitre --- Changes for v2 (was patch 1/3): - Include the total number of tests in the final pr_info(). lib/math/test_mul_u64_u64_div_u64.c | 116 +++++++++++++++++----------- 1 file changed, 70 insertions(+), 46 deletions(-) diff --git a/lib/math/test_mul_u64_u64_div_u64.c b/lib/math/test_mul_u64_u6= 4_div_u64.c index 58d058de4e73..383b0dfc8023 100644 --- a/lib/math/test_mul_u64_u64_div_u64.c +++ b/lib/math/test_mul_u64_u64_div_u64.c @@ -10,61 +10,73 @@ #include #include =20 -typedef struct { u64 a; u64 b; u64 c; u64 result; } test_params; +typedef struct { u64 a; u64 b; u64 c; u64 result; uint round_up;} test_par= ams; =20 static test_params test_values[] =3D { /* this contains many edge values followed by a couple random values */ -{ 0xb, 0x7, 0x3, = 0x19 }, -{ 0xffff0000, 0xffff0000, 0xf, 0x1110eeef00= 000000 }, -{ 0xffffffff, 0xffffffff, 0x1, 0xfffffffe00= 000001 }, -{ 0xffffffff, 0xffffffff, 0x2, 0x7fffffff00= 000000 }, -{ 0x1ffffffff, 0xffffffff, 0x2, 0xfffffffe80= 000000 }, -{ 0x1ffffffff, 0xffffffff, 0x3, 0xaaaaaaa9aa= aaaaab }, -{ 0x1ffffffff, 0x1ffffffff, 0x4, 0xffffffff00= 000000 }, -{ 0xffff000000000000, 0xffff000000000000, 0xffff000000000001, 0xfffeffffff= ffffff }, -{ 0x3333333333333333, 0x3333333333333333, 0x5555555555555555, 0x1eb851eb85= 1eb851 }, -{ 0x7fffffffffffffff, 0x2, 0x3, 0x5555555555= 555554 }, -{ 0xffffffffffffffff, 0x2, 0x8000000000000000, = 0x3 }, -{ 0xffffffffffffffff, 0x2, 0xc000000000000000, = 0x2 }, -{ 0xffffffffffffffff, 0x4000000000000004, 0x8000000000000000, 0x8000000000= 000007 }, -{ 0xffffffffffffffff, 0x4000000000000001, 0x8000000000000000, 0x8000000000= 000001 }, -{ 0xffffffffffffffff, 0x8000000000000001, 0xffffffffffffffff, 0x8000000000= 000001 }, -{ 0xfffffffffffffffe, 0x8000000000000001, 0xffffffffffffffff, 0x8000000000= 000000 }, -{ 0xffffffffffffffff, 0x8000000000000001, 0xfffffffffffffffe, 0x8000000000= 000001 }, -{ 0xffffffffffffffff, 0x8000000000000001, 0xfffffffffffffffd, 0x8000000000= 000002 }, -{ 0x7fffffffffffffff, 0xffffffffffffffff, 0xc000000000000000, 0xaaaaaaaaaa= aaaaa8 }, -{ 0xffffffffffffffff, 0x7fffffffffffffff, 0xa000000000000000, 0xcccccccccc= ccccca }, -{ 0xffffffffffffffff, 0x7fffffffffffffff, 0x9000000000000000, 0xe38e38e38e= 38e38b }, -{ 0x7fffffffffffffff, 0x7fffffffffffffff, 0x5000000000000000, 0xcccccccccc= ccccc9 }, -{ 0xffffffffffffffff, 0xfffffffffffffffe, 0xffffffffffffffff, 0xffffffffff= fffffe }, -{ 0xe6102d256d7ea3ae, 0x70a77d0be4c31201, 0xd63ec35ab3220357, 0x78f8bf8cc8= 6c6e18 }, -{ 0xf53bae05cb86c6e1, 0x3847b32d2f8d32e0, 0xcfd4f55a647f403c, 0x42687f79d8= 998d35 }, -{ 0x9951c5498f941092, 0x1f8c8bfdf287a251, 0xa3c8dc5f81ea3fe2, 0x1d887cb259= 00091f }, -{ 0x374fee9daa1bb2bb, 0x0d0bfbff7b8ae3ef, 0xc169337bd42d5179, 0x03bb2dbaff= cbb961 }, -{ 0xeac0d03ac10eeaf0, 0x89be05dfa162ed9b, 0x92bb1679a41f0e4b, 0xdc5f5cc9e2= 70d216 }, +{ 0xb, 0x7, 0x3, = 0x19, 1 }, +{ 0xffff0000, 0xffff0000, 0xf, 0x1110eeef00= 000000, 0 }, +{ 0xffffffff, 0xffffffff, 0x1, 0xfffffffe00= 000001, 0 }, +{ 0xffffffff, 0xffffffff, 0x2, 0x7fffffff00= 000000, 1 }, +{ 0x1ffffffff, 0xffffffff, 0x2, 0xfffffffe80= 000000, 1 }, +{ 0x1ffffffff, 0xffffffff, 0x3, 0xaaaaaaa9aa= aaaaab, 0 }, +{ 0x1ffffffff, 0x1ffffffff, 0x4, 0xffffffff00= 000000, 1 }, +{ 0xffff000000000000, 0xffff000000000000, 0xffff000000000001, 0xfffeffffff= ffffff, 1 }, +{ 0x3333333333333333, 0x3333333333333333, 0x5555555555555555, 0x1eb851eb85= 1eb851, 1 }, +{ 0x7fffffffffffffff, 0x2, 0x3, 0x5555555555= 555554, 1 }, +{ 0xffffffffffffffff, 0x2, 0x8000000000000000, = 0x3, 1 }, +{ 0xffffffffffffffff, 0x2, 0xc000000000000000, = 0x2, 1 }, +{ 0xffffffffffffffff, 0x4000000000000004, 0x8000000000000000, 0x8000000000= 000007, 1 }, +{ 0xffffffffffffffff, 0x4000000000000001, 0x8000000000000000, 0x8000000000= 000001, 1 }, +{ 0xffffffffffffffff, 0x8000000000000001, 0xffffffffffffffff, 0x8000000000= 000001, 0 }, +{ 0xfffffffffffffffe, 0x8000000000000001, 0xffffffffffffffff, 0x8000000000= 000000, 1 }, +{ 0xffffffffffffffff, 0x8000000000000001, 0xfffffffffffffffe, 0x8000000000= 000001, 1 }, +{ 0xffffffffffffffff, 0x8000000000000001, 0xfffffffffffffffd, 0x8000000000= 000002, 1 }, +{ 0x7fffffffffffffff, 0xffffffffffffffff, 0xc000000000000000, 0xaaaaaaaaaa= aaaaa8, 1 }, +{ 0xffffffffffffffff, 0x7fffffffffffffff, 0xa000000000000000, 0xcccccccccc= ccccca, 1 }, +{ 0xffffffffffffffff, 0x7fffffffffffffff, 0x9000000000000000, 0xe38e38e38e= 38e38b, 1 }, +{ 0x7fffffffffffffff, 0x7fffffffffffffff, 0x5000000000000000, 0xcccccccccc= ccccc9, 1 }, +{ 0xffffffffffffffff, 0xfffffffffffffffe, 0xffffffffffffffff, 0xffffffffff= fffffe, 0 }, +{ 0xe6102d256d7ea3ae, 0x70a77d0be4c31201, 0xd63ec35ab3220357, 0x78f8bf8cc8= 6c6e18, 1 }, +{ 0xf53bae05cb86c6e1, 0x3847b32d2f8d32e0, 0xcfd4f55a647f403c, 0x42687f79d8= 998d35, 1 }, +{ 0x9951c5498f941092, 0x1f8c8bfdf287a251, 0xa3c8dc5f81ea3fe2, 0x1d887cb259= 00091f, 1 }, +{ 0x374fee9daa1bb2bb, 0x0d0bfbff7b8ae3ef, 0xc169337bd42d5179, 0x03bb2dbaff= cbb961, 1 }, +{ 0xeac0d03ac10eeaf0, 0x89be05dfa162ed9b, 0x92bb1679a41f0e4b, 0xdc5f5cc9e2= 70d216, 1 }, }; =20 /* * The above table can be verified with the following shell script: - * - * #!/bin/sh - * sed -ne 's/^{ \+\(.*\), \+\(.*\), \+\(.*\), \+\(.*\) },$/\1 \2 \3 \4/p'= \ - * lib/math/test_mul_u64_u64_div_u64.c | - * while read a b c r; do - * expected=3D$( printf "obase=3D16; ibase=3D16; %X * %X / %X\n" $a $b $= c | bc ) - * given=3D$( printf "%X\n" $r ) - * if [ "$expected" =3D "$given" ]; then - * echo "$a * $b / $c =3D $r OK" - * else - * echo "$a * $b / $c =3D $r is wrong" >&2 - * echo "should be equivalent to 0x$expected" >&2 - * exit 1 - * fi - * done + +#!/bin/sh +sed -ne 's/^{ \+\(.*\), \+\(.*\), \+\(.*\), \+\(.*\), \+\(.*\) },$/\1 \2 \= 3 \4 \5/p' \ + lib/math/test_mul_u64_u64_div_u64.c | +while read a b c r d; do + expected=3D$( printf "obase=3D16; ibase=3D16; %X * %X / %X\n" $a $b $c |= bc ) + given=3D$( printf "%X\n" $r ) + if [ "$expected" =3D "$given" ]; then + echo "$a * $b / $c =3D $r OK" + else + echo "$a * $b / $c =3D $r is wrong" >&2 + echo "should be equivalent to 0x$expected" >&2 + exit 1 + fi + expected=3D$( printf "obase=3D16; ibase=3D16; (%X * %X + %X) / %X\n" $a = $b $((c-1)) $c | bc ) + given=3D$( printf "%X\n" $((r + d)) ) + if [ "$expected" =3D "$given" ]; then + echo "$a * $b +/ $c =3D $(printf '%#x' $((r + d))) OK" + else + echo "$a * $b +/ $c =3D $(printf '%#x' $((r + d))) is wrong" >&2 + echo "should be equivalent to 0x$expected" >&2 + exit 1 + fi +done + */ =20 static int __init test_init(void) { + int errors =3D 0; + int tests =3D 0; int i; =20 pr_info("Starting mul_u64_u64_div_u64() test\n"); @@ -75,16 +87,28 @@ static int __init test_init(void) u64 c =3D test_values[i].c; u64 expected_result =3D test_values[i].result; u64 result =3D mul_u64_u64_div_u64(a, b, c); + u64 result_up =3D mul_u64_u64_div_u64_roundup(a, b, c); + + tests +=3D 2; =20 if (result !=3D expected_result) { pr_err("ERROR: 0x%016llx * 0x%016llx / 0x%016llx\n", a, b, c); pr_err("ERROR: expected result: %016llx\n", expected_result); pr_err("ERROR: obtained result: %016llx\n", result); + errors++; + } + expected_result +=3D test_values[i].round_up; + if (result_up !=3D expected_result) { + pr_err("ERROR: 0x%016llx * 0x%016llx +/ 0x%016llx\n", a, b, c); + pr_err("ERROR: expected result: %016llx\n", expected_result); + pr_err("ERROR: obtained result: %016llx\n", result_up); + errors++; } } =20 - pr_info("Completed mul_u64_u64_div_u64() test\n"); - return 0; + pr_info("Completed mul_u64_u64_div_u64() test, %d tests, %d errors\n", + tests, errors); + return errors ? -EINVAL : 0; } =20 static void __exit test_exit(void) --=20 2.39.5