home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / BNLIB / CPUTIME.H < prev    next >
C/C++ Source or Header  |  1996-05-16  |  5KB  |  198 lines

  1. #ifndef CPUTIME_H
  2. #define CPUTIME_H
  3.  
  4. /*
  5.  * Figure out what clock to use.  Each possibility can be specifically
  6.  * enabled or disabled by predefining USE_XXX to 1 or 0.  For some,
  7.  * the code attempts to detect availability automatically.  If the
  8.  * Symbols HAVE_XXX are defined, they are used.  If not, they are
  9.  * set to reasonable default assumptions while further conditions
  10.  * are checked.  The choices, and the ways they are auto-detected are:
  11.  * - gethrvtime(), if HAVE_GETHRVTIME is set to 1.
  12.  * - clock_gettime(CLOCK_VIRTUAL,...), if CLOCK_VIRTUAL is defined in <time.h>
  13.  * - getrusage(RUSAGE_SELF,...), if RUSAGE_SELF is defined in <sys/resource.h>
  14.  * - clock(), if CLOCKS_PER_SEC or CLK_TCK are defined in <time.h>
  15.  * - time(), unless specifically disabled.
  16.  *
  17.  * The symbol CLOCK_AVAIL is given a value of 1 if a clock is found.
  18.  * The following are then available:
  19.  * timetype (typedef): the type needed to hold a clock value.
  20.  * gettime(t) (macro): A function that gets passed a timetype *.
  21.  * subtime(d,s) (macro): Sets d -= s, essentially.
  22.  * msec(t) (macro): Given a timetype, return the number of milliseconds
  23.  *    in it, as an unsigned integer between 0 and 999.
  24.  * sec(t) (macro): Given a timetype, return the number of seconds in it,
  25.  *    as an unsigned long integer.
  26.  */
  27.  
  28. /* We expect that our caller has already #included "config.h" if possible. */
  29.  
  30. #ifdef UNIX
  31. /* Nothing */
  32. #elif unix
  33. #define UNIX 1
  34. #elif __unix
  35. #define UNIX 1
  36. #elif __unix__
  37. #define UNIX 1
  38. #endif
  39.  
  40. #ifndef TIME_WITH_SYS_TIME
  41. #define TIME_WITH_SYS_TIME 1    /* Assume true if not told */
  42. #endif
  43. #ifndef HAVE_SYS_TIME_H
  44. #define HAVE_SYS_TIME_H 0    /* Assume true if not told */
  45. #endif
  46.  
  47. /*
  48.  * Include <time.h> unless that would prevent us from later including
  49.  * <sys/time.h>, in which case include *that* immediately.
  50.  */
  51. #if TIME_WITH_SYS_TIME
  52. #include <time.h>
  53. #elif HAVE_SYS_TIME_H
  54. #include <sys/time.h>
  55. #else
  56. #include <time.h>
  57. #endif
  58.  
  59. #ifndef USE_GETHRVTIME
  60. #define USE_GETHRVTIME HAVE_GETHRVTIME
  61. #endif
  62.  
  63. #if USE_GETHRVTIME
  64. #define CLOCK_AVAIL 1
  65. typedef hrtime_t timetype;
  66. #define gettime(t) *(t) = gethrvtime()
  67. #define subtime(d,s) d -= s
  68. #define msec(t) (unsigned)((t/1000000)%1000)
  69. #define sec(t) (unsigned long)(t/1000000000)
  70.  
  71. #else
  72. #ifndef USE_CLOCK_GETTIME
  73. #ifndef HAVE_CLOCK_GETTIME
  74. #define HAVE_CLOCK_GETTIME 1    /* Assume the CLOCK_VIRTUAL test will catch */
  75. #endif
  76. /*
  77.  * It turns out to be non-ANSI to use the apparently simpler construct
  78.  * "#define USE_CLOCK_GETTIME defined(CLOCK_VIRTUAL)", since
  79.  * "If the token defined is generated as a result of this replacement
  80.  *  process or use of the defined unary operator does not match one
  81.  *  of the two specified forms prior ro macro replacement, the behaviour
  82.  *  is undefined."  (ANSI/ISO 9899-1990 section 6.8.1)
  83.  * In practice, it breaks the DEC Alpha compiler.
  84.  */
  85. #if HAVE_CLOCK_GETTIME
  86. #ifdef CLOCK_VIRTUAL
  87. #define USE_CLOCK_GETTIME 1
  88. #endif
  89. #endif
  90. #endif
  91.  
  92. #if USE_CLOCK_GETTIME
  93. #define CLOCK_AVAIL 1
  94. typedef struct timespec timetype;
  95. #define gettime(t) clock_gettime(CLOCK_VIRTUAL, t)
  96. #define subtime(d,s) \
  97.     d.tv_sec -= s.tv_sec + (d.tv_nsec >= s.tv_nsec ? \
  98.                             (d.tv_nsec -= s.tv_nsec, 0) : \
  99.                             (d.tv_nsec += 1000000000-s.tv_nsec, 1))
  100. #define msec(t) (unsigned)(t.tv_nsec/1000000)
  101. #define sec(t) (unsigned long)(t.tv_sec)
  102.  
  103. #else
  104. #if UNIX
  105. #ifndef HAVE_GETRUSAGE
  106. #define HAVE_GETRUSAGE 1
  107. #endif
  108. #endif /* UNIX */
  109.  
  110. #if HAVE_GETRUSAGE
  111. #if TIME_WITH_SYS_TIME
  112. #ifndef HAVE_SYS_TIME_H
  113. #include <sys/time.h>
  114. #elif HAVE_SYS_TIME_H
  115. #include <sys/time.h>
  116. #endif
  117. #endif /* TIME_WITH_SYS_TIME */
  118. #include <sys/resource.h>
  119.  
  120. #ifdef RUSAGE_SELF
  121. #define USE_GETRUSAGE 1
  122. #endif
  123. #endif /* HAVE_GETRUSAGE */
  124.  
  125. #if USE_GETRUSAGE
  126. #define CLOCK_AVAIL 1
  127. typedef struct rusage timetype;
  128. #define gettime(t) getrusage(RUSAGE_SELF, t);
  129. #define subtime(d, s) \
  130.     d.ru_utime.tv_sec -= s.ru_utime.tv_sec + \
  131.                  (d.ru_utime.tv_usec >= s.ru_utime.tv_usec ? \
  132.                   (d.ru_utime.tv_usec -= s.ru_utime.tv_usec, 0) : \
  133.                   (d.ru_utime.tv_usec += 1000000-s.ru_utime.tv_usec, 1))
  134. #define msec(t) (unsigned)(t.ru_utime.tv_usec/1000)
  135. #define sec(t) (unsigned long)(t.ru_utime.tv_sec)
  136.  
  137. #else
  138.  
  139. #ifndef HAVE_CLOCK
  140. #define HAVE_CLOCK 1
  141. #endif
  142.  
  143. #if HAVE_CLOCK
  144. #ifndef CLOCKS_PER_SEC
  145. #ifdef CLK_TCK
  146. #define CLOCKS_PER_SEC CLK_TCK
  147. #endif
  148. #endif /* !defined(CLOCKS_PER_SEC) */
  149.  
  150. #ifndef USE_CLOCK
  151. #ifdef CLOCKS_PER_SEC
  152. #define USE_CLOCK 1
  153. #endif
  154. #endif /* !defined(USE_CLOCK) */
  155. #endif /* HAVE_CLOCK */
  156.  
  157. #if USE_CLOCK
  158. #define CLOCK_AVAIL 1
  159. typedef clock_t timetype;
  160. #define gettime(t) *(t) = clock()
  161. #define subtime(d, s) d -= s
  162. #if CLOCKS_PER_SEC == 18.2    /* MS-DOS-ism */
  163. #define msec(t) (unsigned)(t*5 % 91 * 1000 / 91)
  164.  CLOCKS_PER_SEC * 1000 / CLOCKS_PER_SEC)
  165. #define sec(t) (unsigned long)(t*5 / 91)
  166. #else
  167. #define msec(t) (unsigned)(t % CLOCKS_PER_SEC * 1000 / CLOCKS_PER_SEC)
  168. #define sec(t) (unsigned long)(t/CLOCKS_PER_SEC)
  169. #endif
  170.  
  171. #else
  172.  
  173. #ifndef HAVE_TIME
  174. #define HAVE_TIME 1
  175. #endif
  176.  
  177. #if HAVE_TIME
  178. #ifndef USE_TIME
  179. #define USE_TIME 1
  180. #endif
  181. #endif
  182.  
  183. #if USE_TIME
  184. #define CLOCK_AVAIL 1
  185. typedef time_t timetype;
  186. #define gettime(t) time(t)
  187. #define subtime(d, s) d -= s
  188. #define msec(t) (unsigned)0
  189. #define sec(t) (unsigned long)t
  190.  
  191. #endif /* USE_TIME */
  192. #endif /* USE_CLOCK */
  193. #endif /* USE_GETRUSAGE */
  194. #endif /* USE_CLOCK_GETTIME */
  195. #endif /* USE_GETHRVTIME */
  196.  
  197. #endif CPUTIME_H
  198.