aboutsummaryrefslogtreecommitdiffstats
path: root/modules/tm_hard/tm_hard.cpp
blob: 0005f79c2dcf7afc8a441da3b88ea6fe51f19d55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*************************************************
* Hardware Timer Source File                     *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#include <botan/tm_hard.h>
#include <botan/config.h>

namespace Botan {

/*************************************************
* Get the timestamp                              *
*************************************************/
u64bit Hardware_Timer::clock() const
   {
   u64bit rtc = 0;

#if !defined(__GNUC__)
   #error "This module uses GCC-style inline asm"
#endif

#if defined(BOTAN_TARGET_ARCH_IS_IA32) || defined(BOTAN_TARGET_ARCH_IS_AMD64)
   u32bit rtc_low = 0, rtc_high = 0;
   asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low));
   rtc = ((u64bit)rtc_high << 32) | rtc_low;
#elif defined(BOTAN_TARGET_ARCH_IS_PPC) || defined(BOTAN_TARGET_ARCH_IS_PPC64)
   u32bit rtc_low = 0, rtc_high = 0;
   asm volatile("mftbu %0; mftb %1" : "=r" (rtc_high), "=r" (rtc_low));
   rtc = ((u64bit)rtc_high << 32) | rtc_low;
#elif defined(BOTAN_TARGET_ARCH_IS_ALPHA)
   asm volatile("rpcc %0" : "=r" (rtc));
#elif defined(BOTAN_TARGET_ARCH_IS_SPARC64)
   asm volatile("rd %%tick, %0" : "=r" (rtc));
#else
   #error "Unsure how to access hardware timer on this system"
#endif

   return rtc;
   }

}