From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1921) id D77C6385AE6D; Mon, 11 Jul 2022 11:53:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D77C6385AE6D Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Sebastian Huber To: newlib-cvs@sourceware.org Subject: [newlib-cygwin] Expose clang's alignment builtins and use them for roundup2/rounddown2 X-Act-Checkin: newlib-cygwin X-Git-Author: Alex Richardson X-Git-Refname: refs/heads/master X-Git-Oldrev: 5bc5689a6aa4893b2076aace1b6d3b919f8e91ba X-Git-Newrev: 8054ce555fdcd5fe9c2ad75cff6cd80218c9d0b1 Message-Id: <20220711115349.D77C6385AE6D@sourceware.org> Date: Mon, 11 Jul 2022 11:53:49 +0000 (GMT) X-BeenThere: newlib-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib GIT logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Jul 2022 11:53:50 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D8054ce555fd= cd5fe9c2ad75cff6cd80218c9d0b1 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 =20 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. =20 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. =20 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. =20 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=3D98641 =20 Reviewed By: brooks, jhb, imp Differential Revision: https://reviews.freebsd.org/D28332 Diff: --- 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/rt= ems/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 @@ #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))=3D=3D0) =20 /* Macros for min/max. */