Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752548AbcLGIer (ORCPT ); Wed, 7 Dec 2016 03:34:47 -0500 Received: from smtp.codeaurora.org ([198.145.29.96]:47734 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751829AbcLGIep (ORCPT ); Wed, 7 Dec 2016 03:34:45 -0500 DMARC-Filter: OpenDMARC Filter v1.3.1 smtp.codeaurora.org 6015E615BC Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=pass smtp.mailfrom=kvalo@codeaurora.org From: Kalle Valo To: Sebastian Frias Cc: zijun_hu , Sasha Levin , Andrew Morton , linux-kernel@vger.kernel.org, Linus Torvalds , Mason , Maxime Coquelin , Harvey Harrison , Borislav Petkov , Jakub Kicinski Subject: Re: [PATCH] bitops: add equivalent of BIT(x) for bitfields References: <196dd443-e3c7-2c37-1dd1-bc1d249ea2fb@laposte.net> Date: Wed, 07 Dec 2016 10:34:39 +0200 In-Reply-To: <196dd443-e3c7-2c37-1dd1-bc1d249ea2fb@laposte.net> (Sebastian Frias's message of "Mon, 5 Dec 2016 14:36:07 +0100") Message-ID: <87fum0m928.fsf@purkki.adurom.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1460 Lines: 50 Sebastian Frias writes: > Introduce SETBITFIELD(msb, lsb, value) macro to ease dealing with > continuous bitfields, just as BIT(x) does for single bits. > > SETBITFIELD_ULL(msb, lsb, value) macro is also added. > > Signed-off-by: Sebastian Frias > --- > > Code protected with "#ifdef __KERNEL__" just as the BIT(x) > macros. > > I would have preferred another name, like BITS(x) but it is > already used. > > Suggestions for other names welcome. > --- > include/linux/bitops.h | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/include/linux/bitops.h b/include/linux/bitops.h > index a83c822..4659237 100644 > --- a/include/linux/bitops.h > +++ b/include/linux/bitops.h > @@ -24,6 +24,20 @@ > #define GENMASK_ULL(h, l) \ > (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) > > +#ifdef __KERNEL__ > +/* > + * Equivalent of BIT(x) but for contiguous bitfields > + * SETBITFIELD(1, 0,0xff) = 0x00000003 > + * SETBITFIELD(3, 0,0xff) = 0x0000000f > + * SETBITFIELD(15,8,0xff) = 0x0000ff00 > + * SETBITFIELD(6, 6, 1) = 0x00000040 == BIT(6) > + */ > +#define SETBITFIELD(msb, lsb, val) \ > + (((val) << (lsb)) & (GENMASK((msb), (lsb)))) > +#define SETBITFIELD_ULL(msb, lsb, val) \ > + (((val) << (lsb)) & (GENMASK_ULL((msb), (lsb)))) > +#endif Pleaes check FIELD_PREP() from include/linux/bitfield.h, doesn't it already do the same? Adding Jakub to comment more. -- Kalle Valo