Skip to contentSkip to conversation

iio: dac: ad5766: Convert GENMASK() to use static parameters

PATCH Applied Andy Shevchenko 3 participants 5 messages Nov 17 – Nov 22

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: dac: ad5766: Convert GENMASK() to use static parameters

There is no need to use run-time calculated masks in the code. Besides, GENMASK() over non-static parameters might lead compiler to generate much worse code.

---

v2: fixed typo

 drivers/iio/dac/ad5766.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/dac/ad5766.c +2 -2
index 061b43fc0345..ba6c54e2c9ac 100644
@@ -18,9 +18,9 @@
 
 #define AD5766_UPPER_WORD_SPI_MASK		GENMASK(31, 16)
 #define AD5766_LOWER_WORD_SPI_MASK		GENMASK(15, 0)
-#define AD5766_DITHER_SOURCE_MASK(ch)		GENMASK(((2 * ch) + 1), (2 * ch))
+#define AD5766_DITHER_SOURCE_MASK(ch)		(GENMASK(1, 0) << ((ch) * 2))
 #define AD5766_DITHER_SOURCE(ch, source)	BIT((ch * 2) + source)
-#define AD5766_DITHER_SCALE_MASK(x)		AD5766_DITHER_SOURCE_MASK(x)
+#define AD5766_DITHER_SCALE_MASK(ch)		(GENMASK(1, 0) << ((ch) * 2))
 #define AD5766_DITHER_SCALE(ch, scale)		(scale << (ch * 2))
 #define AD5766_DITHER_ENABLE_MASK(ch)		BIT(ch)
 #define AD5766_DITHER_ENABLE(ch, state)		((!state) << ch)
Signature · 1 line
2.50.1
#1 lore raw
Quoted 23 lines · Andy Shevchenko

There is no need to use run-time calculated masks in the code. Besides, GENMASK() over non-static parameters might lead compiler to generate much worse code.

---

v2: fixed typo

 drivers/iio/dac/ad5766.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/dac/ad5766.c +0 -0
index 061b43fc0345..ba6c54e2c9ac 100644
@@ -18,9 +18,9 @@


 #define AD5766_UPPER_WORD_SPI_MASK GENMASK(31, 16)
 #define AD5766_LOWER_WORD_SPI_MASK GENMASK(15, 0)

drivers/iio/dac/ad5766.c +1 -1
-#define AD5766_DITHER_SOURCE_MASK(ch)		GENMASK(((2 * ch) + 1), (2 * ch))
+#define AD5766_DITHER_SOURCE_MASK(ch)		(GENMASK(1, 0) << ((ch) * 2))

 #define AD5766_DITHER_SOURCE(ch, source) BIT((ch * 2) + source)

drivers/iio/dac/ad5766.c line 23

Quoted 2 lines
drivers/iio/dac/ad5766.c +1 -1
-#define AD5766_DITHER_SCALE_MASK(x)		AD5766_DITHER_SOURCE_MASK(x)
+#define AD5766_DITHER_SCALE_MASK(ch)		(GENMASK(1, 0) << ((ch) * 2))

This could have remained unchanged?

Quoted 3 lines

 #define AD5766_DITHER_SCALE(ch, scale) (scale << (ch * 2))
 #define AD5766_DITHER_ENABLE_MASK(ch) BIT(ch)
 #define AD5766_DITHER_ENABLE(ch, state) ((!state) << ch)

#2 lore raw
Quoted 4 lines · Siratul Islam
Quoted 3 lines · Andy Shevchenko

There is no need to use run-time calculated masks in the code. Besides, GENMASK() over non-static parameters might lead compiler to generate much worse code.

...

Quoted 8 lines

drivers/iio/dac/ad5766.c line 23

Quoted 7 lines

 #define AD5766_UPPER_WORD_SPI_MASK GENMASK(31, 16)
 #define AD5766_LOWER_WORD_SPI_MASK GENMASK(15, 0)

drivers/iio/dac/ad5766.c +1 -1
-#define AD5766_DITHER_SOURCE_MASK(ch)		GENMASK(((2 * ch) + 1), (2 * ch))
+#define AD5766_DITHER_SOURCE_MASK(ch)		(GENMASK(1, 0) << ((ch) * 2))

 #define AD5766_DITHER_SOURCE(ch, source) BIT((ch * 2) + source)

drivers/iio/dac/ad5766.c +1 -1
-#define AD5766_DITHER_SCALE_MASK(x)		AD5766_DITHER_SOURCE_MASK(x)
+#define AD5766_DITHER_SCALE_MASK(ch)		(GENMASK(1, 0) << ((ch) * 2))

This could have remained unchanged?

Yes i could, but does it really show the tightness between two semantically? I believe it's not, that's why I explicitly put the numbers instead of indirection.

Quoted 3 lines
Quoted 3 lines

 #define AD5766_DITHER_SCALE(ch, scale) (scale << (ch * 2))
 #define AD5766_DITHER_ENABLE_MASK(ch) BIT(ch)
 #define AD5766_DITHER_ENABLE(ch, state) ((!state) << ch)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by
#3 lore raw
Quoted 1 line · Andy Shevchenko

...

Quoted 7 lines
Quoted 4 lines

-#define AD5766_DITHER_SCALE_MASK(x) AD5766_DITHER_SOURCE_MASK(x) +#define AD5766_DITHER_SCALE_MASK(ch) (GENMASK(1, 0) << ((ch) * 2)) This could have remained unchanged?

Yes i could, but does it really show the tightness between two semantically? I believe it's not, that's why I explicitly put the numbers instead of indirection.

Thats fair.

Best regards,
Sirat

maintainer Applied
#4 lore raw
In reply to Andy Shevchenko
Quoted 7 lines · On Tue, 17 Nov 2026 17:08:43 +0100 Andy Shevchenko…

There is no need to use run-time calculated masks in the code. Besides, GENMASK() over non-static parameters might lead compiler to generate much worse code.

---

I'm too lazy to find out what the compilers actually generate. Lots of options around code like this for what they might do. Anyhow, it is logically simpler so why not!

Applied.

Quoted 21 lines

v2: fixed typo

 drivers/iio/dac/ad5766.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/dac/ad5766.c +2 -2
index 061b43fc0345..ba6c54e2c9ac 100644
@@ -18,9 +18,9 @@
 
 #define AD5766_UPPER_WORD_SPI_MASK		GENMASK(31, 16)
 #define AD5766_LOWER_WORD_SPI_MASK		GENMASK(15, 0)
-#define AD5766_DITHER_SOURCE_MASK(ch)		GENMASK(((2 * ch) + 1), (2 * ch))
+#define AD5766_DITHER_SOURCE_MASK(ch)		(GENMASK(1, 0) << ((ch) * 2))
 #define AD5766_DITHER_SOURCE(ch, source)	BIT((ch * 2) + source)
-#define AD5766_DITHER_SCALE_MASK(x)		AD5766_DITHER_SOURCE_MASK(x)
+#define AD5766_DITHER_SCALE_MASK(ch)		(GENMASK(1, 0) << ((ch) * 2))
 #define AD5766_DITHER_SCALE(ch, scale)		(scale << (ch * 2))
 #define AD5766_DITHER_ENABLE_MASK(ch)		BIT(ch)
 #define AD5766_DITHER_ENABLE(ch, state)		((!state) << ch)