From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 0A0AF347BDC for ; Fri, 17 Apr 2026 19:43:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776455040; cv=none; b=r9uQFXo1Od2k0nOsH30fVP7hT0Vd/tLRubxbyGB1SbKhWtpmLPgKXszgG247e/8slqg8+lE1NEljAX4/gvWRCoG7tsO/2ZfuwlzYuYMk9eIIztfNBNVTQ5peyrbIOIYA9h1Cr6hYBcCsKTOElA0lT2tsk4Np/SVkQFAO/1z51RQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776455040; c=relaxed/simple; bh=KaFMDCFH9CdumsHQVkzpXcZ16qLlfuRi8NWo/FeNfgE=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HBKT1Sgsgak+oAvFvHqRD0MfBrNGGsI/dbIz0s3+ZVvMLFcDjsAtlHEGW7NrKVYcfmWpIHHulwnHMlrtKQL7KBzN5SH5EP0W57pUTNv6D2Oqp3blB41AlHa8jA8HDLVzuyHQWEpKT3IfkrPz1IauLCQ6xxxc4wrzcM9DjewQXIA= 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=htHi75iS; arc=none smtp.client-ip=209.85.128.41 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="htHi75iS" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-488b0046078so10224415e9.1 for ; Fri, 17 Apr 2026 12:43:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776455037; x=1777059837; 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=tSJKJdhPF/oSEv0Q3RwysZnU9Ji4VqpvQLZ2wVMqphA=; b=htHi75iSbURVMZ6Xk9C/IrGjBnOFBWNfL2XmrCASRM+7fW23utl/LnXbfsSHB5bDuY p+JrBm3aDjplc/i+J+r+5z3cqZYRptIqbxLk4d2Fs+yDPu6pdSVhKrJvJ+PsyNXkDq8Y g1KMYTwZhGj3NXZxDotQEs15pNW1EocuyHt11lGYxOUqKuT1I0nP1Z3vqRpAp+1nxcdn u5qYKEtx9eWSjtv+po3EklPDjX5B4dyPS8B3N301kbz6KWAK7z2PDbb39K3lJoEnTUyX 97wf6Z+bFsq0LOcC+MBtKcORuqrcPUAoc9J2Btom1LDSSbQv8je6lVjNxJkMmHQyu4UJ Av+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776455037; x=1777059837; 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=tSJKJdhPF/oSEv0Q3RwysZnU9Ji4VqpvQLZ2wVMqphA=; b=GDcyYwHqSfpkgqX4Mhk8N/mngLSdPg2Pwcw1Dz2yH0MubgES74kYlvP8/YcFnFq3kS bYjNDf2OpQ4+iZ6AwGwWReFj4xLlqRjNHY057nSw+ngtiR3HHOHnhoEpYBlcHlx75Zu/ UKIh1MXrupb21Zegi/XZzwYwwfU/tKrKaFeloIbf1fVy7RJKb4sJoclVo7uXIcdIPCcE buRLIATs17GturjSGJSJdDKIENQ8Y2yO+xxd8wEGZ8JLueIy2ircRD+fJxsASa0Lmq2C Y3JcuxTGzdLYcSHL4r2wCUNrriJiDvcKJ+G1K5ifvx8o5JQcipx5+1JNTw6e+Ookv8mu L0+Q== X-Forwarded-Encrypted: i=1; AFNElJ8pjnkBdV3apby2YvmMzS7ofbH33vY44i4AXODgwzrxasyUe6i4nb8VScwNXA5hX8UIoGdnNkUoK7oQ8Wg=@vger.kernel.org X-Gm-Message-State: AOJu0YzWXdgk56nLumpELZni6n3cjXwri4S4lAs9DXoq7uXY8321JAHL FXgJidIGcdYWiaor4+DNwUJKHRkyO6ruposOHogzO5/WhjfSw1KKu+ha X-Gm-Gg: AeBDies422JS+11XoUniys1MDhP3tIFzpLd7PDkMLogfTJScv1mXubdQQh7Vpf4QkxQ oM5MBqJIolkW19se4GzmNsxXb7b94iJy70SCSGNdr0EGd30O+MEdOmgzyJosjyh0kflmSdoV9cV X11cMybCp1CMCpIS8qEy1QNK31zouzNobE5AuvR8L7SYtT0PVIJTKCR72m0k9iT9GIB1K/zIsgD 0eKeJ9lS9HUNZdPaBLq13gPV8sxy+QiCuBqoWntGTpzfoDHd0ftBJoUYUXzswIJCpUZ+8C0f/3X JvTUvlRbkCpyJGpAS/EkJ5l8qyC7nm+pM8TnqZhS/wCW4tnD3F99IA7zd73YN3W2MYGl1rs3Yyr Zb8fKX44Eoh/5rYZZZl4bCGU3cFzMrmeAZtpHnvmk/hD2zkyarQTVux7e+TWKvHAzuD3nkE14TP OvHPTqRGEZ4T5fWpFEu0Pc+1/T30EKuwn+/fBj0fqGtF9a/eybmFHx/yiWVu9VppvO38eioxZKV sU= X-Received: by 2002:a05:600c:1c11:b0:488:9ed3:148f with SMTP id 5b1f17b1804b1-488fb782a93mr63697245e9.21.1776455037158; Fri, 17 Apr 2026 12:43:57 -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-488fc11ced7sm58174535e9.0.2026.04.17.12.43.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Apr 2026 12:43:56 -0700 (PDT) Date: Fri, 17 Apr 2026 20:43:55 +0100 From: David Laight To: Yury Norov Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , 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: <20260417204355.37fd960d@pumpkin> In-Reply-To: <20260417173621.368914-2-ynorov@nvidia.com> References: <20260417173621.368914-1-ynorov@nvidia.com> <20260417173621.368914-2-ynorov@nvidia.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 Fri, 17 Apr 2026 13:36:12 -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))));\ Have you looked at what that generates on a typical 32bit architecture? It really a bad idea to use __signed_scalar_typeof() on anything that isn't a simple variable. The bloat from all this when 'mask' is an expansion of GENMASK() is horrid. Indeed both signed_scalar_typeof() and unsigned_scalar_typeof() should really not be used - there are generally much better ways. In this case you can just write: ({ auto _mask = mask; unsigned int __sl = __builtin_clzll(_mask); unsigned int __sr = __sl + __builtin_ctzll(_mask); __builtin_chose_expr(sizeof(_mask) <= 4, (int)(reg) << __sl - 32 >> __sr - 32, ((long long)(reg) << __sl >> __sr) }) and let the compiler do any more integer promotions (etc). I'm also not convinced that the checks __BF_FIELD_CHECK() does on 'reg' are in any sense worth the effort. I have tried some simpler alternatives, eg: !__builtin_constant_p(reg) && statically_true((reg & mask) == 0) however that throws up some false positives due to some of weird ways people have used FIELD_GET() where it is nothing like the simplest (or most obvious) way to do things. That might have been the code that split a 32bit value into bytes in a printf with: FIELD_GET(GENMASK(7, 0), val), FIELD_GET(GENMASK(15, 8), val), FIELD_GET(GENMASK(23, 16), val), FIELD_GET(GENMASK(31, 24), val), David > + }) > + > /** > * FIELD_MODIFY() - modify a bitfield element > * @_mask: shifted mask defining the field's length and position