From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 E750D3A5E6D for ; Tue, 21 Apr 2026 09:27:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776763671; cv=none; b=IErJhGjLb3WKNkZ0y91KTUj+HVbferHWZr+6V95FVLX2NW2gAIk9ym54AJvwZ0mMGgfFTZDgwphHAkFWrAGjueaPqgjNQKSt8VcvZZckwg+qxO0Nga+tBSLVeIeI6hLt1L2wvS2iLjv6jN7BOz00Rti8Bb403GDF4xEybZ6Rjw4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776763671; c=relaxed/simple; bh=NWVPQ/ix6la1A9YnRnDlxRH64UYZgVvj6dWNHNlWfm0=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Rz7SyG5o78ykdI7EkB7h8ayytXopgg+XKyoic0k1jVH+J30X5i/r6EkCHaIcqoPkIYIgPByZs2SByUOBLDoGbViNAFc+YrVsxd1Bt3lf2siaS4Kg2iKWBzcRGlL3iSiepN98x54lhftZHuz7xYawGCNQzVVZ2jEoDr2opPzlbBs= 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=KP0YHrJK; arc=none smtp.client-ip=209.85.221.46 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="KP0YHrJK" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-43d73422431so3426590f8f.2 for ; Tue, 21 Apr 2026 02:27:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776763667; x=1777368467; 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=2SK+aw4ngOkm9hF+ynW9KrB9F3h620ajY5HNLbxZ/XI=; b=KP0YHrJKrICGVmlUQNpi+20Q7UPpVxplJOquT+Zqh5wyKP1DFRAT7kdVdPEmcAtOJU brV2ROn1BQQz1KG0Et0nKatui0DkB9eOcKqCe0j55c/oDql4jqlcVe2INgsEj0oYNzsv v4I5gJbI1rbqnzhgEyyCzaFL957VWDYW4GuiX9CSdJK+KYx8hbbz5XGalDkv4CZ0svzP Us87q4HoGBFt6XaW9f+srHB+DxtWbVlIqdrWBMgH57xVDqxUZVkdyFAxN8W0e9FeM1pw oUlSAwAmh3T6pilJgybq1eLs9wpLgcIqAn6PInRw+GpDo3f1p+g9kBingnGSBOlIcuB3 OEBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776763667; x=1777368467; 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=2SK+aw4ngOkm9hF+ynW9KrB9F3h620ajY5HNLbxZ/XI=; b=CCtVVZrpfwi10bTxrGsBUmt6SmUIjnvTYLAiwcRNt7k0ek2qK34vwSiYpfLvXx6gQ3 CLNTLP7e+i5d18BjCykkpcHnoZi7VQgdOomrPo/smicK9oTkmgZduVLYcfu2d1Cfq2J3 tmcOAik1/jvwmYtKED2CCG/Z8QgsaEB7annRdpaDHXQGkOqP97lEr+Q37H3TYP8smJNz PLWeh9Yc49sFbXIZGkwaY2JFJl0fwzWuIlhXTmaEwBgRRAce6DoMOZNfSTKZFKZcRTRD FZwHcCbDF75HKZd1pOpdTUcSnmQL5skfNdAJPzEf8ckApR86YEVUzr49sFCqhLcWxBSm xHcg== X-Forwarded-Encrypted: i=1; AFNElJ/AkQlIM8wIXHLQPvKM1XwNI5DJR5tovyRdb5uYJ5wbSJAmp4wJsKhrqqnsT/8Wm5dxc5RqIM3GHI985TI=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9lzVs20DNDZFzENcDT0hu2J5+6PTOZLfvNpwbC7CziSCP1ywM hfejKBGlzEYqEFwuxMXrnmFp+Vx1JLblH7cAv4e57Le8iXOhIeFhluGz X-Gm-Gg: AeBDievz9FMy8lAk7uYW/KtyoPADh5twKQWfLWupEfijeV6REaKw6UIfBfE0Z9YiCRm tElPmEjyKyRSG243bYiN0Cj1foZpG+gELqui1UedsHa0907bWsIAFFuQLo+11u/gZ1KtqFGlRu6 YvtAHy4d1TaYBlHIyxe9d1SvPiuR72XHBO/kbLFxThyUjaREySxE/C+Ais0zUFe6KCVN4IP6WXW 3iv6fHEKhIClu5G0Sr32BFc+eqt5SVhOGNJyNAh3nPhlD4yQ0O/7ciSY/xEe4tvS1RtILt5esgI BVtHX+blhfSjnjb/EvCRHlFj7C0STMqEg38L5QbCUgqJQZWdUrh3kQ96o9zQRPBgT70Plg7opQm LBBdX1JfXxPIQbcyUwls/AmwPvysI1WbhmxqFrkS7p/CoOkqyw3dHaBCwBbxB29BbtFdFK+tK2d P5JRnT56peJbvoFpEcCOZK/SGcYJBNN8ABpi7EsMFVYVjU1/piHQHF+t6i8aogs/ev1fPPPyfHl Po+2RfpUkLyJw== X-Received: by 2002:a05:6000:1a89:b0:43d:7125:e79f with SMTP id ffacd0b85a97d-43fe3e25c89mr27107393f8f.47.1776763666796; Tue, 21 Apr 2026 02:27:46 -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 ffacd0b85a97d-43fe4cb1405sm35116597f8f.4.2026.04.21.02.27.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Apr 2026 02:27:46 -0700 (PDT) Date: Tue, 21 Apr 2026 10:27:44 +0100 From: David Laight To: Yury Norov Cc: Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Jonathan Cameron , David Lechner , Nuno =?UTF-8?B?U8Oh?= , Andy Shevchenko , Ping-Ke Shih , Richard Cochran , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Alexandre Belloni , Yury Norov , Rasmus Villemoes , Hans de Goede , Linus Walleij , Sakari Ailus , Salah Triki , Achim Gratz , Ben Collins , linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, linux-wireless@vger.kernel.org, netdev@vger.kernel.org, linux-rtc@vger.kernel.org Subject: Re: [PATCH 1/9] bitfield: add FIELD_GET_SIGNED() Message-ID: <20260421102744.6bd30e50@pumpkin> In-Reply-To: References: <20260417173621.368914-1-ynorov@nvidia.com> <20260417173621.368914-2-ynorov@nvidia.com> <20260420111940.GE3102624@noisy.programming.kicks-ass.net> 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 Mon, 20 Apr 2026 13:54:57 -0400 Yury Norov wrote: > On Mon, Apr 20, 2026 at 01:19:40PM +0200, Peter Zijlstra wrote: > > On Fri, Apr 17, 2026 at 01:36:12PM -0400, Yury Norov wrote: > > > The bitfields are designed in assumption that fields contain unsigned > > > integer values, thus extracting the values from the field implies > > > zero-extending. > > > > > > Some drivers need to sign-extend their fields, and currently do it like: > > > > > > dc_re += sign_extend32(FIELD_GET(0xfff000, tmp), 11); > > > dc_im += sign_extend32(FIELD_GET(0xfff, tmp), 11); > > > > > > It's error-prone because it relies on user to provide the correct > > > index of the most significant bit and proper 32 vs 64 function flavor. > > > > > > Thus, introduce a FIELD_GET_SIGNED() macro, which is the more > > > convenient and compiles (on x86_64) to just a couple instructions: > > > shl and sar. > > > > > > Signed-off-by: Yury Norov > > > --- > > > include/linux/bitfield.h | 16 ++++++++++++++++ > > > 1 file changed, 16 insertions(+) > > > > > > diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h > > > index 54aeeef1f0ec..35ef63972810 100644 > > > --- a/include/linux/bitfield.h > > > +++ b/include/linux/bitfield.h > > > @@ -178,6 +178,22 @@ > > > __FIELD_GET(_mask, _reg, "FIELD_GET: "); \ > > > }) > > > > > > +/** > > > + * FIELD_GET_SIGNED() - extract a signed bitfield element > > > + * @mask: shifted mask defining the field's length and position > > > + * @reg: value of entire bitfield > > > + * > > > + * Returns the sign-extended field specified by @_mask from the > > > + * bitfield passed in as @_reg by masking and shifting it down. > > > + */ > > > +#define FIELD_GET_SIGNED(mask, reg) \ > > > + ({ \ > > > + __BF_FIELD_CHECK(mask, reg, 0U, "FIELD_GET_SIGNED: "); \ > > > + ((__signed_scalar_typeof(mask))((long long)(reg) << \ > > > + __builtin_clzll(mask) >> (__builtin_clzll(mask) + \ > > > + __builtin_ctzll(mask))));\ > > > + }) > > > > IIRC clz is count-leading-zeros and ctz is count-trailing-zeros. Most of > > the other FIELD things use __bf_shf() which is defined in terms of ffs - > > 1 (which is another way of writing ctz). > > > > So how about you start by redefining __bf_shf() in ctz, and then add > > another helper for the clz and write the thing something like: > > > > ((long long)(reg) << __bf_clz(mask)) >> (__bf_clz(mask) + __bf_shf(mask)); > > So... > > I like the shorter form, but whatever we add in the bitfield.h - we'll > have to support it. > > For example, __bf_shf() wasn't intended to be used outsize of the > header, thus double underscored. But there's over 100 external users > now. And to make it worse, it's broken for GCC 14 and earlier: For anyone who hasn't followed the gory details it isn't 'very broken'. Basically __builtin_ffsll() doesn't always generate an 'integer constant expression' from constant input so you can get a compile fail. > https://lore.kernel.org/all/20260409-field-prep-fix-v1-1-f0e9ae64f63c@imgtec.com/ > > So needs to get fixed. > > The bitfield.h has two __bf macros: __bf_shf() and __bf_cast_unsigned(). > They are thin wrappers, __bf_cast_unsigned() isn't exactly thin. David > but after all do something with the corresponding > builtins output. The __bf_cls() would be a pure renaming. I'm OK with > that, but some people don't: > > https://lore.kernel.org/all/20260303182845.250bb2de@kernel.org/ > > That's why I didn't make FIELD_GET_SIGNED() implementation looking nicer. > If you strongly prefer the shorter version, I can do that in v2. > > > Also, since the order of the shifts is rather important, I think it > > makes sense to add this extra pair of (), even when not strictly needed, > > just to make it easier to read. > > Sure, will do. >