summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/ptime.h
blob: 82ee2213015b273b84e6eb27e846c180ae3b5551 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef PTIME_H_
#define PTIME_H_

///
/// \file ptime.h
///

#include <type_traits>

#include "pconfig.h"
#include "pmath.h" // std::floor
#include "ptypes.h"

// ----------------------------------------------------------------------------------------
// netlist_time
// ----------------------------------------------------------------------------------------

namespace plib
{

	template <typename T, typename U>
	struct ptime_le
	{
		const static bool value = sizeof(T) <= sizeof(U);
	};

#if 0
	template<typename T, typename U>
	struct ptime_res {
	    using type = typename std::conditional<sizeof(T) >= sizeof(U), T, U>::type;
	};
#endif

	template <typename TYPE, TYPE RES>
	struct ptime final
	{
	public:

		using internal_type = TYPE;
		using mult_type = TYPE;

		template <typename altTYPE, altTYPE altRES>
		friend struct ptime;

		constexpr ptime() noexcept : m_time(0) {}

		~ptime() noexcept = default;

		constexpr ptime(const ptime &rhs) noexcept = default;
		constexpr ptime(ptime &&rhs) noexcept = default;
		constexpr explicit ptime(const internal_type &time) noexcept : m_time(time) {}
		constexpr explicit ptime(internal_type &&time) noexcept : m_time(time) {}
		C14CONSTEXPR ptime &operator=(const ptime &rhs) noexcept = default;
		C14CONSTEXPR ptime &operator=(ptime &&rhs) noexcept  = default;

		constexpr explicit ptime(const double t) = delete;
		//: m_time((internal_type) ( t * (double) resolution)) { }
		constexpr explicit ptime(const internal_type nom, const internal_type den) noexcept
		: m_time(nom * (RES / den)) { }

		// FIXME: check for overflow
		template <typename O>
		constexpr explicit ptime(const ptime<O, RES> &rhs) noexcept
		: m_time(rhs.m_time) { }

		template <typename O>
		C14CONSTEXPR ptime &operator+=(const ptime<O, RES> &rhs) noexcept
		{
			static_assert(ptime_le<ptime<O, RES>, ptime>::value, "Invalid ptime type");
			m_time += rhs.m_time;
			return *this;
		}
		template <typename O>
		C14CONSTEXPR ptime &operator-=(const ptime<O, RES> &rhs) noexcept
		{
			static_assert(ptime_le<ptime<O, RES>, ptime>::value, "Invalid ptime type");
			m_time -= rhs.m_time;
			return *this;
		}

		template <typename M>
		C14CONSTEXPR ptime &operator*=(const M factor) noexcept
		{
			static_assert(plib::is_integral<M>::value, "Factor must be an integral type");
			m_time *= factor;
			return *this;
		}

		template <typename O>
		constexpr const ptime operator-(const ptime<O, RES> &rhs) const noexcept
		{
			static_assert(ptime_le<ptime<O, RES>, ptime>::value, "Invalid ptime type");
			return ptime(m_time - rhs.m_time);
		}

		template <typename O>
		constexpr const ptime operator+(const ptime<O, RES> &rhs) const noexcept
		{
			static_assert(ptime_le<ptime<O, RES>, ptime>::value, "Invalid ptime type");
			return ptime(m_time + rhs.m_time);
		}

		template <typename M>
		constexpr const ptime operator*(const M &factor) const noexcept
		{
			static_assert(plib::is_integral<M>::value, "Factor must be an integral type");
			return ptime(m_time * static_cast<mult_type>(factor));
		}

		template <typename O>
		constexpr const mult_type operator/(const ptime<O, RES> &rhs) const noexcept
		{
			static_assert(ptime_le<ptime<O, RES>, ptime>::value, "Invalid ptime type");
			return static_cast<mult_type>(m_time / rhs.m_time);
		}

		friend constexpr bool operator<(const ptime lhs, const ptime rhs) noexcept
		{
			return (lhs.m_time < rhs.m_time);
		}

		friend constexpr bool operator>(const ptime lhs, const ptime rhs) noexcept
		{
			return (rhs < lhs);
		}

		friend constexpr bool operator<=(const ptime lhs, const ptime rhs) noexcept
		{
			return !(lhs > rhs);
		}

		friend constexpr bool operator>=(const ptime lhs, const ptime rhs) noexcept
		{
			return !(lhs < rhs);
		}

		friend constexpr bool operator==(const ptime lhs, const ptime rhs) noexcept
		{
			return lhs.m_time == rhs.m_time;
		}

		friend constexpr bool operator!=(const ptime lhs, const ptime rhs) noexcept
		{
			return !(lhs == rhs);
		}

		constexpr internal_type as_raw() const noexcept { return m_time; }

		template <typename FT, typename = std::enable_if<std::is_floating_point<FT>::value, FT>>
		constexpr FT
		as_fp() const noexcept
		{
			return static_cast<FT>(m_time) * inv_res<FT>();
		}

#if PUSE_FLOAT128
		constexpr __float128
		as_fp() const noexcept
		{
			return static_cast<__float128>(m_time) * inv_res<__float128>();
		}
#endif

		constexpr double as_double() const noexcept { return as_fp<double>(); }
		constexpr double as_float() const noexcept { return as_fp<float>(); }
		constexpr double as_long_double() const noexcept { return as_fp<long double>(); }


		constexpr ptime shl(unsigned shift) const noexcept { return ptime(m_time << shift); }
		constexpr ptime shr(unsigned shift) const noexcept { return ptime(m_time >> shift); }

		// for save states ....
		C14CONSTEXPR internal_type *get_internaltype_ptr() noexcept { return &m_time; }

		static constexpr const ptime from_nsec(const internal_type ns) noexcept { return ptime(ns, UINT64_C(1000000000)); }
		static constexpr const ptime from_usec(const internal_type us) noexcept { return ptime(us, UINT64_C(   1000000)); }
		static constexpr const ptime from_msec(const internal_type ms) noexcept { return ptime(ms, UINT64_C(      1000)); }
		static constexpr const ptime from_sec(const internal_type s) noexcept   { return ptime(s,  UINT64_C(         1)); }
		static constexpr const ptime from_hz(const internal_type hz) noexcept { return ptime(1 , hz); }
		static constexpr const ptime from_raw(const internal_type raw) noexcept { return ptime(raw); }

		template <typename FT>
		static constexpr const typename std::enable_if<std::is_floating_point<FT>::value
#if PUSE_FLOAT128
			|| std::is_same<FT, __float128>::value
#endif
		, ptime>::type
		from_fp(const FT t) noexcept { return ptime(static_cast<internal_type>(plib::floor(t * static_cast<FT>(RES) + static_cast<FT>(0.5))), RES); }

		static constexpr const ptime from_double(const double t) noexcept
		{ return from_fp<double>(t); }

		static constexpr const ptime from_float(const float t) noexcept
		{ return from_fp<float>(t); }

		static constexpr const ptime from_long_double(const long double t) noexcept
		{ return from_fp<long double>(t); }

		static constexpr const ptime zero() noexcept { return ptime(0, RES); }
		static constexpr const ptime quantum() noexcept { return ptime(1, RES); }
		static constexpr const ptime never() noexcept { return ptime(plib::numeric_limits<internal_type>::max(), RES); }
		static constexpr const internal_type resolution() noexcept { return RES; }

		constexpr internal_type in_nsec() const noexcept { return m_time / (RES / UINT64_C(1000000000)); }
		constexpr internal_type in_usec() const noexcept { return m_time / (RES / UINT64_C(   1000000)); }
		constexpr internal_type in_msec() const noexcept { return m_time / (RES / UINT64_C(      1000)); }
		constexpr internal_type in_sec()  const noexcept { return m_time / (RES / UINT64_C(         1)); }

	private:
		template <typename FT>
		static constexpr FT inv_res() noexcept { return static_cast<FT>(1.0) / static_cast<FT>(RES); }
		internal_type m_time;
	};

} // namespace plib


#endif // PTIME_H_