diff mbox

[v3] constexpr time_point additions

Message ID 20110720195845.53631363@shotwell
State New
Headers show

Commit Message

Benjamin Kosnik July 21, 2011, 2:58 a.m. UTC
This updates <chrono> to N3291, and adds constexpr to time_point
nonmember arithmetic operations, as pointed out to me in private email
by Daniel Krugler. Indeed, they can become constant expressions: thanks!

These additions are on top of N3229.

tested x86/linux

-benjamin
diff mbox

Patch

2011-07-20  Benjamin Kosnik  <bkoz@redhat.com>
	    Daniel Krugler  <daniel.kruegler@googlemail.com>

	* include/std/chrono: (system_clock::is_steady): Update to N3291
	from is_monotonic.
	(time_point): Add constexpr to nonmember arithmetic operators.
	* src/chrono.cc: Modify for above.
	* src/compatibility-c++0x.cc: Same.
	* testsuite/20_util/time_point/nonmember/constexpr.cc: New.
	* testsuite/20_util/time_point/1.cc: Modify.
	* testsuite/20_util/system_clock/constexpr_data.cc: Modify.
	* testsuite/20_util/system_clock/1.cc: Modify.
	* testsuite/20_util/monotonic_clock/constexpr_data.cc: Move to...
	* testsuite/20_util/steady_clock/constexpr_data.cc: ...here.
	* testsuite/30_threads/condition_variable/members/2.cc: Modify.
	* testsuite/30_threads/condition_variable_any/members/2.cc: Modify.

Index: src/chrono.cc
===================================================================
--- src/chrono.cc	(revision 176534)
+++ src/chrono.cc	(working copy)
@@ -39,7 +39,7 @@ 
   {
   _GLIBCXX_BEGIN_NAMESPACE_VERSION
  
-   constexpr bool system_clock::is_monotonic;
+    constexpr bool system_clock::is_steady;
 
     system_clock::time_point
     system_clock::now() throw ()
@@ -63,10 +63,10 @@ 
     }
     
 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
-    constexpr bool monotonic_clock::is_monotonic;
+    constexpr bool steady_clock::is_steady;
     
-    monotonic_clock::time_point
-    monotonic_clock::now()
+    steady_clock::time_point
+    steady_clock::now()
     {
       timespec tp;
       // -EINVAL, -EFAULT
Index: src/compatibility-c++0x.cc
===================================================================
--- src/compatibility-c++0x.cc	(revision 176534)
+++ src/compatibility-c++0x.cc	(working copy)
@@ -1,6 +1,6 @@ 
 // Compatibility symbols for previous versions, C++0x bits -*- C++ -*-
 
-// Copyright (C) 2009, 2010 Free Software Foundation, Inc.
+// Copyright (C) 2009, 2010, 2011 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
@@ -81,4 +81,17 @@ 
       const size_t __tmp = std::_Hash_impl::hash(__e._M_value);
       return std::_Hash_impl::__hash_combine(__e._M_cat, __tmp);
     }
+
+
+  // gcc-4.7.0
+  // <chrono> changes is_monotonic to is_steady.
+  namespace chrono
+  {
+    struct system_clock
+    {
+      static constexpr bool is_monotonic = false;
+    };
+    constexpr bool system_clock::is_monotonic;
+  } // namespace chrono
 }
+
Index: include/std/chrono
===================================================================
--- include/std/chrono	(revision 176534)
+++ include/std/chrono	(working copy)
@@ -585,7 +585,7 @@ 
 
     template<typename _Clock, typename _Dur1,
 	     typename _Rep2, typename _Period2>
-      inline time_point<_Clock,
+      inline constexpr time_point<_Clock,
 	typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
       operator+(const time_point<_Clock, _Dur1>& __lhs,
 		const duration<_Rep2, _Period2>& __rhs)
@@ -593,27 +593,37 @@ 
 	typedef duration<_Rep2, _Period2>			__dur2;
 	typedef typename common_type<_Dur1,__dur2>::type	__ct;
 	typedef time_point<_Clock, __ct> 			__time_point;
-	return __time_point(__lhs) += __rhs;
+	return __time_point(__lhs.time_since_epoch() + __rhs);
       }
 
     template<typename _Rep1, typename _Period1,
 	     typename _Clock, typename _Dur2>
-      inline time_point<_Clock,
+      inline constexpr time_point<_Clock,
 	typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
       operator+(const duration<_Rep1, _Period1>& __lhs,
 		const time_point<_Clock, _Dur2>& __rhs)
-      { return __rhs + __lhs; }
+      { 
+	typedef duration<_Rep1, _Period1>			__dur1;
+	typedef typename common_type<__dur1,_Dur2>::type	__ct;
+	typedef time_point<_Clock, __ct> 			__time_point;
+	return __time_point(__rhs.time_since_epoch() + __lhs); 
+      }
 
     template<typename _Clock, typename _Dur1,
 	     typename _Rep2, typename _Period2>
-      inline time_point<_Clock,
+      inline constexpr time_point<_Clock,
 	typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
       operator-(const time_point<_Clock, _Dur1>& __lhs,
 		const duration<_Rep2, _Period2>& __rhs)
-      { return __lhs + (-__rhs); }
+      { 
+	typedef duration<_Rep2, _Period2>			__dur2;
+	typedef typename common_type<_Dur1,__dur2>::type	__ct;
+	typedef time_point<_Clock, __ct> 			__time_point;
+	return __time_point(__lhs.time_since_epoch() -__rhs); 
+      }
 
     template<typename _Clock, typename _Dur1, typename _Dur2>
-      inline typename common_type<_Dur1, _Dur2>::type
+      inline constexpr typename common_type<_Dur1, _Dur2>::type
       operator-(const time_point<_Clock, _Dur1>& __lhs,
 		const time_point<_Clock, _Dur2>& __rhs)
       { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
@@ -673,7 +683,7 @@ 
 		    < system_clock::duration::zero(),
 		    "a clock's minimum duration cannot be less than its epoch");
 
-      static constexpr bool is_monotonic = false;
+      static constexpr bool is_steady = false;
 
       static time_point
       now() throw ();
@@ -696,21 +706,21 @@ 
     };
 
 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
-    /// monotonic_clock
-    struct monotonic_clock
+    /// steady_clock
+    struct steady_clock
     {
       typedef chrono::nanoseconds 				duration;
       typedef duration::rep	  				rep;
       typedef duration::period	  				period;
-      typedef chrono::time_point<monotonic_clock, duration> 	time_point;
+      typedef chrono::time_point<steady_clock, duration> 	time_point;
 
-      static constexpr bool is_monotonic = true;
+      static constexpr bool is_steady = true;
 
       static time_point
       now();
     };
 #else
-    typedef system_clock monotonic_clock;
+    typedef system_clock steady_clock;
 #endif
 
     typedef system_clock high_resolution_clock;
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 176534)
+++ ChangeLog	(working copy)
@@ -1,3 +1,20 @@ 
+2011-07-20  Benjamin Kosnik  <bkoz@redhat.com>
+	    Daniel Krugler  <daniel.kruegler@googlemail.com>
+
+	* include/std/chrono: (system_clock::is_steady): Update to N3291
+	from is_monotonic.
+	(time_point): Add constexpr to nonmember arithmetic operators.
+	* src/chrono.cc: Modify for above.
+	* src/compatibility-c++0x.cc: Same.
+	* testsuite/20_util/time_point/nonmember/constexpr.cc: New.
+	* testsuite/20_util/time_point/1.cc: Modify.
+	* testsuite/20_util/system_clock/constexpr_data.cc: Modify.
+	* testsuite/20_util/system_clock/1.cc: Modify.
+	* testsuite/20_util/monotonic_clock/constexpr_data.cc: Move to...
+	* testsuite/20_util/steady_clock/constexpr_data.cc: ...here.
+	* testsuite/30_threads/condition_variable/members/2.cc: Modify.
+	* testsuite/30_threads/condition_variable_any/members/2.cc: Modify.
+
 2011-07-20  Paolo Carlini  <paolo.carlini@oracle.com>
 
 	* include/std/system_error: Use noexcept.
@@ -1544,7 +1561,7 @@ 
 2011-05-19  Paolo Carlini  <paolo.carlini@oracle.com>
 
 	* include/std/tuple (tuple<>::operator=(tuple&&)): Specify as
-        noexcept.
+	noexcept.
 	(__get_helper): Likewise.
 	(_Head_base<>::_M_head, _Tuple_impl<>::_M_head, _M_tail): Likewise.
 	* include/bits/move.h (swap): Likewise.
@@ -2237,8 +2254,8 @@ 
 
 2011-03-31  Jeffrey Yasskin  <jyasskin@google.com>
 
-        * libsupc++/exception_ptr.h: Forward-declare std::type_info.
-        * libsupc++/nested_exception.h (__throw_with_nested): Remove a
+	* libsupc++/exception_ptr.h: Forward-declare std::type_info.
+	* libsupc++/nested_exception.h (__throw_with_nested): Remove a
 	redundant default argument from std::__throw_with_nested.
 
 2011-03-31  Paolo Carlini  <paolo.carlini@oracle.com>
Index: testsuite/30_threads/condition_variable/members/2.cc
===================================================================
--- testsuite/30_threads/condition_variable/members/2.cc	(revision 176534)
+++ testsuite/30_threads/condition_variable/members/2.cc	(working copy)
@@ -5,7 +5,7 @@ 
 // { dg-require-cstdint "" }
 // { dg-require-gthreads "" }
 
-// Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
+// Copyright (C) 2008, 2009, 2010, 2011 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
@@ -38,10 +38,10 @@ 
       std::mutex m;
       std::unique_lock<std::mutex> l(m);
 
-      auto then = std::chrono::monotonic_clock::now();
+      auto then = std::chrono::steady_clock::now();
       std::cv_status result = c1.wait_until(l, then + ms);
       VERIFY( result == std::cv_status::timeout );
-      VERIFY( (std::chrono::monotonic_clock::now() - then) >= ms );
+      VERIFY( (std::chrono::steady_clock::now() - then) >= ms );
       VERIFY( l.owns_lock() );
     }
   catch (const std::system_error& e)
Index: testsuite/30_threads/condition_variable_any/members/2.cc
===================================================================
--- testsuite/30_threads/condition_variable_any/members/2.cc	(revision 176534)
+++ testsuite/30_threads/condition_variable_any/members/2.cc	(working copy)
@@ -5,7 +5,7 @@ 
 // { dg-require-cstdint "" }
 // { dg-require-gthreads "" }
 
-// Copyright (C) 2010 Free Software Foundation, Inc.
+// Copyright (C) 2010, 2011 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
@@ -63,10 +63,10 @@ 
       Mutex m;
       m.lock();
 
-      auto then = std::chrono::monotonic_clock::now();
+      auto then = std::chrono::steady_clock::now();
       std::cv_status result = c1.wait_until(m, then + ms);
       VERIFY( result == std::cv_status::timeout );
-      VERIFY( (std::chrono::monotonic_clock::now() - then) >= ms );
+      VERIFY( (std::chrono::steady_clock::now() - then) >= ms );
       VERIFY( m.locked );
     }
   catch (const std::system_error& e)
Index: testsuite/20_util/monotonic_clock/constexpr_data.cc
===================================================================
--- testsuite/20_util/monotonic_clock/constexpr_data.cc	(revision 176534)
+++ testsuite/20_util/monotonic_clock/constexpr_data.cc	(working copy)
@@ -1,52 +0,0 @@ 
-// { dg-do compile }
-// { dg-options "-std=gnu++0x" }
-
-// Copyright (C) 2010 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
-// <http://www.gnu.org/licenses/>.
-
-#include <chrono>
-#include <testsuite_common_types.h>
-
-namespace __gnu_test
-{
-  struct constexpr_member_data
-  {
-    template<typename _Ttesttype>
-      void
-      operator()()
-      {
-	struct _Concept
-	{
-	  void __constraint()
-	  {
-	    constexpr auto v1 __attribute__((unused))
-	      = _Ttesttype::is_monotonic;
-	  }
-	};
-
-	_Concept c;
-	c.__constraint();
-      }
-  };
-}
-
-int main()
-{
-  __gnu_test::constexpr_member_data test;
-  test.operator()<std::chrono::monotonic_clock>();
-  return 0;
-}
Index: testsuite/20_util/time_point/nonmember/constexpr.cc
===================================================================
--- testsuite/20_util/time_point/nonmember/constexpr.cc	(revision 0)
+++ testsuite/20_util/time_point/nonmember/constexpr.cc	(revision 0)
@@ -0,0 +1,45 @@ 
+// { dg-do compile }
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2011 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
+// <http://www.gnu.org/licenses/>.
+
+#include <chrono>
+#include <testsuite_hooks.h>
+
+int main()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std::chrono;
+  
+  typedef time_point<system_clock> time_type;
+
+  constexpr time_type t1(seconds(1));
+  constexpr time_type t2(seconds(30));
+  constexpr time_type t3(seconds(60));
+  
+  constexpr duration<int> d0(12);
+  constexpr duration<int> d1(3);
+
+  constexpr auto r1 = t1 + d0;
+  constexpr auto r2 = d1 + t2;
+
+  constexpr auto r3 = t1 - d0;
+  constexpr auto r4 = t2 - t3;
+
+  return 0;
+}
Index: testsuite/20_util/time_point/1.cc
===================================================================
--- testsuite/20_util/time_point/1.cc	(revision 176534)
+++ testsuite/20_util/time_point/1.cc	(working copy)
@@ -1,7 +1,7 @@ 
 // { dg-options "-std=gnu++0x" }
 // { dg-require-cstdint "" }
 
-// Copyright (C) 2008, 2009 Free Software Foundation
+// Copyright (C) 2008, 2009, 2011 Free Software Foundation
 //
 // 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
@@ -33,8 +33,8 @@ 
   time_point<system_clock> t1;
   VERIFY(t1.time_since_epoch() == system_clock::duration::zero());
 
-  time_point<monotonic_clock> t2;
-  VERIFY(t2.time_since_epoch() == monotonic_clock::duration::zero());
+  time_point<steady_clock> t2;
+  VERIFY(t2.time_since_epoch() == steady_clock::duration::zero());
 
   time_point<high_resolution_clock> t3;
   VERIFY(t3.time_since_epoch() == high_resolution_clock::duration::zero());
Index: testsuite/20_util/system_clock/constexpr_data.cc
===================================================================
--- testsuite/20_util/system_clock/constexpr_data.cc	(revision 176534)
+++ testsuite/20_util/system_clock/constexpr_data.cc	(working copy)
@@ -1,7 +1,7 @@ 
 // { dg-do compile }
 // { dg-options "-std=gnu++0x" }
 
-// Copyright (C) 2010 Free Software Foundation, Inc.
+// Copyright (C) 2010, 2011 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
@@ -34,7 +34,7 @@ 
 	  void __constraint()
 	  {
 	    constexpr auto v1 __attribute__((unused))
-	      = _Ttesttype::is_monotonic;
+	      = _Ttesttype::is_steady;
 	  }
 	};
 
Index: testsuite/20_util/system_clock/1.cc
===================================================================
--- testsuite/20_util/system_clock/1.cc	(revision 176534)
+++ testsuite/20_util/system_clock/1.cc	(working copy)
@@ -1,7 +1,7 @@ 
 // { dg-options "-std=gnu++0x" }
 // { dg-require-cstdint "" }
 
-// Copyright (C) 2008, 2009, 2010 Free Software Foundation
+// Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation
 //
 // 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
@@ -29,8 +29,8 @@ 
   using namespace std::chrono;
 
   system_clock::time_point t1 = system_clock::now();
-  bool is_monotonic = system_clock::is_monotonic;
-  is_monotonic = is_monotonic; // suppress unused warning
+  bool is_steady = system_clock::is_steady;
+  is_steady = is_steady; // suppress unused warning
   std::time_t t2 = system_clock::to_time_t(t1);
   system_clock::time_point t3 = system_clock::from_time_t(t2);
   t3 = t3; // suppress unused warning
Index: testsuite/20_util/steady_clock/constexpr_data.cc
===================================================================
--- testsuite/20_util/steady_clock/constexpr_data.cc	(revision 176534)
+++ testsuite/20_util/steady_clock/constexpr_data.cc	(working copy)
@@ -34,7 +34,7 @@ 
 	  void __constraint()
 	  {
 	    constexpr auto v1 __attribute__((unused))
-	      = _Ttesttype::is_monotonic;
+	      = _Ttesttype::is_steady;
 	  }
 	};
 
@@ -47,6 +47,6 @@ 
 int main()
 {
   __gnu_test::constexpr_member_data test;
-  test.operator()<std::chrono::monotonic_clock>();
+  test.operator()<std::chrono::steady_clock>();
   return 0;
 }