From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id B498E3857039 for ; Mon, 15 Mar 2021 14:18:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B498E3857039 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-604-pT3C9smzP-qIoSS4Eelsiw-1; Mon, 15 Mar 2021 10:18:12 -0400 X-MC-Unique: pT3C9smzP-qIoSS4Eelsiw-1 Received: by mail-qv1-f69.google.com with SMTP id l7so3631581qvz.19 for ; Mon, 15 Mar 2021 07:18:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=SH7AWt/lp8ExGe6zSoMhI8s7yxpbnL+STdZiVnmpEQ8=; b=WoZBCjuUdNS4YujH1uph3W+mfvthdfNPUsEg48AusiRhLSRzx8yLAc/KYsKf2NlIhp 0p4RwjqKXjtCeTWxTxTD2QLHpBRlSBXK8hg99Y9yvStOujCBDCLrrQo9CwPI0aJyHEZF LYXu/Rax4wDr3n4THIDlSk6nrBjOMlNTJQC+KclNGfW2RqfB9sJtQ6hLwzUicoXRiMyv byWKwuOAMmlWQzqWkdsJLiM45sICS4HAFLztUD80bBGdUnZ2BIFBa4dDPl0i4viM8CpS DJPD4OM+ywLDW3Ps0DEcI345h3A0vPkwmf4ZvkyKEZzupmOp6HlgmlCSSS7e+Kb0Z3hE +RaA== X-Gm-Message-State: AOAM532eV7g+x+60CMKeAV7MAWpcz3lkEsvNepEUeQCyNITmAVEbAkja v/paQeCmv8mO/L3TfAHP9sSnjHmcp24QxAa/qkHqJBnluVJ7p4896XhQkDUrolraATNzorzmS6T 3Iw8y5yDwCGtD+QCycd7JAGdHO1bMNkTaS9axrmcGegZLzQOd6VLr5JwYZp9qOCEoXM8= X-Received: by 2002:a37:42cb:: with SMTP id p194mr621178qka.213.1615817891493; Mon, 15 Mar 2021 07:18:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPUlZMGV5azqnAgKoXCU/dwP1XC/nqo+jrOrv1gwZUs9HzXV3jjswd0tIbd9CrmrtT+voiYw== X-Received: by 2002:a37:42cb:: with SMTP id p194mr621143qka.213.1615817891072; Mon, 15 Mar 2021 07:18:11 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id m17sm12774729qkh.82.2021.03.15.07.18.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Mar 2021 07:18:10 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: Implement missing operator overloads in max_size_type.h Date: Mon, 15 Mar 2021 10:18:08 -0400 Message-Id: <20210315141808.2437204-1-ppalka@redhat.com> X-Mailer: git-send-email 2.31.0.rc2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-15.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Mar 2021 14:18:16 -0000 This implements operator++, operator-- and operator<=> for the integer-class types defined in max_size_type.h, which I overlooked when originally implementing the class. Tested on x86_64-pc-linux-gnu, does this look OK for trunk? libstdc++-v3/ChangeLog: * include/bits/max_size_type.h (__max_size_type::operator _Tp): Fix formatting. (__max_size_type::operator++): Define. (__max_size_type::operator--): Likewise. (__max_size_type::operator<=>): Conditionally define (in place of the other comparison operators). (__max_diff_type::operator _Tp): Fix formatting. (__max_diff_type::operator++): Define. (__max_diff_type::operator--): Likewise. (__max_diff_type::operator<=>): Conditionally define (in place of the other comparison operators). * testsuite/std/ranges/iota/max_size_type.cc (test01): Test these operator overloads. --- libstdc++-v3/include/bits/max_size_type.h | 78 ++++++++++++++++++- .../std/ranges/iota/max_size_type.cc | 36 +++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/max_size_type.h b/libstdc++-v3/include/bits/max_size_type.h index 05984a30f1d..27d63797c49 100644 --- a/libstdc++-v3/include/bits/max_size_type.h +++ b/libstdc++-v3/include/bits/max_size_type.h @@ -71,7 +71,8 @@ namespace ranges __max_size_type(const __max_diff_type& __d) noexcept; template requires integral<_Tp> || __is_int128<_Tp> - constexpr explicit operator _Tp() const noexcept + constexpr explicit + operator _Tp() const noexcept { return _M_val; } constexpr explicit @@ -90,6 +91,30 @@ namespace ranges operator-() const noexcept { return operator~() + 1; } + constexpr __max_size_type& + operator++() noexcept + { return *this += 1; } + + constexpr __max_size_type + operator++(int) noexcept + { + auto __tmp = *this; + ++*this; + return __tmp; + } + + constexpr __max_size_type& + operator--() noexcept + { return *this -= 1; } + + constexpr __max_size_type + operator--(int) noexcept + { + auto __tmp = *this; + --*this; + return __tmp; + } + constexpr __max_size_type& operator+=(const __max_size_type& __r) noexcept { @@ -355,6 +380,16 @@ namespace ranges operator==(const __max_size_type& __l, const __max_size_type& __r) noexcept { return __l._M_val == __r._M_val && __l._M_msb == __r._M_msb; } +#if __cpp_lib_three_way_comparison + friend constexpr strong_ordering + operator<=>(const __max_size_type& __l, const __max_size_type& __r) noexcept + { + if (__l._M_msb ^ __r._M_msb) + return __l._M_msb ? strong_ordering::greater : strong_ordering::less; + else + return __l._M_val <=> __r._M_val; + } +#else friend constexpr bool operator!=(const __max_size_type& __l, const __max_size_type& __r) noexcept { return !(__l == __r); } @@ -379,6 +414,7 @@ namespace ranges friend constexpr bool operator>=(const __max_size_type& __l, const __max_size_type& __r) noexcept { return __r <= __l; } +#endif #if __SIZEOF_INT128__ using __rep = unsigned __int128; @@ -417,7 +453,8 @@ namespace ranges { } template requires integral<_Tp> || __is_int128<_Tp> - constexpr explicit operator _Tp() const noexcept + constexpr explicit + operator _Tp() const noexcept { return static_cast<_Tp>(_M_rep); } constexpr explicit @@ -436,6 +473,30 @@ namespace ranges operator~() const noexcept { return __max_diff_type(~_M_rep); } + constexpr __max_diff_type& + operator++() noexcept + { return *this += 1; } + + constexpr __max_diff_type + operator++(int) noexcept + { + auto __tmp = *this; + ++*this; + return __tmp; + } + + constexpr __max_diff_type& + operator--() noexcept + { return *this -= 1; } + + constexpr __max_diff_type + operator--(int) noexcept + { + auto __tmp = *this; + --*this; + return __tmp; + } + constexpr __max_diff_type& operator+=(const __max_diff_type& __r) noexcept { @@ -647,6 +708,18 @@ namespace ranges operator==(const __max_diff_type& __l, const __max_diff_type& __r) noexcept { return __l._M_rep == __r._M_rep; } +#if __cpp_lib_three_way_comparison + constexpr strong_ordering + operator<=>(const __max_diff_type& __r) const noexcept + { + const auto __lsign = _M_rep._M_msb; + const auto __rsign = __r._M_rep._M_msb; + if (__lsign ^ __rsign) + return __lsign ? strong_ordering::less : strong_ordering::greater; + else + return _M_rep <=> __r._M_rep; + } +#else friend constexpr bool operator!=(const __max_diff_type& __l, const __max_diff_type& __r) noexcept { return !(__l == __r); } @@ -673,6 +746,7 @@ namespace ranges friend constexpr bool operator>=(const __max_diff_type& __l, const __max_diff_type& __r) noexcept { return !(__l < __r); } +#endif private: __max_size_type _M_rep = 0; diff --git a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc index 9284d71dbeb..983bdfbdaa6 100644 --- a/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc +++ b/libstdc++-v3/testsuite/std/ranges/iota/max_size_type.cc @@ -145,6 +145,42 @@ test01() auto h = [] (auto a) { a <<= a; return a; }; static_assert(h(max_size_t(3)) == 24); static_assert(h(max_diff_t(3)) == 24); + + auto w = [] (auto a) { + const auto b = a; + VERIFY( a++ == b && a == b+1 ); + VERIFY( a-- == b+1 && a == b ); + + VERIFY( ++(++a) == b+2 ); + VERIFY( --(--a) == b ); + return true; + }; + static_assert(w(max_size_t(10))); + static_assert(w(-max_diff_t(10))); + +#if __cpp_lib_three_way_comparison + static_assert(max_size_t{1} <=> max_size_t{9} == std::strong_ordering::less); + static_assert(max_size_t{3} <=> max_size_t{2} == std::strong_ordering::greater); + static_assert(max_size_t{5} <=> max_size_t{5} == std::strong_ordering::equal); + static_assert(~max_size_t{1} <=> ~max_size_t{9} == std::strong_ordering::greater); + static_assert(~max_size_t{3} <=> ~max_size_t{2} == std::strong_ordering::less); + static_assert(~max_size_t{5} <=> ~max_size_t{5} == std::strong_ordering::equal); + static_assert(~max_size_t{5} <=> max_size_t{9} == std::strong_ordering::greater); + static_assert(~max_size_t{9} <=> max_size_t{5} == std::strong_ordering::greater); + static_assert(max_size_t{5} <=> ~max_size_t{9} == std::strong_ordering::less); + static_assert(max_size_t{9} <=> ~max_size_t{5} == std::strong_ordering::less); + + static_assert(max_diff_t{1} <=> max_diff_t{9} == std::strong_ordering::less); + static_assert(max_diff_t{3} <=> max_diff_t{2} == std::strong_ordering::greater); + static_assert(max_diff_t{5} <=> max_diff_t{5} == std::strong_ordering::equal); + static_assert(max_diff_t{-1} <=> max_diff_t{-9} == std::strong_ordering::greater); + static_assert(max_diff_t{-3} <=> max_diff_t{-2} == std::strong_ordering::less); + static_assert(max_diff_t{-5} <=> max_diff_t{-5} == std::strong_ordering::equal); + static_assert(max_diff_t{-5} <=> max_diff_t{9} == std::strong_ordering::less); + static_assert(max_diff_t{-9} <=> max_diff_t{5} == std::strong_ordering::less); + static_assert(max_diff_t{5} <=> max_diff_t{-9} == std::strong_ordering::greater); + static_assert(max_diff_t{9} <=> max_diff_t{-5} == std::strong_ordering::greater); +#endif } template -- 2.31.0.rc2