From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 DDDA548097F; Wed, 6 May 2026 14:08:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778076529; cv=none; b=BYnEeX9sjvksmQrSiHeuRW96G3ywSUUYI4LL7ncP6hR5g+uHZNgARCd1Pe1KsGLSWzpwAw6jOzxy4g5mSV2iFAYBQO9R0aR9o7fh4qu7WOiCuRULtr98FFmdfSOotVIwrvPsTJCJglKmIPcG2wEcgFnf9lNkThcJsIc+Ve2CHd4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778076529; c=relaxed/simple; bh=+PnoWBfKnfV07TYFgJMKkg0KM9nPZ2plTh6bMfU0YFo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IrlNv7ozehP1IgXZ2HajXUGU0Ob5OL86KgVMrAFdOjidIS//AV433yffz89WSA8+Spw4PkzCrXg5SD2LKFS/cPLttU+7JJUglBtyAuTpIHIHt/Gkh48iGVSXA0Cfgyg8hfWtDiSYNt9M4xLj95/PKcghdqe7rMweURavyFJn9Ls= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YSGVjZn6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YSGVjZn6" Received: by smtp.kernel.org (Postfix) with ESMTPS id BD762C2BCC9; Wed, 6 May 2026 14:08:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778076529; bh=+PnoWBfKnfV07TYFgJMKkg0KM9nPZ2plTh6bMfU0YFo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=YSGVjZn6ngxkEzdVw1mme7i1OhkvW0h6puX+63AQRzronM1KubbLDNOOtkS1njI5u +99wysXmcczYgfprJxoCMs5Zs37w30a/duhD/RGx1w/nZnD3zv23t24pHiarc17Rfh 7rQimxB+v0OX4EHE/Nj8MqHR94UNN3Fu8hQ8VKhXcYja2jVWuY63Rt3Qxl04aAYkUq c2pWuN+xSJxfjhC47+x9qZ+WxfAnGglYWFKsPNt1CDPJceDHdm3p0bBWH6BuVMRmen l2xUwDkajD2a3WLSAChCvk6I+Pq2Ft37E0TNL5Riz89okUZgO7BAi7wYMXBTcL6QjQ AgRqyUXbYmSDQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE37DCD3440; Wed, 6 May 2026 14:08:49 +0000 (UTC) From: Rodrigo Alencar via B4 Relay Date: Wed, 06 May 2026 15:08:48 +0100 Subject: [PATCH v11 04/11] lib: math: div64: add div64_s64_rem() Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260506-adf41513-iio-driver-v11-4-2b7e99cfe8f2@analog.com> References: <20260506-adf41513-iio-driver-v11-0-2b7e99cfe8f2@analog.com> In-Reply-To: <20260506-adf41513-iio-driver-v11-0-2b7e99cfe8f2@analog.com> To: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-doc@vger.kernel.org Cc: Jonathan Cameron , David Lechner , Andy Shevchenko , Lars-Peter Clausen , Michael Hennerich , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , Andrew Morton , Petr Mladek , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Shuah Khan , Rodrigo Alencar X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1778076527; l=2261; i=rodrigo.alencar@analog.com; s=default; h=from:subject:message-id; bh=2v2lygWWRh+YYmi1idrGFbtPNUJ9kh0HFK3mhrgB4es=; b=Iibgqma3FjFih4ZHQPeGQob1ph7EEEmZ6D2J+E/hnphUWl3AJWnE6oStcm7RnRYUsg/Kvnm2J 293XOOo/VsJAix8k9Qbc545JRhiL/5OTcANfcFJ3fMOZojdwHbzF39p X-Developer-Key: i=rodrigo.alencar@analog.com; a=ed25519; pk=ULeHbgU/OYh/PG/4anHDfLgldFItQHAhOktYRVLMFRo= X-Endpoint-Received: by B4 Relay for rodrigo.alencar@analog.com/default with auth_id=561 X-Original-From: Rodrigo Alencar Reply-To: rodrigo.alencar@analog.com From: Rodrigo Alencar Add div64_s64_rem() function, with 32-bit implementation that uses div64_u64_rem() and a branchless approach to resolve the sign of the remainder and quotient (negation in two's complement). Signed-off-by: Rodrigo Alencar --- include/linux/math64.h | 18 ++++++++++++++++++ lib/math/div64.c | 15 +++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/linux/math64.h b/include/linux/math64.h index cc305206d89f..99189410d4bb 100644 --- a/include/linux/math64.h +++ b/include/linux/math64.h @@ -57,6 +57,20 @@ static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder) return dividend / divisor; } +/** + * div64_s64_rem - signed 64bit divide with 64bit divisor and remainder + * @dividend: signed 64bit dividend + * @divisor: signed 64bit divisor + * @remainder: pointer to signed 64bit remainder + * + * Return: sets ``*remainder``, then returns dividend / divisor + */ +static inline s64 div64_s64_rem(s64 dividend, s64 divisor, s64 *remainder) +{ + *remainder = dividend % divisor; + return dividend / divisor; +} + /** * div64_u64 - unsigned 64bit divide with 64bit divisor * @dividend: unsigned 64bit dividend @@ -102,6 +116,10 @@ extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder); extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder); #endif +#ifndef div64_s64_rem +extern s64 div64_s64_rem(s64 dividend, s64 divisor, s64 *remainder); +#endif + #ifndef div64_u64 extern u64 div64_u64(u64 dividend, u64 divisor); #endif diff --git a/lib/math/div64.c b/lib/math/div64.c index d1e92ea24fce..0b10ded09a9b 100644 --- a/lib/math/div64.c +++ b/lib/math/div64.c @@ -158,6 +158,21 @@ u64 div64_u64(u64 dividend, u64 divisor) EXPORT_SYMBOL(div64_u64); #endif +#ifndef div64_s64_rem +s64 div64_s64_rem(s64 dividend, s64 divisor, s64 *remainder) +{ + s64 quot, t, rem; + + quot = div64_u64_rem(abs(dividend), abs(divisor), (u64 *)&rem); + t = dividend >> 63; + *remainder = (rem ^ t) - t; + t = (dividend ^ divisor) >> 63; + + return (quot ^ t) - t; +} +EXPORT_SYMBOL(div64_s64_rem); +#endif + #ifndef div64_s64 s64 div64_s64(s64 dividend, s64 divisor) { -- 2.43.0