]> www.infradead.org Git - mtd-utils.git/commitdiff
common: Add round functions
authorRichard Weinberger <richard@nod.at>
Thu, 18 Oct 2018 14:36:38 +0000 (16:36 +0200)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Thu, 1 Nov 2018 11:31:44 +0000 (12:31 +0100)
Signed-off-by: Richard Weinberger <richard@nod.at>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
include/common.h

index 2f24645fe23b8f52c5e72184de2b90fb6f1d55d2..72707697b170c4ce1a10605e392b126f9c0f65fc 100644 (file)
@@ -65,6 +65,16 @@ extern "C" {
        (_x > _y) ? _x : _y; \
 })
 
+/*
+ * This looks more complex than it should be. But we need to
+ * get the type for the ~ right in round_down (it needs to be
+ * as wide as the result!), and we want to evaluate the macro
+ * arguments just once each.
+ */
+#define __round_mask(x, y) ((__typeof__(x))((y)-1))
+#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
+#define round_down(x, y) ((x) & ~__round_mask(x, y))
+
 #ifndef O_CLOEXEC
 #define O_CLOEXEC 0
 #endif