syncSlave for problem 3

Committer:
the729
Date:
Fri Dec 03 20:53:52 2010 +0000
Revision:
0:362932d519c6

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
the729 0:362932d519c6 1 #include "hdtimeval_math.h"
the729 0:362932d519c6 2
the729 0:362932d519c6 3 void hdtv_add (hdtimeval_t * r, hdtimeval_t * v1, hdtimeval_t * v2)
the729 0:362932d519c6 4 {
the729 0:362932d519c6 5 r->tv_sec = v1->tv_sec + v2->tv_sec;
the729 0:362932d519c6 6 r->ticks = v1->ticks + v2->ticks;
the729 0:362932d519c6 7 if (r->ticks >= MAX_TICK) {
the729 0:362932d519c6 8 r->tv_sec++;
the729 0:362932d519c6 9 r->ticks -= MAX_TICK;
the729 0:362932d519c6 10 }
the729 0:362932d519c6 11 }
the729 0:362932d519c6 12
the729 0:362932d519c6 13 void hdtv_div2 (hdtimeval_t * r, hdtimeval_t * v)
the729 0:362932d519c6 14 {
the729 0:362932d519c6 15 if (v->tv_sec & 0x1) {
the729 0:362932d519c6 16 r->ticks = ((v->ticks+MAX_TICK)>>1);
the729 0:362932d519c6 17 } else {
the729 0:362932d519c6 18 r->ticks = (v->ticks>>1);
the729 0:362932d519c6 19 }
the729 0:362932d519c6 20 r->tv_sec = (v->tv_sec>>1);
the729 0:362932d519c6 21 }
the729 0:362932d519c6 22
the729 0:362932d519c6 23 void hdtv_div8 (hdtimeval_t * r, hdtimeval_t * v)
the729 0:362932d519c6 24 {
the729 0:362932d519c6 25 r->ticks = ((v->ticks+MAX_TICK*(v->tv_sec & 0x7))>>3);
the729 0:362932d519c6 26 r->tv_sec = (v->tv_sec>>3);
the729 0:362932d519c6 27 }
the729 0:362932d519c6 28
the729 0:362932d519c6 29 void hdtv_sub (hdtimeval_t * r, hdtimeval_t * v1, hdtimeval_t * v2)
the729 0:362932d519c6 30 {
the729 0:362932d519c6 31 r->tv_sec = v1->tv_sec - v2->tv_sec;
the729 0:362932d519c6 32 if (v1->ticks < v2->ticks) {
the729 0:362932d519c6 33 r->ticks = MAX_TICK + v1->ticks - v2->ticks;
the729 0:362932d519c6 34 r->tv_sec --;
the729 0:362932d519c6 35 } else {
the729 0:362932d519c6 36 r->ticks = v1->ticks - v2->ticks;
the729 0:362932d519c6 37 }
the729 0:362932d519c6 38 }
the729 0:362932d519c6 39
the729 0:362932d519c6 40 void hdtv_muldiv (hdtimeval_t * r, hdtimeval_t * v, hdtimeval_t * m, hdtimeval_t * d)
the729 0:362932d519c6 41 {
the729 0:362932d519c6 42 double dblm, dbld, factor, rsec, rticks;
the729 0:362932d519c6 43 uint32_t irticks, c;
the729 0:362932d519c6 44 int32_t irsec;
the729 0:362932d519c6 45
the729 0:362932d519c6 46 dblm = (double)(m->tv_sec) * (double)MAX_TICK + (double)(m->ticks);
the729 0:362932d519c6 47 dbld = (double)(d->tv_sec) * (double)MAX_TICK + (double)(d->ticks);
the729 0:362932d519c6 48 factor = dblm / dbld;
the729 0:362932d519c6 49
the729 0:362932d519c6 50 rsec = (double)(v->tv_sec) * factor;
the729 0:362932d519c6 51 rticks = (double)(v->ticks) * factor + (rsec - floor(rsec)) * (double)MAX_TICK;
the729 0:362932d519c6 52 irticks = (uint32_t)rticks;
the729 0:362932d519c6 53 irsec = (int32_t)rsec;
the729 0:362932d519c6 54 c = irticks / MAX_TICK;
the729 0:362932d519c6 55 r->tv_sec = irsec + c;
the729 0:362932d519c6 56 r->ticks = irticks - c * MAX_TICK;
the729 0:362932d519c6 57 }
the729 0:362932d519c6 58
the729 0:362932d519c6 59 void hdtv_totv(timeval_t * r, hdtimeval_t * v)
the729 0:362932d519c6 60 {
the729 0:362932d519c6 61 r->tv_sec = v->tv_sec;
the729 0:362932d519c6 62 r->tv_usec = (v->ticks + 48)/96;
the729 0:362932d519c6 63 if (r->tv_usec >= 1000000) {
the729 0:362932d519c6 64 r->tv_sec ++;
the729 0:362932d519c6 65 r->tv_usec -= 1000000;
the729 0:362932d519c6 66 }
the729 0:362932d519c6 67 }