pelib  2.0.0
src/monitor.c
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Pelib.
00005 
00006  Pelib is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  Pelib is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Pelib. If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 #include <pelib/monitor.h>
00022 
00023 #if defined(__i386__)
00024 
00025 inline
00026 unsigned long long int
00027 rdtsc()
00028 {
00029   unsigned long long int x;
00030      __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
00031      return x;
00032 }
00033 #elif defined(__x86_64__)
00034 
00035 inline
00036 unsigned long long int
00037 rdtsc()
00038 {
00039   unsigned hi, lo;
00040   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
00041   return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
00042 }
00043 
00044 #elif defined(__powerpc__)
00045 
00046 inline
00047 unsigned long long int
00048 rdtsc()
00049 {
00050   unsigned long long int result=0;
00051   unsigned long int upper, lower,tmp;
00052   __asm__ volatile(
00053                 "0:                  \n"
00054                 "\tmftbu   %0           \n"
00055                 "\tmftb    %1           \n"
00056                 "\tmftbu   %2           \n"
00057                 "\tcmpw    %2,%0        \n"
00058                 "\tbne     0b         \n"
00059                 : "=r"(upper),"=r"(lower),"=r"(tmp)
00060                 );
00061   result = upper;
00062   result = result<<32;
00063   result = result|lower;
00064 
00065   return(result);
00066 }
00067 #else
00068 #warning There is no rtdsc implementation for you architecture. Linker may fail if you use this function
00069 #endif
00070