{"id":2237913,"url":"http://patchwork.ozlabs.org/api/patches/2237913/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260513150327.910897-1-tkaminsk@redhat.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260513150327.910897-1-tkaminsk@redhat.com>","list_archive_url":null,"date":"2026-05-13T14:52:50","name":"[v3] libstdc++: Fix reserve of size_t(-1) elements in piecewise_constant_distribution. [PR113761]","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"583d2f9d14c2d2a641b55b92fd9002cfa7730c20","submitter":{"id":90409,"url":"http://patchwork.ozlabs.org/api/people/90409/?format=json","name":"Tomasz Kamiński","email":"tkaminsk@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260513150327.910897-1-tkaminsk@redhat.com/mbox/","series":[{"id":504176,"url":"http://patchwork.ozlabs.org/api/series/504176/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=504176","date":"2026-05-13T14:52:50","name":"[v3] libstdc++: Fix reserve of size_t(-1) elements in piecewise_constant_distribution. [PR113761]","version":3,"mbox":"http://patchwork.ozlabs.org/series/504176/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2237913/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2237913/checks/","tags":{},"related":[],"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=IAKQsL1z;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=IAKQsL1z","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","sourceware.org; arc=none smtp.remote-ip=170.10.129.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4gFxZ94WxSz1yHW\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 14 May 2026 01:04:16 +1000 (AEST)","from vm01.sourceware.org (localhost [IPv6:::1])\n\tby sourceware.org (Postfix) with ESMTP id 621544B920AD\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 13 May 2026 15:04:14 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n by sourceware.org (Postfix) with ESMTP id 61A0B4B92095\n for <gcc-patches@gcc.gnu.org>; Wed, 13 May 2026 15:03:33 +0000 (GMT)","from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-591--uDE84FPOrusJdAdN9K6Ww-1; Wed,\n 13 May 2026 11:03:31 -0400","from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n (No client certificate requested)\n by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id 373D51800612; Wed, 13 May 2026 15:03:30 +0000 (UTC)","from localhost (unknown [10.44.32.179])\n by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id 89D601953947; Wed, 13 May 2026 15:03:29 +0000 (UTC)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 621544B920AD","OpenDKIM Filter v2.11.0 sourceware.org 61A0B4B92095"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 61A0B4B92095","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 61A0B4B92095","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778684613; cv=none;\n b=kbDhxCRrjAf+xzT//7dsLNYCaAu3fVKlB3aT9x8hQKbZ1O6b7+hhoBoJfP9aCZTvcLfrLL2vJCI8RQBJ0b1wdko2ykwyOtWuFSQYZYoOksevTNxtal8SvMmfeVf5HCyUXDJebaJ0TUKJC/nReZOISCUHJf5CXZS+D+cSK47LpBI=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1778684613; c=relaxed/simple;\n bh=BeXy+1r9br8gN4ioRAWQldgtp47Zi+4L7RWqI1l14Wg=;\n h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version;\n b=NSWgVRCQ+6r1+bYw5N9DPoM6TrlVWwd0aNn5rHsb+Z35uDHq2Lf2HW8Kzc/gxCCrxI+NtiMZKl/PdyIA4puHEcKdTISS/dUo5dWRRxVAa8Cpvrk7bb8vfJiYVE9hfmOhQ0MumgNd0o1Fto28IPmxy/EXmX+PFazo1tB8veFE51Y=","ARC-Authentication-Results":"i=1; sourceware.org;\n dkim=pass (1024-bit key, unprotected)\n header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=IAKQsL1z","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1778684613;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=l3SsidTEOkn2wOUWMOaDIHoitLoe8Tz4BGq5WNXQOuM=;\n b=IAKQsL1zNpKFJ0pdDpE5OZWOpoVmMupxby3szOvp6TKfRGZ7JypPcoMoqs6yqbSBPUHmT6\n zBNeh3L2zYYq1dSRlyH+kYFicX6IT1kzHodtobHbJKkEgkGr6PDIRy0lvdERY320W/3oOl\n kgW9qXTMcO5AOUW87Dj1VTyGQo+X0lM=","X-MC-Unique":"-uDE84FPOrusJdAdN9K6Ww-1","X-Mimecast-MFC-AGG-ID":"-uDE84FPOrusJdAdN9K6Ww_1778684610","From":"=?utf-8?q?Tomasz_Kami=C5=84ski?= <tkaminsk@redhat.com>","To":"libstdc++@gcc.gnu.org,\n\tgcc-patches@gcc.gnu.org","Subject":"[PATCH v3] libstdc++: Fix reserve of size_t(-1) elements in\n piecewise_constant_distribution. [PR113761]","Date":"Wed, 13 May 2026 16:52:50 +0200","Message-ID":"<20260513150327.910897-1-tkaminsk@redhat.com>","In-Reply-To":"\n <CAKvuMXAFRPq7=z9PsMPwZnJUtBYgJ_85nCY=WWHC=+79Yc32Qw@mail.gmail.com>","References":"\n <CAKvuMXAFRPq7=z9PsMPwZnJUtBYgJ_85nCY=WWHC=+79Yc32Qw@mail.gmail.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.0 on 10.30.177.17","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"prPrPgQ3w5FHkYPMjdAHbL4w2N622MJT4f-HOEkQ9Ng_1778684610","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","content-type":"text/plain; charset=\"US-ASCII\"; x-default=true","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"The piecewise_constant_distribution constructor from std::initializer_list il,\nunconditionally reserve _M_den for il.size()-1 elements. In case when the il.size()\nwas zero, this led to unsigned overflow, and attempt to allocate size_t(-1)\nelements.\n\nThis patch addresses above, by refactoring the constructors of param_type for\nboth piecewise_constant_distribution and piecewise_linear_distribution, to\nexit early (and do not populate internal vectors) if number of intervals range is\nsmaller than two. For the constructor accepting pair of iterators, this is done\nby checking result of __detail::__load_first2, that extracts up to two elements,\nand returns false, if less than two is found.\n\nFurthermore, we if the number of intervals is equal to two (for iterator __bbegin\nis at __bend after __load_first2), we store densities value on stack, and call\nnewly introduced an _M_initialize2 helper, that does not populate internal vector\nif __ints and __dens values correspond to default configuration.\n\nWith both of above changes, we no longer populate _M_int and _M_den with default\nvalues, and corresponding code that clears them in _M_initialize is not necessary.\nThis avoids any unnecessary memory allocators.\n\nFor constructor accepting two iterators, we reserve required space in _M_int vector,\nif the iterators are forward (or model sized_sentinel in C++20). The _M_den\ninitialization is performed afterwards, so _M_int size is already determined\n(even for input iterators) and it can be used for call to reserve.\n\nFinally, the _M_initialize members are renamed to _M_configure, so the new\nimplementation that skips the check, is not used by TU compiler will other\nthat would invoke it with empty vector, and thus retrigger the issue.\n\n\tPR libstdc++/113761\n\nlibstdc++-v3/ChangeLog:\n\n\t* include/bits/random.h\n\t(piecewise_constant_distribution::param_type::_M_initialize)\n\t(piecewise_linear_distribution::param_type::_M_initialize): Remove.\n\t(piecewise_constant_distribution::param_type::_M_configure)\n\t(piecewise_linear_distribution::param_type::_M_configure): Define.\n\t(piecewise_constant_distribution::param_type::_M_initialize2)\n\t(piecewise_linear_distribution::param_type::_M_initialize2): Declare.\n\t* include/bits/random.tcc (__detail::__load_first2): Define.\n\t(piecewise_constant_distribution::param_type::_M_initialize)\n\t(piecewise_linear_distribution::param_type::_M_initialize):\n\tRename to...\n\t(piecewise_constant_distribution::param_type::_M_configure)\n\t(piecewise_linear_distribution::param_type::_M_configure):\n\tRenamed implementation of _M_initialize, that removes checks for\n\tdefault values.\n\t(piecewise_constant_distribution::param_type::_M_initialize2)\n\t(piecewise_linear_distribution::param_type::_M_initialize2): Define.\n\t(piecewise_constant_distribution::param_type::param_type)\n\t(piecewise_linear_distribution::param_type::param_type):\n\tExit early for less that two intervals. Use _M_initialize2 to handle\n\ttwo intervals case. Reserve _M_int for iterators case.\n\t* testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc:\n\tTest input and forward iterators, in addition to random_access ones.\n\t* testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc:\n\tLikewise.\n\t* testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc:\n\tNew test.\n\t* testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc:\n\tNew test.\n---\nv2 changes:\n- rename _M_intialize to _M_configure to avoid ABI break, I used configure\n  as name, because we call it with already initialized _M_int and _M_den\n- updated commit description to fix the typos and unclear sentences\n- added explanation for rename to commit description\n- moved * to type in _M_initialize2 signatures\n- used for-each to initialize entities for picewise_linear_distribution\n  constructor from initializer_list\nI haven't updated the copying from __wbegin to use _M_assign, as the\nconditions need to look at reference type (if it is _Real), so we add\ncomplexity, without real benefit.\n\nTesting on x86_64-linux. *picewise* test passed again, so didn't make\nnew typos. OK for trunk when all test passes?\n\n libstdc++-v3/include/bits/random.h            |  10 +-\n libstdc++-v3/include/bits/random.tcc          | 224 ++++++++++++++----\n .../cons/fallback.cc                          | 108 +++++++++\n .../cons/range.cc                             |  17 +-\n .../cons/fallback.cc                          | 127 ++++++++++\n .../cons/range.cc                             |  17 +-\n 6 files changed, 439 insertions(+), 64 deletions(-)\n create mode 100644 libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc\n create mode 100644 libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc","diff":"diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h\nindex 3fda69c4399..5d037465d3c 100644\n--- a/libstdc++-v3/include/bits/random.h\n+++ b/libstdc++-v3/include/bits/random.h\n@@ -6466,7 +6466,10 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)\n \n       private:\n \tvoid\n-\t_M_initialize();\n+\t_M_configure();\n+\n+\tvoid\n+\t_M_initialize2(const _RealType* __ints, _RealType __den);\n \n \tstd::vector<_RealType> _M_int;\n \tstd::vector<double> _M_den;\n@@ -6746,7 +6749,10 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)\n \n       private:\n \tvoid\n-\t_M_initialize();\n+\t_M_configure();\n+\n+\tvoid\n+\t_M_initialize2(const _RealType* __ints, const _RealType* __dens);\n \n \tstd::vector<_RealType> _M_int;\n \tstd::vector<double> _M_den;\ndiff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc\nindex 4475e365c2d..80b51d099fa 100644\n--- a/libstdc++-v3/include/bits/random.tcc\n+++ b/libstdc++-v3/include/bits/random.tcc\n@@ -2945,18 +2945,8 @@ namespace __detail\n   template<typename _RealType>\n     void\n     piecewise_constant_distribution<_RealType>::param_type::\n-    _M_initialize()\n+    _M_configure()\n     {\n-      if (_M_int.size() < 2\n-\t  || (_M_int.size() == 2\n-\t      && _M_int[0] == _RealType(0)\n-\t      && _M_int[1] == _RealType(1)))\n-\t{\n-\t  _M_int.clear();\n-\t  _M_den.clear();\n-\t  return;\n-\t}\n-\n       const double __sum = std::accumulate(_M_den.begin(),\n \t\t\t\t\t   _M_den.end(), 0.0);\n       __glibcxx_assert(__sum > 0);\n@@ -2975,6 +2965,46 @@ namespace __detail\n \t_M_den[__k] /= _M_int[__k + 1] - _M_int[__k];\n     }\n \n+  template<typename _RealType>\n+    void\n+    piecewise_constant_distribution<_RealType>::param_type::\n+    _M_initialize2(const _RealType* __ints, _RealType __den)\n+    {\n+      if (__ints[0] == _RealType(0) && __ints[1] == _RealType(1))\n+\treturn;\n+\n+      _M_int.reserve(2);\n+      _M_int.push_back(__ints[0]);\n+      _M_int.push_back(__ints[1]);\n+\n+      _M_den.reserve(1);\n+      _M_den.push_back(__den);\n+      _M_configure();\n+    }\n+\n+namespace __detail\n+{\n+  template<typename _InputIterator, typename _RealType>\n+    bool\n+    __load_first2(_InputIterator& __first, _InputIterator __last,\n+\t\t  _RealType* __out)\n+    {\n+      if (__first == __last)\n+\treturn false;\n+\n+      *__out = *__first;\n+      ++__first;\n+      if (__first == __last)\n+\treturn false;\n+\n+      ++__out;\n+      *__out = *__first;\n+      ++__first;\n+      return true;\n+    }\n+} // namespace __detail\n+\n+\n   template<typename _RealType>\n     template<typename _InputIteratorB, typename _InputIteratorW>\n       piecewise_constant_distribution<_RealType>::param_type::\n@@ -2983,21 +3013,39 @@ namespace __detail\n \t\t _InputIteratorW __wbegin)\n       : _M_int(), _M_den(), _M_cp()\n       {\n-\tif (__bbegin != __bend)\n-\t  {\n-\t    for (;;)\n-\t      {\n-\t\t_M_int.push_back(*__bbegin);\n-\t\t++__bbegin;\n-\t\tif (__bbegin == __bend)\n-\t\t  break;\n+\t_RealType __ints[2];\n+\tif (!__detail::__load_first2(__bbegin, __bend, __ints))\n+\t  return;\n \n-\t\t_M_den.push_back(*__wbegin);\n-\t\t++__wbegin;\n-\t      }\n+\tif (__bbegin == __bend)\n+\t  {\n+\t    _M_initialize2(__ints, *__wbegin);\n+\t    return;\n \t  }\n \n-\t_M_initialize();\n+#if __glibcxx_concepts // C++ >= C++20\n+\tif constexpr (sized_sentinel_for<_InputIteratorB, _InputIteratorB>\n+\t\t\t|| forward_iterator<_InputIteratorB>)\n+\t  _M_int.reserve(2 + size_t(ranges::distance(__bbegin, __bend)));\n+#else\n+#pragma GCC diagnostic push\n+#pragma GCC diagnostic ignored \"-Wc++17-extensions\" // if constexpr\n+\tif constexpr (is_convertible<__iter_category_t<_InputIteratorB>,\n+\t\t\t\t     forward_iterator_tag>::value)\n+\t  _M_int.reserve(2 + size_t(std::distance(__bbegin, __bend)));\n+#pragma GCC diagnostic pop\n+#endif\n+\n+\t_M_int.push_back(__ints[0]);\n+\t_M_int.push_back(__ints[1]);\n+\tfor (; __bbegin != __bend; ++__bbegin)\n+\t  _M_int.push_back(*__bbegin);\n+\n+\t_M_den.reserve(_M_int.size() - 1);\n+\tfor (size_t __k = 0; __k < _M_int.size() - 1; (void)++__k, ++__wbegin)\n+\t  _M_den.push_back(*__wbegin);\n+\n+\t_M_configure();\n       }\n \n   template<typename _RealType>\n@@ -3006,15 +3054,23 @@ namespace __detail\n       param_type(initializer_list<_RealType> __bl, _Func __fw)\n       : _M_int(), _M_den(), _M_cp()\n       {\n-\t_M_int.reserve(__bl.size());\n-\tfor (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)\n-\t  _M_int.push_back(*__biter);\n+\tif (__bl.size() < 2)\n+\t  return;\n \n+\tif (__bl.size() == 2)\n+\t  {\n+\t    const _RealType *__ints = __bl.begin();\n+\t    _RealType __den = __fw(0.5 * (__ints[1] + __ints[0]));\n+\t    _M_initialize2(__ints, __den);\n+\t    return;\n+\t  }\n+\n+\t_M_int = __bl;\n \t_M_den.reserve(_M_int.size() - 1);\n \tfor (size_t __k = 0; __k < _M_int.size() - 1; ++__k)\n \t  _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));\n \n-\t_M_initialize();\n+\t_M_configure();\n       }\n \n   template<typename _RealType>\n@@ -3025,6 +3081,13 @@ namespace __detail\n       {\n \tconst size_t __n = __nw == 0 ? 1 : __nw;\n \tconst _RealType __delta = (__xmax - __xmin) / __n;\n+\tif (__n == 1)\n+\t  {\n+\t    _RealType __ints[2] = { __xmin, __xmin + __delta };\n+\t    _RealType __den = __fw(__xmin * 0.5 * __delta);\n+\t    _M_initialize2(__ints, __den);\n+\t    return;\n+\t  }\n \n \t_M_int.reserve(__n + 1);\n \tfor (size_t __k = 0; __k <= __nw; ++__k)\n@@ -3034,7 +3097,7 @@ namespace __detail\n \tfor (size_t __k = 0; __k < __nw; ++__k)\n \t  _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));\n \n-\t_M_initialize();\n+\t_M_configure();\n       }\n \n   template<typename _RealType>\n@@ -3159,19 +3222,8 @@ namespace __detail\n   template<typename _RealType>\n     void\n     piecewise_linear_distribution<_RealType>::param_type::\n-    _M_initialize()\n+    _M_configure()\n     {\n-      if (_M_int.size() < 2\n-\t  || (_M_int.size() == 2\n-\t      && _M_int[0] == _RealType(0)\n-\t      && _M_int[1] == _RealType(1)\n-\t      && _M_den[0] == _M_den[1]))\n-\t{\n-\t  _M_int.clear();\n-\t  _M_den.clear();\n-\t  return;\n-\t}\n-\n       double __sum = 0.0;\n       _M_cp.reserve(_M_int.size() - 1);\n       _M_m.reserve(_M_int.size() - 1);\n@@ -3194,7 +3246,27 @@ namespace __detail\n \n       //  Make sure the last cumulative probablility is one.\n       _M_cp[_M_cp.size() - 1] = 1.0;\n-     }\n+    }\n+\n+  template<typename _RealType>\n+    void\n+    piecewise_linear_distribution<_RealType>::param_type::\n+    _M_initialize2(const _RealType* __ints, const _RealType* __dens)\n+    {\n+      if (__ints[0] == _RealType(0)\n+\t  && __ints[1] == _RealType(1)\n+\t  && __dens[0] == __dens[1])\n+\treturn;\n+\n+      _M_int.reserve(2);\n+      _M_int.push_back(__ints[0]);\n+      _M_int.push_back(__ints[1]);\n+\n+      _M_den.reserve(2);\n+      _M_den.push_back(__dens[0]);\n+      _M_den.push_back(__dens[1]);\n+      _M_configure();\n+    }\n \n   template<typename _RealType>\n     template<typename _InputIteratorB, typename _InputIteratorW>\n@@ -3204,13 +3276,43 @@ namespace __detail\n \t\t _InputIteratorW __wbegin)\n       : _M_int(), _M_den(), _M_cp(), _M_m()\n       {\n-\tfor (; __bbegin != __bend; ++__bbegin, (void) ++__wbegin)\n+\t_RealType __ints[2];\n+\tif (!__detail::__load_first2(__bbegin, __bend, __ints))\n+\t  return;\n+\n+\tif (__bbegin == __bend)\n \t  {\n-\t    _M_int.push_back(*__bbegin);\n-\t    _M_den.push_back(*__wbegin);\n+\t    _RealType __dens[2];\n+\t    __dens[0] = *__wbegin;\n+\t    ++__wbegin;\n+\t    __dens[1] = *__wbegin;\n+\t    _M_initialize2(__ints, __dens);\n+\t    return;\n \t  }\n \n-\t_M_initialize();\n+#if __glibcxx_concepts // C++ >= C++20\n+\tif constexpr (sized_sentinel_for<_InputIteratorB, _InputIteratorB>\n+\t\t\t|| forward_iterator<_InputIteratorB>)\n+\t  _M_int.reserve(2 + size_t(ranges::distance(__bbegin, __bend)));\n+#else\n+#pragma GCC diagnostic push\n+#pragma GCC diagnostic ignored \"-Wc++17-extensions\" // if constexpr\n+\tif constexpr (is_convertible<__iter_category_t<_InputIteratorB>,\n+\t\t\t\t     forward_iterator_tag>::value)\n+\t  _M_int.reserve(2 + size_t(std::distance(__bbegin, __bend)));\n+#pragma GCC diagnostic pop\n+#endif\n+\n+\t_M_int.push_back(__ints[0]);\n+\t_M_int.push_back(__ints[1]);\n+\tfor (; __bbegin != __bend; ++__bbegin)\n+\t  _M_int.push_back(*__bbegin);\n+\n+\t_M_den.reserve(_M_int.size());\n+\tfor (size_t __i = 0; __i < _M_int.size(); (void)++__i, ++__wbegin)\n+\t  _M_den.push_back(*__wbegin);\n+\n+\t_M_configure();\n       }\n \n   template<typename _RealType>\n@@ -3219,15 +3321,24 @@ namespace __detail\n       param_type(initializer_list<_RealType> __bl, _Func __fw)\n       : _M_int(), _M_den(), _M_cp(), _M_m()\n       {\n-\t_M_int.reserve(__bl.size());\n-\t_M_den.reserve(__bl.size());\n-\tfor (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)\n+\tif (__bl.size() < 2)\n+\t  return;\n+\n+\tif (__bl.size() == 2)\n \t  {\n-\t    _M_int.push_back(*__biter);\n-\t    _M_den.push_back(__fw(*__biter));\n+\t    const _RealType *__ints = __bl.begin();\n+\t    _RealType __den[2];\n+\t    __den[0] = __fw(__ints[0]);\n+\t    __den[1] = __fw(__ints[1]);\n+\t    _M_initialize2(__ints, __den);\n+\t    return;\n \t  }\n \n-\t_M_initialize();\n+\t_M_int = __bl;\n+\t_M_den.reserve(__bl.size());\n+\tfor (_RealType __b : __bl)\n+\t  _M_den.push_back(__fw(__b));\n+\t_M_configure();\n       }\n \n   template<typename _RealType>\n@@ -3238,6 +3349,15 @@ namespace __detail\n       {\n \tconst size_t __n = __nw == 0 ? 1 : __nw;\n \tconst _RealType __delta = (__xmax - __xmin) / __n;\n+\tif (__n == 1)\n+\t  {\n+\t    _RealType __ints[2] = { __xmin, __xmin + __delta };\n+\t    _RealType __dens[2];\n+\t    __dens[0] = __fw(__ints[0]);\n+\t    __dens[1] = __fw(__ints[1]);\n+\t    _M_initialize2(__ints, __dens);\n+\t    return;\n+\t  }\n \n \t_M_int.reserve(__n + 1);\n \t_M_den.reserve(__n + 1);\n@@ -3247,7 +3367,7 @@ namespace __detail\n \t    _M_den.push_back(__fw(_M_int[__k] + __delta));\n \t  }\n \n-\t_M_initialize();\n+\t_M_configure();\n       }\n \n   template<typename _RealType>\ndiff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc\nnew file mode 100644\nindex 00000000000..e03a1e187a7\n--- /dev/null\n+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc\n@@ -0,0 +1,108 @@\n+// { dg-do run { target c++11 } }\n+// { dg-require-cstdint \"\" }\n+\n+#include <random>\n+#include <testsuite_hooks.h>\n+#include <testsuite_iterators.h>\n+\n+using dist = std::piecewise_linear_distribution<>;\n+\n+template<typename... Args>\n+bool\n+is_default(Args&&... args)\n+{\n+  std::piecewise_constant_distribution<> u(std::forward<Args>(args)...);\n+\n+  std::vector<double> ints = u.intervals();\n+  if (ints.size() != 2 || ints[0] != 0.0 || ints[1] != 1.0)\n+    return false;\n+\n+  std::vector<double> dens = u.densities();\n+  if (dens.size() != 1 || dens[0] != 1.0)\n+    return false;\n+\n+  return true;\n+}\n+\n+template<template<typename> class Range>\n+void\n+test_it_pair()\n+{\n+  double wt[2];\n+  double x[3];\n+\n+  Range<double> r0(x, x);\n+  VERIFY( is_default(r0.begin(), r0.end(), wt) );\n+\n+  x[0] = 4.2;\n+  Range<double> r1a(x, x+1);\n+  VERIFY( is_default(r1a.begin(), r1a.end(), wt) );\n+\n+  x[0] = 0.0;\n+  Range<double> r1b(x, x+1);\n+  VERIFY( is_default(r1b.begin(), r1b.end(), wt) );\n+\n+  x[1] = 1.0;\n+  wt[0] = 13.0;\n+  Range<double> r2a(x, x+2);\n+  VERIFY( is_default(r2a.begin(), r2a.end(), wt) );\n+\n+  wt[0] = 4.2;\n+  Range<double> r2b(x, x+2);\n+  VERIFY( is_default(r2b.begin(), r2b.end(), wt) );\n+\n+  x[1] = 0.5;\n+  Range<double> r2c(x, x+2);\n+  VERIFY( !is_default(r2c.begin(), r2c.end(), wt) );\n+\n+  x[2] = 1.0;\n+  wt[1] = 2.0;\n+  Range<double> r3(x, x+3);\n+  VERIFY( !is_default(r3.begin(), r3.end(), wt) );\n+}\n+\n+void test_init_list()\n+{\n+  auto c = [](double x) { return 10; };\n+  VERIFY(  is_default(std::initializer_list<double>{}, c) );\n+  VERIFY(  is_default(std::initializer_list<double>{4.2}, c) );\n+  VERIFY(  is_default(std::initializer_list<double>{0.0, 1.0}, c) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, c) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, c) );\n+\n+  auto rel = [](double x) { return x; };\n+  VERIFY(  is_default(std::initializer_list<double>{}, rel) );\n+  VERIFY(  is_default(std::initializer_list<double>{4.2}, rel) );\n+  VERIFY(  is_default(std::initializer_list<double>{0.0, 1.0}, rel) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, rel) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, rel) );\n+}\n+\n+void test_xbound()\n+{\n+  auto c = [](double x) { return 10; };\n+  VERIFY(  is_default(0, 0.0, 1.0, c) );\n+  VERIFY( !is_default(0, 0.0, 0.5, c) );\n+  VERIFY(  is_default(1, 0.0, 1.0, c) );\n+  VERIFY( !is_default(1, 0.0, 0.5, c) );\n+  VERIFY( !is_default(2, 0.0, 1.0, c) );\n+\n+  auto rel = [](double x) { return x; };\n+  VERIFY(  is_default(0, 0.0, 1.0, rel) );\n+  VERIFY( !is_default(0, 0.0, 0.5, rel) );\n+  VERIFY(  is_default(1, 0.0, 1.0, rel) );\n+  VERIFY( !is_default(1, 0.0, 0.5, rel) );\n+  VERIFY( !is_default(2, 0.0, 1.0, rel) );\n+}\n+\n+int main()\n+{\n+  using namespace __gnu_test;\n+  test_it_pair<input_container>();\n+  test_it_pair<forward_container>();\n+  test_it_pair<random_access_container>();\n+\n+  test_init_list();\n+  test_xbound();\n+  return 0;\n+}\ndiff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc\nindex 3714e460391..4bfa20ea3bb 100644\n--- a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc\n+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc\n@@ -25,13 +25,17 @@\n \n #include <random>\n #include <testsuite_hooks.h>\n+#include <testsuite_iterators.h>\n \n+template<template<typename> class Range>\n void\n-test01()\n+test_it_pair()\n {\n-  std::vector<double> x = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};\n-  std::vector<double> wt = {0.5, 1.0, 2.5, 1.5, 0.5};\n-  std::piecewise_constant_distribution<> u(x.begin(), x.end(), wt.begin());\n+  double x[6] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};\n+  double wt[5] = {0.5, 1.0, 2.5, 1.5, 0.5};\n+\n+  Range<double> r(x, x+6);\n+  std::piecewise_constant_distribution<> u(r.begin(), r.end(), wt);\n   std::vector<double> interval = u.intervals();\n   std::vector<double> density = u.densities();\n   VERIFY( interval.size() == 6 );\n@@ -44,6 +48,9 @@ test01()\n \n int main()\n {\n-  test01();\n+  using namespace __gnu_test;\n+  test_it_pair<input_container>();\n+  test_it_pair<forward_container>();\n+  test_it_pair<random_access_container>();\n   return 0;\n }\ndiff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc\nnew file mode 100644\nindex 00000000000..b2e6ca65123\n--- /dev/null\n+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc\n@@ -0,0 +1,127 @@\n+// { dg-do run { target c++11 } }\n+// { dg-require-cstdint \"\" }\n+\n+#include <random>\n+#include <testsuite_hooks.h>\n+#include <testsuite_iterators.h>\n+\n+using dist = std::piecewise_linear_distribution<>;\n+\n+template<typename... Args>\n+bool\n+is_default(Args&&... args)\n+{\n+  std::piecewise_linear_distribution<> u(std::forward<Args>(args)...);\n+\n+  std::vector<double> ints = u.intervals();\n+  if (ints.size() != 2 || ints[0] != 0.0 || ints[1] != 1.0)\n+    return false;\n+\n+  std::vector<double> dens = u.densities();\n+  if (dens.size() != 2 || dens[0] != 1.0 || dens[1] != 1.0)\n+    return false;\n+\n+  return true;\n+}\n+\n+template<template<typename> class Range>\n+void\n+test_it_pair()\n+{\n+  double wt[3];\n+  double x[3];\n+\n+  Range<double> r0(x, x);\n+  VERIFY( is_default(r0.begin(), r0.end(), wt) );\n+\n+  x[0] = 4.2;\n+  Range<double> r1a(x, x+1);\n+  VERIFY( is_default(r1a.begin(), r1a.end(), wt) );\n+\n+  x[0] = 0.0;\n+  Range<double> r1b(x, x+1);\n+  VERIFY( is_default(r1b.begin(), r1b.end(), wt) );\n+\n+  x[1] = 1.0;\n+  wt[0] = wt[1] = 13.0;\n+  Range<double> r2a(x, x+2);\n+  VERIFY( is_default(r2a.begin(), r2a.end(), wt) );\n+\n+  wt[0] = wt[1] = 1.0;\n+  Range<double> r2b(x, x+2);\n+  VERIFY( is_default(r2b.begin(), r2b.end(), wt) );\n+\n+  wt[1] = 2.0;\n+  Range<double> r2c(x, x+2);\n+  VERIFY( !is_default(r2c.begin(), r2c.end(), wt) );\n+\n+  x[1] = 0.5;\n+  wt[0] = wt[1] = 2.0;\n+  Range<double> r2d(x, x+2);\n+  VERIFY( !is_default(r2d.begin(), r2d.end(), wt) );\n+\n+  x[2] = 1.0;\n+  wt[2] = wt[0];\n+  Range<double> r3(x, x+3);\n+  VERIFY( !is_default(r3.begin(), r3.end(), wt) );\n+}\n+\n+void test_init_list()\n+{\n+  auto c1 = [](double x) { return 1.0; };\n+  VERIFY(  is_default(std::initializer_list<double>{}, c1) );\n+  VERIFY(  is_default(std::initializer_list<double>{4.2}, c1) );\n+  VERIFY(  is_default(std::initializer_list<double>{0.0, 1.0}, c1) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, c1) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, c1) );\n+\n+  auto c2 = [](double x) { return 4.2; };\n+  VERIFY(  is_default(std::initializer_list<double>{}, c2) );\n+  VERIFY(  is_default(std::initializer_list<double>{4.2}, c2) );\n+  VERIFY(  is_default(std::initializer_list<double>{0.0, 1.0}, c2) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, c2) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, c2) );\n+\n+  auto id = [](double x) { return x; };\n+  VERIFY(  is_default(std::initializer_list<double>{}, id) );\n+  VERIFY(  is_default(std::initializer_list<double>{4.2}, id) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 1.0}, id) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, id) );\n+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, id) );\n+}\n+\n+void test_xbound()\n+{\n+  auto c1 = [](double x) { return 1.0; };\n+  VERIFY(  is_default(0, 0.0, 1.0, c1) );\n+  VERIFY( !is_default(0, 0.0, 0.5, c1) );\n+  VERIFY(  is_default(1, 0.0, 1.0, c1) );\n+  VERIFY( !is_default(1, 0.0, 0.5, c1) );\n+  VERIFY( !is_default(2, 0.0, 1.0, c1) );\n+\n+  auto c2 = [](double x) { return 4.2; };\n+  VERIFY(  is_default(0, 0.0, 1.0, c2) );\n+  VERIFY( !is_default(0, 0.0, 0.5, c2) );\n+  VERIFY(  is_default(1, 0.0, 1.0, c2) );\n+  VERIFY( !is_default(1, 0.0, 0.5, c2) );\n+  VERIFY( !is_default(2, 0.0, 1.0, c2) );\n+\n+  auto id = [](double x) { return x; };\n+  VERIFY( !is_default(0, 0.0, 1.0, id) );\n+  VERIFY( !is_default(0, 0.0, 0.5, id) );\n+  VERIFY( !is_default(1, 0.0, 1.0, id) );\n+  VERIFY( !is_default(1, 0.0, 0.5, id) );\n+  VERIFY( !is_default(2, 0.0, 1.0, id) );\n+}\n+\n+int main()\n+{\n+  using namespace __gnu_test;\n+  test_it_pair<input_container>();\n+  test_it_pair<forward_container>();\n+  test_it_pair<random_access_container>();\n+\n+  test_init_list();\n+  test_xbound();\n+  return 0;\n+}\ndiff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc\nindex c698e7a66e6..af979fdac42 100644\n--- a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc\n+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc\n@@ -25,13 +25,17 @@\n \n #include <random>\n #include <testsuite_hooks.h>\n+#include <testsuite_iterators.h>\n \n+template<template<typename> class Range>\n void\n-test01()\n+test_it_pair()\n {\n-  std::vector<double> x = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};\n-  std::vector<double> wt = {0.0, 1.0, 2.5, 1.5, 3.5, 0.0};\n-  std::piecewise_linear_distribution<> u(x.begin(), x.end(), wt.begin());\n+  double x[] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};\n+  double wt[] = {0.0, 1.0, 2.5, 1.5, 3.5, 0.0};\n+\n+  Range<double> r(x, x+6);\n+  std::piecewise_linear_distribution<> u(r.begin(), r.end(), wt);\n   std::vector<double> interval = u.intervals();\n   std::vector<double> density = u.densities();\n   VERIFY( interval.size() == 6 );\n@@ -44,6 +48,9 @@ test01()\n \n int main()\n {\n-  test01();\n+  using namespace __gnu_test;\n+  test_it_pair<input_container>();\n+  test_it_pair<forward_container>();\n+  test_it_pair<random_access_container>();\n   return 0;\n }\n","prefixes":["v3"]}