source: trunk/src/core/time.cc @ 460

Last change on this file since 460 was 402, checked in by epyon, 10 years ago
  • cleanups of clang warnings (gotta love them all)
  • only nv-core for now (this will take a while for the whole source)
  • mainly C++ casts instead of C-style casts, but also a few bugs fixed!
File size: 2.9 KB
RevLine 
[319]1// Copyright (C) 2011-2014 ChaosForge Ltd
[395]2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[34]6
[319]7#include "nv/core/time.hh"
[34]8
[319]9#include "nv/core/logging.hh"
[34]10
[376]11#include <ctime>
12
[34]13#if NV_COMPILER == NV_MSVC
14#define WIN32_LEAN_AND_MEAN
15#include <windows.h>
16#include <intrin.h>
17#pragma intrinsic(__rdtsc)
18#else
[401]19#if NV_PLATFORM == NV_WINDOWS
[376]20// mingw doesn't have usleep nor nanosleep...
21#include <windows.h>
22#endif
23// #include <unistd.h>
24// #include <time.h>
25// #include <sys/time.h>
26// #include <sys/types.h>
27#include <stdio.h>
28#include <time.h>
[34]29#include <sys/time.h>
30#endif
31
32
[376]33
[34]34struct timer_impl
35{
36        timer_impl()
37        {
38                clock_zero = clock();
39#if NV_COMPILER == NV_MSVC
40                QueryPerformanceFrequency(&frequency);
41                QueryPerformanceCounter(&query_zero);
42#else
43                gettimeofday(&timeval_zero, NULL);
44#endif
45        }
46        clock_t clock_zero;
47#if NV_COMPILER == NV_MSVC
48        LARGE_INTEGER query_zero;
49        LARGE_INTEGER frequency;
50#else
51        struct timeval timeval_zero;
52#endif
53};
54
55static timer_impl zero_timer;
56
[121]57nv::uint64 nv::get_ticks()
[34]58{
59#if NV_COMPILER == NV_MSVC
60        return __rdtsc();
[121]61#else
62        register long long ticks asm("eax") = 0;
[34]63        asm volatile (".byte 15, 49" : : : "eax", "edx");
[121]64        return static_cast<nv::uint64>( ticks );
[34]65#endif
66}
67
68void nv::sleep( uint32 ms )
69{
70#if NV_COMPILER == NV_MSVC
71        Sleep( ms );
[121]72#else
[401]73#if NV_PLATFORM == NV_WINDOWS
[376]74        Sleep( ms );
75#else
76        struct timespec ts;
77        ts.tv_sec = 0;
[380]78        ts.tv_nsec = (long)ms * 1000000;
[376]79        nanosleep(&ts, NULL);
80//      usleep( ms * 1000 );
[34]81#endif
[376]82#endif
[34]83}
84
85nv::uint32 nv::get_cpu_ms()
86{
[402]87        return static_cast<uint32>( static_cast<f32>( clock() - zero_timer.clock_zero ) / ( static_cast<f32>(CLOCKS_PER_SEC) / 1000.0f ) ) ;
[34]88}
89
[149]90nv::uint64 nv::get_cpu_us()
[34]91{
[402]92        return static_cast<uint64>( static_cast<f32>( clock() - zero_timer.clock_zero ) / ( static_cast<f32>(CLOCKS_PER_SEC) / 1000000.0f ) ) ;
[34]93}
94
95nv::uint32 nv::get_system_ms()
96{
97#if NV_COMPILER == NV_MSVC
98        LARGE_INTEGER now;
99        QueryPerformanceCounter(&now);
100        LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
[402]101        return static_cast<uint32>(1000.0 * result / static_cast<f64>( zero_timer.frequency.QuadPart ) );
[34]102#else
103        struct timeval now;
104        gettimeofday(&now, NULL);
[402]105        return static_cast<uint32>( (now.tv_sec - zero_timer.timeval_zero.tv_sec)*1000+(now.tv_usec-zero_timer.timeval_zero.tv_usec)/1000 );
[34]106#endif
107}
108
[149]109nv::uint64 nv::get_system_us()
[34]110{
111#if NV_COMPILER == NV_MSVC
112        LARGE_INTEGER now;
113        QueryPerformanceCounter(&now);
114        LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
[402]115        return static_cast<uint64>(1000000.0 * result / static_cast<f64>( zero_timer.frequency.QuadPart ) );
[34]116#else
117        struct timeval now;
118        gettimeofday(&now, NULL);
[402]119        return static_cast<uint64>( (now.tv_sec - zero_timer.timeval_zero.tv_sec)*1000000+(now.tv_usec - zero_timer.timeval_zero.tv_usec) );
[34]120#endif
121}
Note: See TracBrowser for help on using the repository browser.