All of lore.kernel.org
 help / color / mirror / Atom feed
From: Geert Uytterhoeven <geert+renesas@glider.be>
To: Tony Lindgren <tony@atomide.com>,
	Russell King <linux@armlinux.org.uk>,
	Rajendra Nayak <rnayak@codeaurora.org>,
	Paul Walmsley <paul@pwsan.com>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@kernel.org>,
	Nicolas Ferre <nicolas.ferre@microchip.com>,
	Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Ludovic Desroches <ludovic.desroches@microchip.com>,
	Tero Kristo <kristo@kernel.org>,
	Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>,
	Benoit Parrot <bparrot@ti.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Andrew Jeffery <andrew@aj.id.au>,
	Ulf Hansson <ulf.hansson@linaro.org>,
	Joel Stanley <joel@jms.id.au>, Ping-Ke Shih <pkshih@realtek.com>,
	Kalle Valo <kvalo@codeaurora.org>,
	"David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	Linus Walleij <linus.walleij@linaro.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Magnus Damm <magnus.damm@gmail.com>,
	Eduardo Valentin <edubezval@gmail.com>,
	Keerthy <j-keerthy@ti.com>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	Amit Kucheria <amitk@kernel.org>, Zhang Rui <rui.zhang@intel.com>,
	Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>
Cc: linux-arm-kernel@lists.infradead.org, linux-omap@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org,
	linux-renesas-soc@vger.kernel.org, linux-iio@vger.kernel.org,
	linux-media@vger.kernel.org, linux-mmc@vger.kernel.org,
	linux-aspeed@lists.ozlabs.org, openbmc@lists.ozlabs.org,
	linux-wireless@vger.kernel.org, netdev@vger.kernel.org,
	linux-gpio@vger.kernel.org, linux-pm@vger.kernel.org,
	alsa-devel@alsa-project.org,
	Geert Uytterhoeven <geert+renesas@glider.be>
Subject: [PATCH 01/17] bitfield: Add non-constant field_{prep,get}() helpers
Date: Mon, 22 Nov 2021 16:53:54 +0100	[thread overview]
Message-ID: <3a54a6703879d10f08cf0275a2a69297ebd2b1d4.1637592133.git.geert+renesas@glider.be> (raw)
In-Reply-To: <cover.1637592133.git.geert+renesas@glider.be>

The existing FIELD_{GET,PREP}() macros are limited to compile-time
constants.  However, it is very common to prepare or extract bitfield
elements where the bitfield mask is not a compile-time constant.

To avoid this limitation, the AT91 clock driver already has its own
field_{prep,get}() macros.  Make them available for general use by
moving them to <linux/bitfield.h>, and improve them slightly:
  1. Avoid evaluating macro parameters more than once,
  2. Replace "ffs() - 1" by "__ffs()", as the latter operates on
     "unsigned long", just like BIT() and GENMASK().

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
Using __ffs() actually reduces code size (16 bytes for each of
drivers/clk/at91/clk-{generated,peripheral}.o), as __ffs() doesn't
need to verify that the value passed is non-zero.
---
 drivers/clk/at91/clk-peripheral.c |  1 +
 drivers/clk/at91/pmc.h            |  3 ---
 include/linux/bitfield.h          | 30 ++++++++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/at91/clk-peripheral.c b/drivers/clk/at91/clk-peripheral.c
index e14fa5ac734cead7..e2f33498139a1b8c 100644
--- a/drivers/clk/at91/clk-peripheral.c
+++ b/drivers/clk/at91/clk-peripheral.c
@@ -3,6 +3,7 @@
  *  Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  */
 
+#include <linux/bitfield.h>
 #include <linux/bitops.h>
 #include <linux/clk-provider.h>
 #include <linux/clkdev.h>
diff --git a/drivers/clk/at91/pmc.h b/drivers/clk/at91/pmc.h
index 3a1bf6194c287d09..1256e1ab91526a25 100644
--- a/drivers/clk/at91/pmc.h
+++ b/drivers/clk/at91/pmc.h
@@ -114,9 +114,6 @@ struct at91_clk_pms {
 	unsigned int parent;
 };
 
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
-
 #define ndck(a, s) (a[s - 1].id + 1)
 #define nck(a) (a[ARRAY_SIZE(a) - 1].id + 1)
 struct pmc_data *pmc_data_allocate(unsigned int ncore, unsigned int nsystem,
diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 4e035aca6f7e6000..f03b0712e4babec1 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -156,4 +156,34 @@ __MAKE_OP(64)
 #undef __MAKE_OP
 #undef ____MAKE_OP
 
+/**
+ * field_prep() - prepare a bitfield element
+ * @_mask: shifted mask defining the field's length and position
+ * @_val:  value to put in the field
+ *
+ * field_prep() masks and shifts up the value.  The result should be
+ * combined with other fields of the bitfield using logical OR.
+ * Unlike FIELD_PREP(), @_mask is not limited to a compile-time constant.
+ */
+#define field_prep(_mask, _val)						\
+	({								\
+		typeof(_mask) ___mask = (_mask);			\
+		(((_val) << __ffs(___mask)) & (___mask));		\
+	})
+
+/**
+ * field_get() - extract a bitfield element
+ * @_mask: shifted mask defining the field's length and position
+ * @_reg:  value of entire bitfield
+ *
+ * field_get() extracts the field specified by @_mask from the
+ * bitfield passed in as @_reg by masking and shifting it down.
+ * Unlike FIELD_GET(), @_mask is not limited to a compile-time constant.
+ */
+#define field_get(_mask, _reg)						\
+	({								\
+		typeof(_mask) ___mask = _mask;				\
+		(((_reg) & (___mask)) >> __ffs(___mask));		\
+	})
+
 #endif
-- 
2.25.1


WARNING: multiple messages have this Message-ID (diff)
From: Geert Uytterhoeven <geert+renesas@glider.be>
To: Tony Lindgren <tony@atomide.com>,
	Russell King <linux@armlinux.org.uk>,
	Rajendra Nayak <rnayak@codeaurora.org>,
	Paul Walmsley <paul@pwsan.com>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@kernel.org>,
	Nicolas Ferre <nicolas.ferre@microchip.com>,
	Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Ludovic Desroches <ludovic.desroches@microchip.com>,
	Tero Kristo <kristo@kernel.org>,
	Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>,
	Benoit Parrot <bparrot@ti.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Andrew Jeffery <andrew@aj.id.au>,
	Ulf Hansson <ulf.hansson@linaro.org>,
	Joel Stanley <joel@jms.id.au>, Ping-Ke Shih <pkshih@realtek.com>,
	Kalle Valo <kvalo@codeaurora.org>,
	"David S . Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	Linus Walleij <linus.walleij@linaro.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	Magnus Damm <magnus.damm@gmail.com>,
	Eduardo Valentin <edubezval@gmail.com>,
	Keerthy <j-keerthy@ti.com>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	Amit Kucheria <amitk@kernel.org>, Zhang Rui <rui.zhang@intel.com>,
	Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>
Cc: alsa-devel@alsa-project.org,
	Geert Uytterhoeven <geert+renesas@glider.be>,
	linux-aspeed@lists.ozlabs.org, linux-pm@vger.kernel.org,
	linux-iio@vger.kernel.org, linux-wireless@vger.kernel.org,
	openbmc@lists.ozlabs.org, linux-mmc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-renesas-soc@vger.kernel.org,
	linux-gpio@vger.kernel.org, netdev@vger.kernel.org,
	linux-omap@vger.kernel.org, linux-clk@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-media@vger.kernel.org
Subject: [PATCH 01/17] bitfield: Add non-constant field_{prep,get}() helpers
Date: Mon, 22 Nov 2021 16:53:54 +0100	[thread overview]
Message-ID: <3a54a6703879d10f08cf0275a2a69297ebd2b1d4.1637592133.git.geert+renesas@glider.be> (raw)
In-Reply-To: <cover.1637592133.git.geert+renesas@glider.be>

The existing FIELD_{GET,PREP}() macros are limited to compile-time
constants.  However, it is very common to prepare or extract bitfield
elements where the bitfield mask is not a compile-time constant.

To avoid this limitation, the AT91 clock driver already has its own
field_{prep,get}() macros.  Make them available for general use by
moving them to <linux/bitfield.h>, and improve them slightly:
  1. Avoid evaluating macro parameters more than once,
  2. Replace "ffs() - 1" by "__ffs()", as the latter operates on
     "unsigned long", just like BIT() and GENMASK().

Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
Using __ffs() actually reduces code size (16 bytes for each of
drivers/clk/at91/clk-{generated,peripheral}.o), as __ffs() doesn't
need to verify that the value passed is non-zero.
---
 drivers/clk/at91/clk-peripheral.c |  1 +
 drivers/clk/at91/pmc.h            |  3 ---
 include/linux/bitfield.h          | 30 ++++++++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/at91/clk-peripheral.c b/drivers/clk/at91/clk-peripheral.c
index e14fa5ac734cead7..e2f33498139a1b8c 100644
--- a/drivers/clk/at91/clk-peripheral.c
+++ b/drivers/clk/at91/clk-peripheral.c
@@ -3,6 +3,7 @@
  *  Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
  */
 
+#include <linux/bitfield.h>
 #include <linux/bitops.h>
 #include <linux/clk-provider.h>
 #include <linux/clkdev.h>
diff --git a/drivers/clk/at91/pmc.h b/drivers/clk/at91/pmc.h
index 3a1bf6194c287d09..1256e1ab91526a25 100644
--- a/drivers/clk/at91/pmc.h
+++ b/drivers/clk/at91/pmc.h
@@ -114,9 +114,6 @@ struct at91_clk_pms {
 	unsigned int parent;
 };
 
-#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
-#define field_prep(_mask, _val) (((_val) << (ffs(_mask) - 1)) & (_mask))
-
 #define ndck(a, s) (a[s - 1].id + 1)
 #define nck(a) (a[ARRAY_SIZE(a) - 1].id + 1)
 struct pmc_data *pmc_data_allocate(unsigned int ncore, unsigned int nsystem,
diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 4e035aca6f7e6000..f03b0712e4babec1 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -156,4 +156,34 @@ __MAKE_OP(64)
 #undef __MAKE_OP
 #undef ____MAKE_OP
 
+/**
+ * field_prep() - prepare a bitfield element
+ * @_mask: shifted mask defining the field's length and position
+ * @_val:  value to put in the field
+ *
+ * field_prep() masks and shifts up the value.  The result should be
+ * combined with other fields of the bitfield using logical OR.
+ * Unlike FIELD_PREP(), @_mask is not limited to a compile-time constant.
+ */
+#define field_prep(_mask, _val)						\
+	({								\
+		typeof(_mask) ___mask = (_mask);			\
+		(((_val) << __ffs(___mask)) & (___mask));		\
+	})
+
+/**
+ * field_get() - extract a bitfield element
+ * @_mask: shifted mask defining the field's length and position
+ * @_reg:  value of entire bitfield
+ *
+ * field_get() extracts the field specified by @_mask from the
+ * bitfield passed in as @_reg by masking and shifting it down.
+ * Unlike FIELD_GET(), @_mask is not limited to a compile-time constant.
+ */
+#define field_get(_mask, _reg)						\
+	({								\
+		typeof(_mask) ___mask = _mask;				\
+		(((_reg) & (___mask)) >> __ffs(___mask));		\
+	})
+
 #endif
-- 
2.25.1


  reply	other threads:[~2021-11-22 15:56 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-22 15:53 [PATCH 00/17] Non-const bitfield helper conversions Geert Uytterhoeven
2021-11-22 15:53 ` Geert Uytterhoeven
2021-11-22 15:53 ` Geert Uytterhoeven [this message]
2021-11-22 15:53   ` [PATCH 01/17] bitfield: Add non-constant field_{prep,get}() helpers Geert Uytterhoeven
2021-11-22 16:32   ` Johannes Berg
2021-11-22 16:32     ` Johannes Berg
2021-11-23  1:17     ` Jakub Kicinski
2021-11-23  1:17       ` Jakub Kicinski
2021-11-23  8:36       ` Geert Uytterhoeven
2021-11-23  8:36         ` [PATCH 01/17] bitfield: Add non-constant field_{prep, get}() helpers Geert Uytterhoeven
2021-11-23 16:24         ` [PATCH 01/17] bitfield: Add non-constant field_{prep,get}() helpers Johannes Berg
2021-11-23 16:24           ` Johannes Berg
2021-11-23 23:49           ` Jakub Kicinski
2021-11-23 23:49             ` Jakub Kicinski
2021-11-24  8:03             ` Johannes Berg
2021-11-24 13:59               ` Jakub Kicinski
2021-11-24 14:07                 ` Johannes Berg
2021-11-23 23:39         ` Jakub Kicinski
2021-11-23 23:39           ` Jakub Kicinski
2021-11-23  1:52     ` Alex Elder
2021-11-23  1:52       ` Alex Elder
2021-11-23  8:38       ` Geert Uytterhoeven
2021-11-23  8:38         ` [PATCH 01/17] bitfield: Add non-constant field_{prep, get}() helpers Geert Uytterhoeven
2021-11-23  8:30     ` [PATCH 01/17] bitfield: Add non-constant field_{prep,get}() helpers Geert Uytterhoeven
2021-11-23  8:30       ` [PATCH 01/17] bitfield: Add non-constant field_{prep, get}() helpers Geert Uytterhoeven
2021-11-23 16:21       ` [PATCH 01/17] bitfield: Add non-constant field_{prep,get}() helpers Johannes Berg
2021-11-23 16:21         ` Johannes Berg
2021-11-23 16:31         ` Geert Uytterhoeven
2021-11-23 16:31           ` [PATCH 01/17] bitfield: Add non-constant field_{prep, get}() helpers Geert Uytterhoeven
2021-11-24  8:24       ` [PATCH 01/17] bitfield: Add non-constant field_{prep,get}() helpers Kalle Valo
2021-11-22 15:53 ` [PATCH 02/17] clk: renesas: Use bitfield helpers Geert Uytterhoeven
2021-11-22 15:53   ` Geert Uytterhoeven
2021-11-22 15:53 ` [PATCH/RFC 03/17] soc: " Geert Uytterhoeven
2021-11-22 15:53   ` Geert Uytterhoeven
2021-11-22 15:53 ` [PATCH/RFC 04/17] ARM: OMAP2+: " Geert Uytterhoeven
2021-11-22 15:53   ` Geert Uytterhoeven
2021-11-22 15:53 ` [PATCH/RFC 05/17] bus: omap_l3_noc: " Geert Uytterhoeven
2021-11-22 15:53   ` Geert Uytterhoeven
2021-11-22 15:53 ` [PATCH/RFC 06/17] clk: ti: " Geert Uytterhoeven
2021-11-22 15:53   ` Geert Uytterhoeven
2021-11-22 15:54 ` [PATCH/RFC 07/17] iio: st_sensors: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-23 23:35   ` Linus Walleij
2021-11-23 23:35     ` Linus Walleij
2021-11-22 15:54 ` [PATCH/RFC 08/17] iio: humidity: hts221: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-24 15:21   ` Jonathan Cameron
2021-11-22 15:54 ` [PATCH/RFC 09/17] iio: imu: st_lsm6dsx: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-22 15:54 ` [PATCH/RFC 10/17] media: ti-vpe: cal: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-22 15:54 ` [PATCH/RFC 11/17] mmc: sdhci-of-aspeed: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-22 15:54 ` [PATCH/RFC 12/17] pinctrl: aspeed: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-22 15:54 ` [PATCH/RFC 13/17] pinctl: ti: iodelay: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-22 17:52   ` Alexandre Belloni
2021-11-22 17:52     ` Alexandre Belloni
2021-11-22 15:54 ` [PATCH/RFC 14/17] regulator: ti-abb: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-22 16:31   ` Mark Brown
2021-11-22 16:31     ` Mark Brown
2021-11-22 15:54 ` [PATCH/RFC 15/17] thermal/ti-soc-thermal: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-22 15:54 ` [PATCH/RFC 16/17] ALSA: ice1724: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-23 11:24   ` Takashi Iwai
2021-11-23 11:24     ` Takashi Iwai
2021-11-22 15:54 ` [PATCH/RFC 17/17] rtw89: " Geert Uytterhoeven
2021-11-22 15:54   ` Geert Uytterhoeven
2021-11-22 18:38   ` Larry Finger
2021-11-22 18:38     ` Larry Finger
2021-11-22 17:50 ` [PATCH 00/17] Non-const bitfield helper conversions Alexandre Belloni
2021-11-22 17:50   ` Alexandre Belloni
2021-11-23  8:20   ` Geert Uytterhoeven
2021-11-23  8:20     ` Geert Uytterhoeven

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3a54a6703879d10f08cf0275a2a69297ebd2b1d4.1637592133.git.geert+renesas@glider.be \
    --to=geert+renesas@glider.be \
    --cc=adrian.hunter@intel.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=amitk@kernel.org \
    --cc=andrew@aj.id.au \
    --cc=bparrot@ti.com \
    --cc=broonie@kernel.org \
    --cc=daniel.lezcano@linaro.org \
    --cc=davem@davemloft.net \
    --cc=edubezval@gmail.com \
    --cc=j-keerthy@ti.com \
    --cc=jic23@kernel.org \
    --cc=joel@jms.id.au \
    --cc=kristo@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kvalo@codeaurora.org \
    --cc=lars@metafoo.de \
    --cc=lgirdwood@gmail.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-aspeed@lists.ozlabs.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mmc@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=lorenzo.bianconi83@gmail.com \
    --cc=ludovic.desroches@microchip.com \
    --cc=magnus.damm@gmail.com \
    --cc=mchehab@kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=netdev@vger.kernel.org \
    --cc=nicolas.ferre@microchip.com \
    --cc=openbmc@lists.ozlabs.org \
    --cc=paul@pwsan.com \
    --cc=perex@perex.cz \
    --cc=pkshih@realtek.com \
    --cc=rafael@kernel.org \
    --cc=rnayak@codeaurora.org \
    --cc=rui.zhang@intel.com \
    --cc=sboyd@kernel.org \
    --cc=tiwai@suse.com \
    --cc=tony@atomide.com \
    --cc=ulf.hansson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.