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 6A5BB3FAE11; Thu, 4 Jun 2026 09:59:43 +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=1780567183; cv=none; b=Y3zRsgbVbshc0i4urjUsV8j2X7rAWWDw239JkYLfzdQvTjghDII51Ygxeca0XGuVpXQPnrRMPhrwl/Nb2qSMRNiSjFWiD1geSDWMIsh+up1QzufN8d7xFGrAx+9TpgMISRolSZ9flzQPkrOraFdcOloMsCCoY4TAyQAIkgddUwM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780567183; c=relaxed/simple; bh=0MiPE4pAGNIHkDMT5fwtfqtbshqWVCMkNClPirEcpH4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cpnzG6tAK+lyWKOBbMPJTRLiD3+laOVTYLuWVHaOJLRH7zcBKMTJ6aeqoCkMFNVRxps790TEAOunulGQFcgyswZSs1r807KkvEi9Fpct1OhbV4FTq/zVzssmP/G8zjXTe82z38XjA2QpXuQFlQWvRTu35SDiOLrpqwvyJAWQK5U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rRshlagP; 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="rRshlagP" Received: by smtp.kernel.org (Postfix) with ESMTPS id 48996C2BCF4; Thu, 4 Jun 2026 09:59:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1780567183; bh=0MiPE4pAGNIHkDMT5fwtfqtbshqWVCMkNClPirEcpH4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=rRshlagPsulHVf97XLIxiHfVAsSBqUrxpi1vH31IsgBCt8qswGjm3AiFwS4acw0aZ o4t2MEMubi9/+jnK9u+qV0is/4pl+L5yHrdoU7ZnK2UaU5g6qcFWv2aYHO9jSiVqTe qHmTafdIKmxiRNpGzLJ4Pcgt/4p1IarfbUGdJ1Fquqox21XHVGTFtsa/Z6MRhZhL/O XLe8ok9fWMGJUNvh+13a8NqnxdcTJT5hQ3VLghYFlmtYoT/yW2Tm8oZfORkc2xalk6 lqIyNAUbIqJwISwprqv++CNNmDnSIcj+i+lgTzwhunwheYFhIIkQlwv+Y7Q/wogsGQ NwrZ1MbPqLC3g== 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 3F496CD6E6B; Thu, 4 Jun 2026 09:59:43 +0000 (UTC) From: Rodrigo Alencar via B4 Relay Date: Thu, 04 Jun 2026 10:59:01 +0100 Subject: [PATCH v16 07/14] lib: math: div64: add div64_s64_rem() Precedence: bulk X-Mailing-List: linux-kernel@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: <20260604-adf41513-iio-driver-v16-7-1a7d09143bc2@analog.com> References: <20260604-adf41513-iio-driver-v16-0-1a7d09143bc2@analog.com> In-Reply-To: <20260604-adf41513-iio-driver-v16-0-1a7d09143bc2@analog.com> To: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-doc@vger.kernel.org, linux@analog.com 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=1780567181; l=2327; i=rodrigo.alencar@analog.com; s=default; h=from:subject:message-id; bh=ROKa5iAD0w5bgrXddKrko8IjoFXS2V0nXvPl5DFoZoI=; b=2iEc8P2m9zI2rqyUKhM1ARNBTEtG0NbMD7xgrFgqOEyAemqJqxLvDZBFO78qLgf2fMsmvYOBP ONLU5IzgHIxBB7tyZVc85ZpKMgUP1OyUu07vEez5TY+MRBAMCFRzZ8O 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). Reviewed-by: Andy Shevchenko 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