/************************************************************************* * * Following are software versions of the leading zero and popcount * functions needed for those architectures without these features. * * The flags SW_POPCNT and SW_LEADZ are defined in the makefile * * It also contains versions of the timing routines. The timing * routines most likely will need to be modified for each architecture. * * Contents: * first 2 are copied from bitops.h * popcnt - Popcount function * leadz - Leading zero function * wall - Wall Clock timer function * cputime - CPU timer function * processTime - min, max, and average times for a subroutine * *************************************************************************/ /* CVS info */ /* $Date: 2005/01/10 21:05:28 $ */ /* $Revision: 1.2 $ */ /* $RCSfile: util.c,v $ */ /* $Name: rel_5 $ */ #ifdef _USELONGLONG typedef unsigned long long uint64; #else // DEFAULT typedef unsigned long uint64; #endif #ifdef SW_POPCNT #define MASK1BIT 0x5555555555555555uL #define MASK2BITS 0x3333333333333333uL #define MASK4BITS 0x0f0f0f0f0f0f0f0fuL int popcnt ( uint64 x ) { x = x - ((x >> 1) & MASK1BIT); x = ((x >> 2) & MASK2BITS) + (x & MASK2BITS); x = ((x >> 4) + x) & MASK4BITS; x = (x >> 8) + x; x = (x >> 16) + x; x = (x >> 32) + x; return (int)(x & 0xffuL); } #endif // SW_POPCNT #ifdef SW_LEADZ #define LDZ_LKUP 0x11112234 int leadz ( uint64 x ) { int i = 0; i = (x >> 32) ? 0 : 32; x = (x >> 32) ? (x >> 32) : x; i += (x >> 16) ? 0 : 16; x = (x >> 16) ? (x >> 16) : x; i += (x >> 8) ? 0 : 8; x = (x >> 8) ? (x >> 8) : x; i += (x >> 4) ? 0 : 4; x = (x >> 4) ? (x >> 4) : x; /* 4-bit lookup of leadz vals */ i += ( LDZ_LKUP >> (4*(int)x) ) & 0xf; return i; } #endif // SW_LEADZ /************************************************************************* * * wall() computes the amount of wall clock time that has elapsed. * * Make suitable changes to provide access to wall clock time * on architecture being used. * *************************************************************************/ #include #include //#include // also has types.h with uint64 on Cray T3E struct timeval tim; struct timezone tz; static char cvs_info[] = "BMARKGRP $Date: 2005/01/10 21:05:28 $ $Revision: 1.2 $ $RCSfile: util.c,v $ $Name: rel_5 $"; double wall() { gettimeofday(&tim, &tz); return (double)tim.tv_sec + (double)(tim.tv_usec*0.000001); } char * date() { gettimeofday(&tim, &tz); return ctime(&tim.tv_sec); } /************************************************************************* * * cputime() computes the amount of cpu time that has elapsed. * * Make suitable changes to provide access to cpu time * on architecture being used. * *************************************************************************/ double cputime() { return ( 0.000001L * (double)clock() ); } /************************************************************************** * * Determine the min, max, and average time for a process to complete * a specific subroutine. * * Param array: an array of doubles which are the times for each process * Param asize: an integer indicating the size of 'array' * Param tbuff: an array of 3 doubles where index 0 is the max, index 1 is * the min, and index 2 is the average time * *************************************************************************/ void processTime ( double array[], int asize, double tbuff[] ) { double total, max, min, avg; int i; total = max = avg = 0.0; min = array[1]; for (i = 0; i < asize; i++) { if (array[i] > max) max = array[i]; if (array[i] < min) min = array[i]; total = total + array[i]; } avg = total / ((double)asize); tbuff[0] = max; tbuff[1] = min; tbuff[2] = avg; }