summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Weinberger <richard@nod.at>2018-10-18 16:36:38 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2018-11-01 12:31:44 +0100
commit23b99432e59b4ee994888f7bd8ef1f77bb424756 (patch)
treee08d5eb2b01298e93cc8855fbf8da3d4345a305a
parentf15154ddc2fa5989a570e0b830ad2d2de357cdd3 (diff)
common: Add round functions
Signed-off-by: Richard Weinberger <richard@nod.at> Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--include/common.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/include/common.h b/include/common.h
index 2f24645..7270769 100644
--- a/include/common.h
+++ b/include/common.h
@@ -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