Home | History | Annotate | Download | only in vpx_ports
      1 /*
      2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 
     12 #ifndef VPX_TIMER_H
     13 #define VPX_TIMER_H
     14 
     15 #if defined(_WIN32)
     16 /*
     17  * Win32 specific includes
     18  */
     19 #ifndef WIN32_LEAN_AND_MEAN
     20 #define WIN32_LEAN_AND_MEAN
     21 #endif
     22 #include <windows.h>
     23 #else
     24 /*
     25  * POSIX specific includes
     26  */
     27 #include <sys/time.h>
     28 
     29 /* timersub is not provided by msys at this time. */
     30 #ifndef timersub
     31 #define timersub(a, b, result) \
     32     do { \
     33         (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
     34         (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
     35         if ((result)->tv_usec < 0) { \
     36             --(result)->tv_sec; \
     37             (result)->tv_usec += 1000000; \
     38         } \
     39     } while (0)
     40 #endif
     41 #endif
     42 
     43 
     44 struct vpx_usec_timer
     45 {
     46 #if defined(_WIN32)
     47     LARGE_INTEGER  begin, end;
     48 #else
     49     struct timeval begin, end;
     50 #endif
     51 };
     52 
     53 
     54 static void
     55 vpx_usec_timer_start(struct vpx_usec_timer *t)
     56 {
     57 #if defined(_WIN32)
     58     QueryPerformanceCounter(&t->begin);
     59 #else
     60     gettimeofday(&t->begin, NULL);
     61 #endif
     62 }
     63 
     64 
     65 static void
     66 vpx_usec_timer_mark(struct vpx_usec_timer *t)
     67 {
     68 #if defined(_WIN32)
     69     QueryPerformanceCounter(&t->end);
     70 #else
     71     gettimeofday(&t->end, NULL);
     72 #endif
     73 }
     74 
     75 
     76 static long
     77 vpx_usec_timer_elapsed(struct vpx_usec_timer *t)
     78 {
     79 #if defined(_WIN32)
     80     LARGE_INTEGER freq, diff;
     81 
     82     diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
     83 
     84     if (QueryPerformanceFrequency(&freq) && diff.QuadPart < freq.QuadPart)
     85         return (long)(diff.QuadPart * 1000000 / freq.QuadPart);
     86 
     87     return 1000000;
     88 #else
     89     struct timeval diff;
     90 
     91     timersub(&t->end, &t->begin, &diff);
     92     return diff.tv_sec ? 1000000 : diff.tv_usec;
     93 #endif
     94 }
     95 
     96 
     97 #endif
     98