Commit 54068117 authored by Tristan Gingold's avatar Tristan Gingold

libwrtd: add wrtd_ts_sub.

parent d5179e21
......@@ -268,3 +268,31 @@ void wrtd_sec_pico_to_ts(uint64_t sec, uint64_t pico, struct wr_timestamp *ts)
ts->ticks = pico / 8000;
ts->frac = (pico % 8000) * 4096 / 8000;
}
/**
* Substract two timestamp. Returns -1 if the result would be negative.
* @param[in] l left operand
* @param[in] r right operand
* @param[out] res result
*/
int wrtd_ts_sub(struct wr_timestamp *res,
struct wr_timestamp *l, struct wr_timestamp *r)
{
int carry;
uint32_t rticks;
uint64_t rsecs;
carry = (l->frac < r->frac);
res->frac = (carry ? 4096 : 0) + l->frac - r->frac;
rticks = r->ticks + (carry ? 1 : 0);
carry = l->ticks < rticks;
res->ticks = (carry ? 125000000 : 0) + l->ticks - r->ticks;
rsecs = r->seconds + (carry ? 1 : 0);
if (l->seconds < rsecs)
return - 1;
res->seconds = l->seconds - rsecs;
return 0;
}
......@@ -181,6 +181,8 @@ extern void wrtd_ts_to_sec_pico(struct wr_timestamp *ts,
uint64_t *sec, uint64_t *pico);
extern void wrtd_sec_pico_to_ts(uint64_t sec, uint64_t pico,
struct wr_timestamp *ts);
extern int wrtd_ts_sub(struct wr_timestamp *res,
struct wr_timestamp *l, struct wr_timestamp *r);
/**@}*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment