syncSlave for problem 2

Committer:
the729
Date:
Fri Dec 03 20:52:44 2010 +0000
Revision:
0:988132ee7271

        

Who changed what in which revision?

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