Commit 0146ae64 authored by Sandrine Bailleux's avatar Sandrine Bailleux
Browse files

Introduce round_up/down() macros

This patch introduces the round_up() and round_down() macros,
which round up (respectively down) a value to a given boundary.
The boundary must be a power of two.

Change-Id: I589dd1074aeb5ec730dd523b4ebf098d55a7e967
parent ed81f3eb
...@@ -38,4 +38,21 @@ ...@@ -38,4 +38,21 @@
#define IS_POWER_OF_TWO(x) \ #define IS_POWER_OF_TWO(x) \
(((x) & ((x) - 1)) == 0) (((x) & ((x) - 1)) == 0)
/*
* The round_up() macro rounds up a value to the given boundary in a
* type-agnostic yet type-safe manner. The boundary must be a power of two.
* In other words, it computes the smallest multiple of boundary which is
* greater than or equal to value.
*
* round_down() is similar but rounds the value down instead.
*/
#define round_boundary(value, boundary) \
((__typeof__(value))((boundary) - 1))
#define round_up(value, boundary) \
((((value) - 1) | round_boundary(value, boundary)) + 1)
#define round_down(value, boundary) \
((value) & ~round_boundary(value, boundary))
#endif /* __UTILS_H__ */ #endif /* __UTILS_H__ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment