From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) (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 E745E3A5E69 for ; Tue, 21 Apr 2026 09:27:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776763670; cv=none; b=C7hjyvrsf6W7xAenNuzTXfePe/M3nkL7vpbRrAaierAXV13bDuvrSDHqO/clNVcrrusZORq2Xtl5F02hlaSGlwmCfKHAJVvoURTJdrf5Y7Q6weeBeZ+4N1XvOwT+3AUECGsicjcvlpHQDoNzjqEjlCxmESJOfy8ERBPqPEuUCbY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776763670; c=relaxed/simple; bh=NWVPQ/ix6la1A9YnRnDlxRH64UYZgVvj6dWNHNlWfm0=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zb7y+hc/bLlY20nEKSjxX9k6xxYnjkVVNyA4muVdwL6qjUazJ/Uf8mAi9R4RtBa8hdNffwPr2GkeDX/2ITbzVI8zuiyA4exz+8t/tBRDwFiKYl9sOT4cEU6Iq7+fQc5y9JFnlIZjmaOyBoo3tvfG8dn2+YWnwFtw6QFbNs+VghQ= 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.42 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-f42.google.com with SMTP id ffacd0b85a97d-43cff5dafc3so3305900f8f.1 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=qFySUJWivM89nRhfY8J70FCYfm6OzVihnraIwMdr21+vf5aVgY+8ZJth4xf4uPA5gn dgQVvwBkkQqbKHFz9u+l9EHcVrJnEQzmYBOneORwbXscF3Y/m134jQ23qtCyIb85ZRfP CW6lfw4IZ+pP0XT1MEoekQxzdirO2PrJLNxbWusFOSTka6dcncHQgzlc+CcuX8ROrwCx kRcYnjQGM4bvvKsl3GekYDFIkV+tns4ySs8FJ5mHAWzd7ljeMIPq1M9nn9e2gmcSQPKW ZlaZbIIMgckb6BiQchqNAA3AayfkWc602iwxYl+oG9zZ/uGJeVzT8ilaB8JFiSf/xqGo UQMg== X-Forwarded-Encrypted: i=1; AFNElJ/xQ1lHnTK8jj48XhMQ5pEusFuo4Y7LBiO1Ak3ABw9wqvYuczX9tgwU3icpUK8vQxH5qqw60qs=@vger.kernel.org X-Gm-Message-State: AOJu0YyG5f6lEgnFCMNvBxn+wOTlp9t46bK6mWStknCo1UuprCn9NS4n V0vbr2Z2ia5JTbsPO4KA0O40ujTSFtPGJpcMuuJBnkiLzzUa6gIU+lmK X-Gm-Gg: AeBDietzNt+duWMqmlus4QJ3ibLV/qcE1NMSdy2i2E/Prk0wjfYnpVrQUTkVxZk17lG qrejEBHg4KAijJ/CC4k/o5sU1tJ+stzvoyFlbUZeB4YZukqSoQ3LM5CVdv7695E6TQwxjM19qiC UKPuLd22NT4kwLw0hx40ogXMQs3eY6hy/XpLr1p/QVW8TJ6mjg0vipil5wUYCgmI6U0q9CseRj6 QEcQ/EXntdMV1pxbRNwq0AFI00iAPPakpRK52pqKwES+mG1XDULL1askluNUzVn9wrequNEfeaB BW2iZPR/tLnWMrXZfIz8UU2VxIEqgiEotZLCrCvBpondoBKd0DkvE/iKrLB4kyIRmobGQJ4ETWZ KQjKCC++4bSH0RO8EON7c7gKygCBubbjO4cSlvcGR5UQj4MT9bO2k3B0VP/ngyPyCQtoCLZ9IDS OkWmdu01EosNrazZhtLK+ahoaLhwpWlLM55+o2l4gMEZzKzt8e4qYPkapLlwJg2svwrwItw5+4n c4xla63LR297A== 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: netdev@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. >