mirror of
https://github.com/TomHarte/CLK.git
synced 2024-12-02 02:49:28 +00:00
Experimentally switches to double
s for TimedEventLoop
time tracking.
This commit is contained in:
parent
b32538f3c8
commit
dbd9282efc
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
using namespace Storage;
|
using namespace Storage;
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ unsigned int TimedEventLoop::get_input_clock_rate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TimedEventLoop::reset_timer() {
|
void TimedEventLoop::reset_timer() {
|
||||||
subcycles_until_event_.set_zero();
|
subcycles_until_event_ = 0.0;
|
||||||
cycles_until_event_ = 0;
|
cycles_until_event_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,59 +64,22 @@ void TimedEventLoop::jump_to_next_event() {
|
|||||||
process_next_event();
|
process_next_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char text[256];
|
||||||
|
|
||||||
void TimedEventLoop::set_next_event_time_interval(Time interval) {
|
void TimedEventLoop::set_next_event_time_interval(Time interval) {
|
||||||
// Calculate [interval]*[input clock rate] + [subcycles until this event]
|
// Calculate [interval]*[input clock rate] + [subcycles until this event]
|
||||||
// = interval.numerator * input clock / interval.denominator + subcycles.numerator / subcycles.denominator
|
double double_interval = interval.get<double>() * static_cast<double>(input_clock_rate_) + subcycles_until_event_;
|
||||||
// = (interval.numerator * input clock * subcycles.denominator + subcycles.numerator * interval.denominator) / (interval.denominator * subcycles.denominator)
|
|
||||||
int64_t denominator = static_cast<int64_t>(interval.clock_rate) * static_cast<int64_t>(subcycles_until_event_.clock_rate);
|
|
||||||
int64_t numerator =
|
|
||||||
static_cast<int64_t>(subcycles_until_event_.clock_rate) * static_cast<int64_t>(input_clock_rate_) * static_cast<int64_t>(interval.length) +
|
|
||||||
static_cast<int64_t>(interval.clock_rate) * static_cast<int64_t>(subcycles_until_event_.length);
|
|
||||||
|
|
||||||
// Simplify if necessary: try just simplifying the interval and recalculating; if that doesn't
|
|
||||||
// work then try simplifying the whole thing.
|
|
||||||
if(numerator < 0 || denominator < 0 || denominator > std::numeric_limits<int>::max()) {
|
|
||||||
interval.simplify();
|
|
||||||
denominator = static_cast<int64_t>(interval.clock_rate) * static_cast<int64_t>(subcycles_until_event_.clock_rate);
|
|
||||||
numerator =
|
|
||||||
static_cast<int64_t>(subcycles_until_event_.clock_rate) * static_cast<int64_t>(input_clock_rate_) * static_cast<int64_t>(interval.length) +
|
|
||||||
static_cast<int64_t>(interval.clock_rate) * static_cast<int64_t>(subcycles_until_event_.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(numerator < 0 || denominator < 0 || denominator > std::numeric_limits<int>::max()) {
|
|
||||||
int64_t common_divisor = NumberTheory::greatest_common_divisor(numerator % denominator, denominator);
|
|
||||||
denominator /= common_divisor;
|
|
||||||
numerator /= common_divisor;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If even that doesn't work then reduce precision.
|
|
||||||
if(numerator < 0 || denominator < 0 || denominator > std::numeric_limits<int>::max()) {
|
|
||||||
// printf(".");
|
|
||||||
const double double_interval = interval.get<double>();
|
|
||||||
const double double_subcycles_remaining = subcycles_until_event_.get<double>();
|
|
||||||
const double output = double_interval * static_cast<double>(input_clock_rate_) + double_subcycles_remaining;
|
|
||||||
|
|
||||||
if(output < 1.0) {
|
|
||||||
denominator = std::numeric_limits<int>::max();
|
|
||||||
numerator = static_cast<int>(denominator * output);
|
|
||||||
} else {
|
|
||||||
numerator = std::numeric_limits<int>::max();
|
|
||||||
denominator = static_cast<int>(numerator / output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// So this event will fire in the integral number of cycles from now, putting us at the remainder
|
// So this event will fire in the integral number of cycles from now, putting us at the remainder
|
||||||
// number of subcycles
|
// number of subcycles
|
||||||
const int addition = static_cast<int>(numerator / denominator);
|
const int addition = static_cast<int>(double_interval);
|
||||||
assert(cycles_until_event_ == 0);
|
|
||||||
assert(addition >= 0);
|
|
||||||
if(addition < 0) {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
cycles_until_event_ += addition;
|
cycles_until_event_ += addition;
|
||||||
subcycles_until_event_.length = static_cast<unsigned int>(numerator % denominator);
|
subcycles_until_event_ = fmod(double_interval, 1.0);
|
||||||
subcycles_until_event_.clock_rate = static_cast<unsigned int>(denominator);
|
|
||||||
subcycles_until_event_.simplify();
|
assert(cycles_until_event_ >= 0);
|
||||||
|
assert(subcycles_until_event_ >= 0.0);
|
||||||
|
|
||||||
|
sprintf(text, " + %0.8f -> %d / %0.4f", interval.get<double>(), cycles_until_event_, subcycles_until_event_);
|
||||||
}
|
}
|
||||||
|
|
||||||
Time TimedEventLoop::get_time_into_next_event() {
|
Time TimedEventLoop::get_time_into_next_event() {
|
||||||
|
@ -102,7 +102,7 @@ namespace Storage {
|
|||||||
private:
|
private:
|
||||||
unsigned int input_clock_rate_ = 0;
|
unsigned int input_clock_rate_ = 0;
|
||||||
int cycles_until_event_ = 0;
|
int cycles_until_event_ = 0;
|
||||||
Time subcycles_until_event_;
|
double subcycles_until_event_ = 0.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user