openMSX
EmuDuration.hh
Go to the documentation of this file.
1 #ifndef EMUDUARTION_HH
2 #define EMUDUARTION_HH
3 
4 #include <cassert>
5 #include <cstdint>
6 
7 namespace openmsx {
8 
9 // constants
10 static const uint64_t MAIN_FREQ = 3579545ULL * 960;
11 static const unsigned MAIN_FREQ32 = MAIN_FREQ;
12 static_assert(MAIN_FREQ < (1ull << 32), "must fit in 32 bit");
13 
14 
16 {
17 public:
18  // This is only a very small class (one 64-bit member). On 64-bit CPUs
19  // it's cheaper to pass this by value. On 32-bit CPUs pass-by-reference
20  // is cheaper.
21 #ifdef __x86_64
22  using param = const EmuDuration;
23 #else
24  using param = const EmuDuration&;
25 #endif
26 
27  // friends
28  friend class EmuTime;
29 
30  // constructors
31  EmuDuration() = default;
32  explicit EmuDuration(uint64_t n) : time(n) {}
33  explicit EmuDuration(double duration)
34  : time(uint64_t(duration * MAIN_FREQ)) {}
35 
36  static EmuDuration sec(unsigned x)
37  { return EmuDuration(x * MAIN_FREQ); }
38  static EmuDuration msec(unsigned x)
39  { return EmuDuration(x * MAIN_FREQ / 1000); }
40  static EmuDuration usec(unsigned x)
41  { return EmuDuration(x * MAIN_FREQ / 1000000); }
42  static EmuDuration hz(unsigned x)
43  { return EmuDuration(MAIN_FREQ / x); }
44 
45  // conversions
46  double toDouble() const { return double(time) / MAIN_FREQ32; }
47  uint64_t length() const { return time; }
48 
49  // comparison operators
51  { return time == d.time; }
53  { return time != d.time; }
55  { return time < d.time; }
57  { return time <= d.time; }
59  { return time > d.time; }
61  { return time >= d.time; }
62 
63  // arithmetic operators
65  { return EmuDuration(time % d.time); }
67  { return EmuDuration(time + d.time); }
68  const EmuDuration operator*(unsigned fact) const
69  { return EmuDuration(time * fact); }
70  const EmuDuration operator/(unsigned fact) const
71  { return EmuDuration(time / fact); }
72  const EmuDuration divRoundUp(unsigned fact) const
73  { return EmuDuration((time + fact - 1) / fact); }
74  unsigned operator/(EmuDuration::param d) const
75  {
76  uint64_t result = time / d.time;
77 #ifdef DEBUG
78  // we don't even want this overhead in devel builds
79  assert(result == unsigned(result));
80 #endif
81  return unsigned(result);
82  }
83  unsigned divUp(EmuDuration::param d) const {
84  uint64_t result = (time + d.time - 1) / d.time;
85 #ifdef DEBUG
86  assert(result == unsigned(result));
87 #endif
88  return unsigned(result);
89  }
90  double div(EmuDuration::param d) const
91  { return double(time) / d.time; }
92 
93  EmuDuration& operator*=(unsigned fact)
94  { time *= fact; return *this; }
95  EmuDuration& operator*=(double fact)
96  { time = uint64_t(time * fact); return *this; }
97  EmuDuration& operator/=(double fact)
98  { time = uint64_t(time / fact); return *this; }
99 
100  // ticks
101  // TODO: Used in WavAudioInput. Keep or use DynamicClock instead?
102  unsigned getTicksAt(unsigned freq) const
103  {
104  uint64_t result = time / (MAIN_FREQ32 / freq);
105 #ifdef DEBUG
106  // we don't even want this overhead in devel builds
107  assert(result == unsigned(result));
108 #endif
109  return unsigned(result);
110  }
111 
112  template<typename Archive>
113  void serialize(Archive& ar, unsigned version);
114 
115  static const EmuDuration zero;
116  static const EmuDuration infinity;
117 
118 private:
119  uint64_t time = 0;
120 };
121 
122 } // namespace openmsx
123 
124 #endif
unsigned divUp(EmuDuration::param d) const
Definition: EmuDuration.hh:83
static EmuDuration sec(unsigned x)
Definition: EmuDuration.hh:36
static EmuDuration hz(unsigned x)
Definition: EmuDuration.hh:42
static EmuDuration msec(unsigned x)
Definition: EmuDuration.hh:38
bool operator>=(EmuDuration::param d) const
Definition: EmuDuration.hh:60
bool operator>(EmuDuration::param d) const
Definition: EmuDuration.hh:58
const EmuDuration operator/(unsigned fact) const
Definition: EmuDuration.hh:70
bool operator!=(EmuDuration::param d) const
Definition: EmuDuration.hh:52
const EmuDuration operator+(EmuDuration::param d) const
Definition: EmuDuration.hh:66
EmuDuration & operator/=(double fact)
Definition: EmuDuration.hh:97
double toDouble() const
Definition: EmuDuration.hh:46
EmuDuration & operator*=(double fact)
Definition: EmuDuration.hh:95
EmuDuration & operator*=(unsigned fact)
Definition: EmuDuration.hh:93
EmuDuration(double duration)
Definition: EmuDuration.hh:33
static const EmuDuration zero
Definition: EmuDuration.hh:115
uint64_t length() const
Definition: EmuDuration.hh:47
bool operator<(EmuDuration::param d) const
Definition: EmuDuration.hh:54
static const EmuDuration infinity
Definition: EmuDuration.hh:116
double div(EmuDuration::param d) const
Definition: EmuDuration.hh:90
friend class EmuTime
Definition: EmuDuration.hh:28
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
static EmuDuration usec(unsigned x)
Definition: EmuDuration.hh:40
const EmuDuration operator*(unsigned fact) const
Definition: EmuDuration.hh:68
const EmuDuration operator%(EmuDuration::param d) const
Definition: EmuDuration.hh:64
unsigned getTicksAt(unsigned freq) const
Definition: EmuDuration.hh:102
unsigned operator/(EmuDuration::param d) const
Definition: EmuDuration.hh:74
bool operator<=(EmuDuration::param d) const
Definition: EmuDuration.hh:56
void serialize(Archive &ar, unsigned version)
Definition: EmuDuration.cc:11
EmuDuration(uint64_t n)
Definition: EmuDuration.hh:32
const EmuDuration divRoundUp(unsigned fact) const
Definition: EmuDuration.hh:72
bool operator==(EmuDuration::param d) const
Definition: EmuDuration.hh:50