From patchwork Sun Jul 14 10:53:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Nazzareno Trimarchi X-Patchwork-Id: 3279 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-lj1-f198.google.com (mail-lj1-f198.google.com [209.85.208.198]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 88A1740F59 for ; Sun, 14 Jul 2024 12:54:04 +0200 (CEST) Received: by mail-lj1-f198.google.com with SMTP id 38308e7fff4ca-2ee87e69b53sf32896941fa.0 for ; Sun, 14 Jul 2024 03:54:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1720954444; cv=pass; d=google.com; s=arc-20160816; b=Tu77k0025Fvlnsn5SP/H7o0HRlT2zFx+83xZ4R2hqkliORFfkk1TudoekIyvE6+Exh HQhFhjEGFiKiQ/5JCeRelAWxnzydd4P4tqrJOvfLyPvse+nG72s1pMrblFR+ilqx2tEI OMOg8e8NL+c2sVdecnfinljMMTjE4NTlmmXXftMxorXT2eigPnwllE2undt5E110x8Du SpRPKH0ah15L8K1NOmKZ5tLVVbc0wvTJztkksEl304kjkuqFL2NbZSrZanEdto8JQDTu wPEEbfJsl4JN7+Hl/ss0iFhYAhQo0ptBN1XpDcuPckROrGbWjDrUoDupIxYqm3jArZaT wfFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=IX2bI+AYYcyF4AJ8YY33jhHNvGyHcGPs5OrGSUzBR5k=; fh=Ljk0VZQfULdBYc0EWcQQSlxMAnCxUc9h1LLGkAXmb7w=; b=kxibdrEqphsbg2ZNWC6LJ7f5JKky0DWCAxZjWwP7ILweshe/yTsKFvCJfh1mw5JSG4 0U48ijBM113qoz+Xh+S+d644DX+F8ZGsiB9mKOYS82mRbZZOakP3DbcajViwXcJs60SI +yiSQjhnYMxPbr1P+imXQnPungkQWrre9Har1RFBweZOSkDtuXzxADzYFwkkkYfN9XEA pQnVsDKIpkV0K51xd8zfA0sU+hjpVcE0NvuwsPnoEVpylJHF2oP+ywp4up73q4nd4zWJ YZ2Zcx3fNBun3sef74EIvQBbsLivY50x3wHT9ji0NIwXDUW4KIjWrdEFDrFQi85k1DwN ihRA==; darn=patchwork.amarulasolutions.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=jl5qOMRt; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@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=1720954444; x=1721559244; 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:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=IX2bI+AYYcyF4AJ8YY33jhHNvGyHcGPs5OrGSUzBR5k=; b=KD4BAcNkSvTtfrsEmXIbNuz3nZXg3f9crLditi+G/SoPIuJmnaeW4CRAl0yCg7FOZj 6+C/+fiVApcOdJJGIBPQ1wFMd86AO2jeWcSe+vpjrRHM8fs9St0d9bP9ADBsL8HBfTMx fY8C5JZKvDRP5000RQ6NoQENAsqsbKT9Kh8jA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720954444; x=1721559244; 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:cc:to:from :x-beenthere:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IX2bI+AYYcyF4AJ8YY33jhHNvGyHcGPs5OrGSUzBR5k=; b=Z3DmrUNnyn3f+EYdgwD3AbAbPsnz6qFXyFYSr3oVbbDQRWORQEOZCoqN76h4xEZJ6V BEWK4310/m1l/4GWdvn9xE2Pyk8oyufvJaX5cmYBo/2VUS7PkavBLpMT4Qd9dUAaQNKD jIONpWZgSX/BDq5VIFY8yiJSn3GF66mVSu7C+3CvoFc4BmQlnafNhnpwaPINVG+hgVuf q7IEbXjzGwhAvd/kTp2sFeVgrXPnWisNfddQG7gdABnNTG+1Qz2O9fBby43EopObjMWy VkkihVeMZD5Zgfguxkv4fkh7Xp4bJw4/IE5l0ubt6WZGJnopbhqJ2tKB1oWpmpxV3Ns2 0UVQ== X-Forwarded-Encrypted: i=2; AJvYcCWLWKpCkssxKldDV5TeX861YGTdmTB8CBpnSV6vl+FFv/4knJIpCeMwBOI5Aku1mIFl/1jYQj4xpw9ACvLPvGjMVf0ifEmSIrTsnJeSP/PwHricpZLPTraimw1J1w== X-Gm-Message-State: AOJu0YyjRPglShPavLnP1FjIZ0ourzg5DhKqErKfJtPxZueDfV9ON4dF 9r9FFuuZjFPHoEQ2rSgpWjPAlNzZQw9MIhkR5rGU2KaaINyIwWwYpp4Xm5UDH5sUdA== X-Google-Smtp-Source: AGHT+IG3revoa2K/UtM3fsxMDfOgu6VF7RSgCtAqLNE6wmOovwo9MMVJiKxAInC4R3NmqUl+I4tOJw== X-Received: by 2002:a2e:a792:0:b0:2ec:4f0c:36f9 with SMTP id 38308e7fff4ca-2eeb3169f11mr175355521fa.36.1720954444013; Sun, 14 Jul 2024 03:54:04 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a2e:a481:0:b0:2ec:6de2:327 with SMTP id 38308e7fff4ca-2eec93a6dfbls18377161fa.1.-pod-prod-05-eu; Sun, 14 Jul 2024 03:54:02 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWIs5dNOsrgizytc2+w2CQJU3owodSYxL8AglW8+f/M2RYHUPNljbIPWEwu2DxmIvROlI6Zt14UusCyAjR3+VDFpMxIg+iIdLKjn9OjtUHAOYfK X-Received: by 2002:a2e:2ac3:0:b0:2ee:7a7e:8ebd with SMTP id 38308e7fff4ca-2eeb318892fmr129353391fa.46.1720954442029; Sun, 14 Jul 2024 03:54:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720954442; cv=none; d=google.com; s=arc-20160816; b=v3J5OVJ+KSoBOGy2LgHHm8YDQvFQD40Ke/+gHV1vUJ1TjnhGUq0pixQOEoSypbiKGL SXMpeVmW/oqe9xlJmv1utPyxUuSBIIP/ogrDtGVOtomlwWPhYugmgQMuHTn8vBab4QTL S4sK52bGC56U8R4HXO7Ad61bQAPxgacDgFgJR0uEA34AvmOidVcuBSUmDOuGgfl26v6H au/l5Gx7XxHYCrtnacUkWaDLh9Ui2rMBfWcAZdzDpYfQYjch5ZQ4SdZKCXVI70RXlH7Z YIGF/N5E0FeM/wJb2YfqfYya/piMAeJ7wCMv3A7gmUAFyDZG2IBOrg/PtKmkDTdaK2IX I9Mw== 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:cc:to:from:dkim-signature; bh=WD7ZmfhocDYBRfei8FW+9wSfSoFIOqwvjpGxjOjRVBs=; fh=M2DrecPPASAAKlS4yAhqvuK1lkqPBnnUYLdjWtlCtco=; b=KNwNds/5IenS4xRdYZQkuvvn1RC61bLEjWQU5is64hz7XEiAIR3TZJOrKwtaWa9Lyt 93p4J+s2j/hVNTLsdf1J7iTAwdy3V7b9thm4slLxARyxzZNNQnNpU18KXrO4Hd8sPV68 9AmYkKPTo1sqeZNZ1+7XIInMx6VGp/nrqfUvzg3mHOQuYmNMRGw1gaoRh101P4KiwzW5 YpKDMNYvG0xa5Zu9mKI0btDPCW39P2kQSNodySVgwzOqfjAkBU+UcBdDSlSey5VMtwje 3nb4sODs5r5Jj9RvvUAGjzk6F/6O2H8WCKoVndDQRXlFQDkUulCm43nH4J6TAvNe87o0 Ku/A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=jl5qOMRt; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@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 38308e7fff4ca-2eee179d7e4sor4910261fa.2.2024.07.14.03.54.01 for (Google Transport Security); Sun, 14 Jul 2024 03:54:02 -0700 (PDT) Received-SPF: pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Forwarded-Encrypted: i=1; AJvYcCXDF2umL3kPo1HGq8LZEwj20p9VOM8cD8nPxjPqvSoNN2eyIqRz5e42lV4tFhnXUiaPINGSv4YSlewKhljO00eGt508xIRXxnO1HcZ1c8lVoH9d X-Received: by 2002:a2e:3306:0:b0:2ee:8454:1c25 with SMTP id 38308e7fff4ca-2eeb316b020mr127177841fa.34.1720954441617; Sun, 14 Jul 2024 03:54:01 -0700 (PDT) Received: from panicking.. (mob-5-91-58-211.net.vodafone.it. [5.91.58.211]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc5a36c7sm119966166b.7.2024.07.14.03.54.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Jul 2024 03:54:01 -0700 (PDT) From: Michael Trimarchi To: Michael Trimarchi Cc: Dario Binacchi , Patrick Barsanti , linux-amarula@amarulasolutions.com Subject: [PATCH 22/25] lib: div64: sync with Linux Date: Sun, 14 Jul 2024 12:53:25 +0200 Message-ID: <20240714105328.35825-22-michael@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240714105328.35825-1-michael@amarulasolutions.com> References: <20240714105328.35825-1-michael@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: michael@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=jl5qOMRt; spf=pass (google.com: domain of michael@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=michael@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: , Sync with Linux commit fc4a0db4149af ("Merge tag '6.5'"). Cc: Tom Rini 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 eaa9fd5b96..0e684bb2be 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 */