xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/duration.h (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
1*04eeddc0SDimitry Andric // -*- C++ -*-
2*04eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
3*04eeddc0SDimitry Andric //
4*04eeddc0SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*04eeddc0SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6*04eeddc0SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*04eeddc0SDimitry Andric //
8*04eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
9*04eeddc0SDimitry Andric 
10*04eeddc0SDimitry Andric #ifndef _LIBCPP___CHRONO_DURATION_H
11*04eeddc0SDimitry Andric #define _LIBCPP___CHRONO_DURATION_H
12*04eeddc0SDimitry Andric 
13*04eeddc0SDimitry Andric #include <__config>
14*04eeddc0SDimitry Andric #include <limits>
15*04eeddc0SDimitry Andric #include <ratio>
16*04eeddc0SDimitry Andric #include <type_traits>
17*04eeddc0SDimitry Andric 
18*04eeddc0SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19*04eeddc0SDimitry Andric #pragma GCC system_header
20*04eeddc0SDimitry Andric #endif
21*04eeddc0SDimitry Andric 
22*04eeddc0SDimitry Andric _LIBCPP_PUSH_MACROS
23*04eeddc0SDimitry Andric #include <__undef_macros>
24*04eeddc0SDimitry Andric 
25*04eeddc0SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
26*04eeddc0SDimitry Andric 
27*04eeddc0SDimitry Andric namespace chrono
28*04eeddc0SDimitry Andric {
29*04eeddc0SDimitry Andric 
30*04eeddc0SDimitry Andric template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
31*04eeddc0SDimitry Andric 
32*04eeddc0SDimitry Andric template <class _Tp>
33*04eeddc0SDimitry Andric struct __is_duration : false_type {};
34*04eeddc0SDimitry Andric 
35*04eeddc0SDimitry Andric template <class _Rep, class _Period>
36*04eeddc0SDimitry Andric struct __is_duration<duration<_Rep, _Period> > : true_type  {};
37*04eeddc0SDimitry Andric 
38*04eeddc0SDimitry Andric template <class _Rep, class _Period>
39*04eeddc0SDimitry Andric struct __is_duration<const duration<_Rep, _Period> > : true_type  {};
40*04eeddc0SDimitry Andric 
41*04eeddc0SDimitry Andric template <class _Rep, class _Period>
42*04eeddc0SDimitry Andric struct __is_duration<volatile duration<_Rep, _Period> > : true_type  {};
43*04eeddc0SDimitry Andric 
44*04eeddc0SDimitry Andric template <class _Rep, class _Period>
45*04eeddc0SDimitry Andric struct __is_duration<const volatile duration<_Rep, _Period> > : true_type  {};
46*04eeddc0SDimitry Andric 
47*04eeddc0SDimitry Andric } // namespace chrono
48*04eeddc0SDimitry Andric 
49*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
50*04eeddc0SDimitry Andric struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
51*04eeddc0SDimitry Andric                                          chrono::duration<_Rep2, _Period2> >
52*04eeddc0SDimitry Andric {
53*04eeddc0SDimitry Andric     typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
54*04eeddc0SDimitry Andric                              typename __ratio_gcd<_Period1, _Period2>::type> type;
55*04eeddc0SDimitry Andric };
56*04eeddc0SDimitry Andric 
57*04eeddc0SDimitry Andric namespace chrono {
58*04eeddc0SDimitry Andric 
59*04eeddc0SDimitry Andric // duration_cast
60*04eeddc0SDimitry Andric 
61*04eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration,
62*04eeddc0SDimitry Andric           class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
63*04eeddc0SDimitry Andric           bool = _Period::num == 1,
64*04eeddc0SDimitry Andric           bool = _Period::den == 1>
65*04eeddc0SDimitry Andric struct __duration_cast;
66*04eeddc0SDimitry Andric 
67*04eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration, class _Period>
68*04eeddc0SDimitry Andric struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
69*04eeddc0SDimitry Andric {
70*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
71*04eeddc0SDimitry Andric     _ToDuration operator()(const _FromDuration& __fd) const
72*04eeddc0SDimitry Andric     {
73*04eeddc0SDimitry Andric         return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
74*04eeddc0SDimitry Andric     }
75*04eeddc0SDimitry Andric };
76*04eeddc0SDimitry Andric 
77*04eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration, class _Period>
78*04eeddc0SDimitry Andric struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
79*04eeddc0SDimitry Andric {
80*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
81*04eeddc0SDimitry Andric     _ToDuration operator()(const _FromDuration& __fd) const
82*04eeddc0SDimitry Andric     {
83*04eeddc0SDimitry Andric         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
84*04eeddc0SDimitry Andric         return _ToDuration(static_cast<typename _ToDuration::rep>(
85*04eeddc0SDimitry Andric                            static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
86*04eeddc0SDimitry Andric     }
87*04eeddc0SDimitry Andric };
88*04eeddc0SDimitry Andric 
89*04eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration, class _Period>
90*04eeddc0SDimitry Andric struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
91*04eeddc0SDimitry Andric {
92*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
93*04eeddc0SDimitry Andric     _ToDuration operator()(const _FromDuration& __fd) const
94*04eeddc0SDimitry Andric     {
95*04eeddc0SDimitry Andric         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
96*04eeddc0SDimitry Andric         return _ToDuration(static_cast<typename _ToDuration::rep>(
97*04eeddc0SDimitry Andric                            static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
98*04eeddc0SDimitry Andric     }
99*04eeddc0SDimitry Andric };
100*04eeddc0SDimitry Andric 
101*04eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration, class _Period>
102*04eeddc0SDimitry Andric struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
103*04eeddc0SDimitry Andric {
104*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
105*04eeddc0SDimitry Andric     _ToDuration operator()(const _FromDuration& __fd) const
106*04eeddc0SDimitry Andric     {
107*04eeddc0SDimitry Andric         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
108*04eeddc0SDimitry Andric         return _ToDuration(static_cast<typename _ToDuration::rep>(
109*04eeddc0SDimitry Andric                            static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
110*04eeddc0SDimitry Andric                                                           / static_cast<_Ct>(_Period::den)));
111*04eeddc0SDimitry Andric     }
112*04eeddc0SDimitry Andric };
113*04eeddc0SDimitry Andric 
114*04eeddc0SDimitry Andric template <class _ToDuration, class _Rep, class _Period>
115*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
116*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
117*04eeddc0SDimitry Andric typename enable_if
118*04eeddc0SDimitry Andric <
119*04eeddc0SDimitry Andric     __is_duration<_ToDuration>::value,
120*04eeddc0SDimitry Andric     _ToDuration
121*04eeddc0SDimitry Andric >::type
122*04eeddc0SDimitry Andric duration_cast(const duration<_Rep, _Period>& __fd)
123*04eeddc0SDimitry Andric {
124*04eeddc0SDimitry Andric     return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
125*04eeddc0SDimitry Andric }
126*04eeddc0SDimitry Andric 
127*04eeddc0SDimitry Andric template <class _Rep>
128*04eeddc0SDimitry Andric struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
129*04eeddc0SDimitry Andric 
130*04eeddc0SDimitry Andric #if _LIBCPP_STD_VER > 14
131*04eeddc0SDimitry Andric template <class _Rep>
132*04eeddc0SDimitry Andric inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<_Rep>::value;
133*04eeddc0SDimitry Andric #endif
134*04eeddc0SDimitry Andric 
135*04eeddc0SDimitry Andric template <class _Rep>
136*04eeddc0SDimitry Andric struct _LIBCPP_TEMPLATE_VIS duration_values
137*04eeddc0SDimitry Andric {
138*04eeddc0SDimitry Andric public:
139*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() _NOEXCEPT {return _Rep(0);}
140*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max()  _NOEXCEPT {return numeric_limits<_Rep>::max();}
141*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min()  _NOEXCEPT {return numeric_limits<_Rep>::lowest();}
142*04eeddc0SDimitry Andric };
143*04eeddc0SDimitry Andric 
144*04eeddc0SDimitry Andric #if _LIBCPP_STD_VER > 14
145*04eeddc0SDimitry Andric template <class _ToDuration, class _Rep, class _Period>
146*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
147*04eeddc0SDimitry Andric typename enable_if
148*04eeddc0SDimitry Andric <
149*04eeddc0SDimitry Andric     __is_duration<_ToDuration>::value,
150*04eeddc0SDimitry Andric     _ToDuration
151*04eeddc0SDimitry Andric >::type
152*04eeddc0SDimitry Andric floor(const duration<_Rep, _Period>& __d)
153*04eeddc0SDimitry Andric {
154*04eeddc0SDimitry Andric     _ToDuration __t = duration_cast<_ToDuration>(__d);
155*04eeddc0SDimitry Andric     if (__t > __d)
156*04eeddc0SDimitry Andric         __t = __t - _ToDuration{1};
157*04eeddc0SDimitry Andric     return __t;
158*04eeddc0SDimitry Andric }
159*04eeddc0SDimitry Andric 
160*04eeddc0SDimitry Andric template <class _ToDuration, class _Rep, class _Period>
161*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
162*04eeddc0SDimitry Andric typename enable_if
163*04eeddc0SDimitry Andric <
164*04eeddc0SDimitry Andric     __is_duration<_ToDuration>::value,
165*04eeddc0SDimitry Andric     _ToDuration
166*04eeddc0SDimitry Andric >::type
167*04eeddc0SDimitry Andric ceil(const duration<_Rep, _Period>& __d)
168*04eeddc0SDimitry Andric {
169*04eeddc0SDimitry Andric     _ToDuration __t = duration_cast<_ToDuration>(__d);
170*04eeddc0SDimitry Andric     if (__t < __d)
171*04eeddc0SDimitry Andric         __t = __t + _ToDuration{1};
172*04eeddc0SDimitry Andric     return __t;
173*04eeddc0SDimitry Andric }
174*04eeddc0SDimitry Andric 
175*04eeddc0SDimitry Andric template <class _ToDuration, class _Rep, class _Period>
176*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
177*04eeddc0SDimitry Andric typename enable_if
178*04eeddc0SDimitry Andric <
179*04eeddc0SDimitry Andric     __is_duration<_ToDuration>::value,
180*04eeddc0SDimitry Andric     _ToDuration
181*04eeddc0SDimitry Andric >::type
182*04eeddc0SDimitry Andric round(const duration<_Rep, _Period>& __d)
183*04eeddc0SDimitry Andric {
184*04eeddc0SDimitry Andric     _ToDuration __lower = floor<_ToDuration>(__d);
185*04eeddc0SDimitry Andric     _ToDuration __upper = __lower + _ToDuration{1};
186*04eeddc0SDimitry Andric     auto __lowerDiff = __d - __lower;
187*04eeddc0SDimitry Andric     auto __upperDiff = __upper - __d;
188*04eeddc0SDimitry Andric     if (__lowerDiff < __upperDiff)
189*04eeddc0SDimitry Andric         return __lower;
190*04eeddc0SDimitry Andric     if (__lowerDiff > __upperDiff)
191*04eeddc0SDimitry Andric         return __upper;
192*04eeddc0SDimitry Andric     return __lower.count() & 1 ? __upper : __lower;
193*04eeddc0SDimitry Andric }
194*04eeddc0SDimitry Andric #endif
195*04eeddc0SDimitry Andric 
196*04eeddc0SDimitry Andric // duration
197*04eeddc0SDimitry Andric 
198*04eeddc0SDimitry Andric template <class _Rep, class _Period>
199*04eeddc0SDimitry Andric class _LIBCPP_TEMPLATE_VIS duration
200*04eeddc0SDimitry Andric {
201*04eeddc0SDimitry Andric     static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
202*04eeddc0SDimitry Andric     static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
203*04eeddc0SDimitry Andric     static_assert(_Period::num > 0, "duration period must be positive");
204*04eeddc0SDimitry Andric 
205*04eeddc0SDimitry Andric     template <class _R1, class _R2>
206*04eeddc0SDimitry Andric     struct __no_overflow
207*04eeddc0SDimitry Andric     {
208*04eeddc0SDimitry Andric     private:
209*04eeddc0SDimitry Andric         static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
210*04eeddc0SDimitry Andric         static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
211*04eeddc0SDimitry Andric         static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
212*04eeddc0SDimitry Andric         static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
213*04eeddc0SDimitry Andric         static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
214*04eeddc0SDimitry Andric         static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
215*04eeddc0SDimitry Andric         static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
216*04eeddc0SDimitry Andric 
217*04eeddc0SDimitry Andric         template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
218*04eeddc0SDimitry Andric         struct __mul    // __overflow == false
219*04eeddc0SDimitry Andric         {
220*04eeddc0SDimitry Andric             static const intmax_t value = _Xp * _Yp;
221*04eeddc0SDimitry Andric         };
222*04eeddc0SDimitry Andric 
223*04eeddc0SDimitry Andric         template <intmax_t _Xp, intmax_t _Yp>
224*04eeddc0SDimitry Andric         struct __mul<_Xp, _Yp, true>
225*04eeddc0SDimitry Andric         {
226*04eeddc0SDimitry Andric             static const intmax_t value = 1;
227*04eeddc0SDimitry Andric         };
228*04eeddc0SDimitry Andric 
229*04eeddc0SDimitry Andric     public:
230*04eeddc0SDimitry Andric         static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
231*04eeddc0SDimitry Andric         typedef ratio<__mul<__n1, __d2, !value>::value,
232*04eeddc0SDimitry Andric                       __mul<__n2, __d1, !value>::value> type;
233*04eeddc0SDimitry Andric     };
234*04eeddc0SDimitry Andric 
235*04eeddc0SDimitry Andric public:
236*04eeddc0SDimitry Andric     typedef _Rep rep;
237*04eeddc0SDimitry Andric     typedef typename _Period::type period;
238*04eeddc0SDimitry Andric private:
239*04eeddc0SDimitry Andric     rep __rep_;
240*04eeddc0SDimitry Andric public:
241*04eeddc0SDimitry Andric 
242*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
243*04eeddc0SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
244*04eeddc0SDimitry Andric         duration() = default;
245*04eeddc0SDimitry Andric #else
246*04eeddc0SDimitry Andric         duration() {}
247*04eeddc0SDimitry Andric #endif
248*04eeddc0SDimitry Andric 
249*04eeddc0SDimitry Andric     template <class _Rep2>
250*04eeddc0SDimitry Andric         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
251*04eeddc0SDimitry Andric         explicit duration(const _Rep2& __r,
252*04eeddc0SDimitry Andric             typename enable_if
253*04eeddc0SDimitry Andric             <
254*04eeddc0SDimitry Andric                is_convertible<_Rep2, rep>::value &&
255*04eeddc0SDimitry Andric                (treat_as_floating_point<rep>::value ||
256*04eeddc0SDimitry Andric                !treat_as_floating_point<_Rep2>::value)
257*04eeddc0SDimitry Andric             >::type* = nullptr)
258*04eeddc0SDimitry Andric                 : __rep_(__r) {}
259*04eeddc0SDimitry Andric 
260*04eeddc0SDimitry Andric     // conversions
261*04eeddc0SDimitry Andric     template <class _Rep2, class _Period2>
262*04eeddc0SDimitry Andric         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
263*04eeddc0SDimitry Andric         duration(const duration<_Rep2, _Period2>& __d,
264*04eeddc0SDimitry Andric             typename enable_if
265*04eeddc0SDimitry Andric             <
266*04eeddc0SDimitry Andric                 __no_overflow<_Period2, period>::value && (
267*04eeddc0SDimitry Andric                 treat_as_floating_point<rep>::value ||
268*04eeddc0SDimitry Andric                 (__no_overflow<_Period2, period>::type::den == 1 &&
269*04eeddc0SDimitry Andric                  !treat_as_floating_point<_Rep2>::value))
270*04eeddc0SDimitry Andric             >::type* = nullptr)
271*04eeddc0SDimitry Andric                 : __rep_(chrono::duration_cast<duration>(__d).count()) {}
272*04eeddc0SDimitry Andric 
273*04eeddc0SDimitry Andric     // observer
274*04eeddc0SDimitry Andric 
275*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
276*04eeddc0SDimitry Andric 
277*04eeddc0SDimitry Andric     // arithmetic
278*04eeddc0SDimitry Andric 
279*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
280*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
281*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++()      {++__rep_; return *this;}
282*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration  operator++(int)   {return duration(__rep_++);}
283*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--()      {--__rep_; return *this;}
284*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration  operator--(int)   {return duration(__rep_--);}
285*04eeddc0SDimitry Andric 
286*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
287*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
288*04eeddc0SDimitry Andric 
289*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;}
290*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;}
291*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;}
292*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
293*04eeddc0SDimitry Andric 
294*04eeddc0SDimitry Andric     // special values
295*04eeddc0SDimitry Andric 
296*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() _NOEXCEPT {return duration(duration_values<rep>::zero());}
297*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min()  _NOEXCEPT {return duration(duration_values<rep>::min());}
298*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max()  _NOEXCEPT {return duration(duration_values<rep>::max());}
299*04eeddc0SDimitry Andric };
300*04eeddc0SDimitry Andric 
301*04eeddc0SDimitry Andric typedef duration<long long,         nano> nanoseconds;
302*04eeddc0SDimitry Andric typedef duration<long long,        micro> microseconds;
303*04eeddc0SDimitry Andric typedef duration<long long,        milli> milliseconds;
304*04eeddc0SDimitry Andric typedef duration<long long              > seconds;
305*04eeddc0SDimitry Andric typedef duration<     long, ratio<  60> > minutes;
306*04eeddc0SDimitry Andric typedef duration<     long, ratio<3600> > hours;
307*04eeddc0SDimitry Andric #if _LIBCPP_STD_VER > 17
308*04eeddc0SDimitry Andric typedef duration<     int, ratio_multiply<ratio<24>, hours::period>>         days;
309*04eeddc0SDimitry Andric typedef duration<     int, ratio_multiply<ratio<7>,   days::period>>         weeks;
310*04eeddc0SDimitry Andric typedef duration<     int, ratio_multiply<ratio<146097, 400>, days::period>> years;
311*04eeddc0SDimitry Andric typedef duration<     int, ratio_divide<years::period, ratio<12>>>           months;
312*04eeddc0SDimitry Andric #endif
313*04eeddc0SDimitry Andric // Duration ==
314*04eeddc0SDimitry Andric 
315*04eeddc0SDimitry Andric template <class _LhsDuration, class _RhsDuration>
316*04eeddc0SDimitry Andric struct __duration_eq
317*04eeddc0SDimitry Andric {
318*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
319*04eeddc0SDimitry Andric     bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
320*04eeddc0SDimitry Andric         {
321*04eeddc0SDimitry Andric             typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
322*04eeddc0SDimitry Andric             return _Ct(__lhs).count() == _Ct(__rhs).count();
323*04eeddc0SDimitry Andric         }
324*04eeddc0SDimitry Andric };
325*04eeddc0SDimitry Andric 
326*04eeddc0SDimitry Andric template <class _LhsDuration>
327*04eeddc0SDimitry Andric struct __duration_eq<_LhsDuration, _LhsDuration>
328*04eeddc0SDimitry Andric {
329*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
330*04eeddc0SDimitry Andric     bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
331*04eeddc0SDimitry Andric         {return __lhs.count() == __rhs.count();}
332*04eeddc0SDimitry Andric };
333*04eeddc0SDimitry Andric 
334*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
335*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
336*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
337*04eeddc0SDimitry Andric bool
338*04eeddc0SDimitry Andric operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
339*04eeddc0SDimitry Andric {
340*04eeddc0SDimitry Andric     return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
341*04eeddc0SDimitry Andric }
342*04eeddc0SDimitry Andric 
343*04eeddc0SDimitry Andric // Duration !=
344*04eeddc0SDimitry Andric 
345*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
346*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
347*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
348*04eeddc0SDimitry Andric bool
349*04eeddc0SDimitry Andric operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
350*04eeddc0SDimitry Andric {
351*04eeddc0SDimitry Andric     return !(__lhs == __rhs);
352*04eeddc0SDimitry Andric }
353*04eeddc0SDimitry Andric 
354*04eeddc0SDimitry Andric // Duration <
355*04eeddc0SDimitry Andric 
356*04eeddc0SDimitry Andric template <class _LhsDuration, class _RhsDuration>
357*04eeddc0SDimitry Andric struct __duration_lt
358*04eeddc0SDimitry Andric {
359*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
360*04eeddc0SDimitry Andric     bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
361*04eeddc0SDimitry Andric         {
362*04eeddc0SDimitry Andric             typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
363*04eeddc0SDimitry Andric             return _Ct(__lhs).count() < _Ct(__rhs).count();
364*04eeddc0SDimitry Andric         }
365*04eeddc0SDimitry Andric };
366*04eeddc0SDimitry Andric 
367*04eeddc0SDimitry Andric template <class _LhsDuration>
368*04eeddc0SDimitry Andric struct __duration_lt<_LhsDuration, _LhsDuration>
369*04eeddc0SDimitry Andric {
370*04eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
371*04eeddc0SDimitry Andric     bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
372*04eeddc0SDimitry Andric         {return __lhs.count() < __rhs.count();}
373*04eeddc0SDimitry Andric };
374*04eeddc0SDimitry Andric 
375*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
376*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
377*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
378*04eeddc0SDimitry Andric bool
379*04eeddc0SDimitry Andric operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
380*04eeddc0SDimitry Andric {
381*04eeddc0SDimitry Andric     return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
382*04eeddc0SDimitry Andric }
383*04eeddc0SDimitry Andric 
384*04eeddc0SDimitry Andric // Duration >
385*04eeddc0SDimitry Andric 
386*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
387*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
388*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
389*04eeddc0SDimitry Andric bool
390*04eeddc0SDimitry Andric operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
391*04eeddc0SDimitry Andric {
392*04eeddc0SDimitry Andric     return __rhs < __lhs;
393*04eeddc0SDimitry Andric }
394*04eeddc0SDimitry Andric 
395*04eeddc0SDimitry Andric // Duration <=
396*04eeddc0SDimitry Andric 
397*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
398*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
399*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
400*04eeddc0SDimitry Andric bool
401*04eeddc0SDimitry Andric operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
402*04eeddc0SDimitry Andric {
403*04eeddc0SDimitry Andric     return !(__rhs < __lhs);
404*04eeddc0SDimitry Andric }
405*04eeddc0SDimitry Andric 
406*04eeddc0SDimitry Andric // Duration >=
407*04eeddc0SDimitry Andric 
408*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
409*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
410*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
411*04eeddc0SDimitry Andric bool
412*04eeddc0SDimitry Andric operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
413*04eeddc0SDimitry Andric {
414*04eeddc0SDimitry Andric     return !(__lhs < __rhs);
415*04eeddc0SDimitry Andric }
416*04eeddc0SDimitry Andric 
417*04eeddc0SDimitry Andric // Duration +
418*04eeddc0SDimitry Andric 
419*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
420*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
421*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
422*04eeddc0SDimitry Andric typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
423*04eeddc0SDimitry Andric operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
424*04eeddc0SDimitry Andric {
425*04eeddc0SDimitry Andric     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
426*04eeddc0SDimitry Andric     return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
427*04eeddc0SDimitry Andric }
428*04eeddc0SDimitry Andric 
429*04eeddc0SDimitry Andric // Duration -
430*04eeddc0SDimitry Andric 
431*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
432*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
433*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
434*04eeddc0SDimitry Andric typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
435*04eeddc0SDimitry Andric operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
436*04eeddc0SDimitry Andric {
437*04eeddc0SDimitry Andric     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
438*04eeddc0SDimitry Andric     return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
439*04eeddc0SDimitry Andric }
440*04eeddc0SDimitry Andric 
441*04eeddc0SDimitry Andric // Duration *
442*04eeddc0SDimitry Andric 
443*04eeddc0SDimitry Andric template <class _Rep1, class _Period, class _Rep2>
444*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
445*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
446*04eeddc0SDimitry Andric typename enable_if
447*04eeddc0SDimitry Andric <
448*04eeddc0SDimitry Andric     is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
449*04eeddc0SDimitry Andric     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
450*04eeddc0SDimitry Andric >::type
451*04eeddc0SDimitry Andric operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
452*04eeddc0SDimitry Andric {
453*04eeddc0SDimitry Andric     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
454*04eeddc0SDimitry Andric     typedef duration<_Cr, _Period> _Cd;
455*04eeddc0SDimitry Andric     return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
456*04eeddc0SDimitry Andric }
457*04eeddc0SDimitry Andric 
458*04eeddc0SDimitry Andric template <class _Rep1, class _Period, class _Rep2>
459*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
460*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
461*04eeddc0SDimitry Andric typename enable_if
462*04eeddc0SDimitry Andric <
463*04eeddc0SDimitry Andric     is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
464*04eeddc0SDimitry Andric     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
465*04eeddc0SDimitry Andric >::type
466*04eeddc0SDimitry Andric operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
467*04eeddc0SDimitry Andric {
468*04eeddc0SDimitry Andric     return __d * __s;
469*04eeddc0SDimitry Andric }
470*04eeddc0SDimitry Andric 
471*04eeddc0SDimitry Andric // Duration /
472*04eeddc0SDimitry Andric 
473*04eeddc0SDimitry Andric template <class _Rep1, class _Period, class _Rep2>
474*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
475*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
476*04eeddc0SDimitry Andric typename enable_if
477*04eeddc0SDimitry Andric <
478*04eeddc0SDimitry Andric     !__is_duration<_Rep2>::value &&
479*04eeddc0SDimitry Andric       is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
480*04eeddc0SDimitry Andric     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
481*04eeddc0SDimitry Andric >::type
482*04eeddc0SDimitry Andric operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
483*04eeddc0SDimitry Andric {
484*04eeddc0SDimitry Andric     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
485*04eeddc0SDimitry Andric     typedef duration<_Cr, _Period> _Cd;
486*04eeddc0SDimitry Andric     return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
487*04eeddc0SDimitry Andric }
488*04eeddc0SDimitry Andric 
489*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
490*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
491*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
492*04eeddc0SDimitry Andric typename common_type<_Rep1, _Rep2>::type
493*04eeddc0SDimitry Andric operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
494*04eeddc0SDimitry Andric {
495*04eeddc0SDimitry Andric     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
496*04eeddc0SDimitry Andric     return _Ct(__lhs).count() / _Ct(__rhs).count();
497*04eeddc0SDimitry Andric }
498*04eeddc0SDimitry Andric 
499*04eeddc0SDimitry Andric // Duration %
500*04eeddc0SDimitry Andric 
501*04eeddc0SDimitry Andric template <class _Rep1, class _Period, class _Rep2>
502*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
503*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
504*04eeddc0SDimitry Andric typename enable_if
505*04eeddc0SDimitry Andric <
506*04eeddc0SDimitry Andric     !__is_duration<_Rep2>::value &&
507*04eeddc0SDimitry Andric       is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
508*04eeddc0SDimitry Andric     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
509*04eeddc0SDimitry Andric >::type
510*04eeddc0SDimitry Andric operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
511*04eeddc0SDimitry Andric {
512*04eeddc0SDimitry Andric     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
513*04eeddc0SDimitry Andric     typedef duration<_Cr, _Period> _Cd;
514*04eeddc0SDimitry Andric     return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
515*04eeddc0SDimitry Andric }
516*04eeddc0SDimitry Andric 
517*04eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
518*04eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
519*04eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
520*04eeddc0SDimitry Andric typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
521*04eeddc0SDimitry Andric operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
522*04eeddc0SDimitry Andric {
523*04eeddc0SDimitry Andric     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
524*04eeddc0SDimitry Andric     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
525*04eeddc0SDimitry Andric     return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
526*04eeddc0SDimitry Andric }
527*04eeddc0SDimitry Andric 
528*04eeddc0SDimitry Andric } // namespace chrono
529*04eeddc0SDimitry Andric 
530*04eeddc0SDimitry Andric #if _LIBCPP_STD_VER > 11
531*04eeddc0SDimitry Andric // Suffixes for duration literals [time.duration.literals]
532*04eeddc0SDimitry Andric inline namespace literals
533*04eeddc0SDimitry Andric {
534*04eeddc0SDimitry Andric   inline namespace chrono_literals
535*04eeddc0SDimitry Andric   {
536*04eeddc0SDimitry Andric 
537*04eeddc0SDimitry Andric     constexpr chrono::hours operator""h(unsigned long long __h)
538*04eeddc0SDimitry Andric     {
539*04eeddc0SDimitry Andric         return chrono::hours(static_cast<chrono::hours::rep>(__h));
540*04eeddc0SDimitry Andric     }
541*04eeddc0SDimitry Andric 
542*04eeddc0SDimitry Andric     constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
543*04eeddc0SDimitry Andric     {
544*04eeddc0SDimitry Andric         return chrono::duration<long double, ratio<3600,1>>(__h);
545*04eeddc0SDimitry Andric     }
546*04eeddc0SDimitry Andric 
547*04eeddc0SDimitry Andric 
548*04eeddc0SDimitry Andric     constexpr chrono::minutes operator""min(unsigned long long __m)
549*04eeddc0SDimitry Andric     {
550*04eeddc0SDimitry Andric         return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
551*04eeddc0SDimitry Andric     }
552*04eeddc0SDimitry Andric 
553*04eeddc0SDimitry Andric     constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
554*04eeddc0SDimitry Andric     {
555*04eeddc0SDimitry Andric         return chrono::duration<long double, ratio<60,1>> (__m);
556*04eeddc0SDimitry Andric     }
557*04eeddc0SDimitry Andric 
558*04eeddc0SDimitry Andric 
559*04eeddc0SDimitry Andric     constexpr chrono::seconds operator""s(unsigned long long __s)
560*04eeddc0SDimitry Andric     {
561*04eeddc0SDimitry Andric         return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
562*04eeddc0SDimitry Andric     }
563*04eeddc0SDimitry Andric 
564*04eeddc0SDimitry Andric     constexpr chrono::duration<long double> operator""s(long double __s)
565*04eeddc0SDimitry Andric     {
566*04eeddc0SDimitry Andric         return chrono::duration<long double> (__s);
567*04eeddc0SDimitry Andric     }
568*04eeddc0SDimitry Andric 
569*04eeddc0SDimitry Andric 
570*04eeddc0SDimitry Andric     constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
571*04eeddc0SDimitry Andric     {
572*04eeddc0SDimitry Andric         return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
573*04eeddc0SDimitry Andric     }
574*04eeddc0SDimitry Andric 
575*04eeddc0SDimitry Andric     constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
576*04eeddc0SDimitry Andric     {
577*04eeddc0SDimitry Andric         return chrono::duration<long double, milli>(__ms);
578*04eeddc0SDimitry Andric     }
579*04eeddc0SDimitry Andric 
580*04eeddc0SDimitry Andric 
581*04eeddc0SDimitry Andric     constexpr chrono::microseconds operator""us(unsigned long long __us)
582*04eeddc0SDimitry Andric     {
583*04eeddc0SDimitry Andric         return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
584*04eeddc0SDimitry Andric     }
585*04eeddc0SDimitry Andric 
586*04eeddc0SDimitry Andric     constexpr chrono::duration<long double, micro> operator""us(long double __us)
587*04eeddc0SDimitry Andric     {
588*04eeddc0SDimitry Andric         return chrono::duration<long double, micro> (__us);
589*04eeddc0SDimitry Andric     }
590*04eeddc0SDimitry Andric 
591*04eeddc0SDimitry Andric 
592*04eeddc0SDimitry Andric     constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
593*04eeddc0SDimitry Andric     {
594*04eeddc0SDimitry Andric         return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
595*04eeddc0SDimitry Andric     }
596*04eeddc0SDimitry Andric 
597*04eeddc0SDimitry Andric     constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
598*04eeddc0SDimitry Andric     {
599*04eeddc0SDimitry Andric         return chrono::duration<long double, nano> (__ns);
600*04eeddc0SDimitry Andric     }
601*04eeddc0SDimitry Andric 
602*04eeddc0SDimitry Andric } // namespace chrono_literals
603*04eeddc0SDimitry Andric } // namespace literals
604*04eeddc0SDimitry Andric 
605*04eeddc0SDimitry Andric namespace chrono { // hoist the literals into namespace std::chrono
606*04eeddc0SDimitry Andric    using namespace literals::chrono_literals;
607*04eeddc0SDimitry Andric } // namespace chrono
608*04eeddc0SDimitry Andric 
609*04eeddc0SDimitry Andric #endif // _LIBCPP_STD_VER > 11
610*04eeddc0SDimitry Andric 
611*04eeddc0SDimitry Andric _LIBCPP_END_NAMESPACE_STD
612*04eeddc0SDimitry Andric 
613*04eeddc0SDimitry Andric _LIBCPP_POP_MACROS
614*04eeddc0SDimitry Andric 
615*04eeddc0SDimitry Andric #endif // _LIBCPP___CHRONO_DURATION_H
616