OLD | NEW |
1 // Copyright (c) 2004, Google Inc. | 1 // Copyright (c) 2004, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // ---------------------------------------------------------------------- | 43 // ---------------------------------------------------------------------- |
44 | 44 |
45 #ifndef GOOGLE_BASE_CYCLECLOCK_H_ | 45 #ifndef GOOGLE_BASE_CYCLECLOCK_H_ |
46 #define GOOGLE_BASE_CYCLECLOCK_H_ | 46 #define GOOGLE_BASE_CYCLECLOCK_H_ |
47 | 47 |
48 #include "base/basictypes.h" // make sure we get the def for int64 | 48 #include "base/basictypes.h" // make sure we get the def for int64 |
49 #include "base/arm_instruction_set_select.h" | 49 #include "base/arm_instruction_set_select.h" |
50 #if defined(__MACH__) && defined(__APPLE__) | 50 #if defined(__MACH__) && defined(__APPLE__) |
51 # include <mach/mach_time.h> | 51 # include <mach/mach_time.h> |
52 #endif | 52 #endif |
53 // For MSVC, we want the __rdtsc intrinsic, declared in <intrin.h>. | 53 // For MSVC, we want to use '_asm rdtsc' when possible (since it works |
54 // Unfortunately, in some environments, <windows.h> and <intrin.h> have | 54 // with even ancient MSVC compilers), and when not possible the |
55 // conflicting declarations of some other intrinsics, breaking compilation. | 55 // __rdtsc intrinsic, declared in <intrin.h>. Unfortunately, in some |
| 56 // environments, <windows.h> and <intrin.h> have conflicting |
| 57 // declarations of some other intrinsics, breaking compilation. |
56 // Therefore, we simply declare __rdtsc ourselves. See also | 58 // Therefore, we simply declare __rdtsc ourselves. See also |
57 // http://connect.microsoft.com/VisualStudio/feedback/details/262047 | 59 // http://connect.microsoft.com/VisualStudio/feedback/details/262047 |
58 #if defined(_MSC_VER) | 60 #if defined(_MSC_VER) && !defined(_M_IX86) |
59 extern "C" uint64 __rdtsc(); | 61 extern "C" uint64 __rdtsc(); |
60 #pragma intrinsic(__rdtsc) | 62 #pragma intrinsic(__rdtsc) |
61 #endif | 63 #endif |
62 #ifdef HAVE_SYS_TIME_H | 64 #ifdef ARMV3 |
63 #include <sys/time.h> | 65 #include <sys/time.h> |
64 #endif | 66 #endif |
65 | 67 |
66 // NOTE: only i386 and x86_64 have been well tested. | 68 // NOTE: only i386 and x86_64 have been well tested. |
67 // PPC, sparc, alpha, and ia64 are based on | 69 // PPC, sparc, alpha, and ia64 are based on |
68 // http://peter.kuscsik.com/wordpress/?p=14 | 70 // http://peter.kuscsik.com/wordpress/?p=14 |
69 // with modifications by m3b. See also | 71 // with modifications by m3b. See also |
70 // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h | 72 // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h |
71 struct CycleClock { | 73 struct CycleClock { |
72 // This should return the number of cycles since power-on. Thread-safe. | 74 // This should return the number of cycles since power-on. Thread-safe. |
(...skipping 28 matching lines...) Expand all Loading... |
101 return (tbu1 << 32) | tbl; | 103 return (tbu1 << 32) | tbl; |
102 #elif defined(__sparc__) | 104 #elif defined(__sparc__) |
103 int64 tick; | 105 int64 tick; |
104 asm(".byte 0x83, 0x41, 0x00, 0x00"); | 106 asm(".byte 0x83, 0x41, 0x00, 0x00"); |
105 asm("mov %%g1, %0" : "=r" (tick)); | 107 asm("mov %%g1, %0" : "=r" (tick)); |
106 return tick; | 108 return tick; |
107 #elif defined(__ia64__) | 109 #elif defined(__ia64__) |
108 int64 itc; | 110 int64 itc; |
109 asm("mov %0 = ar.itc" : "=r" (itc)); | 111 asm("mov %0 = ar.itc" : "=r" (itc)); |
110 return itc; | 112 return itc; |
| 113 #elif defined(_MSC_VER) && defined(_M_IX86) |
| 114 // Older MSVC compilers (like 7.x) don't seem to support the |
| 115 // __rdtsc intrinsic properly, so I prefer to use _asm instead |
| 116 // when I know it will work. Otherwise, I'll use __rdtsc and hope |
| 117 // the code is being compiled with a non-ancient compiler. |
| 118 _asm rdtsc |
111 #elif defined(_MSC_VER) | 119 #elif defined(_MSC_VER) |
112 return __rdtsc(); | 120 return __rdtsc(); |
113 #elif defined(ARMV3) | 121 #elif defined(ARMV3) |
114 #if defined(ARMV6) // V6 is the earliest arch that has a standard cyclecount | 122 #if defined(ARMV6) // V6 is the earliest arch that has a standard cyclecount |
115 uint32 pmccntr; | 123 uint32 pmccntr; |
116 uint32 pmuseren; | 124 uint32 pmuseren; |
117 uint32 pmcntenset; | 125 uint32 pmcntenset; |
118 // Read the user mode perf monitor counter access permissions. | 126 // Read the user mode perf monitor counter access permissions. |
119 asm("mrc p15, 0, %0, c9, c14, 0" : "=r" (pmuseren)); | 127 asm("mrc p15, 0, %0, c9, c14, 0" : "=r" (pmuseren)); |
120 if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. | 128 if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. |
(...skipping 12 matching lines...) Expand all Loading... |
133 // The soft failover to a generic implementation is automatic only for ARM. | 141 // The soft failover to a generic implementation is automatic only for ARM. |
134 // For other platforms the developer is expected to make an attempt to create | 142 // For other platforms the developer is expected to make an attempt to create |
135 // a fast implementation and use generic version if nothing better is available. | 143 // a fast implementation and use generic version if nothing better is available. |
136 #error You need to define CycleTimer for your O/S and CPU | 144 #error You need to define CycleTimer for your O/S and CPU |
137 #endif | 145 #endif |
138 } | 146 } |
139 }; | 147 }; |
140 | 148 |
141 | 149 |
142 #endif // GOOGLE_BASE_CYCLECLOCK_H_ | 150 #endif // GOOGLE_BASE_CYCLECLOCK_H_ |
OLD | NEW |