From 57ad99db728e114096cad8461b04cf42464b0524 Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Tue, 18 Nov 2008 01:22:09 +0000 Subject: -- Split ToggleBitRun into {Set,Clear}BitRun -- Simplify switch logic to choose which function to run git-svn-id: svn://mattst88.com/svn/cleanbench/trunk@83 0d43b9a7-5ab2-4d7b-af9d-f64450cef757 --- bitfield.c | 220 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 113 insertions(+), 107 deletions(-) diff --git a/bitfield.c b/bitfield.c index 77bfbfa..9aad5ad 100644 --- a/bitfield.c +++ b/bitfield.c @@ -25,13 +25,9 @@ static clock_t DoBitfieldIteration(unsigned long *bitarray, unsigned long *bitoparray, long bitop_array_size, unsigned long *nbitops); -static void ToggleBitRun(unsigned long *bitmap, - unsigned long bit_addr, - unsigned long nbits, - unsigned int val); -static void FlipBitRun(unsigned long *bitmap, - unsigned long bit_addr, - unsigned long nbits); +static void SetBitRun(unsigned long* bitmap, unsigned long bit_address, unsigned long num_bits); +static void ClearBitRun(unsigned long* bitmap, unsigned long bit_address, unsigned long num_bits); +static void FlipBitRun(unsigned long* bitmap, unsigned long bit_address, unsigned long num_bits); /************* ** DoBitops ** @@ -101,71 +97,66 @@ DoBitops(void) static clock_t DoBitfieldIteration(unsigned long *bitarray, unsigned long *bitoparray, long bitop_array_size, unsigned long *nbitops) { - clock_t start, stop; - long i; - unsigned long bitoffset; - - /* - ** Clear # bitops counter - */ - *nbitops = 0L; - - /* - ** Construct a set of bitmap offsets and run lengths. - ** The offset can be any random number from 0 to the - ** size of the bitmap (in bits). The run length can - ** be any random number from 1 to the number of bits - ** between the offset and the end of the bitmap. - ** Note that the bitmap has 8192 * 32 bits in it. - ** (262,144 bits) - */ - - /* - ** Reset random number generator so things repeat. - ** Also reset the bit array we work on. - ** added by Uwe F. Mayer - */ - randnum(13); - - for (i = 0; i < ARRAY_SIZE; i++) { + + clock_t start, stop; + long i; + unsigned long bitoffset; + void (*DoBitOp[])(unsigned long* bitmap, unsigned long bit_address, unsigned long num_bits) = { + SetBitRun, + ClearBitRun, + FlipBitRun + }; + + /* + * ** Clear # bitops counter + * */ + *nbitops = 0L; + + /* + * ** Construct a set of bitmap offsets and run lengths. + * ** The offset can be any random number from 0 to the + * ** size of the bitmap (in bits). The run length can + * ** be any random number from 1 to the number of bits + * ** between the offset and the end of the bitmap. + * ** Note that the bitmap has 8192 * 32 bits in it. + * ** (262,144 bits) + * */ + + /* + * ** Reset random number generator so things repeat. + * ** Also reset the bit array we work on. + * ** added by Uwe F. Mayer + * */ + randnum(13); + + + for (i = 0; i < ARRAY_SIZE; i++) { #ifdef _LP64 - *(bitarray+i) = 0x5555555555555555UL; + *(bitarray+i) = 0x5555555555555555UL; #else - *(bitarray+i) = 0x55555555UL; + *(bitarray+i) = 0x55555555UL; #endif - } - - randnum(13); - - for (i = 0; i < bitop_array_size; i++) { - /* First item is offset */ - *(bitoparray + i + i) = bitoffset = abs_randwc((int32_t)262140); - - /* Next item is run length */ - *nbitops += *(bitoparray + i + i + 1L) = abs_randwc((int32_t)262140 - bitoffset); - } - - start = clock(); - - for(i = 0; i < bitop_array_size; i++) { - switch(i % 3) { - case 2: /* Complement run of bits */ - FlipBitRun(bitarray, - *(bitoparray+i+i), - *(bitoparray+i+i+1)); - break; - default: - ToggleBitRun(bitarray, - *(bitoparray+i+i), - *(bitoparray+i+i+1), - !i); - break; - } - } - - stop = clock(); - - return stop - start; + } + + randnum(13); + + for (i = 0; i < bitop_array_size; i++) { + /* First item is offset */ + *(bitoparray + i + i) = bitoffset = abs_randwc((int32_t)262140); + + /* Next item is run length */ + *nbitops += *(bitoparray + i + i + 1L) = abs_randwc((int32_t)262140 - bitoffset); + } + + start = clock(); + + for(i = 0; i < bitop_array_size; i++) { + (*DoBitOp[i % 3])(bitarray, *(bitoparray + i + i), *(bitoparray + i + i + 1)); + } + + stop = clock(); + + return stop - start; } /*************** @@ -174,55 +165,70 @@ DoBitfieldIteration(unsigned long *bitarray, unsigned long *bitoparray, long bit ** Complements a run of bits. */ static void -FlipBitRun(unsigned long *bitmap, unsigned long bit_addr, unsigned long nbits) +FlipBitRun(unsigned long* bitmap, unsigned long bit_address, unsigned long num_bits) { - unsigned long bindex; /* Index into array */ - unsigned long bitnumb; /* Bit number */ + unsigned long bit_index; + unsigned long bit_number; - while (nbits--) { + while (num_bits--) { #ifdef _LP64 - bindex=bit_addr>>6; /* Index is number /64 */ - bitnumb=bit_addr % 64; /* Bit number in longword */ + bit_index = bit_address >> 6; + bit_number = bit_address % 64; #else - bindex=bit_addr>>5; /* Index is number /32 */ - bitnumb=bit_addr % 32; /* Bit number in longword */ + bit_index = bit_address >> 5; + bit_number = bit_address % 32; #endif - bitmap[bindex]^=(1L<> 6; - bitnumb = bit_addr % 64; - - bitmap[bindex] |= (1L << bitnumb); + unsigned long bit_index; + unsigned long bit_number; - bit_addr++; - } - } else { - for (; nbits != 0; nbits--) { - bindex = bit_addr >> 6; - bitnumb = bit_addr % 64; + while (num_bits--) { +#ifdef _LP64 + bit_index = bit_address >> 6; + bit_number = bit_address % 64; +#else + bit_index = bit_address >> 5; + bit_number = bit_address % 32; +#endif + bitmap[bit_index] |= (1L << bit_number); + bit_address++; + } +} - bitmap[bindex] &= ~(1L << bitnumb); +/***************************** + * ** ClearBitRun + * *************************** + * ** Clear a run of num_bits starting + * ** at bit_address in bitmap + * */ +static void +ClearBitRun(unsigned long* bitmap, unsigned long bit_address, unsigned long num_bits) +{ + unsigned long bit_index; + unsigned long bit_number; - bit_addr++; - } + while (num_bits--) { +#ifdef _LP64 + bit_index = bit_address >> 6; + bit_number = bit_address % 64; +#else + bit_index = bit_address >> 5; + bit_number = bit_address % 32; +#endif + bitmap[bit_index] &= ~(1L << bit_number); + bit_address++; } } -- cgit v1.2.3