From d08b195de44d2e85467de1764884c14973f92115 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Fri, 21 Jun 2013 00:08:01 -0700 Subject: gras: first cut at time tag class --- lib/time_tag.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 lib/time_tag.cpp (limited to 'lib/time_tag.cpp') diff --git a/lib/time_tag.cpp b/lib/time_tag.cpp new file mode 100644 index 0000000..d438fc3 --- /dev/null +++ b/lib/time_tag.cpp @@ -0,0 +1,89 @@ +// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +#include +#include +#include //uint64 +#include + +using namespace gras; + +static TimeTag &normalize(TimeTag &t) +{ + int num = int(t._ticks/time_tps()); + if (num < 0) num--; //stops negative ticks + t._fsecs += num; + t._ticks -= num*time_tps(); + return t; +} + +TimeTag::TimeTag(void): + _fsecs(0), _ticks(0) +{/*NOP*/} + +TimeTag TimeTag::from_ticks(const time_ticks_t ticks) +{ + TimeTag t; + t._ticks = ticks; + return normalize(t); +} + +TimeTag TimeTag::from_ticks(const time_ticks_t ticks, const double rate) +{ + TimeTag t; + t._fsecs = time_ticks_t(ticks/rate); + const double error = ticks - (t._fsecs*rate); + t._ticks = boost::math::llround((error*time_tps())/rate); + return normalize(t); +} + +TimeTag TimeTag::from_pmc(const PMCC &p) +{ + TimeTag t; + const PMCTuple<2> &tuple = p.as >(); + t._fsecs = tuple[0].as(); + t._ticks = boost::math::llround(tuple[1].as()*time_tps()); + return normalize(t); +} + +time_ticks_t TimeTag::to_ticks(void) +{ + return _fsecs*time_tps() + _ticks; +} + +time_ticks_t TimeTag::to_ticks(const double rate) +{ + return _fsecs*time_tps() + boost::math::llround((_ticks*rate)/time_tps()); +} + +PMCC TimeTag::to_pmc(void) +{ + PMCTuple<2> tuple; + tuple[0] = PMC_M(_fsecs); + tuple[1] = PMC_M(_ticks/double(time_tps())); + return PMC_M(tuple); +} + +TimeTag &TimeTag::operator+=(const TimeTag &rhs) +{ + _fsecs += rhs._fsecs; + _ticks += rhs._ticks; + return normalize(*this); +} + +TimeTag &TimeTag::operator-=(const TimeTag &rhs) +{ + _fsecs -= rhs._fsecs; + _ticks -= rhs._ticks; + return normalize(*this); +} + +bool gras::operator<(const TimeTag &lhs, const TimeTag &rhs) +{ + if (lhs._fsecs == rhs._fsecs) return lhs._ticks < rhs._ticks; + return lhs._fsecs < rhs._fsecs; +} + +bool gras::operator==(const TimeTag &lhs, const TimeTag &rhs) +{ + return (lhs._fsecs == rhs._fsecs) and (lhs._ticks == rhs._ticks); +} -- cgit