From patchwork Fri Oct 12 19:45:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 10639229 X-Patchwork-Delegate: johannes@sipsolutions.net Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E2A1513AD for ; Fri, 12 Oct 2018 19:46:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D086E2C5A5 for ; Fri, 12 Oct 2018 19:46:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C369F2C5B6; Fri, 12 Oct 2018 19:46:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4B62C2C5A5 for ; Fri, 12 Oct 2018 19:46:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726647AbeJMDUQ (ORCPT ); Fri, 12 Oct 2018 23:20:16 -0400 Received: from s3.sipsolutions.net ([144.76.43.62]:53914 "EHLO sipsolutions.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726054AbeJMDUQ (ORCPT ); Fri, 12 Oct 2018 23:20:16 -0400 Received: by sipsolutions.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.91) (envelope-from ) id 1gB3Nz-0001Dn-68; Fri, 12 Oct 2018 21:46:11 +0200 From: Johannes Berg To: linux-wireless@vger.kernel.org Cc: linux-kernel@vger.kernel.org, John Garry , nbd@nbd.name, Johannes Berg Subject: [PATCH] bitfield: add constant field preparation macros Date: Fri, 12 Oct 2018 21:45:56 +0200 Message-Id: <20181012194556.31004-1-johannes@sipsolutions.net> X-Mailer: git-send-email 2.17.2 Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Johannes Berg John Garry requested to be able to use FIELD_PREP() and friends in constant initializers, but we cannot completely switch all of the current assertions to BUILD_BUG_ON_ZERO(). So instead of this, add __FIELD_PREP() which is suitable in such contexts, and also add __{u,le,be}{16,32,64}encode_bits() like the existing versions without underscores, but again suitable in constant contexts. Requested-by: John Garry Signed-off-by: Johannes Berg --- include/linux/bitfield.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 3f1ef4450a7c..245dfb47d201 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -63,6 +63,14 @@ (1ULL << __bf_shf(_mask))); \ }) +#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0) + +#define __BF_FIELD_CHECK_CONST(_mask, _reg, _val) \ + (BUILD_BUG_ON_ZERO((_mask) == 0) + \ + BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \ + BUILD_BUG_ON_ZERO((_mask) > (typeof(_reg))~0ull) + \ + __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask)))) + /** * FIELD_FIT() - check if value fits in the field * @_mask: shifted mask defining the field's length and position @@ -90,6 +98,21 @@ ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \ }) +/** + * __FIELD_PREP() - prepare a constant bitfield element + * @_mask: shifted mask defining the field's length and position + * @_val: value to put in the field + * + * __FIELD_PREP() masks and shifts up the value. The result should + * be combined with other fields of the bitfield using local OR. + * + * This version is suitable for use in a pure constant context, e.g. + * a constant initializer. + */ +#define __FIELD_PREP(_mask, _val) \ + ((typeof(_mask))__BF_FIELD_CHECK_CONST(_mask, 0ULL, _val) + \ + (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))) + /** * FIELD_GET() - extract a bitfield element * @_mask: shifted mask defining the field's length and position @@ -150,4 +173,15 @@ __MAKE_OP(64) #undef __MAKE_OP #undef ____MAKE_OP +#define __encode_bits(w, v, field) __FIELD_PREP((u##w)(field), v) +#define __u16_encode_bits(v, field) __encode_bits(16, v, field) +#define __le16_encode_bits(v, field) cpu_to_le16(__encode_bits(16, v, field)) +#define __be16_encode_bits(v, field) cpu_to_be16(__encode_bits(16, v, field)) +#define __u32_encode_bits(v, field) __encode_bits(32, v, field) +#define __le32_encode_bits(v, field) cpu_to_le32(__encode_bits(32, v, field)) +#define __be32_encode_bits(v, field) cpu_to_be32(__encode_bits(32, v, field)) +#define __u64_encode_bits(v, field) __encode_bits(64, v, field) +#define __le64_encode_bits(v, field) cpu_to_le64(__encode_bits(64, v, field)) +#define __be64_encode_bits(v, field) cpu_to_be64(__encode_bits(64, v, field)) + #endif