public inbox for
help / color / mirror / Atom feed
From: Sebastian Huber <>
Subject: [newlib-cygwin] Expose clang's alignment builtins and use them for roundup2/rounddown2
Date: Mon, 11 Jul 2022 11:53:49 +0000 (GMT)	[thread overview]
Message-ID: <> (raw);h=8054ce555fdcd5fe9c2ad75cff6cd80218c9d0b1

commit 8054ce555fdcd5fe9c2ad75cff6cd80218c9d0b1
Author: Alex Richardson <>
Date:   Wed Feb 3 15:27:17 2021 +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:
    Reviewed By:    brooks, jhb, imp
    Differential Revision:

 newlib/libc/sys/rtems/include/sys/param.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/newlib/libc/sys/rtems/include/sys/param.h b/newlib/libc/sys/rtems/include/sys/param.h
index 925f0fc44..2e44057dd 100644
--- a/newlib/libc/sys/rtems/include/sys/param.h
+++ b/newlib/libc/sys/rtems/include/sys/param.h
@@ -233,9 +233,9 @@
 #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. */

             reply	other threads:[~2022-07-11 11:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-11 11:53 Sebastian Huber [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-07-11  9:53 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).