From patchwork Tue Sep 3 15:30:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 3390 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id C93523F1F3 for ; Tue, 3 Sep 2024 17:31:20 +0200 (CEST) Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42c7a5563cfsf3962305e9.2 for ; Tue, 03 Sep 2024 08:31:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1725377480; cv=pass; d=google.com; s=arc-20240605; b=MruzLy5ChFwoUokqMzENJKr048OEp4Wl8DLcmpluNlg9WGaz/9nQuIYsE4eX1thyPo ziK3rtZs6F+IBwvfYFFThP26OwDoUkSVjylGKLpQjeXcbsUz6SefHIAS33peCav7R6BP soKvfJ+g28tos1dhqGtfJCmeMdN4BhNAb1Tdijl1JTzLi3kmDJDyikwuA5KBu5TkFIbw Okn7ttAYW6mxrqIxlVqhgPWZktdvJNzMXrqrbX3cJJFoSuM3Es8gB0TDO8xUidefZj0y LF6WIQBG8d3uL7CjpocoUfRql09vmPxJcuj1PMad5h136blO/EgJnqZQ4wNgakTGuqxO ii2A== 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=Mci6E9zU6IVJMkyxM4N11ScMORSoGb3MKFgUa6KM6Z0=; b=MoHgV/QdzR+FrcUcVn9AmMd0PI6+CB7DAEVpzYjGYB3zmPWSMyuZ0hoph+JcHOpLx1 Xm3CFlaxFjl/cUdy+NEEpUSwpFBmjTTOxoTFT+a7TtAox4aIWU5V9upAaPNepnDV6+Zd vR7dwP6ZR+1Gr4cZRHg5+rxh2/T0Sf2hxDH8FoE20YHPybgtQGmvSfdC9ZbrQseYlhdO 2hkmnfCN82Pn6b48EHWGTaicOmPR/PHUgRpZQGx3zyaRT4pe4Y8moYAYpmWI+KZp14nC KiyzTIaTBtAAjUI/jY/4Ev0KhWjr6p64+Gu4UuuMlmfKnF7KZIgsnJst7w8GWc1BRU+N LYOg==; darn=patchwork.amarulasolutions.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="C/sqHaX2"; 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=1725377480; x=1725982280; 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=MeZsp19IGfPgel0PyVN2gSFXl7SAE4VAq8qfcx5TzakJh4M+T2MrVKL1B8bag2vSq9 xZUaE1Ll6RF9CKakRMqh4RhBkGFBQHAZPM/+NT2zV+OFNU7x7GF+vgnlM9+WIeitrlfO rChztSZpHEvRos3xRcqoURqQTd75SY5Rv/mng= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725377480; x=1725982280; 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=U+LOT/wSa59dzleA8HyIjIZZJOv08fT4WFMfcNnvkV8qLZeqtMYlaD3aFwjTYyORSF JbDO1Gn66mGV6lrX97bK7WbMNAEpcpjgovYxX9XXtDC62W4fXpKcl21NqPzAWskrpdFB GFEaNNzm8hPIBZYfqUaxPwGmjGRu5xdHEw38eBlVTJENnunLffhQdHIMe21rngWdGupX 7MJ6abbu9iTVd4R2pQe+Bd22GRkLVQzgHxy8jeZA/NteY9jYomE3YcoMA+G4ZNUVtynT 6IABRrZF34DJ3ATPg/rfXC7YupyK4DBw8mjlzcIX1PfGxQCVz0skjGGGblfLuLARAGea uNww== X-Forwarded-Encrypted: i=2; AJvYcCXJgB02kP64TJbUm/9eia5TU9DgOCDm7xrT8V1qvVSMdbnB8U6P8+CGnXj5N6ybQiPw6G+/q7nyMuRZLIGa@patchwork.amarulasolutions.com X-Gm-Message-State: AOJu0YxVLyGTgF36QFg5dBIzpI0G5gJLt2F/4KLfJiT4RPJ6Egft+Ekd Ho26qR9wvaRWhk/mG1U4GDX5UszJOyNM9k8bfWa6GxTkwo8atre+aL0vJWLF8FwGCg== X-Google-Smtp-Source: AGHT+IFo0l/oxyhnQnocQE66Um5OcGAiMiRTNAiEufoFNM7KzC7YDfDaCRrPJ7oQRhuhuV89Mt7nxg== X-Received: by 2002:a05:600c:470f:b0:426:5ef5:bcb1 with SMTP id 5b1f17b1804b1-42bb01aa115mr123468565e9.6.1725377480423; Tue, 03 Sep 2024 08:31:20 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a05:600c:4686:b0:427:9377:3cc4 with SMTP id 5b1f17b1804b1-42bb2a0d072ls3943115e9.1.-pod-prod-06-eu; Tue, 03 Sep 2024 08:31:19 -0700 (PDT) X-Received: by 2002:a05:600c:470f:b0:426:5ef5:bcb1 with SMTP id 5b1f17b1804b1-42bb01aa115mr123467675e9.6.1725377478713; Tue, 03 Sep 2024 08:31:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1725377478; cv=none; d=google.com; s=arc-20160816; b=JJ9o8HE/NDpvy/MhhwM2RzGlsm5Fnba43IHUMhT4KO0eCpsiEpDL3TfUHTS/zJhbBN T9Qm7alEPODpcantyZN9mcc8rbYvdch6GrLs2LK5l97csrFC7mBr+N0JliLzjMZNGa+E f0szGGip/+hf1urQfP43PW4CD04MaKR4piLCt2mlVY1eTyE6Piava9kwYb+BXoK51Wgs FrXHeH0AhzoWuEsC8cps2GHNZb3GnzKYgY54M7KQJoQdGkxPJJhAVRszC8H/2RTruptK j1WBMqRM050gmgK92dsOchpiKho1bYFKOZxkuaPV6QGgYTpINBsGKpBbNRPymoaEyEx3 6lUg== 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=qZgRbvMEEr0wB9IqVK7mZCtoikjKfTkxuHxolABhFxtC90cOr8J/7xQEUZ+kTHWv/D CPRrhqTf4kKdbf8gzsa6X29qUgP4NWG0lRppHlF300Nlwl1ZqP8Dv73IXmfJx+hWzn+N U6DyMJYTPpR+9MhGWMNXQdrgcgXHMI8JNe2xYbL19zv9RgtfBYnK4lRZgLvgO/oZqPVf TCWgWPC8B6FHr4qf0zgjai8GXFiXJ+Y1faissTPyni7CPoBoY3cg54DvW/eZjrex9aDa yVveMhNqsiA92TOvNijMiJqDoKxHYiweaQt1i2gq+gF4EdvRjbs/mN6v0TVWbte2EvkV 7E6w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="C/sqHaX2"; 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-42bb70b7031sor44104735e9.12.2024.09.03.08.31.18 for (Google Transport Security); Tue, 03 Sep 2024 08:31:18 -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:1c83:b0:426:6ed5:d682 with SMTP id 5b1f17b1804b1-42bb01b4419mr123306235e9.12.1725377477933; Tue, 03 Sep 2024 08:31:17 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.amarulasolutions.com ([2.196.42.65]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42bb6e273e3sm174168685e9.30.2024.09.03.08.31.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2024 08:31:17 -0700 (PDT) From: Dario Binacchi To: linux-amarula@amarulasolutions.com Subject: [PATCH 18/29] lib: div64: sync with Linux Date: Tue, 3 Sep 2024 17:30:49 +0200 Message-ID: <20240903153100.918077-18-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240903153100.918077-1-dario.binacchi@amarulasolutions.com> References: <20240903153100.918077-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="C/sqHaX2"; 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 */