From patchwork Mon Aug 13 19:00:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 957227 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-483586-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="j8i8QV2O"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41q4nY0Hyrz9rvt for ; Tue, 14 Aug 2018 05:01:04 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=gmBMA7g88lPiH2BX9q8ha1jR1FFb9Wce0BtajMgFCYBsIJ7yw2 uu/Pap2+0um4j8eSk8B9Xe0VEmJTmVXbqn/3Vf+/XLftFBfBcZHxkJpuWv7Y/r7p A8MmhY6Mc5hJ0fyxe42LGHI9t2VLMLk3aK58SxP9+KzbMkOafA5gmi4eI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=AcZtkdEn6pJSg/aRhcsJcK7s3u0=; b=j8i8QV2Ok7FhpySTQciZ cX6KIxuYUHUA0PibEyowEbc9f/WEbzETX46nS0toc6UcJFnT4piHCCxJoHNZ9P3f D4nAqmoz4tnbuACKUC+2hVDkOHZDZ8CXQ4sooqSRnD/+faiYlfJUwSz9Ai53lLh3 AIvdBkZGJN7Y1yexJ8sZx8k= Received: (qmail 117046 invoked by alias); 13 Aug 2018 19:00:56 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 116816 invoked by uid 89); 13 Aug 2018 19:00:41 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 13 Aug 2018 19:00:38 +0000 Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AC478C0733DF; Mon, 13 Aug 2018 19:00:28 +0000 (UTC) Received: from localhost (ovpn-117-178.phx2.redhat.com [10.3.117.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C7A8AD004; Mon, 13 Aug 2018 19:00:28 +0000 (UTC) Date: Mon, 13 Aug 2018 20:00:27 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Lars Gullik =?iso-8859-1?q?Bj=F8nnes?= Subject: [PATCH] Minor optimisations in operator new(size_t, align_val_t) Message-ID: <20180813190027.GA10327@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) Thanks to Lars for the suggestions. * 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. Tested x86_64-linux, committed to trunk. commit 766770a4f8692ef5fda2d1f987054864efb1c3a8 Author: Jonathan Wakely Date: Mon Aug 13 13:56:04 2018 +0100 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. 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;