Tianji Wu
/
syncMaster_2
syncMaster for problem 2
hdtimeval_math.cpp@0:6a0716eb2e73, 2010-12-03 (annotated)
- Committer:
- the729
- Date:
- Fri Dec 03 20:52:00 2010 +0000
- Revision:
- 0:6a0716eb2e73
working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
the729 | 0:6a0716eb2e73 | 1 | #include "hdtimeval_math.h" |
the729 | 0:6a0716eb2e73 | 2 | |
the729 | 0:6a0716eb2e73 | 3 | void hdtv_add (hdtimeval_t * r, hdtimeval_t * v1, hdtimeval_t * v2) |
the729 | 0:6a0716eb2e73 | 4 | { |
the729 | 0:6a0716eb2e73 | 5 | r->tv_sec = v1->tv_sec + v2->tv_sec; |
the729 | 0:6a0716eb2e73 | 6 | r->ticks = v1->ticks + v2->ticks; |
the729 | 0:6a0716eb2e73 | 7 | if (r->ticks >= MAX_TICK) { |
the729 | 0:6a0716eb2e73 | 8 | r->tv_sec++; |
the729 | 0:6a0716eb2e73 | 9 | r->ticks -= MAX_TICK; |
the729 | 0:6a0716eb2e73 | 10 | } |
the729 | 0:6a0716eb2e73 | 11 | } |
the729 | 0:6a0716eb2e73 | 12 | |
the729 | 0:6a0716eb2e73 | 13 | void hdtv_div2 (hdtimeval_t * r, hdtimeval_t * v) |
the729 | 0:6a0716eb2e73 | 14 | { |
the729 | 0:6a0716eb2e73 | 15 | if (v->tv_sec & 0x1) { |
the729 | 0:6a0716eb2e73 | 16 | r->ticks = ((v->ticks+MAX_TICK)>>1); |
the729 | 0:6a0716eb2e73 | 17 | } else { |
the729 | 0:6a0716eb2e73 | 18 | r->ticks = (v->ticks>>1); |
the729 | 0:6a0716eb2e73 | 19 | } |
the729 | 0:6a0716eb2e73 | 20 | r->tv_sec = (v->tv_sec>>1); |
the729 | 0:6a0716eb2e73 | 21 | } |
the729 | 0:6a0716eb2e73 | 22 | |
the729 | 0:6a0716eb2e73 | 23 | void hdtv_div8 (hdtimeval_t * r, hdtimeval_t * v) |
the729 | 0:6a0716eb2e73 | 24 | { |
the729 | 0:6a0716eb2e73 | 25 | r->ticks = ((v->ticks+MAX_TICK*(v->tv_sec & 0x7))>>3); |
the729 | 0:6a0716eb2e73 | 26 | r->tv_sec = (v->tv_sec>>3); |
the729 | 0:6a0716eb2e73 | 27 | } |
the729 | 0:6a0716eb2e73 | 28 | |
the729 | 0:6a0716eb2e73 | 29 | void hdtv_sub (hdtimeval_t * r, hdtimeval_t * v1, hdtimeval_t * v2) |
the729 | 0:6a0716eb2e73 | 30 | { |
the729 | 0:6a0716eb2e73 | 31 | r->tv_sec = v1->tv_sec - v2->tv_sec; |
the729 | 0:6a0716eb2e73 | 32 | if (v1->ticks < v2->ticks) { |
the729 | 0:6a0716eb2e73 | 33 | r->ticks = MAX_TICK + v1->ticks - v2->ticks; |
the729 | 0:6a0716eb2e73 | 34 | r->tv_sec --; |
the729 | 0:6a0716eb2e73 | 35 | } else { |
the729 | 0:6a0716eb2e73 | 36 | r->ticks = v1->ticks - v2->ticks; |
the729 | 0:6a0716eb2e73 | 37 | } |
the729 | 0:6a0716eb2e73 | 38 | } |
the729 | 0:6a0716eb2e73 | 39 | |
the729 | 0:6a0716eb2e73 | 40 | void hdtv_muldiv (hdtimeval_t * r, hdtimeval_t * v, hdtimeval_t * m, hdtimeval_t * d) |
the729 | 0:6a0716eb2e73 | 41 | { |
the729 | 0:6a0716eb2e73 | 42 | double dblm, dbld, factor, rsec, rticks; |
the729 | 0:6a0716eb2e73 | 43 | uint32_t irticks, c; |
the729 | 0:6a0716eb2e73 | 44 | int32_t irsec; |
the729 | 0:6a0716eb2e73 | 45 | |
the729 | 0:6a0716eb2e73 | 46 | dblm = (double)(m->tv_sec) * (double)MAX_TICK + (double)(m->ticks); |
the729 | 0:6a0716eb2e73 | 47 | dbld = (double)(d->tv_sec) * (double)MAX_TICK + (double)(d->ticks); |
the729 | 0:6a0716eb2e73 | 48 | factor = dblm / dbld; |
the729 | 0:6a0716eb2e73 | 49 | |
the729 | 0:6a0716eb2e73 | 50 | rsec = (double)(v->tv_sec) * factor; |
the729 | 0:6a0716eb2e73 | 51 | rticks = (double)(v->ticks) * factor + (rsec - floor(rsec)) * (double)MAX_TICK; |
the729 | 0:6a0716eb2e73 | 52 | irticks = (uint32_t)rticks; |
the729 | 0:6a0716eb2e73 | 53 | irsec = (int32_t)rsec; |
the729 | 0:6a0716eb2e73 | 54 | c = irticks / MAX_TICK; |
the729 | 0:6a0716eb2e73 | 55 | r->tv_sec = irsec + c; |
the729 | 0:6a0716eb2e73 | 56 | r->ticks = irticks - c * MAX_TICK; |
the729 | 0:6a0716eb2e73 | 57 | } |
the729 | 0:6a0716eb2e73 | 58 | |
the729 | 0:6a0716eb2e73 | 59 | void hdtv_totv(timeval_t * r, hdtimeval_t * v) |
the729 | 0:6a0716eb2e73 | 60 | { |
the729 | 0:6a0716eb2e73 | 61 | r->tv_sec = v->tv_sec; |
the729 | 0:6a0716eb2e73 | 62 | r->tv_usec = (v->ticks + 48)/96; |
the729 | 0:6a0716eb2e73 | 63 | if (r->tv_usec >= 1000000) { |
the729 | 0:6a0716eb2e73 | 64 | r->tv_sec ++; |
the729 | 0:6a0716eb2e73 | 65 | r->tv_usec -= 1000000; |
the729 | 0:6a0716eb2e73 | 66 | } |
the729 | 0:6a0716eb2e73 | 67 | } |