Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1536838
| Path | csiph.com!news.redatomik.org!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!news.panservice.it!bofh.it!news.nic.it!robomod |
|---|---|
| From | Sebastian Frias <sf84@laposte.net> |
| Newsgroups | linux.kernel |
| Subject | [PATCH v3] add equivalent of BIT(x) for bitfields |
| Date | Tue, 06 Dec 2016 11:40:01 +0100 |
| Message-ID | <sLlkd-Rj-7@gated-at.bofh.it> (permalink) |
| Dkim-Signature | v=1; a=rsa-sha256; c=simple/simple; d=laposte.net; s=mail0; t=1481018803; bh=hEfPfV9VLBkbWulOaWZEMa4ivueIno+IYzYUQK/LVG0=; h=From:Subject:To:Cc:Date; b=JDtF6Q8AhFcl/f2n3Hmm+7vusz9QhqiAYXhDl7aqIg6jXrjBE427rUGAHOiLKgP4f t0rwykPjzqzt7Duu5EYa6+9LaV0Rxo4S6KHOcuC71l5wnqhmh79BO4z7bJvVl2PWJy H6j2XZg8OaMT2ZSGf3uXjtJ9y3VT0gEyCH7m45SFKBny475lW8kpraPCPTFMNuQObN 4jXEGdarwqv7fotvCAh5LjzPZVRHWlEZV9x/ApGxRTPrIwwzvxb5kuv0Zce3FgGnoH XrGhauGtoPWskJofupiAICh7lB0GhTRQE/KzvP29yKNF7XaYcSj7lSwwAy8z/51qiu YCyuNid80RDMw== |
| 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-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 |
| Sender | robomod@news.nic.it |
| List-ID | <linux-kernel.vger.kernel.org> |
| X-Mailing-List | linux-kernel@vger.kernel.org |
| Approved | robomod@news.nic.it |
| Lines | 72 |
| Organization | linux.* mail to news gateway |
| X-Original-Cc | Mason <slash.tmp@free.fr>, Harvey Harrison <harvey.harrison@gmail.com>, Borislav Petkov <bp@alien8.de> |
| X-Original-Date | Tue, 6 Dec 2016 11:06:42 +0100 |
| X-Original-Message-ID | <daba3c13-0344-7bca-aa05-91ec9c7946ae@laposte.net> |
| X-Original-Sender | linux-kernel-owner@vger.kernel.org |
| Xref | csiph.com linux.kernel:1536838 |
Show key headers only | View raw
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 <sf84@laposte.net> 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
Back to linux.kernel | Previous | Next — Next in thread | Find similar | Unroll thread
[PATCH v3] add equivalent of BIT(x) for bitfields Sebastian Frias <sf84@laposte.net> - 2016-12-06 11:40 +0100
Re: [PATCH v3] add equivalent of BIT(x) for bitfields Kalle Valo <kvalo@codeaurora.org> - 2016-12-07 09:50 +0100
Re: [PATCH v3] add equivalent of BIT(x) for bitfields Sebastian Frias <sf84@laposte.net> - 2016-12-07 11:10 +0100
Re: [PATCH v3] add equivalent of BIT(x) for bitfields Jakub Kicinski <jakub.kicinski@netronome.com> - 2016-12-07 12:10 +0100
Re: [PATCH v3] add equivalent of BIT(x) for bitfields Sebastian Frias <sf84@laposte.net> - 2016-12-07 12:30 +0100
Re: [PATCH v3] add equivalent of BIT(x) for bitfields Jakub Kicinski <jakub.kicinski@netronome.com> - 2016-12-07 13:40 +0100
Re: [PATCH v3] add equivalent of BIT(x) for bitfields Sebastian Frias <sf84@laposte.net> - 2016-12-07 15:00 +0100
Re: [PATCH v3] add equivalent of BIT(x) for bitfields Jakub Kicinski <jakub.kicinski@netronome.com> - 2016-12-07 15:10 +0100
Re: [PATCH v3] add equivalent of BIT(x) for bitfields Sebastian Frias <sf84@laposte.net> - 2016-12-07 15:40 +0100
csiph-web