1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-01-11 08:30:55 +00:00

Added saturation test, fixed code as indicated.

This commit is contained in:
Thomas Harte 2016-12-24 23:29:37 -05:00
parent 09ff9d6a26
commit d606bd7ce5
2 changed files with 26 additions and 3 deletions

View File

@ -34,4 +34,11 @@
XCTAssert(time == Storage::Time(0), @"Numbers too small to be represented should be 0");
}
- (void)testTooBigFloat
{
float original = powf(2.0f, 48.0f);
Storage::Time time(original);
XCTAssert(time == Storage::Time::max(), @"Numbers too big to be represented should saturate");
}
@end

View File

@ -189,6 +189,11 @@ struct Time {
clock_rate = 1;
}
static Time max()
{
return Time(std::numeric_limits<unsigned int>::max());
}
private:
inline void install_result(uint64_t long_length, uint64_t long_clock_rate)
{
@ -207,15 +212,26 @@ struct Time {
long_clock_rate /= common_divisor;
// Okay, in desperation accept a loss of accuracy.
while(long_length > std::numeric_limits<unsigned int>::max() || long_clock_rate > std::numeric_limits<unsigned int>::max())
while(
(long_length > std::numeric_limits<unsigned int>::max() || long_clock_rate > std::numeric_limits<unsigned int>::max()) &&
(long_clock_rate > 1))
{
long_length >>= 1;
long_clock_rate >>= 1;
}
}
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;
}
else
{
length = std::numeric_limits<unsigned int>::max();
clock_rate = 1u;
}
}
inline void install_float(float value)
{