home *** CD-ROM | disk | FTP | other *** search
- /* tvmath.c -- functions for calculating time value by structures
- %
- % struct timeval {
- % long tv_sec; / seconds /
- % long tv_usec; / and microseconds /
- % };
- %
- % Usage: tvadd(tv1, tv2)
- % struct timeval *tv1, *tv2
- % tv1 = tv1 + tv2
- %
- % tvsub(tv1, tv2)
- % struct timeval *tv1, *tv2
- % tv1 = tv1 - tv2
- %
- % Author: Jin Guojun - LBL
- */
-
- #include <sys/time.h>
-
-
- void
- tvadd(tv1, tv2)
- struct timeval *tv1, *tv2;
- {
- if( (tv1->tv_usec += tv2->tv_usec) > 1000000 ) {
- tv1->tv_sec++;
- tv1->tv_usec -= 1000000;
- }
- tv1->tv_sec += tv2->tv_sec;
- }
-
-
- void
- tvsub( tv1, tv2 )
- register struct timeval *tv1, *tv2;
- {
- if( (tv1->tv_usec -= tv2->tv_usec) < 0 ) {
- tv1->tv_sec--;
- tv1->tv_usec += 1000000;
- }
- tv1->tv_sec -= tv2->tv_sec;
- }
-
- float
- tvdiff( tv1, tv2 )
- register struct timeval *tv1, *tv2;
- {
- register time_t usec=tv1->tv_usec, sec=tv1->tv_sec;
- if( (usec -= tv2->tv_usec) < 0 ) {
- sec--;
- usec += 1000000;
- }
- sec -= tv2->tv_sec;
- return sec + usec * 0.000001;
- }
-
- /* avg min delay is 2 ms. on Sparc10/40 when 0 < us < 1000000.
- if us = 0 or us > 1000000, no delay at all,
- but we add 50 us to it on Sparc10/40, or 100 us on Sparc2.
- */
- #define MIN_DELAY 2000
- #ifndef TS_COST
- #define TS_COST 35.
- #endif
-
- float
- udelay(s, us)
- {
- struct timeval tv, tt;
- gettimeofday(&tt, 0);
- #ifdef vax11c
- {
- int status, delta_time[2];
-
- delta_time[0] = -10 * us;
- delta_time[1] = -s;
- status = sys$setimr(0, delta_time, 0, 0);
- /* if (!(status&1)) sys$exit(status); */
- sys$waitfr(0);
- }
- #else
- tv.tv_sec = s;
- tv.tv_usec = us - MIN_DELAY;
- select(1, 0, 0, 0, &tv);
- #endif
- if (us < 0) {
- gettimeofday(&tv, 0);
- return tvdiff(&tv, &tt);
- }
- return TS_COST;
- }
-