diff mbox series

[02/42] common: Add round functions

Message ID 20181018143718.26298-3-richard@nod.at
State Accepted
Delegated to: David Oberhollenzer
Headers show
Series mtd-utils: Add fscrypt support to mkfs.ubifs | expand

Commit Message

Richard Weinberger Oct. 18, 2018, 2:36 p.m. UTC
Signed-off-by: Richard Weinberger <richard@nod.at>
---
 include/common.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
diff mbox series

Patch

diff --git a/include/common.h b/include/common.h
index 2f24645fe23b..72707697b170 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