timer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* $Id: timer.h 767 2004-10-06 12:48:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner, Matt Bing.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef _TIMER_H_
  32. #define _TIMER_H_
  33. #include <time.h>
  34. #include <sys/time.h>
  35. #include <math.h>
  36. inline void timerdiv(struct timeval *tvp, float div);
  37. inline void float2timer(float time, struct timeval *tvp);
  38. #ifndef TIMEVAL_TO_TIMESPEC
  39. #define TIMEVAL_TO_TIMESPEC(tv, ts) { (ts)->tv_sec = (tv)->tv_sec; (ts)->tv_nsec = (tv)->tv_usec * 1000; }
  40. #endif
  41. #ifndef timerclear
  42. #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  43. #endif
  44. #ifndef timerisset
  45. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  46. #endif
  47. #ifndef timeradd
  48. #define timeradd(tvp, uvp, vvp) \
  49. do { \
  50. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  51. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  52. if ((vvp)->tv_usec >= 1000000) { \
  53. (vvp)->tv_sec++; \
  54. (vvp)->tv_usec -= 1000000; \
  55. } \
  56. } while (0)
  57. #endif
  58. #ifndef timersub
  59. #define timersub(tvp, uvp, vvp) \
  60. do { \
  61. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  62. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  63. if ((vvp)->tv_usec < 0) { \
  64. (vvp)->tv_sec--; \
  65. (vvp)->tv_usec += 1000000; \
  66. } \
  67. } while (0)
  68. #endif
  69. #ifndef timercmp
  70. #define timercmp(tvp, uvp, cmp) \
  71. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  72. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  73. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  74. #endif
  75. #define timermul(tvp, uvp, x) \
  76. do { \
  77. (uvp)->tv_sec = (tvp)->tv_sec * x; \
  78. (uvp)->tv_usec = (tvp)->tv_usec * x; \
  79. while((uvp)->tv_usec > 1000000) { \
  80. (uvp)->tv_sec++; \
  81. (uvp)->tv_usec -= 1000000; \
  82. } \
  83. } while(0)
  84. #define timerdiv2(tvp, x) \
  85. do { \
  86. (tvp)->tv_sec = (tvp)->tv_sec / x; \
  87. (tvp)->tv_usec = (tvp)->tv_usec / x; \
  88. } while(0)
  89. #endif