diff options
author | Sandrine Bailleux <sandrine.bailleux@arm.com> | 2016-06-16 15:05:39 +0100 |
---|---|---|
committer | Sandrine Bailleux <sandrine.bailleux@arm.com> | 2016-07-08 14:37:11 +0100 |
commit | 0146ae64c006956a281865f5688858d4846c781e (patch) | |
tree | 305287d0f6e4fcf349b7e0382905bdbd78de8b4b | |
parent | ed81f3ebbfb5abc7d0d250fbc71f297a904d71ae (diff) |
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
-rw-r--r-- | include/lib/utils.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/include/lib/utils.h b/include/lib/utils.h index d45dff34..9cc5468b 100644 --- a/include/lib/utils.h +++ b/include/lib/utils.h @@ -38,4 +38,21 @@ #define IS_POWER_OF_TWO(x) \ (((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__ */ |