public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/113761] piecewise_constant_distribution's initializer_list constructor is not correct for `size < 2`
Date: Mon, 05 Feb 2024 10:55:28 +0000	[thread overview]
Message-ID: <bug-113761-4-7mOeoneF2P@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-113761-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113761

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> It seems like libstdc++ miss that part, I wonder if it changed ...

No, it didn't change. We've just always had that bug. Well, not quite always,
looks like it was a regression in r0-94140-gf8dd9e0de06aef which added the
reserve call.

It's easy enough to fix:


--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -2877,10 +2877,10 @@ namespace __detail
       param_type(initializer_list<_RealType> __bl, _Func __fw)
       : _M_int(), _M_den(), _M_cp()
       {
-       _M_int.reserve(__bl.size());
-       for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
-         _M_int.push_back(*__biter);
+       if (__bl.size() < 2)
+         return;

+       _M_int = __bl;
        _M_den.reserve(_M_int.size() - 1);
        for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
          _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));


But I don't understand why piecewise_constant_distribution is implemented this
way:

  template<typename _RealType>
    void
    piecewise_constant_distribution<_RealType>::param_type::
    _M_initialize()
    {
      if (_M_int.size() < 2
          || (_M_int.size() == 2
              && _M_int[0] == _RealType(0)
              && _M_int[1] == _RealType(1)))
        {
          _M_int.clear();
          _M_den.clear();
          return;
        }

And then:

        std::vector<_RealType>
        intervals() const
        {
          if (_M_int.empty())
            {
              std::vector<_RealType> __tmp(2);
              __tmp[1] = _RealType(1);
              return __tmp;
            }
          else
            return _M_int;
        }

        std::vector<double>
        densities() const
        { return _M_den.empty() ? std::vector<double>(1, 1.0) : _M_den; }

This seems incredibly wasteful, why keep reallocating a new vector instead of
just populating _M_int with {1} and _M_den with {0,1}? Paolo did that in
r0-103705-g879b9073c87cea and it makes no sense to me, I'll see if the mailing
list has any clues.

Also, we're missing https://cplusplus.github.io/LWG/issue1439 which fixed the
return type of densities()

  parent reply	other threads:[~2024-02-05 10:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-05  8:38 [Bug libstdc++/113761] New: Abnormal program termination using libstdc++ arakawamasahiro at jp dot fujitsu.com
2024-02-05  8:57 ` [Bug libstdc++/113761] " pinskia at gcc dot gnu.org
2024-02-05  9:00 ` pinskia at gcc dot gnu.org
2024-02-05  9:03 ` [Bug libstdc++/113761] piecewise_constant_distribution's initializer_list constructor is not correct for `size < 2` pinskia at gcc dot gnu.org
2024-02-05 10:55 ` redi at gcc dot gnu.org [this message]
2024-02-05 11:32 ` redi at gcc dot gnu.org
2024-02-05 11:51 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-113761-4-7mOeoneF2P@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).