From 5a3fa7c9ffa4b5d716d9e966498d153f66763894 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 13 Aug 2018 18:54:38 +0000 Subject: Minor optimisations in operator new(size_t, align_val_t) * libsupc++/new_opa.cc (operator new(size_t, align_val_t)): Use __is_pow2 to check for valid alignment. Avoid branching when rounding size to multiple of alignment. git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@263515 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 4 ++++ libstdc++-v3/libsupc++/new_opa.cc | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 2a785c38916..5f4e8221ab3 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,9 @@ 2018-08-13 Jonathan Wakely + * libsupc++/new_opa.cc (operator new(size_t, align_val_t)): Use + __is_pow2 to check for valid alignment. Avoid branching when rounding + size to multiple of alignment. + * include/Makefile.am: Install and for freestanding. * include/Makefile.in: Regenerate. * testsuite/17_intro/freestanding.cc: Check for and . diff --git a/libstdc++-v3/libsupc++/new_opa.cc b/libstdc++-v3/libsupc++/new_opa.cc index aa3e5dc4ce5..abb7451fafe 100644 --- a/libstdc++-v3/libsupc++/new_opa.cc +++ b/libstdc++-v3/libsupc++/new_opa.cc @@ -27,6 +27,7 @@ #include #include #include +#include #include "new" #if !_GLIBCXX_HAVE_ALIGNED_ALLOC && !_GLIBCXX_HAVE__ALIGNED_MALLOC \ @@ -105,7 +106,7 @@ operator new (std::size_t sz, std::align_val_t al) /* Alignment must be a power of two. */ /* XXX This should be checked by the compiler (PR 86878). */ - if (__builtin_expect (align & (align - 1), false)) + if (__builtin_expect (!std::__ispow2(align), false)) _GLIBCXX_THROW_OR_ABORT(bad_alloc()); /* malloc (0) is unpredictable; avoid it. */ @@ -120,8 +121,7 @@ operator new (std::size_t sz, std::align_val_t al) align = sizeof(void*); # endif /* C11: the value of size shall be an integral multiple of alignment. */ - if (std::size_t rem = sz & (align - 1)) - sz += align - rem; + sz = (sz + align - 1) & ~(align - 1); #endif void *p; -- cgit v1.2.3