From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B79138D688 for ; Thu, 2 Apr 2026 09:27:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775122050; cv=none; b=lWAlyzSJ4EWvQYoyHORUSv6wBkLHU5wL0ctpCRCRqilGvLFlVnYpn6BNc6FVVUX2cNW17vMGz/wgV+wzgQkH1q/iWU+bp3bv8271iEnKCuezKW1jCjJUurl6Hola/q7R0nlQYgG6eJxLDStwqGofvyebbUJDElL/VJPNUifXIDU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775122050; c=relaxed/simple; bh=vGu0j1BWdrTbhRQ4Oaw/kZt4VRjMGk3aihw1/eBe/b8=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=i5RRuHbKnK/h2FqYT6FjoYGtTZdCDMTHsJG5ajpKjUzSeZMQ9yl7fp84mL0myKzvcmIXMyvS2n+KdqPLY4ZIYRWbMwR4EGc+7hsE2bcW1vPzr3lARYDAuTggD2FsCvvc0uWV5f/EBelhzj0q1aqBXk57cUXs+szvtC+UUx5kIyA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bkh6fBFa; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bkh6fBFa" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-48700b1ba53so5791455e9.1 for ; Thu, 02 Apr 2026 02:27:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775122040; x=1775726840; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=6YINHRsfnSuEmSggTqij5TiRWncArUtra2UoEEP8ANg=; b=bkh6fBFa885MMREHG+4D8eUs1K/Q1RHpjoisKPctszwxao0trO+Xyrdq+pCoEkEU5/ 1b6t+jW1lw9wUAULyfh/a9NH+s8lxbYeon4siIddgiKFQr/KfUpwAYfEakz0xyj79M81 HI4DVZSYvHp4tcHabTeVOUbBiKcBNAcwvA5NSvhLvkitk4enqGgLoglwFuFDfyGBInxZ N4GWnN2ORpUe8WyA6aWD4fQ6YEm80FJeqnzqzKsNp+TnNM7q8wuPFn5A8bK4GFzQzt8a F3Zz87xwY9gGqYHlV86fB7j5P7+bA9PZ+go9reaKNTm3i9frRhCHzxiaSsj5MMdgP+gp Fflw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775122040; x=1775726840; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=6YINHRsfnSuEmSggTqij5TiRWncArUtra2UoEEP8ANg=; b=WqEULvstj/DUw2YbfYDi64aGYNpP1eyG3aEf/BEyrcydBXq/8HHAhCeajgFmZbyH/i 121wBgPOV2Pb9ugd8N59Rs4Hb25aXZtmMF30XZ4r+7mQ+ijPPOsinLQUUbuKOoW8nUgS xf3xbYwTjWEGJv0hTkY5h8LzS/w8m9WQSrm1H2+z6fd714/y5s/4s/TEKvh6/1O9SS+B 58czzjratZS8S5VSL76+gsvG9gU2kpnh3/ibfPBE7HH3JZImBDok9SL83ZXmxEqTKhuB TzQ5wQn6eW4yPycNRaPnakk26RDBVM0dXJEBLH8DHGLlcfdW7AZpMOC9y2sNYe0wyD01 6CPg== X-Forwarded-Encrypted: i=1; AJvYcCW51NPhPOMMS7IGZi2iPXNPxc4W2ngk/+IvLF1AyyelD5V1cDpib7NjsMIaXYDYRbM2mYsx86oklhNss2o=@vger.kernel.org X-Gm-Message-State: AOJu0YxF3psNe/jmA69ijqx9n8AHUNPbkz/YBtUXZiYo9vsXGID9UYwA fUlFk4XRgkqrd4b2L9iwQxpcMNL4EOuU//AS24UDvNfg9soKI9Vj2KRN X-Gm-Gg: ATEYQzzDPzO2Hra53/f3MqVMB+pFnHgPtokwv3dqQZQepVaEVfqxmt9ue7ENd8Oo3EE d+G88JhnGh9/GycqN/jYid40eM+/wXxhY2kAWxhT0GWucgSRiJqNVIxDE3/7eWThzlum48v06sK HlGAVrcv5i9HQAvpcGsLvR1Wgsfzo3lXmRR5PUWIU242bomp0og57yE8Tz441toL9SbICjyonR1 dEGjwPp5pjAcwjOOrSKX8g/2r5JhjZc2uKU94IQlHbCXSoB9qE/0zQPDBU0KTeCjYTKqmuxF/kh ovCWri9VgHDP7Pr4Bl6OtpahKXIahOFv3QdJSMqfS1OOwr1UDOv9YQ03ved5smw2noeULQrYh5q nF/vdmKMLCG4RtQuKSIaPakuuSWbGlsCEDW6FxK3osy6O+QXy1/b2/9gcVTw/AhiUpNvDYorcK3 vPaaXnopivNv+lVNiUQI7MHeuTyFI2qkaHuoHHOAIJBxawkJ+hoAVsTcVmyp9s X-Received: by 2002:a05:600c:1f11:b0:485:2a85:e5ec with SMTP id 5b1f17b1804b1-48883568b02mr113620225e9.2.1775122039987; Thu, 02 Apr 2026 02:27:19 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4887adadf4fsm63280675e9.11.2026.04.02.02.27.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Apr 2026 02:27:19 -0700 (PDT) Date: Thu, 2 Apr 2026 10:27:17 +0100 From: David Laight To: Luiz Angelo Daros de Luca Cc: Yury Norov , Andrew Lunn , Vladimir Oltean , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Linus Walleij , Alvin =?UTF-8?B?xaBpcHJhZ2E=?= , Yury Norov , Rasmus Villemoes , Russell King , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH() Message-ID: <20260402102717.5eb48393@pumpkin> In-Reply-To: References: <20260331-realtek_forward-v1-0-44fb63033b7e@gmail.com> <20260331-realtek_forward-v1-4-44fb63033b7e@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 2 Apr 2026 01:00:20 -0300 Luiz Angelo Daros de Luca wrote: > > > +/** > > > + * FIELD_WIDTH() - return the width of a bitfield > > > + * @_mask: shifted mask defining the field's length and position > > > + * > > > + * Returns the number of contiguous bits covered by @_mask. > > > + * This corresponds to the bit width of FIELD_MAX(@_mask). > > > + */ > > > +#define FIELD_WIDTH(_mask) \ > > > > Please no underscored names unless necessary. > > I used _mask to maintain consistency with the existing public macros > in this file, such as FIELD_GET, FIELD_PREP, FIELD_MAX, and FIELD_FIT. > All of them use the underscore prefix for parameters. Should I diverge > from them? > > > > + ({ \ > > > + __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \ > > > + __bf_shf(~FIELD_MAX(_mask)); \ > > > + }) > > > > I believe, this should be: > > > > #define FIELD_WIDTH(mask) ({ \ > > __BF_FIELD_CHECK_MASK(mask, 0ULL, "FIELD_WIDTH: "); \ > > HWEIGHT(mask); \ > > }) > > HWEIGHT() is indeed much cleaner. However, to keep bitfield.h > self-contained and avoid adding more includes, I'll try > __builtin_popcountll() in a similar way __builtin_ffsll is already > used. > > I also noticed the suggestion to use __BF_FIELD_CHECK_MASK instead of > __BF_FIELD_CHECK. Both FIELD_MAX and FIELD_FIT currently use the full > __BF_FIELD_CHECK with 0ULL as a dummy register to ensure the mask fits > within the header's supported limits. If __BF_FIELD_CHECK_MASK is > preferred for FIELD_WIDTH, I can certainly use it, but should we also > update FIELD_MAX and FIELD_FIT for consistency? All of the calls with the 0ULL placeholder (especially for the register) should really be removed. Last time I looked there where some calls that only had placeholders. They just bloat the pre-processor output and slow down compilation. > > I intend to send a v2 with the following implementation: > > #define __bf_shf(x) (__builtin_ffsll(x) - 1) > +#define __bf_hweight(x) __builtin_popcountll(x) > > #define __scalar_type_to_unsigned_cases(type) \ > unsigned type: (unsigned type)0, \ > @@ -111,6 +112,19 @@ > (typeof(_mask))((_mask) >> __bf_shf(_mask)); \ > }) > > +/** > + * FIELD_WIDTH() - return the width of a bitfield > + * @_mask: shifted mask defining the field's length and position > + * > + * Returns the number of contiguous bits covered by @_mask. > + * This corresponds to the bit width of FIELD_MAX(@_mask). > + */ > +#define FIELD_WIDTH(_mask) \ > + ({ \ You ought to have: auto _fw_mask = mask; here. While _mask has to be a constant, if it comes from GENMASK() it is very long. > + __BF_FIELD_CHECK_MASK(_mask, 0ULL, "FIELD_WIDTH: "); \ > + (typeof(_mask))__bf_hweight(_mask); \ Why the cast of the result? They are everywhere in that file, and many are pointless. But there is no point adding another one. I'm not even sure you need the extra define. Just use __builtin_popcountll(). David > + }) > + > > Regards, > > Luiz >