public inbox for
 help / color / mirror / Atom feed
From: Sebastian Huber <>
Subject: [PATCH 2/6] Expose clang's alignment builtins and use them for roundup2/rounddown2
Date: Mon, 11 Jul 2022 09:15:43 +0200	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

From: Alex Richardson <>

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:

Reviewed By:	brooks, jhb, imp
Differential Revision:
 newlib/libc/include/sys/cdefs.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/newlib/libc/include/sys/cdefs.h b/newlib/libc/include/sys/cdefs.h
index e6a31bd51..7dff2c8d8 100644
--- a/newlib/libc/include/sys/cdefs.h
+++ b/newlib/libc/include/sys/cdefs.h
@@ -716,4 +716,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)
+#if !__has_builtin(__builtin_align_up)
+#define __builtin_align_up(x, align)	\
+	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
+#if !__has_builtin(__builtin_align_down)
+#define __builtin_align_down(x, align)	\
+	((__typeof__(x))((x)&(~((align)-1))))
+#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_ */

  parent reply	other threads:[~2022-07-11  7:15 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-11  7:15 [PATCH 0/6] Synchronize <sys/cdefs.h> with FreeBSD Sebastian Huber
2022-07-11  7:15 ` [PATCH 1/6] cdefs.h: remove intel_compiler support Sebastian Huber
2022-07-11  8:32   ` Corinna Vinschen
2022-07-11  9:10     ` Sebastian Huber
2022-07-11  9:43       ` Corinna Vinschen
2022-07-11  7:15 ` Sebastian Huber [this message]
2022-07-11  7:15 ` [PATCH 3/6] cdefs.h: Remove __GNUCLIKE___OFFSETOF, it's unused Sebastian Huber
2022-07-11  7:15 ` [PATCH 4/6] cdefs: Make __nosanitizeaddress work for KASAN as well Sebastian Huber
2022-07-11  7:15 ` [PATCH 5/6] cdefs: Add a default definition for __nosanitizememory Sebastian Huber
2022-07-11  7:15 ` [PATCH 6/6] cdefs.h: Remove redundant #ifdefs Sebastian Huber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).