openMSX
EmuDuration.hh
Go to the documentation of this file.
1#ifndef EMUDURATION_HH
2#define EMUDURATION_HH
3
4#include "narrow.hh"
5#include "serialize.hh"
6#include <cassert>
7#include <concepts>
8#include <cstdint>
9#include <limits>
10#include <type_traits>
11
12namespace openmsx {
13
14// constants
15inline constexpr uint64_t MAIN_FREQ = 3579545ULL * 960;
16inline constexpr unsigned MAIN_FREQ32 = MAIN_FREQ;
17static_assert(MAIN_FREQ < (1ull << 32), "must fit in 32 bit");
18
19
21{
22public:
23 // This is only a very small class (one 64-bit member). On 64-bit CPUs
24 // it's cheaper to pass this by value. On 32-bit CPUs pass-by-reference
25 // is cheaper.
26#ifdef __x86_64
27 using param = EmuDuration;
28#else
29 using param = const EmuDuration&;
30#endif
31
32 // friends
33 friend class EmuTime;
34
35 // constructors
36 constexpr EmuDuration() = default;
37 constexpr explicit EmuDuration(uint64_t n) : time(n) {}
38 constexpr explicit EmuDuration(double duration)
39 : time(uint64_t(duration * MAIN_FREQ + 0.5)) {}
40
41 static constexpr EmuDuration sec(unsigned x)
42 { return EmuDuration(x * MAIN_FREQ); }
43 static constexpr EmuDuration msec(unsigned x)
44 { return EmuDuration(x * MAIN_FREQ / 1000); }
45 static constexpr EmuDuration usec(unsigned x)
46 { return EmuDuration(x * MAIN_FREQ / 1000000); }
47 static constexpr EmuDuration hz(unsigned x)
48 { return EmuDuration(MAIN_FREQ / x); }
49
50 // conversions
51 [[nodiscard]] constexpr double toDouble() const { return double(time) / MAIN_FREQ32; }
52 [[nodiscard]] constexpr uint64_t length() const { return time; }
53
54 // comparison operators
55 [[nodiscard]] constexpr bool operator== (const EmuDuration&) const = default;
56 [[nodiscard]] constexpr auto operator<=>(const EmuDuration&) const = default;
57
58 // arithmetic operators
59 [[nodiscard]] constexpr EmuDuration operator%(EmuDuration::param d) const
60 { return EmuDuration(time % d.time); }
61 [[nodiscard]] constexpr EmuDuration operator+(EmuDuration::param d) const
62 { return EmuDuration(time + d.time); }
63 [[nodiscard]] constexpr EmuDuration operator*(uint64_t fact) const
64 { return EmuDuration(time * fact); }
65 [[nodiscard]] constexpr EmuDuration operator/(unsigned fact) const
66 { return EmuDuration(time / fact); }
67 [[nodiscard]] constexpr EmuDuration divRoundUp(unsigned fact) const
68 { return EmuDuration((time + fact - 1) / fact); }
69 [[nodiscard]] constexpr unsigned operator/(EmuDuration::param d) const
70 {
71 uint64_t result = time / d.time;
72#ifdef DEBUG
73 // we don't even want this overhead in devel builds
74 assert(result == unsigned(result));
75#endif
76 return unsigned(result);
77 }
78 [[nodiscard]] constexpr unsigned divUp(EmuDuration::param d) const {
79 uint64_t result = (time + d.time - 1) / d.time;
80#ifdef DEBUG
81 assert(result == unsigned(result));
82#endif
83 return unsigned(result);
84 }
85 [[nodiscard]] constexpr double div(EmuDuration::param d) const
86 { return narrow_cast<double>(time) / narrow_cast<double>(d.time); }
87
88 constexpr EmuDuration& operator*=(unsigned fact)
89 { time *= fact; return *this; }
90 constexpr EmuDuration& operator*=(double fact)
91 { time = narrow_cast<uint64_t>(narrow_cast<double>(time) * fact); return *this; }
92 constexpr EmuDuration& operator/=(double fact)
93 { time = narrow_cast<uint64_t>(narrow_cast<double>(time) / fact); return *this; }
94
95 // ticks
96 // TODO: Used in WavAudioInput. Keep or use DynamicClock instead?
97 [[nodiscard]] constexpr unsigned getTicksAt(unsigned freq) const
98 {
99 uint64_t result = time / (MAIN_FREQ32 / freq);
100#ifdef DEBUG
101 // we don't even want this overhead in devel builds
102 assert(result == unsigned(result));
103#endif
104 return unsigned(result);
105 }
106
107 [[nodiscard]] static constexpr EmuDuration zero()
108 {
109 return EmuDuration(uint64_t(0));
110 }
111
112 [[nodiscard]] static constexpr EmuDuration infinity()
113 {
115 }
116
117 template<typename Archive>
118 void serialize(Archive& ar, unsigned /*version*/)
119 {
120 ar.serialize("time", time);
121 }
122
123private:
124 uint64_t time = 0;
125};
126
127template<> struct SerializeAsMemcpy<EmuDuration> : std::true_type {};
128
129
130template<std::unsigned_integral T> class EmuDurationCompactStorage
131{
132public:
134 : time(T(e.length()))
135 {
136 assert(e.length() <= std::numeric_limits<T>::max());
137 }
138
139 [[nodiscard]] constexpr operator EmuDuration() const
140 {
141 return EmuDuration(uint64_t(time));
142 }
143private:
144 T time;
145};
146
150
151namespace detail {
152 // via intermediate variable to work around gcc-10 warning
153 inline constexpr uint64_t max32 = std::numeric_limits<uint32_t>::max();
154 inline constexpr uint64_t max16 = std::numeric_limits<uint16_t>::max();
155 inline constexpr uint64_t max8 = std::numeric_limits<uint8_t >::max();
156}
157template<uint64_t MAX>
158using EmuDurationStorageFor = std::conditional_t<(MAX > detail::max32), EmuDuration,
159 std::conditional_t<(MAX > detail::max16), EmuDuration32,
160 std::conditional_t<(MAX > detail::max8 ), EmuDuration16,
161 EmuDuration8>>>;
162} // namespace openmsx
163
164#endif
constexpr EmuDurationCompactStorage(EmuDuration e)
Definition: EmuDuration.hh:133
void serialize(Archive &ar, unsigned)
Definition: EmuDuration.hh:118
static constexpr EmuDuration sec(unsigned x)
Definition: EmuDuration.hh:41
constexpr EmuDuration operator+(EmuDuration::param d) const
Definition: EmuDuration.hh:61
constexpr unsigned divUp(EmuDuration::param d) const
Definition: EmuDuration.hh:78
constexpr EmuDuration(double duration)
Definition: EmuDuration.hh:38
constexpr bool operator==(const EmuDuration &) const =default
constexpr uint64_t length() const
Definition: EmuDuration.hh:52
constexpr EmuDuration operator/(unsigned fact) const
Definition: EmuDuration.hh:65
constexpr EmuDuration(uint64_t n)
Definition: EmuDuration.hh:37
constexpr EmuDuration()=default
constexpr EmuDuration & operator/=(double fact)
Definition: EmuDuration.hh:92
static constexpr EmuDuration infinity()
Definition: EmuDuration.hh:112
friend class EmuTime
Definition: EmuDuration.hh:33
static constexpr EmuDuration hz(unsigned x)
Definition: EmuDuration.hh:47
constexpr EmuDuration & operator*=(double fact)
Definition: EmuDuration.hh:90
constexpr EmuDuration divRoundUp(unsigned fact) const
Definition: EmuDuration.hh:67
static constexpr EmuDuration msec(unsigned x)
Definition: EmuDuration.hh:43
constexpr double toDouble() const
Definition: EmuDuration.hh:51
constexpr EmuDuration & operator*=(unsigned fact)
Definition: EmuDuration.hh:88
constexpr unsigned getTicksAt(unsigned freq) const
Definition: EmuDuration.hh:97
constexpr unsigned operator/(EmuDuration::param d) const
Definition: EmuDuration.hh:69
constexpr auto operator<=>(const EmuDuration &) const =default
static constexpr EmuDuration zero()
Definition: EmuDuration.hh:107
static constexpr EmuDuration usec(unsigned x)
Definition: EmuDuration.hh:45
constexpr EmuDuration operator%(EmuDuration::param d) const
Definition: EmuDuration.hh:59
constexpr double div(EmuDuration::param d) const
Definition: EmuDuration.hh:85
constexpr EmuDuration operator*(uint64_t fact) const
Definition: EmuDuration.hh:63
constexpr double e
Definition: Math.hh:20
Definition: join.hh:10
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:340
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:284
constexpr uint64_t max16
Definition: EmuDuration.hh:154
constexpr uint64_t max8
Definition: EmuDuration.hh:155
constexpr uint64_t max32
Definition: EmuDuration.hh:153
This file implemented 3 utility functions:
Definition: Autofire.cc:9
EmuDurationCompactStorage< uint32_t > EmuDuration32
Definition: EmuDuration.hh:147
std::conditional_t<(MAX > detail::max32), EmuDuration, std::conditional_t<(MAX > detail::max16), EmuDuration32, std::conditional_t<(MAX > detail::max8), EmuDuration16, EmuDuration8 > > > EmuDurationStorageFor
Definition: EmuDuration.hh:161
constexpr unsigned MAIN_FREQ32
Definition: EmuDuration.hh:16
constexpr uint64_t MAIN_FREQ
Definition: EmuDuration.hh:15