2016-07-10 12:54:39 +00:00
|
|
|
//
|
|
|
|
// Storage.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Storage_hpp
|
|
|
|
#define Storage_hpp
|
|
|
|
|
2016-07-29 11:31:02 +00:00
|
|
|
#include "../NumberTheory/Factors.hpp"
|
2016-12-24 18:07:23 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
2016-07-29 11:31:02 +00:00
|
|
|
|
2016-07-10 12:54:39 +00:00
|
|
|
namespace Storage {
|
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Contains either an absolute time or a time interval, described as a quotient, in terms of a
|
|
|
|
clock rate to which the time is relative and its length in cycles based on that clock rate.
|
|
|
|
*/
|
2016-07-10 12:54:39 +00:00
|
|
|
struct Time {
|
|
|
|
unsigned int length, clock_rate;
|
2016-08-03 10:59:45 +00:00
|
|
|
Time() : length(0), clock_rate(1) {}
|
2016-12-24 18:07:23 +00:00
|
|
|
Time(unsigned int unsigned_int_value) : length(unsigned_int_value), clock_rate(1) {}
|
|
|
|
Time(int int_value) : Time((unsigned int)int_value) {}
|
2017-07-21 22:20:27 +00:00
|
|
|
Time(unsigned int length, unsigned int clock_rate) : length(length), clock_rate(clock_rate) {}
|
2016-12-24 18:27:57 +00:00
|
|
|
Time(int length, int clock_rate) : Time((unsigned int)length, (unsigned int)clock_rate) {}
|
2017-03-26 18:34:47 +00:00
|
|
|
Time(uint64_t length, uint64_t clock_rate) {
|
2016-12-24 18:07:23 +00:00
|
|
|
install_result(length, clock_rate);
|
|
|
|
}
|
2017-03-26 18:34:47 +00:00
|
|
|
Time(float value) {
|
2016-12-24 18:07:23 +00:00
|
|
|
install_float(value);
|
|
|
|
}
|
2016-07-29 11:31:02 +00:00
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
Reduces this @c Time to its simplest form — eliminates all common factors from @c length
|
|
|
|
and @c clock_rate.
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
void simplify() {
|
2016-07-29 11:31:02 +00:00
|
|
|
unsigned int common_divisor = NumberTheory::greatest_common_divisor(length, clock_rate);
|
|
|
|
length /= common_divisor;
|
|
|
|
clock_rate /= common_divisor;
|
|
|
|
}
|
2016-07-31 17:32:30 +00:00
|
|
|
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
2016-08-03 10:59:45 +00:00
|
|
|
@returns the floating point conversion of this @c Time. This will often be less precise.
|
2016-08-01 10:04:55 +00:00
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
inline float get_float() const {
|
2016-07-31 17:32:30 +00:00
|
|
|
return (float)length / (float)clock_rate;
|
|
|
|
}
|
2016-08-03 10:59:45 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline unsigned int get_unsigned_int() const {
|
2016-12-18 03:44:33 +00:00
|
|
|
return length / clock_rate;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool operator < (const Time &other) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
return (uint64_t)other.clock_rate * (uint64_t)length < (uint64_t)clock_rate * (uint64_t)other.length;
|
2016-08-03 10:59:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool operator <= (const Time &other) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
return (uint64_t)other.clock_rate * (uint64_t)length <= (uint64_t)clock_rate * (uint64_t)other.length;
|
2016-12-18 03:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool operator > (const Time &other) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
return (uint64_t)other.clock_rate * (uint64_t)length > (uint64_t)clock_rate * (uint64_t)other.length;
|
2016-12-18 03:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool operator >= (const Time &other) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
return (uint64_t)other.clock_rate * (uint64_t)length >= (uint64_t)clock_rate * (uint64_t)other.length;
|
2016-12-18 03:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool operator == (const Time &other) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
return (uint64_t)other.clock_rate * (uint64_t)length == (uint64_t)clock_rate * (uint64_t)other.length;
|
2016-12-19 03:53:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time operator + (const Time &other) const {
|
2016-12-29 16:00:47 +00:00
|
|
|
if(!other.length) return *this;
|
|
|
|
|
2016-12-25 20:46:49 +00:00
|
|
|
uint64_t result_length;
|
|
|
|
uint64_t result_clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(clock_rate == other.clock_rate) {
|
2016-12-25 20:46:49 +00:00
|
|
|
result_length = (uint64_t)length + (uint64_t)other.length;
|
|
|
|
result_clock_rate = clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-25 20:46:49 +00:00
|
|
|
result_length = (uint64_t)length * (uint64_t)other.clock_rate + (uint64_t)other.length * (uint64_t)clock_rate;
|
|
|
|
result_clock_rate = (uint64_t)clock_rate * (uint64_t)other.clock_rate;
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
return Time(result_length, result_clock_rate);
|
2016-08-03 10:59:45 +00:00
|
|
|
}
|
2016-08-03 11:26:05 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time &operator += (const Time &other) {
|
2016-12-29 16:00:47 +00:00
|
|
|
if(!other.length) return *this;
|
2017-07-21 22:20:27 +00:00
|
|
|
if(!length) {
|
|
|
|
*this = other;
|
|
|
|
return *this;
|
|
|
|
}
|
2016-12-29 16:00:47 +00:00
|
|
|
|
2016-12-25 20:46:49 +00:00
|
|
|
uint64_t result_length;
|
|
|
|
uint64_t result_clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(clock_rate == other.clock_rate) {
|
2016-12-25 20:46:49 +00:00
|
|
|
result_length = (uint64_t)length + (uint64_t)other.length;
|
|
|
|
result_clock_rate = (uint64_t)clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-25 20:46:49 +00:00
|
|
|
result_length = (uint64_t)length * (uint64_t)other.clock_rate + (uint64_t)other.length * (uint64_t)clock_rate;
|
|
|
|
result_clock_rate = (uint64_t)clock_rate * (uint64_t)other.clock_rate;
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
install_result(result_length, result_clock_rate);
|
2016-08-03 11:49:00 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time operator - (const Time &other) const {
|
2016-12-29 16:00:47 +00:00
|
|
|
if(!other.length) return *this;
|
|
|
|
|
2016-12-25 20:46:49 +00:00
|
|
|
uint64_t result_length;
|
|
|
|
uint64_t result_clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(clock_rate == other.clock_rate) {
|
2016-12-25 20:46:49 +00:00
|
|
|
result_length = (uint64_t)length - (uint64_t)other.length;
|
|
|
|
result_clock_rate = clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-25 20:46:49 +00:00
|
|
|
result_length = (uint64_t)length * (uint64_t)other.clock_rate - (uint64_t)other.length * (uint64_t)clock_rate;
|
|
|
|
result_clock_rate = (uint64_t)clock_rate * (uint64_t)other.clock_rate;
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
return Time(result_length, result_clock_rate);
|
2016-08-03 11:33:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time operator -= (const Time &other) {
|
2016-12-29 16:00:47 +00:00
|
|
|
if(!other.length) return *this;
|
|
|
|
|
2016-12-25 20:46:49 +00:00
|
|
|
uint64_t result_length;
|
|
|
|
uint64_t result_clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(clock_rate == other.clock_rate) {
|
2016-12-25 20:46:49 +00:00
|
|
|
result_length = (uint64_t)length - (uint64_t)other.length;
|
|
|
|
result_clock_rate = (uint64_t)clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-25 20:46:49 +00:00
|
|
|
result_length = (uint64_t)length * (uint64_t)other.clock_rate - (uint64_t)other.length * (uint64_t)clock_rate;
|
|
|
|
result_clock_rate = (uint64_t)clock_rate * (uint64_t)other.clock_rate;
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
install_result(result_length, result_clock_rate);
|
2016-08-05 01:36:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time operator * (const Time &other) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t result_length = (uint64_t)length * (uint64_t)other.length;
|
|
|
|
uint64_t result_clock_rate = (uint64_t)clock_rate * (uint64_t)other.clock_rate;
|
|
|
|
return Time(result_length, result_clock_rate);
|
2016-08-05 01:36:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time &operator *= (const Time &other) {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t result_length = (uint64_t)length * (uint64_t)other.length;
|
|
|
|
uint64_t result_clock_rate = (uint64_t)clock_rate * (uint64_t)other.clock_rate;
|
|
|
|
install_result(result_length, result_clock_rate);
|
2016-08-05 01:36:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time operator * (unsigned int multiplier) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t result_length = (uint64_t)length * (uint64_t)multiplier;
|
|
|
|
uint64_t result_clock_rate = (uint64_t)clock_rate;
|
|
|
|
return Time(result_length, result_clock_rate);
|
2016-12-18 03:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time &operator *= (unsigned int multiplier) {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t result_length = (uint64_t)length * (uint64_t)multiplier;
|
|
|
|
uint64_t result_clock_rate = (uint64_t)clock_rate;
|
|
|
|
install_result(result_length, result_clock_rate);
|
2016-12-18 03:44:33 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time operator / (const Time &other) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t result_length = (uint64_t)length * (uint64_t)other.clock_rate;
|
|
|
|
uint64_t result_clock_rate = (uint64_t)clock_rate * (uint64_t)other.length;
|
|
|
|
return Time(result_length, result_clock_rate);
|
2016-08-05 01:36:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time &operator /= (const Time &other) {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t result_length = (uint64_t)length * (uint64_t)other.clock_rate;
|
|
|
|
uint64_t result_clock_rate = (uint64_t)clock_rate * (uint64_t)other.length;
|
|
|
|
install_result(result_length, result_clock_rate);
|
2016-08-05 01:36:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time operator / (unsigned int divisor) const {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t result_length = (uint64_t)length;
|
|
|
|
uint64_t result_clock_rate = (uint64_t)clock_rate * (uint64_t)divisor;
|
|
|
|
return Time(result_length, result_clock_rate);
|
2016-12-18 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline Time &operator /= (unsigned int divisor) {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t result_length = (uint64_t)length;
|
|
|
|
uint64_t result_clock_rate = (uint64_t)clock_rate * (uint64_t)divisor;
|
|
|
|
install_result(result_length, result_clock_rate);
|
2016-12-18 15:19:24 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void set_zero() {
|
2016-08-03 11:26:05 +00:00
|
|
|
length = 0;
|
|
|
|
clock_rate = 1;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void set_one() {
|
2016-08-03 11:26:05 +00:00
|
|
|
length = 1;
|
|
|
|
clock_rate = 1;
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
static Time max() {
|
2016-12-25 04:29:37 +00:00
|
|
|
return Time(std::numeric_limits<unsigned int>::max());
|
|
|
|
}
|
|
|
|
|
2016-12-24 18:07:23 +00:00
|
|
|
private:
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void install_result(uint64_t long_length, uint64_t long_clock_rate) {
|
2017-07-21 22:20:27 +00:00
|
|
|
if(long_length <= std::numeric_limits<unsigned int>::max() && long_clock_rate <= std::numeric_limits<unsigned int>::max()) {
|
|
|
|
length = (unsigned int)long_length;
|
|
|
|
clock_rate = (unsigned int)long_clock_rate;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-24 18:07:23 +00:00
|
|
|
// TODO: switch to appropriate values if the result is too large or small to fit, even with trimmed accuracy.
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!long_length) {
|
2016-12-29 16:02:21 +00:00
|
|
|
length = 0;
|
|
|
|
clock_rate = 1;
|
|
|
|
return;
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
|
2017-07-21 22:20:27 +00:00
|
|
|
while(!(long_length&0xf) && !(long_clock_rate&0xf)) {
|
|
|
|
long_length >>= 4;
|
|
|
|
long_clock_rate >>= 4;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
while(!(long_length&1) && !(long_clock_rate&1)) {
|
2016-12-24 18:07:23 +00:00
|
|
|
long_length >>= 1;
|
|
|
|
long_clock_rate >>= 1;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(long_length > std::numeric_limits<unsigned int>::max() || long_clock_rate > std::numeric_limits<unsigned int>::max()) {
|
2016-12-24 18:07:23 +00:00
|
|
|
uint64_t common_divisor = NumberTheory::greatest_common_divisor(long_length, long_clock_rate);
|
|
|
|
long_length /= common_divisor;
|
|
|
|
long_clock_rate /= common_divisor;
|
|
|
|
|
|
|
|
// Okay, in desperation accept a loss of accuracy.
|
2016-12-25 04:29:37 +00:00
|
|
|
while(
|
2017-03-26 18:34:47 +00:00
|
|
|
(long_length > std::numeric_limits<unsigned int>::max() || long_clock_rate > std::numeric_limits<unsigned int>::max()) &&
|
|
|
|
(long_clock_rate > 1)) {
|
2016-12-24 18:07:23 +00:00
|
|
|
long_length >>= 1;
|
|
|
|
long_clock_rate >>= 1;
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 04:29:37 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(long_length <= std::numeric_limits<unsigned int>::max() && long_clock_rate <= std::numeric_limits<unsigned int>::max()) {
|
2016-12-25 04:29:37 +00:00
|
|
|
length = (unsigned int)long_length;
|
|
|
|
clock_rate = (unsigned int)long_clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-25 04:29:37 +00:00
|
|
|
length = std::numeric_limits<unsigned int>::max();
|
|
|
|
clock_rate = 1u;
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
inline void install_float(float value) {
|
2016-12-24 18:07:23 +00:00
|
|
|
int exponent;
|
|
|
|
float mantissa = frexpf(value, &exponent);
|
|
|
|
float loaded_mantissa = ldexpf(mantissa, 24);
|
|
|
|
|
2016-12-25 04:21:19 +00:00
|
|
|
uint64_t result_length;
|
|
|
|
uint64_t result_clock_rate;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(exponent < 0) {
|
2016-12-25 04:21:19 +00:00
|
|
|
int right_shift = -exponent;
|
|
|
|
result_length = (uint64_t)loaded_mantissa >> right_shift;
|
|
|
|
result_clock_rate = 1;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
|
|
|
if(exponent <= 24) {
|
2016-12-25 04:21:19 +00:00
|
|
|
result_length = (uint64_t)loaded_mantissa;
|
|
|
|
result_clock_rate = 1 << (24 - exponent);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-25 04:21:19 +00:00
|
|
|
result_length = std::numeric_limits<uint64_t>::max();
|
|
|
|
result_clock_rate = 1;
|
|
|
|
}
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
install_result(result_length, result_clock_rate);
|
|
|
|
}
|
2016-07-10 12:54:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Storage_h */
|