Commit 9e155ca1 authored by Alessandro Rubini's avatar Alessandro Rubini

wr-servo.c: simplified some conversions

Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 8b1eac65
...@@ -43,24 +43,22 @@ static void dump_timestamp(char *what, TimeInternal ts) ...@@ -43,24 +43,22 @@ static void dump_timestamp(char *what, TimeInternal ts)
static int64_t ts_to_picos(TimeInternal ts) static int64_t ts_to_picos(TimeInternal ts)
{ {
return (int64_t) ts.seconds * (int64_t)1000000000000LL return ts.seconds * 1000000000000LL
+ (int64_t) ts.nanoseconds * (int64_t)1000LL + ts.nanoseconds * 1000LL
+ (int64_t) ts.phase; + ts.phase;
} }
static TimeInternal picos_to_ts(int64_t picos) static TimeInternal picos_to_ts(int64_t picos)
{ {
int64_t phase = picos % 1000LL; int64_t nsec, phase;
picos = (picos - phase) / 1000LL;
int64_t nsec = picos % 1000000000LL;
picos = (picos-nsec)/1000000000LL;
TimeInternal ts; TimeInternal ts;
ts.seconds = (int64_t) picos;
ts.nanoseconds = (int32_t) nsec;
ts.phase = (int32_t) phase;
phase = picos % 1000;
nsec = picos / 1000;
ts.seconds = nsec / PP_NSEC_PER_SEC;
ts.nanoseconds = nsec % PP_NSEC_PER_SEC;
ts.phase = phase;
return ts; return ts;
} }
...@@ -68,25 +66,18 @@ static TimeInternal ts_add(TimeInternal a, TimeInternal b) ...@@ -68,25 +66,18 @@ static TimeInternal ts_add(TimeInternal a, TimeInternal b)
{ {
TimeInternal c; TimeInternal c;
c.seconds = 0;
c.nanoseconds = 0;
c.phase = a.phase + b.phase; c.phase = a.phase + b.phase;
c.nanoseconds = a.nanoseconds + b.nanoseconds;
c.seconds = a.seconds + b.seconds;
while(c.phase >= 1000) { while (c.phase >= 1000) {
c.phase -= 1000; c.phase -= 1000;
c.nanoseconds++; c.nanoseconds++;
} }
while (c.nanoseconds >= PP_NSEC_PER_SEC) {
c.nanoseconds += (a.nanoseconds + b.nanoseconds); c.nanoseconds -= PP_NSEC_PER_SEC;
while(c.nanoseconds >= 1000000000L) {
c.nanoseconds -= 1000000000L;
c.seconds++; c.seconds++;
} }
c.seconds += (a.seconds + b.seconds);
return c; return c;
} }
...@@ -94,24 +85,18 @@ static TimeInternal ts_sub(TimeInternal a, TimeInternal b) ...@@ -94,24 +85,18 @@ static TimeInternal ts_sub(TimeInternal a, TimeInternal b)
{ {
TimeInternal c; TimeInternal c;
c.seconds = 0;
c.nanoseconds = 0;
c.phase = a.phase - b.phase; c.phase = a.phase - b.phase;
c.nanoseconds = a.nanoseconds - b.nanoseconds;
c.seconds = a.seconds - b.seconds;
while(c.phase < 0) { while(c.phase < 0) {
c.phase += 1000; c.phase += 1000;
c.nanoseconds--; c.nanoseconds--;
} }
c.nanoseconds += a.nanoseconds - b.nanoseconds;
while(c.nanoseconds < 0) { while(c.nanoseconds < 0) {
c.nanoseconds += 1000000000L; c.nanoseconds += PP_NSEC_PER_SEC;
c.seconds--; c.seconds--;
} }
c.seconds += a.seconds - b.seconds;
return c; return c;
} }
...@@ -133,13 +118,13 @@ static TimeInternal ts_hardwarize(TimeInternal ts, int clock_period_ps) ...@@ -133,13 +118,13 @@ static TimeInternal ts_hardwarize(TimeInternal ts, int clock_period_ps)
} }
if (ts.nanoseconds < 0) { if (ts.nanoseconds < 0) {
ts.nanoseconds += 1000000000LL; ts.nanoseconds += PP_NSEC_PER_SEC;
ts.seconds--; ts.seconds--;
} }
if (ts.seconds == -1 && ts.nanoseconds > 0) { if (ts.seconds == -1 && ts.nanoseconds > 0) {
ts.seconds++; ts.seconds++;
ts.nanoseconds -= 1000000000LL; ts.nanoseconds -= PP_NSEC_PER_SEC;
} }
if (ts.nanoseconds < 0 && ts.nanoseconds >= (-q_threshold) if (ts.nanoseconds < 0 && ts.nanoseconds >= (-q_threshold)
......
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