00001 /* 00002 * cycles.h - measures CPU cycles using architecture specific extensions. 00003 * 00004 * Currently only setup for the pentuim using the RDTSC instruction 00005 * 00006 * Copyright (c) 1998 Phil Maker <pjm@gnu.org> 00007 * All rights reserved. 00008 * 00009 * Redistribution and use in source and binary forms, with or without 00010 * modification, are permitted provided that the following conditions 00011 * are met: 00012 * 1. Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 2. Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the distribution. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00019 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00022 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00023 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00024 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00025 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00026 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00027 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00028 * SUCH DAMAGE. 00029 * 00030 * $Id: cycles.h,v 1.1 2002/07/14 08:26:56 jrush Exp $ 00031 */ 00032 00033 #ifndef _cycles_h_ 00034 #define _cycles_h_ 1 00035 00036 #ifdef __cplusplus 00037 extern "C" { 00038 #endif 00039 00040 #include <nana-config.h> 00041 00042 #if HAVE_CYCLES 00043 00044 #if HAVE_RDTSC /* enabled by configure --enable-rdtsc instruction */ 00045 00046 /* 00047 * Pentium RDTSC instruction - 00048 * 00049 * 0. 64 bit counter running at clock speed so 00050 * (2^64)/(1000*1000*1000*60*60*24*365) = 584 years @ 1GHz 00051 * 1. reset to 0 on power on (is it) 00052 * 2. on pentium or later CPUs 00053 */ 00054 00055 typedef unsigned long long CYCLES; 00056 00057 #define cycles() \ 00058 ({ \ 00059 CYCLES __t; \ 00060 asm volatile ("rdtsc ; movl %%edx,%1; movl %%eax,%0" \ 00061 : "=m" (__t), "=m" (*(((char *)&__t) + 4)) \ 00062 : /* no input */ \ 00063 : "eax", "edx", "cc", "memory" \ 00064 ); \ 00065 __t; \ 00066 }) 00067 00068 #endif /* HAVE_RDTSC */ 00069 00070 /* 00071 * Calibration routines which should be available for all platforms 00072 * 00073 * cycles_per_second(t,n) - performs n measurements using a spin loop 00074 * using the now() function to wait for t or more seconds. It returns 00075 * the max number of cycles per second that it measures. As a side 00076 * effect (gasp) it also sets the values return by cycles_per_second_min() 00077 * and cycles_per_second_max(). 00078 * 00079 * Notes: 00080 * 00081 * 1. each call to cycles_per_second() resets our min/max values. 00082 * 2. overflows are always possible if t gets too large. 00083 * 00084 */ 00085 00086 CYCLES cycles_per_second(double t, int n); 00087 CYCLES cycles_per_second_min(); 00088 CYCLES cycles_per_second_max(); 00089 00090 /* 00091 * cycles_diff - returns the time in second between two cycle measurement 00092 * 00093 * Note: start <= stop 00094 * stop - start should be representable in a double. 00095 * !No overflow detection 00096 */ 00097 00098 double cycles_diff(CYCLES start, CYCLES stop); 00099 00100 #endif /* HAVE_CYCLES */ 00101 #ifdef _cplusplus 00102 }; 00103 #endif 00104 00105 #endif /* _cycles_h_ */ 00106
1.3.4