From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Fix std::chrono::hh_mm_ss with unsigned rep [PR108265]
Date: Thu, 5 Jan 2023 00:52:25 +0000 [thread overview]
Message-ID: <20230105005225.140099-1-jwakely@redhat.com> (raw)
Tested x86_64-linux. Pushed to trunk, backports to gcc-11 and gcc-12
will follow.
-- >8 --
libstdc++-v3/ChangeLog:
PR libstdc++/108265
* include/std/chrono (hh_mm_ss): Do not use chrono::abs if
duration rep is unsigned.
* testsuite/std/time/hh_mm_ss/1.cc: Check unsigned rep.
---
libstdc++-v3/include/std/chrono | 15 ++++++++++++---
libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc | 16 ++++++++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index 27f391a1455..e7fd6ed57ab 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -2294,7 +2294,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
constexpr
- hh_mm_ss(_Duration __d, bool __is_neg) noexcept
+ hh_mm_ss(_Duration __d, bool __is_neg)
: _M_h (duration_cast<chrono::hours>(__d)),
_M_m (duration_cast<chrono::minutes>(__d - hours())),
_M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
@@ -2307,6 +2307,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_ss._M_r = duration_cast<precision>(__ss).count();
}
+ static constexpr _Duration
+ _S_abs(_Duration __d)
+ {
+ if constexpr (numeric_limits<typename _Duration::rep>::is_signed)
+ return chrono::abs(__d);
+ else
+ return __d;
+ }
+
public:
static constexpr unsigned fractional_width = {_S_fractional_width()};
@@ -2318,8 +2327,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr hh_mm_ss() noexcept = default;
constexpr explicit
- hh_mm_ss(_Duration __d) noexcept
- : hh_mm_ss(chrono::abs(__d), __d < _Duration::zero())
+ hh_mm_ss(_Duration __d)
+ : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
{ }
constexpr bool
diff --git a/libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc b/libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc
index 3f8a838c477..3c61145317c 100644
--- a/libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc
+++ b/libstdc++-v3/testsuite/std/time/hh_mm_ss/1.cc
@@ -115,3 +115,19 @@ size()
struct S4 { long long h; char m, s; bool neg; double ss; };
static_assert(sizeof(hh_mm_ss<duration<double, std::micro>>) == sizeof(S4));
}
+
+constexpr void
+unsigned_rep()
+{
+ using namespace std::chrono;
+
+ constexpr duration<unsigned, std::milli> ms(3690001);
+
+ constexpr hh_mm_ss hms(ms); // PR libstdc++/108265
+ static_assert( ! hms.is_negative() );
+ static_assert( hms.to_duration() == milliseconds(ms.count()) );
+ static_assert( hms.hours() == 1h );
+ static_assert( hms.minutes() == 1min );
+ static_assert( hms.seconds() == 30s );
+ static_assert( hms.subseconds() == 1ms );
+}
--
2.39.0
reply other threads:[~2023-01-05 0:52 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20230105005225.140099-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@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).