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