timing.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Portable interface to the CPU cycle counter
  3. *
  4. * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
  5. *
  6. * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the names of PolarSSL or XySSL nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  29. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include "polarssl/config.h"
  36. #if defined(POLARSSL_TIMING_C)
  37. #include "polarssl/timing.h"
  38. #if defined(WIN32)
  39. #include <windows.h>
  40. #include <winbase.h>
  41. struct _hr_time
  42. {
  43. LARGE_INTEGER start;
  44. };
  45. #else
  46. #include <unistd.h>
  47. #include <sys/types.h>
  48. #include <sys/time.h>
  49. #include <signal.h>
  50. #include <time.h>
  51. struct _hr_time
  52. {
  53. struct timeval start;
  54. };
  55. #endif
  56. #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
  57. unsigned long hardclock( void )
  58. {
  59. unsigned long tsc;
  60. __asm rdtsc
  61. __asm mov [tsc], eax
  62. return( tsc );
  63. }
  64. #else
  65. #if defined(__GNUC__) && defined(__i386__)
  66. unsigned long hardclock( void )
  67. {
  68. unsigned long tsc;
  69. asm( "rdtsc" : "=a" (tsc) );
  70. return( tsc );
  71. }
  72. #else
  73. #if defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
  74. unsigned long hardclock( void )
  75. {
  76. unsigned long lo, hi;
  77. asm( "rdtsc" : "=a" (lo), "=d" (hi) );
  78. return( lo | (hi << 32) );
  79. }
  80. #else
  81. #if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  82. unsigned long hardclock( void )
  83. {
  84. unsigned long tbl, tbu0, tbu1;
  85. do
  86. {
  87. asm( "mftbu %0" : "=r" (tbu0) );
  88. asm( "mftb %0" : "=r" (tbl ) );
  89. asm( "mftbu %0" : "=r" (tbu1) );
  90. }
  91. while( tbu0 != tbu1 );
  92. return( tbl );
  93. }
  94. #else
  95. #if defined(__GNUC__) && defined(__sparc__)
  96. unsigned long hardclock( void )
  97. {
  98. unsigned long tick;
  99. asm( ".byte 0x83, 0x41, 0x00, 0x00" );
  100. asm( "mov %%g1, %0" : "=r" (tick) );
  101. return( tick );
  102. }
  103. #else
  104. #if defined(__GNUC__) && defined(__alpha__)
  105. unsigned long hardclock( void )
  106. {
  107. unsigned long cc;
  108. asm( "rpcc %0" : "=r" (cc) );
  109. return( cc & 0xFFFFFFFF );
  110. }
  111. #else
  112. #if defined(__GNUC__) && defined(__ia64__)
  113. unsigned long hardclock( void )
  114. {
  115. unsigned long itc;
  116. asm( "mov %0 = ar.itc" : "=r" (itc) );
  117. return( itc );
  118. }
  119. #else
  120. static int hardclock_init = 0;
  121. static struct timeval tv_init;
  122. unsigned long hardclock( void )
  123. {
  124. struct timeval tv_cur;
  125. if( hardclock_init == 0 )
  126. {
  127. gettimeofday( &tv_init, NULL );
  128. hardclock_init = 1;
  129. }
  130. gettimeofday( &tv_cur, NULL );
  131. return( ( tv_cur.tv_sec - tv_init.tv_sec ) * 1000000
  132. + ( tv_cur.tv_usec - tv_init.tv_usec ) );
  133. }
  134. #endif /* generic */
  135. #endif /* IA-64 */
  136. #endif /* Alpha */
  137. #endif /* SPARC8 */
  138. #endif /* PowerPC */
  139. #endif /* AMD64 */
  140. #endif /* i586+ */
  141. int alarmed = 0;
  142. #if defined(WIN32)
  143. unsigned long get_timer( struct hr_time *val, int reset )
  144. {
  145. unsigned long delta;
  146. LARGE_INTEGER offset, hfreq;
  147. struct _hr_time *t = (struct _hr_time *) val;
  148. QueryPerformanceCounter( &offset );
  149. QueryPerformanceFrequency( &hfreq );
  150. delta = (unsigned long)( ( 1000 *
  151. ( offset.QuadPart - t->start.QuadPart ) ) /
  152. hfreq.QuadPart );
  153. if( reset )
  154. QueryPerformanceCounter( &t->start );
  155. return( delta );
  156. }
  157. DWORD WINAPI TimerProc( LPVOID uElapse )
  158. {
  159. Sleep( (DWORD) uElapse );
  160. alarmed = 1;
  161. return( TRUE );
  162. }
  163. void set_alarm( int seconds )
  164. {
  165. DWORD ThreadId;
  166. alarmed = 0;
  167. CloseHandle( CreateThread( NULL, 0, TimerProc,
  168. (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) );
  169. }
  170. void m_sleep( int milliseconds )
  171. {
  172. Sleep( milliseconds );
  173. }
  174. #else
  175. unsigned long get_timer( struct hr_time *val, int reset )
  176. {
  177. unsigned long delta;
  178. struct timeval offset;
  179. struct _hr_time *t = (struct _hr_time *) val;
  180. gettimeofday( &offset, NULL );
  181. delta = ( offset.tv_sec - t->start.tv_sec ) * 1000
  182. + ( offset.tv_usec - t->start.tv_usec ) / 1000;
  183. if( reset )
  184. {
  185. t->start.tv_sec = offset.tv_sec;
  186. t->start.tv_usec = offset.tv_usec;
  187. }
  188. return( delta );
  189. }
  190. static void sighandler( int signum )
  191. {
  192. alarmed = 1;
  193. signal( signum, sighandler );
  194. }
  195. void set_alarm( int seconds )
  196. {
  197. alarmed = 0;
  198. signal( SIGALRM, sighandler );
  199. alarm( seconds );
  200. }
  201. void m_sleep( int milliseconds )
  202. {
  203. struct timeval tv;
  204. tv.tv_sec = milliseconds / 1000;
  205. tv.tv_usec = milliseconds * 1000;
  206. select( 0, NULL, NULL, NULL, &tv );
  207. }
  208. #endif
  209. #endif