From patchwork Thu Jul 14 23:50:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ed Smith-Rowland <3dw4rd@verizon.net> X-Patchwork-Id: 648624 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 3rrCBL1hvVz9s9Z for ; Fri, 15 Jul 2016 09:50:33 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=kEsHPIDR; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=tv+5FJyYJk1qanvgOAryrUHfo2BG4saV1E4DKgobmnxMsqSJpb 6Ndx6WciTR3TO7DWaH6qtGv49ZtP6qtU32iJ+bf8fRtWJCASuEaCbardv9lPk3rE adisxLaAbXjtQteb69C1NuXb701le18ydk0p+IY7ppRHtN7S5TYO1o9xk= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=BfCLghvnLyKwcFmFcCmTETQHHGU=; b=kEsHPIDREjL3BkabSO5F 6+9ATH7ys2id7+JbzHPsyHd+ZepqX1sRiVkUGVDgQaqzUe0QOZ9SWKNMRjvs72kU rawocOw3gkElmPZRPJHH5BDQvZ98249SF9kR04gW/3Ngfkw9D49EG2BWcrTJGhpC w9Qm3njSo+k48D90znSTSaY= Received: (qmail 28822 invoked by alias); 14 Jul 2016 23:50:25 -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 28796 invoked by uid 89); 14 Jul 2016 23:50:24 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL, BAYES_00, KAM_ASCII_DIVIDERS, NO_RDNS_DOTCOM_HELO, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy=2016-07-15, 20160715, edward, Edward X-Spam-User: qpsmtpd, 2 recipients X-HELO: vms173025pub.verizon.net Received: from vms173025pub.verizon.net (HELO vms173025pub.verizon.net) (206.46.173.25) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Thu, 14 Jul 2016 23:50:14 +0000 Received: from vz-proxy-l007.mx.aol.com ([64.236.82.157]) by vms173025.mailsrvcs.net (Oracle Communications Messaging Server 7.0.5.32.0 64bit (built Jul 16 2014)) with ESMTPA id <0OAB00GDFWVD9X60@vms173025.mailsrvcs.net>; Thu, 14 Jul 2016 18:50:02 -0500 (CDT) X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=WpDWSorv c=1 sm=1 tr=0 a=EDyElNE8DnJcdewKkuvZJw==:117 a=hTZS6uLwhHpx5LOQAS/v3KalXBI=:19 a=cAmyUtKerLwA:10 a=r77TgQKjGQsHNAKrUKIA:9 a=GcBqaS38gA9bM9vVyX0A:9 a=QEXdDO2ut3YA:10 a=2itzioW3hLIA:10 a=o1OHuDzbAAAA:8 a=XFh8-BuExFa9XTJDcwoA:9 a=mDV3o1hIAAAA:8 a=pjIMvr14dnag6GJap28A:9 Received: by 71.121.253.69 with SMTP id 0b1beb10; Thu, 14 Jul 2016 23:50:01 GMT To: "libstdc++@gcc.gnu.org" , gcc-patches , Jonathan Wakely From: Ed Smith-Rowland <3dw4rd@verizon.net> Subject: [libstdc++] Add C++17clamp Message-id: <57882528.8010501@verizon.net> Date: Thu, 14 Jul 2016 19:50:00 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.8.0 MIME-version: 1.0 Content-type: multipart/mixed; boundary=------------070303090407010901080504 Here is an implementation of P0025 An algorithm to "clamp" a value between a pair of boundary values. Testing is almost finished - looks good so far. OK if testing passes? I didn't see a feature test in any of the SD-6 papers or P0025. 2016-07-15 Edward Smith-Rowland <3dw4rd@verizon.net> Implement C++17 P0025 clamp. * include/bits/algorithmfwd.h: Declare clamp overloads. * include/bits/stl_algo.h: Implement clamp. * testsuite/25_algorithms/clamp/1.cc: New test. * testsuite/25_algorithms/clamp/2.cc: New test. * testsuite/25_algorithms/clamp/constexpr.cc: New test. * testsuite/25_algorithms/clamp/requirements/explicit_instantiation/ 1.cc: New test. * testsuite/25_algorithms/clamp/requirements/explicit_instantiation/ pod.cc: New test. Index: include/bits/algorithmfwd.h =================================================================== --- include/bits/algorithmfwd.h (revision 238302) +++ include/bits/algorithmfwd.h (working copy) @@ -48,6 +48,7 @@ all_of (C++0x) any_of (C++0x) binary_search + clamp (C++17) copy copy_backward copy_if (C++0x) @@ -208,6 +209,18 @@ bool binary_search(_FIter, _FIter, const _Tp&, _Compare); +#if __cplusplus > 201402L + template + _GLIBCXX14_CONSTEXPR + const _Tp& + clamp(const _Tp&, const _Tp&, const _Tp&); + + template + _GLIBCXX14_CONSTEXPR + const _Tp& + clamp(const _Tp&, const _Tp&, const _Tp&, _Compare); +#endif + template _OIter copy(_IIter, _IIter, _OIter); Index: include/bits/stl_algo.h =================================================================== --- include/bits/stl_algo.h (revision 238302) +++ include/bits/stl_algo.h (working copy) @@ -3698,8 +3698,44 @@ return std::__is_permutation(__first1, __last1, __first2, __last2, __gnu_cxx::__ops::__iter_comp_iter(__pred)); } -#endif +#if __cplusplus > 201402L + /** + * @brief Returns the value clamped between lo and hi. + * @ingroup sorting_algorithms + * @param __val A value of arbitrary type. + * @param __lo A lower limit of arbitrary type. + * @param __hi An upper limit of arbitrary type. + * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise. + */ + template + constexpr const _Tp& + clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi) + { + __glibcxx_assert(!(__hi < __lo)); + return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val; + } + + /** + * @brief Returns the value clamped between lo and hi. + * @ingroup sorting_algorithms + * @param __val A value of arbitrary type. + * @param __lo A lower limit of arbitrary type. + * @param __hi An upper limit of arbitrary type. + * @param __comp A comparison functor. + * @return max(__val, __lo, __comp) if __comp(__val, __hi) + * or min(__val, __hi, __comp) otherwise. + */ + template + constexpr const _Tp& + clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp) + { + __glibcxx_assert(!__comp(__hi, __lo)); + return __comp(__val, __lo) ? __lo : __comp(__hi, __val) ? __hi : __val; + } +#endif // C++17 +#endif // C++14 + #ifdef _GLIBCXX_USE_C99_STDINT_TR1 /** * @brief Shuffle the elements of a sequence using a uniform random Index: testsuite/25_algorithms/clamp/1.cc =================================================================== --- testsuite/25_algorithms/clamp/1.cc (nonexistent) +++ testsuite/25_algorithms/clamp/1.cc (working copy) @@ -0,0 +1,48 @@ +// { dg-options "-std=gnu++17" } + +// Copyright (C) 2016 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 +// . + +#include +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + const int x = std::clamp(1, 2, 4); + const int y = std::clamp(3, 2, 4); + const int z = std::clamp(5, 2, 4); + VERIFY( x == 2 ); + VERIFY( y == 3 ); + VERIFY( z == 4 ); + + const int xc = std::clamp(1, 2, 4, std::greater()); + const int yc = std::clamp(3, 2, 4, std::greater()); + const int zc = std::clamp(5, 2, 4, std::greater()); + VERIFY( xc == 4 ); + VERIFY( yc == 2 ); + VERIFY( zc == 2 ); +} + +int +main() +{ + test01(); + return 0; +} Index: testsuite/25_algorithms/clamp/2.cc =================================================================== --- testsuite/25_algorithms/clamp/2.cc (nonexistent) +++ testsuite/25_algorithms/clamp/2.cc (working copy) @@ -0,0 +1,102 @@ +// { dg-options "-std=gnu++17" } + +// Copyright (C) 2000-2016 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 +// . + +#include +#include +#include + +template + struct A { static const T a; }; + +template +const T A::a = T(3); + +void test02() +{ + bool test __attribute__((unused)) = true; + + VERIFY( 3 == std::clamp(A::a, 2, 4) ); + VERIFY( 2 == std::clamp(A::a, 1, 2) ); + VERIFY( 4 == std::clamp(A::a, 4, 6) ); + + VERIFY( 3u == std::clamp(A::a, 2u, 4u) ); + VERIFY( 2u == std::clamp(A::a, 1u, 2u) ); + VERIFY( 4u == std::clamp(A::a, 4u, 6u) ); + + VERIFY( 3l == std::clamp(A::a, 2l, 4l) ); + VERIFY( 2l == std::clamp(A::a, 1l, 2l) ); + VERIFY( 4l == std::clamp(A::a, 4l, 6l) ); + + VERIFY( 3ul == std::clamp(A::a, 2ul, 4ul) ); + VERIFY( 2ul == std::clamp(A::a, 1ul, 2ul) ); + VERIFY( 4ul == std::clamp(A::a, 4ul, 6ul) ); + +#ifdef _GLIBCXX_USE_LONG_LONG + VERIFY( 3ll == std::clamp(A::a, 2ll, 4ll) ); + VERIFY( 2ll == std::clamp(A::a, 1ll, 2ll) ); + VERIFY( 4ll == std::clamp(A::a, 4ll, 6ll) ); + + VERIFY( 3ull == std::clamp(A::a, 2ull, 4ull) ); + VERIFY( 2ull == std::clamp(A::a, 1ull, 2ull) ); + VERIFY( 4ull == std::clamp(A::a, 4ull, 6ull) ); +#endif + + VERIFY( (short)3 == std::clamp(A::a, (short)2, (short)4) ); + VERIFY( (short)2 == std::clamp(A::a, (short)1, (short)2) ); + VERIFY( (short)4 == std::clamp(A::a, (short)4, (short)6) ); + + VERIFY( (unsigned short)3 == std::clamp(A::a, (unsigned short)2, (unsigned short)4) ); + VERIFY( (unsigned short)2 == std::clamp(A::a, (unsigned short)1, (unsigned short)2) ); + VERIFY( (unsigned short)4 == std::clamp(A::a, (unsigned short)4, (unsigned short)6) ); + + VERIFY( (char)3 == std::clamp(A::a, (char)2, (char)4) ); + VERIFY( (char)2 == std::clamp(A::a, (char)1, (char)2) ); + VERIFY( (char)4 == std::clamp(A::a, (char)4, (char)6) ); + + VERIFY( (signed char)3 == std::clamp(A::a, (signed char)2, (signed char)4) ); + VERIFY( (signed char)2 == std::clamp(A::a, (signed char)1, (signed char)2) ); + VERIFY( (signed char)4 == std::clamp(A::a, (signed char)4, (signed char)6) ); + + VERIFY( (unsigned char)3 == std::clamp(A::a, (unsigned char)2, (unsigned char)4) ); + VERIFY( (unsigned char)2 == std::clamp(A::a, (unsigned char)1, (unsigned char)2) ); + VERIFY( (unsigned char)4 == std::clamp(A::a, (unsigned char)4, (unsigned char)6) ); + + VERIFY( (wchar_t)3 == std::clamp(A::a, (wchar_t)2, (wchar_t)4) ); + VERIFY( (wchar_t)2 == std::clamp(A::a, (wchar_t)1, (wchar_t)2) ); + VERIFY( (wchar_t)4 == std::clamp(A::a, (wchar_t)4, (wchar_t)6) ); + + VERIFY( 3.0 == std::clamp(A::a, 2.0, 4.0) ); + VERIFY( 2.0 == std::clamp(A::a, 1.0, 2.0) ); + VERIFY( 4.0 == std::clamp(A::a, 4.0, 6.0) ); + + VERIFY( 3.0f == std::clamp(A::a, 2.0f, 4.0f) ); + VERIFY( 2.0f == std::clamp(A::a, 1.0f, 2.0f) ); + VERIFY( 4.0f == std::clamp(A::a, 4.0f, 6.0f) ); + + VERIFY( 3.0l == std::clamp(A::a, 2.0l, 4.0l) ); + VERIFY( 2.0l == std::clamp(A::a, 1.0l, 2.0l) ); + VERIFY( 4.0l == std::clamp(A::a, 4.0l, 6.0l) ); +} + +int +main() +{ + test02(); + return 0; +} Index: testsuite/25_algorithms/clamp/constexpr.cc =================================================================== --- testsuite/25_algorithms/clamp/constexpr.cc (nonexistent) +++ testsuite/25_algorithms/clamp/constexpr.cc (working copy) @@ -0,0 +1,25 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 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 +// . + +#include +#include + +static_assert(std::clamp(2, 0, 1) == 1, ""); +static_assert(std::clamp(2, 0, 1, std::greater()) == 0, ""); Index: testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc =================================================================== --- testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc (nonexistent) +++ testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc (working copy) @@ -0,0 +1,38 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 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 +// . + +#include +#include +#include + +namespace std +{ + using __gnu_test::NonDefaultConstructible; + + typedef NonDefaultConstructible value_type; + typedef value_type* iterator_type; + typedef std::less compare_type; + + template const value_type& clamp(const value_type&, + const value_type&, const value_type&); + template const value_type& clamp(const value_type&, + const value_type&, const value_type&, + compare_type); +} Index: testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc =================================================================== --- testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc (nonexistent) +++ testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc (working copy) @@ -0,0 +1,38 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 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 +// . + + +#include +#include + +namespace std +{ + using __gnu_test::pod_int; + + typedef pod_int value_type; + typedef value_type* iterator_type; + typedef std::less compare_type; + + template const value_type& clamp(const value_type&, + const value_type&, const value_type&); + template const value_type& clamp(const value_type&, + const value_type&, const value_type&, + compare_type); +}