From patchwork Tue Jan 7 09:01:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 13928489 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 639A2E7719B for ; Tue, 7 Jan 2025 09:04:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=mTuP9yWLvgTX7AWXsr3gNvehLx4V/bwQi8Z2u3/J/IM=; b=1GoDWVuvVDIYDD MMyqPw85E37kRbeybU+/xDj9Fl/N5d/JIHfHTbvmNjTDp7F1zMKV6m9CrYFv7UQq5cBt2YG3Rjoja F8tIJWyMx/sOgG1ay4zNaDhiHIrOazskj6iBuK6Hn43nVaTgu0c8mXiXG3YsqvFms37d2w3cxGofv StxQIipBtXdqgfgLFcV5NkVZD+lB6YDeSWCV8tM0UCHue7IBDvr6k0XUQgo2MHEKJ7OplUrwN+LR9 NVkYuQCceOyKjCNduqCudOu/ahGOdcbYbMdcM/zkERuoORrL8/IRW0W7EVC1pMdgaspkGwzImssXk 9PWLISF9wJHPDyir9Pug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tV5Vx-0000000429m-0iBH; Tue, 07 Jan 2025 09:04:41 +0000 Received: from zeus03.de ([194.117.254.33] helo=mail.zeus03.de) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tV5Tb-000000041Ja-1b1u for linux-i3c@lists.infradead.org; Tue, 07 Jan 2025 09:02:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= sang-engineering.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; s=k1; bh=/HzJlxcDzDQ4BvZyB5ZfxXV6i05giy4HK5Wtt3ukFzE=; b=PJbMEm FeBJDnwLQstpDw9GMUXk+6Y0U1d7YWzOC+mZ7YSE8aQwze6sLLhzOhOjYpO+dwox cznTO0/eLFRIsFEwSa66lfC3UVAcoxmY2dvjsof5AsTa79SBRVJPbAVfpxdRQST7 xuFTkv7naViu9gsj6XP78wqzZSyiwqrOiLO4pU8oZjKEAuVVoRBuAaafJljbHqgW 73KpszNJ9VEGq45Hi1uVFtBJYUC908ei9tM5DPG9Cesm/SxT/+H45tECbSqStYAD kZjNqEK9cnDy17Q1/kg5na6rgXd08DSTBUMmjMQO/iD3p0pY1AAhKyvWrG11ZyhB yY4Oc8vjA3rUVNgg== Received: (qmail 2125225 invoked from network); 7 Jan 2025 10:02:07 +0100 Received: by mail.zeus03.de with ESMTPSA (TLS_AES_256_GCM_SHA384 encrypted, authenticated); 7 Jan 2025 10:02:07 +0100 X-UD-Smtp-Session: l3s3148p1@PhXi/xkrTpMgAwDPXw20AOMQ2KO98fSH From: Wolfram Sang To: linux-i3c@lists.infradead.org Cc: linux-kernel@vger.kernel.org, linux-renesas-soc@vger.kernel.org, Wolfram Sang , Guenter Roeck , Geert Uytterhoeven , Yury Norov , Kuan-Wei Chiu , Rasmus Villemoes Subject: [PATCH v4 1/5] bitops: add generic parity calculation for u8 Date: Tue, 7 Jan 2025 10:01:59 +0100 Message-ID: <20250107090204.6593-2-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250107090204.6593-1-wsa+renesas@sang-engineering.com> References: <20250107090204.6593-1-wsa+renesas@sang-engineering.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250107_010216_289168_12828457 X-CRM114-Status: GOOD ( 15.38 ) X-BeenThere: linux-i3c@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-i3c" Errors-To: linux-i3c-bounces+linux-i3c=archiver.kernel.org@lists.infradead.org There are multiple open coded implementations for getting the parity of a byte in the kernel, even using different approaches. Take the pretty efficient version from SPD5118 driver and make it generally available by putting it into the bitops header. As long as there is just one parity calculation helper, the creation of a distinct 'parity.h' header was discarded. Also, the usage of hweight8() for architectures having a popcnt instruction is postponed until a use case within hot paths is desired. The motivation for this patch is the frequent use of odd parity in the I3C specification and to simplify drivers there. Changes compared to the original SPD5118 version are the addition of kernel documentation, switching the return type from bool to int, and renaming the argument of the function. Signed-off-by: Wolfram Sang Tested-by: Guenter Roeck Reviewed-by: Geert Uytterhoeven Acked-by: Yury Norov Reviewed-by: Kuan-Wei Chiu Tested-by: Kuan-Wei Chiu --- include/linux/bitops.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index ba35bbf07798..c1cb53cf2f0f 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -229,6 +229,37 @@ static inline int get_count_order_long(unsigned long l) return (int)fls_long(--l); } +/** + * parity8 - get the parity of an u8 value + * @value: the value to be examined + * + * Determine the parity of the u8 argument. + * + * Returns: + * 0 for even parity, 1 for odd parity + * + * Note: This function informs you about the current parity. Example to bail + * out when parity is odd: + * + * if (parity8(val) == 1) + * return -EBADMSG; + * + * If you need to calculate a parity bit, you need to draw the conclusion from + * this result yourself. Example to enforce odd parity, parity bit is bit 7: + * + * if (parity8(val) == 0) + * val ^= BIT(7); + */ +static inline int parity8(u8 val) +{ + /* + * One explanation of this algorithm: + * https://funloop.org/codex/problem/parity/README.html + */ + val ^= val >> 4; + return (0x6996 >> (val & 0xf)) & 1; +} + /** * __ffs64 - find first set bit in a 64 bit word * @word: The 64 bit word