From patchwork Wed Sep 11 15:10:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 3448 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 1D1864355B for ; Wed, 11 Sep 2024 17:11:00 +0200 (CEST) Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-378929f1a4esf2248971f8f.1 for ; Wed, 11 Sep 2024 08:11:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1726067460; cv=pass; d=google.com; s=arc-20240605; b=fyjRPTDiuOw3HGkJGSRhr335ujeAhKYR9lFUwZiGrnS2rJG+4Sz6dsak3egymhPjeZ mMf7bVZ5CJHxYIWoxGYr1K+BtESNrEsp6VJRpBFe5wmJ1QpVX343say/HTgVV53PXhaG yS+Tp11J+m+QYK0uGgoErnbcy8PctNwK9xxjAqfu1Zd0r8BMOgIIBUSg7SP6MeWm9Xyy 7jssQ9ciwiW2u0FcyKSO3UDy7j5c5iuHfjawIavVsrHAOgPXx9WObr/GHWdGSNWhZY+g X9nTt9ij+LHgH+6t3/zKb+VvPCouykUAwMPieus6SScd7gZy6b15MXEPfGTj6pxeV6Sr y+5Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature; bh=+PjFIaIwye3hiWKqNtFxvxOu+c5W414KF3J2fkwTkrA=; fh=L8oLKBqEXjJrCbiSC0ZfbcYnhmjV1gdtR7i8lnNY1Qs=; b=lxULrjm9MnR2L0FlcVg4naxplYP9dmKwMVsOXS+afrjnDmdx7Yfo3z7pAcywm1WNJR rUnmd4xSddLlnEoD/GyEtyyJ5V3QaXFP7FGz2BCw9zH03JaWZ6yWdaqdWibsglZAVbBN J8eKjHAVwSe/Vujww26J4tH2C7mlisVaVQwyUMIkKsmWr5eKN5d83j6JJQ7J7cFhsJ40 GrUpDLWiOegFAitAuf93VHfb9e/LnlkJwYnjbWy3t/WKA5PkOahCyA69A7LEmoVoEaot g7pxmwpFG5+y9AWPHluz0/RbodXa5tWBYm9SphF7IeDeTxBR4L+W38II17FQVA/ODebV ZiBA==; darn=patchwork.amarulasolutions.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=efPvNUfW; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; t=1726067460; x=1726672260; darn=patchwork.amarulasolutions.com; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:to:from:from:to:cc:subject:date:message-id:reply-to; bh=+PjFIaIwye3hiWKqNtFxvxOu+c5W414KF3J2fkwTkrA=; b=b4lsLTBIIY58zT/b7rDvV9Y43vTUyDlRNi+S61xUgqvgYVULnk59TloIr6c0Cui6ya i5lJSx5T19hx0hrPuWYHqJCtw+K8zaXwpNkXXaUYhN8+vA5T3g1+Sap2RPdsFsrE7AHz 0SEhNPzKuy6OA/J0nI1jmOUGqcCAmOWlyMd4Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726067460; x=1726672260; h=list-unsubscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:to:from:x-beenthere :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+PjFIaIwye3hiWKqNtFxvxOu+c5W414KF3J2fkwTkrA=; b=YwBH1qH0vq76iw+8Uh4QpwrXbhI2Wjz8dA4CXqphyrsNd9+0yNQmNij+K2YYsTFKUF G4b7jnWcZLSudl18986dqVh197RHH4qNAxnBgR4p+OJZA4IaYXKgmB44W8IErJaS1MSr VFAzSl8DJvjWJswX29CoTCs5nbvG3aa/atkg+BoZU8yC1m0OF1vwxJS7Hi6PfncVPfoN R+6xX8klZcmV5w/Lmp+ldJSqHHeASkiJyKjel1QlqKw1+RDlOFamtE7IgH684FKDafSx 4GRXPu6fYz0kFpjTKwuwkuKADWGiFSj1iFo3UZ4PPoz+BAWKwa/qrZPAPoUzrbbmeWWS XWiQ== X-Forwarded-Encrypted: i=2; AJvYcCVF9Q2+fWKJGsaiWK2koH8PmBioQCDSUg9CMxGWRqOPSKPfAX3NkgA2uD/rL7KfekcDMOrTIT9bjSA117PS@patchwork.amarulasolutions.com X-Gm-Message-State: AOJu0Yygx6KNzPdws1xlzu+8ij2e4/kJe6UkyrQ/OFVaPS73WitqwLCo BVy6xvrDehE3BuvjR9RIWUra4G28OaF5F7/fYGcYWIbyJC9TCgx9oXXwIy1bS1OR+g== X-Google-Smtp-Source: AGHT+IHcaRF8oEuooq3+AtFQ5Y2vJofBy+a0nUa974zOzqxMqdozMQBRxejqCkPGnFEJO1+d/MkEUQ== X-Received: by 2002:a5d:5d82:0:b0:378:90fe:f753 with SMTP id ffacd0b85a97d-37890fef859mr9114302f8f.28.1726067459672; Wed, 11 Sep 2024 08:10:59 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:600c:1caa:b0:42c:c82f:e5b with SMTP id 5b1f17b1804b1-42cc82f10ddls7954745e9.2.-pod-prod-09-eu; Wed, 11 Sep 2024 08:10:58 -0700 (PDT) X-Received: by 2002:a05:600c:5248:b0:42c:b23f:7ba5 with SMTP id 5b1f17b1804b1-42cb23f7d26mr116925675e9.10.1726067458184; Wed, 11 Sep 2024 08:10:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1726067458; cv=none; d=google.com; s=arc-20160816; b=gqyhKL98GVUc61hIEceuh6G3CxGqBIkjrYmjGE0xvfOgThY//73L2tdNrgIaeK28XJ PP07cjXQcnfnpJbSoywte2RaBofoaKmy3GnkO3NzZOlunflUPtjsPrU3XbkwsnVgARUo Jrqoqbj2qcGBnoDCqkx/X9rtJw7vxUmm5SetuuRLygdco46/K0eIWM3Uj0ou5bqVpBCt spk/woNfcwbp2xuiA+uvoGmRczPCd13bGpHBDzWe+WjxMNn8RCT+NV7ZN865iSuF5cEX hASYLTaxOki+tYKuWSkgY/rvjTV9kJN+JSzYGMFCsOBFmNeNIHd+W0kdQsbZme9k08En mC9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature; bh=XzvabhpATxW1DmCBaEyNON19zj4DFTmPK3v1IpWaSMA=; fh=FtQepR0uNoeKT2PVdKfTKOkvEYfwbRmoVE9BmlYw3tk=; b=Hf1bku91XqoS9l++uk9kdWtIgotK/Ddw1HIUu8lRLoQJgFeOAGo+rJcpQL0AHahqcw 7gb60Jq6KGMvTNf6P5n9mcLixr+QweENqMDXvHYSSXNYMROBmVlCGwbs/cWXp92EHluB CsC+8JKzHmCuFIwIUdf6gkIpAemBHZ9mukeJqdO9qsFZDuggKrcWkdmvnSpX50Of/Rbl n/9M4ocPgtNFguwViIjhQR9CbZaTSP8UfhxWD2RBe1cPGXqZuOQncIVnTYV7B82PMB3w UXQ2eD0Ho2CEKhZPb25mlTMEqVs0G3gxDMVVqiowFisTP2rwPM1XbhAwBCkBqFrNOd2Q N9DA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=efPvNUfW; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id 5b1f17b1804b1-42caf2ab4d6sor39547535e9.2.2024.09.11.08.10.58 for (Google Transport Security); Wed, 11 Sep 2024 08:10:58 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a05:600c:3b1d:b0:42c:b750:1a1e with SMTP id 5b1f17b1804b1-42cb7501dc3mr89469495e9.0.1726067457374; Wed, 11 Sep 2024 08:10:57 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.amarulasolutions.com ([2.196.41.3]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42cb116f44esm137688375e9.45.2024.09.11.08.10.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Sep 2024 08:10:57 -0700 (PDT) From: Dario Binacchi To: linux-amarula@amarulasolutions.com Subject: [PATCH v2 18/27] lib: div64: sync with Linux Date: Wed, 11 Sep 2024 17:10:30 +0200 Message-ID: <20240911151039.2914886-18-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240911151039.2914886-1-dario.binacchi@amarulasolutions.com> References: <20240911151039.2914886-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=efPvNUfW; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=pass header.i=@amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Michael Trimarchi Sync with Linux commit fc4a0db4149af ("Merge tag '6.5'"). Signed-off-by: Michael Trimarchi --- include/linux/math64.h | 110 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/include/linux/math64.h b/include/linux/math64.h index eaa9fd5b9685..0e684bb2beef 100644 --- a/include/linux/math64.h +++ b/include/linux/math64.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ #ifndef _LINUX_MATH64_H #define _LINUX_MATH64_H @@ -12,6 +13,11 @@ /** * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 32bit divisor + * @remainder: pointer to unsigned 32bit remainder + * + * Return: sets ``*remainder``, then returns dividend / divisor * * This is commonly provided by 32bit archs to provide an optimized 64bit * divide. @@ -24,6 +30,11 @@ static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder) /** * div_s64_rem - signed 64bit divide with 32bit divisor with remainder + * @dividend: signed 64bit dividend + * @divisor: signed 32bit divisor + * @remainder: pointer to signed 32bit remainder + * + * Return: sets ``*remainder``, then returns dividend / divisor */ static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) { @@ -33,6 +44,11 @@ static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) /** * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 64bit divisor + * @remainder: pointer to unsigned 64bit remainder + * + * Return: sets ``*remainder``, then returns dividend / divisor */ static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) { @@ -42,6 +58,10 @@ static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) /** * div64_u64 - unsigned 64bit divide with 64bit divisor + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 64bit divisor + * + * Return: dividend / divisor */ static inline u64 div64_u64(u64 dividend, u64 divisor) { @@ -53,6 +73,10 @@ static inline u64 div64_u64(u64 dividend, u64 divisor) /** * div64_s64 - signed 64bit divide with 64bit divisor + * @dividend: signed 64bit dividend + * @divisor: signed 64bit divisor + * + * Return: dividend / divisor */ static inline s64 div64_s64(s64 dividend, s64 divisor) { @@ -92,10 +116,14 @@ extern s64 div64_s64(s64 dividend, s64 divisor); /** * div_u64 - unsigned 64bit divide with 32bit divisor + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 32bit divisor * * This is the most common 64bit divide and should be used if possible, * as many 32bit archs can optimize this variant better than a full 64bit * divide. + * + * Return: dividend / divisor */ #ifndef div_u64 static inline u64 div_u64(u64 dividend, u32 divisor) @@ -107,6 +135,10 @@ static inline u64 div_u64(u64 dividend, u32 divisor) /** * div_s64 - signed 64bit divide with 32bit divisor + * @dividend: signed 64bit dividend + * @divisor: signed 32bit divisor + * + * Return: dividend / divisor */ #ifndef div_s64 static inline s64 div_s64(s64 dividend, s32 divisor) @@ -228,6 +260,24 @@ static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift) #endif +#ifndef mul_s64_u64_shr +static inline u64 mul_s64_u64_shr(s64 a, u64 b, unsigned int shift) +{ + u64 ret; + + /* + * Extract the sign before the multiplication and put it back + * afterwards if needed. + */ + ret = mul_u64_u64_shr(abs(a), b, shift); + + if (a < 0) + ret = -((s64) ret); + + return ret; +} +#endif /* mul_s64_u64_shr */ + #ifndef mul_u64_u32_div static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor) { @@ -257,4 +307,64 @@ static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor) } #endif /* mul_u64_u32_div */ +u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div); + +/** + * DIV64_U64_ROUND_UP - unsigned 64bit divide with 64bit divisor rounded up + * @ll: unsigned 64bit dividend + * @d: unsigned 64bit divisor + * + * Divide unsigned 64bit dividend by unsigned 64bit divisor + * and round up. + * + * Return: dividend / divisor rounded up + */ +#define DIV64_U64_ROUND_UP(ll, d) \ + ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); }) + +/** + * DIV64_U64_ROUND_CLOSEST - unsigned 64bit divide with 64bit divisor rounded to nearest integer + * @dividend: unsigned 64bit dividend + * @divisor: unsigned 64bit divisor + * + * Divide unsigned 64bit dividend by unsigned 64bit divisor + * and round to closest integer. + * + * Return: dividend / divisor rounded to nearest integer + */ +#define DIV64_U64_ROUND_CLOSEST(dividend, divisor) \ + ({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); }) + +/** + * 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); }) + +/** + * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer + * @dividend: signed 64bit dividend + * @divisor: signed 32bit divisor + * + * Divide signed 64bit dividend by signed 32bit divisor + * and round to closest integer. + * + * Return: dividend / divisor rounded to nearest integer + */ +#define DIV_S64_ROUND_CLOSEST(dividend, divisor)( \ +{ \ + s64 __x = (dividend); \ + s32 __d = (divisor); \ + ((__x > 0) == (__d > 0)) ? \ + div_s64((__x + (__d / 2)), __d) : \ + div_s64((__x - (__d / 2)), __d); \ +} \ +) #endif /* _LINUX_MATH64_H */