public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v7 6/8] math.h Add macros to round to closest specified power of 2
@ 2024-05-09 18:39 Devarsh Thakkar
  2024-05-10 15:01 ` Andy Shevchenko
  0 siblings, 1 reply; 13+ messages in thread
From: Devarsh Thakkar @ 2024-05-09 18:39 UTC (permalink / raw)
  To: mchehab, hverkuil-cisco, linux-media, linux-kernel,
	benjamin.gaignard, sebastian.fricke, akpm, gregkh,
	andriy.shevchenko, adobriyan, jani.nikula, p.zabel, airlied,
	daniel, dri-devel
  Cc: laurent.pinchart, praneeth, nm, vigneshr, a-bhatia1, j-luthra,
	b-brnich, detheridge, p-mantena, vijayp, devarsht, andrzej.p,
	nicolas

Add macros to round to nearest specified power of 2. Two macros are added :
round_closest_up and round_closest_down which round up to nearest multiple
of 2 with a preference to round up or round down respectively if there are
two possible nearest values to the given number.

This patch is inspired from the Mentor Graphics IPU driver [1] which uses
similar macro locally and which can be updated to use this generic macro
instead along with other drivers having similar requirements.

[1]:
https://elixir.bootlin.com/linux/v6.8.9/source/drivers/gpu/ipu-v3/ipu-image-convert.c#L480

Signed-off-by: Devarsh Thakkar <devarsht@ti.com>
---
V1->V6 (No change, patch introduced in V7)
---
 include/linux/math.h | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/include/linux/math.h b/include/linux/math.h
index dd4152711de7..82ee3265c910 100644
--- a/include/linux/math.h
+++ b/include/linux/math.h
@@ -34,6 +34,42 @@
  */
 #define round_down(x, y) ((x) & ~__round_mask(x, y))
 
+/**
+ * round_closest_up - round to nearest specified power of 2 with preference
+ *		      to rounding up
+ * @x: the value to round
+ * @y: multiple to round to (must be a power of 2)
+ *
+ * Rounds @x to nearest multiple of @y (which must be a power of 2).
+ * The rounded value can be greater than or less than @x depending
+ * upon it's nearness to @x. If there are two nearest possible values,
+ * then preference will be given to the greater value.
+ *
+ * Examples :
+ * round_closest_up(17, 4) = 16
+ * round_closest_up(15, 4) = 16
+ * round_closest_up(14, 4) = 16
+ */
+#define round_closest_up(x, y) round_down((x) + (y) / 2, (y))
+
+/**
+ * round_closest_down - round to nearest specified power of 2 with preference
+ *			to rounding down
+ * @x: the value to round
+ * @y: multiple to round down to (must be a power of 2)
+ *
+ * Rounds @x to nearest multiple of @y (which must be a power of 2).
+ * The rounded value can be greater than or less than @x depending
+ * upon it's nearness to @x. If there are two nearest possible values,
+ * then preference will be given to the lesser value.
+ *
+ * Examples :
+ * round_closest_down(17, 4) = 16
+ * round_closest_down(15, 4) = 16
+ * round_closest_down(14, 4) = 12
+ */
+#define round_closest_down(x, y) round_up((x) - (y) / 2, (y))
+
 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
 
 #define DIV_ROUND_DOWN_ULL(ll, d) \
-- 
2.39.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-05-13 13:14 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-09 18:39 [PATCH v7 6/8] math.h Add macros to round to closest specified power of 2 Devarsh Thakkar
2024-05-10 15:01 ` Andy Shevchenko
2024-05-10 15:15   ` Jani Nikula
2024-05-10 15:28     ` Andy Shevchenko
2024-05-11 17:41     ` Devarsh Thakkar
2024-05-13  8:59       ` Andy Shevchenko
2024-05-13 11:25         ` Devarsh Thakkar
2024-05-13 12:25           ` Andy Shevchenko
2024-05-13 13:04             ` Devarsh Thakkar
2024-05-13 13:14               ` Andy Shevchenko
2024-05-11 17:26   ` Devarsh Thakkar
2024-05-12  4:46     ` Alexey Dobriyan
2024-05-13  8:55       ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox