timing.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Portable interface to the CPU cycle counter
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include "config.h"
  26. #if defined(POLARSSL_TIMING_C)
  27. #include "polarssl/timing.h"
  28. #if defined(_WIN32)
  29. #include <windows.h>
  30. #include <winbase.h>
  31. struct _hr_time
  32. {
  33. LARGE_INTEGER start;
  34. };
  35. #else
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <signal.h>
  40. #include <time.h>
  41. struct _hr_time
  42. {
  43. struct timeval start;
  44. };
  45. #endif
  46. #if defined(POLARSSL_HAVE_ASM) && \
  47. (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  48. unsigned long hardclock( void )
  49. {
  50. unsigned long tsc;
  51. __asm rdtsc
  52. __asm mov [tsc], eax
  53. return( tsc );
  54. }
  55. #else
  56. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__i386__)
  57. unsigned long hardclock( void )
  58. {
  59. unsigned long tsc;
  60. asm( "rdtsc" : "=a" (tsc) );
  61. return( tsc );
  62. }
  63. #else
  64. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
  65. (defined(__amd64__) || defined(__x86_64__))
  66. unsigned long hardclock( void )
  67. {
  68. unsigned long lo, hi;
  69. asm( "rdtsc" : "=a" (lo), "=d" (hi) );
  70. return( lo | (hi << 32) );
  71. }
  72. #else
  73. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
  74. (defined(__powerpc__) || defined(__ppc__))
  75. unsigned long hardclock( void )
  76. {
  77. unsigned long tbl, tbu0, tbu1;
  78. do
  79. {
  80. asm( "mftbu %0" : "=r" (tbu0) );
  81. asm( "mftb %0" : "=r" (tbl ) );
  82. asm( "mftbu %0" : "=r" (tbu1) );
  83. }
  84. while( tbu0 != tbu1 );
  85. return( tbl );
  86. }
  87. #else
  88. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__sparc__)
  89. unsigned long hardclock( void )
  90. {
  91. unsigned long tick;
  92. asm( ".byte 0x83, 0x41, 0x00, 0x00" );
  93. asm( "mov %%g1, %0" : "=r" (tick) );
  94. return( tick );
  95. }
  96. #else
  97. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__alpha__)
  98. unsigned long hardclock( void )
  99. {
  100. unsigned long cc;
  101. asm( "rpcc %0" : "=r" (cc) );
  102. return( cc & 0xFFFFFFFF );
  103. }
  104. #else
  105. #if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && defined(__ia64__)
  106. unsigned long hardclock( void )
  107. {
  108. unsigned long itc;
  109. asm( "mov %0 = ar.itc" : "=r" (itc) );
  110. return( itc );
  111. }
  112. #else
  113. #if defined(_MSC_VER)
  114. unsigned long hardclock( void )
  115. {
  116. LARGE_INTEGER offset;
  117. QueryPerformanceCounter( &offset );
  118. return (unsigned long)( offset.QuadPart );
  119. }
  120. #else
  121. static int hardclock_init = 0;
  122. static struct timeval tv_init;
  123. unsigned long hardclock( void )
  124. {
  125. struct timeval tv_cur;
  126. if( hardclock_init == 0 )
  127. {
  128. gettimeofday( &tv_init, NULL );
  129. hardclock_init = 1;
  130. }
  131. gettimeofday( &tv_cur, NULL );
  132. return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
  133. + ( tv_cur.tv_usec - tv_init.tv_usec ) );
  134. }
  135. #endif /* generic */
  136. #endif /* WIN32 */
  137. #endif /* IA-64 */
  138. #endif /* Alpha */
  139. #endif /* SPARC8 */
  140. #endif /* PowerPC */
  141. #endif /* AMD64 */
  142. #endif /* i586+ */
  143. volatile int alarmed = 0;
  144. #if defined(_WIN32)
  145. unsigned long get_timer( struct hr_time *val, int reset )
  146. {
  147. unsigned long delta;
  148. LARGE_INTEGER offset, hfreq;
  149. struct _hr_time *t = (struct _hr_time *) val;
  150. QueryPerformanceCounter( &offset );
  151. QueryPerformanceFrequency( &hfreq );
  152. delta = (unsigned long)( ( 1000 *
  153. ( offset.QuadPart - t->start.QuadPart ) ) /
  154. hfreq.QuadPart );
  155. if( reset )
  156. QueryPerformanceCounter( &t->start );
  157. return( delta );
  158. }
  159. DWORD WINAPI TimerProc( LPVOID uElapse )
  160. {
  161. Sleep( (DWORD) uElapse );
  162. alarmed = 1;
  163. return( TRUE );
  164. }
  165. void set_alarm( int seconds )
  166. {
  167. DWORD ThreadId;
  168. alarmed = 0;
  169. CloseHandle( CreateThread( NULL, 0, TimerProc,
  170. (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
  171. }
  172. void m_sleep( int milliseconds )
  173. {
  174. Sleep( milliseconds );
  175. }
  176. #else
  177. unsigned long get_timer( struct hr_time *val, int reset )
  178. {
  179. unsigned long delta;
  180. struct timeval offset;
  181. struct _hr_time *t = (struct _hr_time *) val;
  182. gettimeofday( &offset, NULL );
  183. delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
  184. + ( offset.tv_usec - t->start.tv_usec ) / 1000;
  185. if( reset )
  186. {
  187. t->start.tv_sec = offset.tv_sec;
  188. t->start.tv_usec = offset.tv_usec;
  189. }
  190. return( delta );
  191. }
  192. static void sighandler( int signum )
  193. {
  194. alarmed = 1;
  195. signal( signum, sighandler );
  196. }
  197. void set_alarm( int seconds )
  198. {
  199. alarmed = 0;
  200. signal( SIGALRM, sighandler );
  201. alarm( seconds );
  202. }
  203. void m_sleep( int milliseconds )
  204. {
  205. struct timeval tv;
  206. tv.tv_sec = milliseconds / 1000;
  207. tv.tv_usec = milliseconds * 1000;
  208. select( 0, NULL, NULL, NULL, &tv );
  209. }
  210. #endif
  211. #endif