1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2019-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#pragma once
#include "types.h"
#if defined _WIN32
# include <wtypes.h>
# include <winbase.h>
# include <intrin.h>
#else
# include <time.h>
# ifndef NSEC_PER_SEC
# define NSEC_PER_SEC 1000000000L
# endif
#endif
namespace iSTD
{
/*****************************************************************************\
Inline Function:
Pause
Description:
executes __asm pause
\*****************************************************************************/
__forceinline void Pause( void )
{
#if defined(_M_IX86) || defined (_M_AMD64) || \
defined(__i386__) || defined(__x86_64__)
_mm_pause();
#endif
}
#if defined _WIN32
/*****************************************************************************\
Inline Function:
GetTimestampCounter
Description:
Returns the number of CPU clock cycles since the last reset.
\*****************************************************************************/
__forceinline QWORD GetTimestampCounter( void )
{
#ifdef ISTDLIB_UMD
{
QWORD count = 0;
::QueryPerformanceCounter( (LARGE_INTEGER*)&count );
return count;
}
#else // !ISTDLIB_UMD
{
#ifdef _WIN64
{
return __rdtsc();
}
#else // !_WIN64
{
__asm rdtsc;
}
#endif // _WIN64
}
#endif // ISTDLIB_UMD
}
/*****************************************************************************\
Inline Function:
GetTimestampFrequency
Description:
Returns the frequency of the CPU clock cycles
\*****************************************************************************/
__forceinline QWORD GetTimestampFrequency( void )
{
#ifdef ISTDLIB_UMD
{
QWORD frequency = 0;
::QueryPerformanceFrequency( (LARGE_INTEGER*)&frequency );
return frequency;
}
#else // !ISTDLIB_UMD
{
// Note: Use the following for Conroe 2.4GHz
// return 2394050000;
return 0;
}
#endif // ISTDLIB_UMD
}
/*****************************************************************************\
Inline Function:
Wait
Description:
Waits for some number of milliseconds to complete
\*****************************************************************************/
__forceinline void Wait( const DWORD milliseconds )
{
const QWORD clocksPerSecond = GetTimestampFrequency();
const QWORD clocksPerMilliSecond = ( clocksPerSecond > 1000 ) ? clocksPerSecond / 1000 : 1;
const QWORD clocks = milliseconds * clocksPerMilliSecond;
const QWORD start = GetTimestampCounter();
while( clocks < ( GetTimestampCounter() - start ) )
{
Pause();
};
}
#else
/*****************************************************************************\
Inline Function:
GetTimestampCounter
Description:
Returns the value of high resolution performance counter
\*****************************************************************************/
__forceinline QWORD GetTimestampCounter( void )
{
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return (QWORD)time.tv_nsec +
(QWORD)time.tv_sec * (QWORD)NSEC_PER_SEC;
}
/*****************************************************************************\
Inline Function:
GetTimestampFrequency
Description:
Returns the frequency of high resolution performance counter.
On Linux/Android we use timer with nsec accuracy
\*****************************************************************************/
__forceinline QWORD GetTimestampFrequency( void )
{
return NSEC_PER_SEC;
}
#endif
} // iSTD
|