Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752469AbcLFKae (ORCPT ); Tue, 6 Dec 2016 05:30:34 -0500 Received: from smtpoutz29.laposte.net ([194.117.213.104]:34070 "EHLO smtp.laposte.net" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751166AbcLFKac (ORCPT ); Tue, 6 Dec 2016 05:30:32 -0500 From: Sebastian Frias Subject: [PATCH v3] add equivalent of BIT(x) for bitfields To: zijun_hu , Andrew Morton , linux-kernel@vger.kernel.org, Linus Torvalds Cc: Mason , Harvey Harrison , Borislav Petkov Message-ID: Date: Tue, 6 Dec 2016 11:06:42 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-VR-SrcIP: 92.154.11.170 X-VR-FullState: 0 X-VR-Score: -100 X-VR-Cause-1: gggruggvucftvghtrhhoucdtuddrfeelfedrhedtgddtvdcutefuodetggdotefrodftvfcurfhrohhf X-VR-Cause-2: ihhlvgemucfntefrqffuvffgnecuuegrihhlohhuthemucehtddtnecusecvtfgvtghiphhivghnthhs X-VR-Cause-3: ucdlqddutddtmdenucfjughrpefhuffvkffffgggtgfgsehtjeertddtfeejnecuhfhrohhmpefuvggs X-VR-Cause-4: rghsthhirghnucfhrhhirghsuceoshhfkeegsehlrghpohhsthgvrdhnvghtqeenucffohhmrghinhep X-VR-Cause-5: mhgrrhgtrdhinhhfohenucfkphepledvrdduheegrdduuddrudejtdenucfrrghrrghmpehmohguvgep X-VR-Cause-6: shhmthhpohhuthdphhgvlhhopegludejvddrvdejrddtrddvudegngdpihhnvghtpeelvddrudehgedr X-VR-Cause-7: uddurddujedtpdhmrghilhhfrhhomhepshhfkeegsehlrghpohhsthgvrdhnvghtpdhrtghpthhtohep X-VR-Cause-8: iihijhhunhgphhhusehhthgtrdgtohhm X-VR-AvState: No X-VR-State: 0 X-VR-State: 0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2191 Lines: 72 Introduce GENVALUE(msb, lsb, value) macro to ease dealing with continuous bitfields, just as BIT(x) does for single bits. GENVALUE_ULL(msb, lsb, value) macro is also added. This is useful mostly for creating values to be packed together via OR operations, ex: u32 val = 0x11110000; val |= GENVALUE(19, 12, 0x5a); now 'val = 0x1115a000' Signed-off-by: Sebastian Frias Link: https://marc.info/?l=linux-kernel&m=148094498711000&w=2 --- Change in v2: - rename the macro to GENVALUE as proposed by Linus - longer comment attempts to show use case for the macro as proposed by Borislav Change in v3: - use BUILD_BUG_ON_ZERO() to break if some input parameters (essentially 'lsb' but also 'msb') are not constants as proposed by Linus. Indeed, 'lsb' is used twice so it cannot have side-effects; 'msb' is subjected to same constraints for consistency. --- include/linux/bitops.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index a83c822..419529a0 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -24,6 +24,31 @@ #define GENMASK_ULL(h, l) \ (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) +#ifdef __KERNEL__ +/* + * Equivalent of BIT(x) but for contiguous bitfields + * GENVALUE(1, 0,0xff) = 0x00000003 + * GENVALUE(3, 0,0xff) = 0x0000000f + * GENVALUE(15,8,0xff) = 0x0000ff00 + * GENVALUE(6, 6, 1) = 0x00000040 == BIT(6) + */ +#define GENVALUE(msb, lsb, val) \ + ( \ + /* BUILD_BUG_ON_ZERO() evaluates to 0 */ \ + BUILD_BUG_ON_ZERO(!__builtin_constant_p(msb)) | \ + BUILD_BUG_ON_ZERO(!__builtin_constant_p(lsb)) | \ + (((val) << (lsb)) & (GENMASK((msb), (lsb)))) \ + ) + +#define GENVALUE_ULL(msb, lsb, val) \ + ( \ + /* BUILD_BUG_ON_ZERO() evaluates to 0 */ \ + BUILD_BUG_ON_ZERO(!__builtin_constant_p(msb)) | \ + BUILD_BUG_ON_ZERO(!__builtin_constant_p(lsb)) | \ + (((val) << (lsb)) & (GENMASK_ULL((msb), (lsb)))) \ + ) +#endif + extern unsigned int __sw_hweight8(unsigned int w); extern unsigned int __sw_hweight16(unsigned int w); extern unsigned int __sw_hweight32(unsigned int w); -- 1.8.3.1