public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Fix std::chrono::hh_mm_ss with unsigned rep [PR108265]
@ 2023-01-05  0:52 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-01-05  0:52 UTC (permalink / raw)
  To: libstdc++, gcc-patches

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-05  0:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-05  0:52 [committed] libstdc++: Fix std::chrono::hh_mm_ss with unsigned rep [PR108265] Jonathan Wakely

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).