From patchwork Mon Jun 2 13:56:07 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 354901 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 2DE9C140086 for ; Mon, 2 Jun 2014 23:56:29 +1000 (EST) 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:references:mime-version :content-type:content-transfer-encoding:in-reply-to; q=dns; s= default; b=tdDvpoE01fdaP5Vt/Q9NzXuYTdV86PAL7NKk09V4oJcHyIV/9P0kg 8FVw35wvESRo563ILD1qrzbdP4tgyKZx4eY8VCEAjda22qPNJEkCU5K91Z2S7CEp tvzXzHAWx1NdFYGNXASuCHZuskY49aun9L6IafJnvD1SpQIqcGt0/Q= 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:references:mime-version :content-type:content-transfer-encoding:in-reply-to; s=default; bh=n6IeoZxbtNHxSATdgWix/+oby9o=; b=ZAvYScde9uE416Z17w9eTNUJ3Pd0 OgfuankmOeDhus/H6fCaMEDqQ/S0Mwn7Qg1d2J0uDY1essQeqKxEapS+QMSwKLZH gtByhGXCwsVXFnm1+3A3LWerQF3NdrM5Y+uArUEvM16B6WOn0JynJKu25g17R66i WrqeSG70nriyX3Y= Received: (qmail 29423 invoked by alias); 2 Jun 2014 13:56:22 -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 29406 invoked by uid 89); 2 Jun 2014 13:56:22 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients 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, 02 Jun 2014 13:56:13 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s52Du9sx010990 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 2 Jun 2014 09:56:09 -0400 Received: from localhost (vpn1-5-165.ams2.redhat.com [10.36.5.165]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s52Du83v018895; Mon, 2 Jun 2014 09:56:08 -0400 Date: Mon, 2 Jun 2014 14:56:07 +0100 From: Jonathan Wakely To: =?iso-8859-1?Q?R=FCdiger?= Sonderfeld Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [PATCHv3 2/2] libstdc++: Add std::aligned_union. Message-ID: <20140602135607.GU6953@redhat.com> References: <2914265.gtnDFNoboG@descartes> <4764373.hhBqorrHSa@descartes> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <4764373.hhBqorrHSa@descartes> User-Agent: Mutt/1.5.23 (2014-03-12) On 16/04/14 17:47 +0200, RĂ¼diger Sonderfeld wrote: >Of course I forgot to replace one _M_ instance. This should work now. >Sorry about this. > >-- 8< --------------------------------------------------------- >8 -- > >C++11: [meta.trans.other] > >* libstdc++-v3/testsuite/20_util/aligned_union/1.cc: New file. >* libstdc++-v3/include/std/type_traits (__strictest_alignment): New > helper struct. > (aligned_union): New struct (C++11). > (aligned_union_t): New type alias (C++14). Hi, I've fixed up the patch to meet the coding standards, define the static member aligned_union::alignment_value, check the precondition, update the docs and fix a test failure. Tested x86_64-linux, committed to trunk. Thanks for adding this missing piece! commit 7018ff5142ba1413b140d9d69c7263565ecae000 Author: Jonathan Wakely Date: Sun Jun 1 23:33:29 2014 +0100 2014-06-02 R??diger Sonderfeld Jonathan Wakely * libstdc++-v3/include/std/type_traits (__strictest_alignment): New helper struct. (aligned_union): New struct (C++11). (aligned_union_t): New type alias (C++14). * doc/xml/manual/status_cxx2011.xml: Update. * libstdc++-v3/testsuite/20_util/aligned_union/1.cc: New file. * testsuite/20_util/declval/requirements/1_neg.cc: Adjust dg-error line number. diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2011.xml b/libstdc++-v3/doc/xml/manual/status_cxx2011.xml index b3c24d8..cad4111 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2011.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2011.xml @@ -871,11 +871,10 @@ particular release. - 20.9.7.6 Other transformations - Partial - Missing aligned_union. + Y + 20.10 diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 1ff2e62..da8a95f 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -1870,6 +1870,52 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION }; }; + template + struct __strictest_alignment + { + static const size_t _S_alignment = 0; + static const size_t _S_size = 0; + }; + + template + struct __strictest_alignment<_Tp, _Types...> + { + static const size_t _S_alignment = + alignof(_Tp) > __strictest_alignment<_Types...>::_S_alignment + ? alignof(_Tp) : __strictest_alignment<_Types...>::_S_alignment; + static const size_t _S_size = + sizeof(_Tp) > __strictest_alignment<_Types...>::_S_size + ? sizeof(_Tp) : __strictest_alignment<_Types...>::_S_size; + }; + + /** + * @brief Provide aligned storage for types. + * + * [meta.trans.other] + * + * Provides aligned storage for any of the provided types of at + * least size _Len. + * + * @see aligned_storage + */ + template + struct aligned_union + { + private: + static_assert(sizeof...(_Types) != 0, "At least one type is required"); + + using __strictest = __strictest_alignment<_Types...>; + static const size_t _S_len = _Len > __strictest::_S_size + ? _Len : __strictest::_S_size; + public: + /// The value of the strictest alignment of _Types. + static const size_t alignment_value = __strictest::_S_alignment; + /// The storage. + typedef typename aligned_storage<_S_len, alignment_value>::type type; + }; + + template + const size_t aligned_union<_Len, _Types...>::alignment_value; // Decay trait for arrays and functions, used for perfect forwarding // in make_pair, make_tuple, etc. @@ -2206,6 +2252,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __alignof__(typename __aligned_storage_msa<_Len>::__type)> using aligned_storage_t = typename aligned_storage<_Len, _Align>::type; + template + using aligned_union_t = typename aligned_union<_Len, _Types...>::type; + /// Alias template for decay template using decay_t = typename decay<_Tp>::type; diff --git a/libstdc++-v3/testsuite/20_util/aligned_union/1.cc b/libstdc++-v3/testsuite/20_util/aligned_union/1.cc new file mode 100644 index 0000000..5285bb0 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/aligned_union/1.cc @@ -0,0 +1,72 @@ +// { dg-options " -std=gnu++11 " } +// { dg-do compile } + +// 2014-04-16 R??diger Sonderfeld + +// Copyright (C) 2014 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software +// Foundation; either version 3, or (at your option) any later +// version. + +// This library is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING3. If not see +// . + +// C++11 [meta.trans.other] 20.9.7.6: aligned_union + +#include +#include + +struct MSAlignType { } __attribute__((__aligned__)); + +template + struct mymax + { + static const std::size_t alignment = 0; + static const std::size_t size = 0; + }; + +template + struct mymax + { + static const std::size_t alignment = alignof(L) > mymax::alignment + ? alignof(L) : mymax::alignment; + static const std::size_t size = sizeof(L) > mymax::size + ? sizeof(L) : mymax::size; + }; + +void test01() +{ + using std::aligned_union; + using std::alignment_of; + using std::size_t; + using namespace __gnu_test; + + const size_t max_a = mymax::alignment; + const size_t max_s = mymax::size; + + typedef aligned_union<0, char, short, int, double, int[4], + ClassType, MSAlignType> au_type; + static_assert(au_type::alignment_value == max_a, "Alignment value"); + static_assert(sizeof(au_type::type) >= max_s, "Storage size"); + + typedef aligned_union au_type2; + static_assert(sizeof(au_type2::type) >= max_s+100, + "Storage size (at least len)"); +} + +int main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc b/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc index 04e6b71..774858c 100644 --- a/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc +++ b/libstdc++-v3/testsuite/20_util/declval/requirements/1_neg.cc @@ -19,7 +19,7 @@ // with this library; see the file COPYING3. If not see // . -// { dg-error "static assertion failed" "" { target *-*-* } 2036 } +// { dg-error "static assertion failed" "" { target *-*-* } 2082 } #include