git: 3b2ae1c2bbfb - stable/12 - Expose clang's alignment builtins and use them for roundup2/rounddown2
Dimitry Andric
dim at FreeBSD.org
Tue Aug 31 19:26:08 UTC 2021
The branch stable/12 has been updated by dim:
URL: https://cgit.FreeBSD.org/src/commit/?id=3b2ae1c2bbfba2173c2b7f9c8f6a1382dcb4f081
commit 3b2ae1c2bbfba2173c2b7f9c8f6a1382dcb4f081
Author: Alex Richardson <arichardson at FreeBSD.org>
AuthorDate: 2021-02-03 15:27:17 +0000
Commit: Dimitry Andric <dim at FreeBSD.org>
CommitDate: 2021-08-31 19:24:52 +0000
Expose clang's alignment builtins and use them for roundup2/rounddown2
This makes roundup2/rounddown2 type- and const-preserving and allows
using it on pointer types without casting to uintptr_t first. Not
performing pointer-to-integer conversions also helps the compiler's
optimization passes and can therefore result in better code generation.
When using it with integer values there should be no change other than
the compiler checking that the alignment value is a valid power-of-two.
I originally implemented these builtins for CHERI a few years ago and
they have been very useful for CheriBSD. However, they are also useful
for non-CHERI code so I was able to upstream them for Clang 10.0.
Rationale from the clang documentation:
Clang provides builtins to support checking and adjusting alignment
of pointers and integers. These builtins can be used to avoid relying
on implementation-defined behavior of arithmetic on integers derived
from pointers. Additionally, these builtins retain type information
and, unlike bitwise arithmetic, they can perform semantic checking on
the alignment value.
There is also a feature request for GCC, so GCC may also support it in
the future: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98641
Reviewed By: brooks, jhb, imp
Differential Revision: https://reviews.freebsd.org/D28332
(cherry picked from commit 8fa6abb6f4f64f4f23e2920e2aea7996566851a4)
---
sys/sys/cdefs.h | 19 +++++++++++++++++++
sys/sys/param.h | 4 ++--
2 files changed, 21 insertions(+), 2 deletions(-)
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index c8485fa58f54..a5e8abc173bd 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -876,4 +876,23 @@
#define __guarded_by(x) __lock_annotate(guarded_by(x))
#define __pt_guarded_by(x) __lock_annotate(pt_guarded_by(x))
+/* Alignment builtins for better type checking and improved code generation. */
+/* Provide fallback versions for other compilers (GCC/Clang < 10): */
+#if !__has_builtin(__builtin_is_aligned)
+#define __builtin_is_aligned(x, align) \
+ (((__uintptr_t)x & ((align) - 1)) == 0)
+#endif
+#if !__has_builtin(__builtin_align_up)
+#define __builtin_align_up(x, align) \
+ ((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
+#endif
+#if !__has_builtin(__builtin_align_down)
+#define __builtin_align_down(x, align) \
+ ((__typeof__(x))((x)&(~((align)-1))))
+#endif
+
+#define __align_up(x, y) __builtin_align_up(x, y)
+#define __align_down(x, y) __builtin_align_down(x, y)
+#define __is_aligned(x, y) __builtin_is_aligned(x, y)
+
#endif /* !_SYS_CDEFS_H_ */
diff --git a/sys/sys/param.h b/sys/sys/param.h
index 15099849688d..b9f61eef0d58 100644
--- a/sys/sys/param.h
+++ b/sys/sys/param.h
@@ -296,9 +296,9 @@
#endif
#define nitems(x) (sizeof((x)) / sizeof((x)[0]))
#define rounddown(x, y) (((x)/(y))*(y))
-#define rounddown2(x, y) ((x)&(~((y)-1))) /* if y is power of two */
+#define rounddown2(x, y) __align_down(x, y) /* if y is power of two */
#define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
-#define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
+#define roundup2(x, y) __align_up(x, y) /* if y is powers of two */
#define powerof2(x) ((((x)-1)&(x))==0)
/* Macros for min/max. */
More information about the dev-commits-src-all
mailing list