Add alignment macros.
Introduces align_up, align_down and is_aligned macros that use compiler
builtins if available.
Note: when we fallback to the non-builtin case we could cast back from
uintptr_t to the original type using __typeof() if we accept the
non-standard extension.
Change-Id: Ie0c56f19cb0edc8a211d8c1182b9af5eb329cd8c
diff --git a/inc/hf/std.h b/inc/hf/std.h
index 6e5e82a..6609978 100644
--- a/inc/hf/std.h
+++ b/inc/hf/std.h
@@ -31,6 +31,39 @@
#define ctz(x) __builtin_ctz(x)
+/* Compatibility with old compilers */
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
+
+/**
+ * Check whether the value `v` is aligned to the boundary `a`,
+ * with `a` power of 2.
+ */
+#if __has_builtin(__builtin_is_aligned)
+#define is_aligned(v, a) __builtin_is_aligned((v), (a))
+#else
+#define is_aligned(v, a) (((uintptr_t)(v) & (a - 1)) == 0)
+#endif
+
+/**
+ * Align up the value `v` to the boundary `a`, with `a` power of 2.
+ */
+#if __has_builtin(__builtin_align_up)
+#define align_up(v, a) __builtin_align_up((v), (a))
+#else
+#define align_up(v, a) (((uintptr_t)(v) + (a - 1)) & ~(a - 1))
+#endif
+
+/**
+ * Align down the value `v` to the boundary `a`, with `a` power of 2.
+ */
+#if __has_builtin(__builtin_align_down)
+#define align_down(v, a) __builtin_align_down((v), (a))
+#else
+#define align_down(v, a) ((uintptr_t)(v) & ~(a - 1))
+#endif
+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define be16toh(v) __builtin_bswap16(v)