LCOV - code coverage report
Current view: top level - src - pl_clock.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 5 6 83.3 %
Date: 2025-03-29 09:04:10 Functions: 0 0 -
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 4 8 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of libplacebo.
       3                 :            :  *
       4                 :            :  * libplacebo is free software; you can redistribute it and/or
       5                 :            :  * modify it under the terms of the GNU Lesser General Public
       6                 :            :  * License as published by the Free Software Foundation; either
       7                 :            :  * version 2.1 of the License, or (at your option) any later version.
       8                 :            :  *
       9                 :            :  * libplacebo is distributed in the hope that it will be useful,
      10                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12                 :            :  * GNU Lesser General Public License for more details.
      13                 :            :  *
      14                 :            :  * You should have received a copy of the GNU Lesser General Public
      15                 :            :  * License along with libplacebo. If not, see <http://www.gnu.org/licenses/>.
      16                 :            :  */
      17                 :            : 
      18                 :            : #pragma once
      19                 :            : 
      20                 :            : #include <time.h>
      21                 :            : #include <stdint.h>
      22                 :            : 
      23                 :            : #include "os.h"
      24                 :            : 
      25                 :            : #ifdef PL_HAVE_WIN32
      26                 :            : # include <windows.h>
      27                 :            : # define PL_CLOCK_QPC
      28                 :            : #elif defined(PL_HAVE_APPLE)
      29                 :            : # include <Availability.h>
      30                 :            : # if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED)  && __MAC_OS_X_VERSION_MIN_REQUIRED  < 101200) || \
      31                 :            :      (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 100000) || \
      32                 :            :      (defined(__TV_OS_VERSION_MIN_REQUIRED)     && __TV_OS_VERSION_MIN_REQUIRED     < 100000) || \
      33                 :            :      (defined(__WATCH_OS_VERSION_MIN_REQUIRED)  && __WATCH_OS_VERSION_MIN_REQUIRED  < 30000)  || \
      34                 :            :      !defined(CLOCK_MONOTONIC_RAW)
      35                 :            : #  include <mach/mach_time.h>
      36                 :            : #  define PL_CLOCK_MACH
      37                 :            : # else
      38                 :            : #  define PL_CLOCK_MONOTONIC_RAW
      39                 :            : # endif
      40                 :            : #elif defined(CLOCK_MONOTONIC_RAW)
      41                 :            : # define PL_CLOCK_MONOTONIC_RAW
      42                 :            : #elif defined(TIME_UTC)
      43                 :            : # define PL_CLOCK_TIMESPEC_GET
      44                 :            : #else
      45                 :            : # warning "pl_clock not implemented for this platform!"
      46                 :            : #endif
      47                 :            : 
      48                 :            : typedef uint64_t pl_clock_t;
      49                 :            : 
      50                 :            : static inline pl_clock_t pl_clock_now(void)
      51                 :            : {
      52                 :            : #if defined(PL_CLOCK_QPC)
      53                 :            : 
      54                 :            :     LARGE_INTEGER counter;
      55                 :            :     QueryPerformanceCounter(&counter);
      56                 :            :     return counter.QuadPart;
      57                 :            : 
      58                 :            : #elif defined(PL_CLOCK_MACH)
      59                 :            : 
      60                 :            :     return mach_absolute_time();
      61                 :            : 
      62                 :            : #else
      63                 :            : 
      64                 :      74178 :     struct timespec tp = { .tv_sec = 0, .tv_nsec = 0 };
      65                 :            : #if defined(PL_CLOCK_MONOTONIC_RAW)
      66                 :      74178 :     clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
      67                 :            : #elif defined(PL_CLOCK_TIMESPEC_GET)
      68                 :            :     timespec_get(&tp, TIME_UTC);
      69                 :            : #endif
      70   [ +  +  -  - ]:       2651 :     return tp.tv_sec * UINT64_C(1000000000) + tp.tv_nsec;
      71                 :            : 
      72                 :            : #endif
      73                 :            : }
      74                 :            : 
      75                 :            : static inline double pl_clock_diff(pl_clock_t a, pl_clock_t b)
      76                 :            : {
      77                 :            :     double frequency = 1e9;
      78                 :            : 
      79                 :            : #if defined(PL_CLOCK_QPC)
      80                 :            : 
      81                 :            :     LARGE_INTEGER freq;
      82                 :            :     QueryPerformanceFrequency(&freq);
      83                 :            :     frequency = freq.QuadPart;
      84                 :            : 
      85                 :            : #elif defined(PL_CLOCK_MACH)
      86                 :            : 
      87                 :            :     mach_timebase_info_data_t time_base;
      88                 :            :     if (mach_timebase_info(&time_base) != KERN_SUCCESS)
      89                 :            :         return 0;
      90                 :            :     frequency = (time_base.denom * 1e9) / time_base.numer;
      91                 :            : 
      92                 :            : #endif
      93                 :            : 
      94   [ -  +  -  + ]:      72930 :     if (b > a)
      95                 :          0 :         return (b - a) / -frequency;
      96                 :            :     else
      97                 :      72930 :         return (a - b) / frequency;
      98                 :            : }

Generated by: LCOV version 1.16