Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 14 | |
| 15 | #include "Timer.hpp" |
| 16 | |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 17 | #if !defined(__i386__) && defined(_M_IX86) |
| 18 | #define __i386__ 1 |
| 19 | #endif |
| 20 | |
| 21 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64)) |
| 22 | #define __x86_64__ 1 |
| 23 | #endif |
| 24 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 25 | #if defined(_WIN32) |
| 26 | #ifndef WIN32_LEAN_AND_MEAN |
| 27 | #define WIN32_LEAN_AND_MEAN |
| 28 | #endif |
| 29 | #include <windows.h> |
Alexis Hetu | 61e8bb1 | 2016-08-31 17:10:36 -0400 | [diff] [blame] | 30 | #include <intrin.h> |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 31 | #else |
| 32 | #include <sys/time.h> |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 33 | #if defined(__i386__) || defined(__x86_64__) |
| 34 | #include <x86intrin.h> |
| 35 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 36 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 37 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 38 | namespace sw |
| 39 | { |
| 40 | Timer::Timer() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | Timer::~Timer() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | double Timer::seconds() |
| 49 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 50 | #if defined(_WIN32) |
| 51 | return (double)counter() / (double)frequency(); |
| 52 | #else |
| 53 | timeval t; |
| 54 | gettimeofday(&t, 0); |
| 55 | return (double)t.tv_sec + (double)t.tv_usec * 1.0e-6; |
| 56 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | int64_t Timer::ticks() |
| 60 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 61 | #if defined(_WIN32) |
Tom Tan | f6d56f1 | 2019-01-02 22:01:55 -0800 | [diff] [blame] | 62 | #if defined(_M_ARM64) |
| 63 | return _ReadStatusReg(ARM64_PMCCNTR_EL0); |
| 64 | #else |
| 65 | return __rdtsc(); |
| 66 | #endif |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 67 | #elif defined(__i386__) || defined(__x86_64__) |
Jeff Muizelaar | 221a274 | 2019-07-13 21:06:29 -0400 | [diff] [blame] | 68 | return __builtin_ia32_rdtsc(); |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 69 | #else |
| 70 | return 0; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 71 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | int64_t Timer::counter() |
| 75 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 76 | #if defined(_WIN32) |
| 77 | int64_t counter; |
| 78 | QueryPerformanceCounter((LARGE_INTEGER*)&counter); |
| 79 | return counter; |
| 80 | #else |
| 81 | timeval t; |
| 82 | gettimeofday(&t, 0); |
| 83 | return t.tv_sec * 1000000 + t.tv_usec; |
| 84 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | int64_t Timer::frequency() |
| 88 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 89 | #if defined(_WIN32) |
| 90 | int64_t frequency; |
| 91 | QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); |
| 92 | return frequency; |
| 93 | #else |
| 94 | return 1000000; // gettimeofday uses microsecond resolution |
| 95 | #endif |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 96 | } |
| 97 | } |