From patchwork Fri Oct 12 09:21:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 10638153 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 7554F933 for ; Fri, 12 Oct 2018 09:21:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 617692B75D for ; Fri, 12 Oct 2018 09:21:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 51A092BBDB; Fri, 12 Oct 2018 09:21:26 +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 D50862B75D for ; Fri, 12 Oct 2018 09:21:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728228AbeJLQwu (ORCPT ); Fri, 12 Oct 2018 12:52:50 -0400 Received: from s3.sipsolutions.net ([144.76.43.62]:46068 "EHLO sipsolutions.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728222AbeJLQwt (ORCPT ); Fri, 12 Oct 2018 12:52:49 -0400 Received: by sipsolutions.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.91) (envelope-from ) id 1gAtdF-0008Dj-Si; Fri, 12 Oct 2018 11:21:18 +0200 From: Johannes Berg To: linux-wireless@vger.kernel.org Cc: linux-kernel@vger.kernel.org, John Garry , Johannes Berg Subject: [PATCH] bitfield: use BUILD_BUG_ON_ZERO() Date: Fri, 12 Oct 2018 11:21:04 +0200 Message-Id: <20181012092104.2361-1-johannes@sipsolutions.net> X-Mailer: git-send-email 2.14.4 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, so the thing we can do is switch all the compile-time assertions to use BUILD_BUG_ON_ZERO() instead of regular BUILD_BUG_ON(). The only downside is that we lose all the error messages, but it doesn't seem worthwhile to duplicate all of this into e.g. CONST_FIELD_PREP() (for constants) just for that. Requested-by: John Garry Signed-off-by: Johannes Berg --- include/linux/bitfield.h | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 3f1ef4450a7c..1840dae6448a 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -49,19 +49,15 @@ #define __bf_shf(x) (__builtin_ffsll(x) - 1) -#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \ - ({ \ - BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \ - _pfx "mask is not constant"); \ - BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero"); \ - BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \ - ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \ - _pfx "value too large for the field"); \ - BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \ - _pfx "type of reg too small for mask"); \ - __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \ - (1ULL << __bf_shf(_mask))); \ - }) +#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) != 0) + +#define __BF_FIELD_CHECK(_mask, _reg, _val) \ + BUILD_BUG_ON_ZERO(!__builtin_constant_p(_mask)) + \ + BUILD_BUG_ON_ZERO((_mask) == 0) + \ + BUILD_BUG_ON_ZERO(__builtin_constant_p(_val) ? \ + ~((_mask) >> __bf_shf(_mask)) & (_val) : 0) + \ + 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 @@ -71,10 +67,8 @@ * Return: true if @_val can fit inside @_mask, false if @_val is too big. */ #define FIELD_FIT(_mask, _val) \ - ({ \ - __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: "); \ - !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \ - }) + (__BF_FIELD_CHECK(_mask, 0ULL, _val) + \ + !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask))) /** * FIELD_PREP() - prepare a bitfield element @@ -85,10 +79,8 @@ * be combined with other fields of the bitfield using logical OR. */ #define FIELD_PREP(_mask, _val) \ - ({ \ - __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \ - ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \ - }) + (__BF_FIELD_CHECK(_mask, 0ULL, _val) + \ + (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask))) /** * FIELD_GET() - extract a bitfield element @@ -99,10 +91,8 @@ * bitfield passed in as @_reg by masking and shifting it down. */ #define FIELD_GET(_mask, _reg) \ - ({ \ - __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \ - (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \ - }) + (__BF_FIELD_CHECK(_mask, _reg, 0U) + \ + ((typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)))) extern void __compiletime_error("value doesn't fit into mask") __field_overflow(void);