home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11.lha / ccs-lib / lib / tvmath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-05  |  1.6 KB  |  93 lines

  1. /* tvmath.c -- functions for calculating time value by structures
  2. %
  3. %    struct    timeval    {
  4. %        long    tv_sec;        / seconds /
  5. %        long    tv_usec;    / and microseconds /
  6. %        };
  7. %
  8. % Usage:    tvadd(tv1, tv2)
  9. %        struct timeval *tv1, *tv2
  10. %        tv1 = tv1 + tv2
  11. %
  12. %           tvsub(tv1, tv2)
  13. %           struct timeval *tv1, *tv2
  14. %        tv1 = tv1 - tv2
  15. %
  16. % Author:    Jin Guojun - LBL
  17. */
  18.  
  19. #include <sys/time.h>
  20.  
  21.  
  22. void
  23. tvadd(tv1, tv2)
  24. struct timeval *tv1, *tv2;
  25. {
  26.     if( (tv1->tv_usec += tv2->tv_usec) > 1000000 )   {
  27.         tv1->tv_sec++;
  28.         tv1->tv_usec -= 1000000;
  29.     }
  30.     tv1->tv_sec += tv2->tv_sec;
  31. }
  32.  
  33.  
  34. void
  35. tvsub( tv1, tv2 )
  36. register struct timeval *tv1, *tv2;
  37. {
  38.     if( (tv1->tv_usec -= tv2->tv_usec) < 0 )   {
  39.         tv1->tv_sec--;
  40.         tv1->tv_usec += 1000000;
  41.     }
  42.     tv1->tv_sec -= tv2->tv_sec;
  43. }
  44.  
  45. float
  46. tvdiff( tv1, tv2 )
  47. register struct timeval *tv1, *tv2;
  48. {
  49. register time_t    usec=tv1->tv_usec, sec=tv1->tv_sec;
  50.     if( (usec -= tv2->tv_usec) < 0 )   {
  51.         sec--;
  52.         usec += 1000000;
  53.     }
  54.     sec -= tv2->tv_sec;
  55. return    sec + usec * 0.000001;
  56. }
  57.  
  58. /*    avg min delay is 2 ms. on Sparc10/40 when 0 < us < 1000000.
  59.     if us = 0 or us > 1000000, no delay at all,
  60.     but we add 50 us to it on Sparc10/40, or 100 us on Sparc2.
  61. */
  62. #define MIN_DELAY    2000
  63. #ifndef    TS_COST
  64. #define    TS_COST    35.
  65. #endif
  66.  
  67. float
  68. udelay(s, us)
  69. {
  70. struct timeval    tv, tt;
  71.     gettimeofday(&tt, 0);
  72. #ifdef vax11c
  73.     {
  74.     int status, delta_time[2];
  75.  
  76.     delta_time[0] = -10 * us;
  77.     delta_time[1] = -s;
  78.     status = sys$setimr(0, delta_time, 0, 0);
  79. /*    if (!(status&1)) sys$exit(status);    */
  80.     sys$waitfr(0);
  81.     }
  82. #else
  83.     tv.tv_sec = s;
  84.     tv.tv_usec = us - MIN_DELAY;
  85.     select(1, 0, 0, 0, &tv);
  86. #endif
  87.     if (us < 0)    {
  88.         gettimeofday(&tv, 0);
  89.         return    tvdiff(&tv, &tt);
  90.     }
  91. return    TS_COST;
  92. }
  93.