Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

//**************************************

//     

//INCLUDE files for :Benchmark to test B

//     ITCNT_x.C functions

//**************************************

//     

/* bittops.h +++Date last modified: 05-Jul-1997 */

/*

** Macros and prototypes for bit operations

**

** public domain for SNIPPETS by:

**Scott Dudley

**Auke Reitsma

**Ratko Tomic

**Aare Tali

**J. Blauth

**Bruce Wedding

**Bob Stout

*/

#ifndef BITOPS__H

#define BITOPS__H

#include 

#include  /* for size_t*/

#include  /* for CHAR_BIT */

#include "sniptype.h"/* for TOBOOL() */

#include "extkword.h"/* for CDECL*/

/*

** Macros to manipulate bits in any integral data type.

*/

#define BitSet(arg,posn) ((arg) | (1L << (posn)))

#define BitClr(arg,posn) ((arg) & ~(1L << (posn)))

#define BitFlp(arg,posn) ((arg) ^ (1L << (posn)))

#define BitTst(arg,posn) TOBOOL((arg) & (1L << (posn)))

/*

** Macros to manipulate bits in an array of char.

** These macros assume CHAR_BIT is one of either 8, 16, or 32.

*/

#define MASK CHAR_BIT-1

#define SHIFT ((CHAR_BIT==8)?3:(CHAR_BIT==16)?4:8)

#define BitOff(a,x) ((void)((a)[(x)>>SHIFT] &= ~(1 << ((x)&MASK))))

#define BitOn(a,x)((void)((a)[(x)>>SHIFT] |= (1 << ((x)&MASK))))

#define BitFlip(a,x) ((void)((a)[(x)>>SHIFT] ^= (1 << ((x)&MASK))))

#define IsBit(a,x)((a)[(x)>>SHIFT]&(1 << ((x)&MASK)))

/*

** BITARRAY.C

*/

char *alloc_bit_array(size_t bits);

intgetbit(char *set, int number);

void setbit(char *set, int number, int value);

void flipbit(char *set, int number);

/*

** BITFILES.C

*/

    typedef struct {

    FILE * file;/* for stream I/O*/

    charrbuf;/* read bit buffer */

    charrcnt;/* read bit count*/

    charwbuf;/* write bit buffer */

    charwcnt;/* write bit count */

} bfile;

bfile * bfopen(char *name, char *mode);

int bfread(bfile *bf);

voidbfwrite(int bit, bfile *bf);

voidbfclose(bfile *bf);

/*

** BITSTRNG.C

*/

void bitstring(char *str, long byze, int biz, int strwid);

/*

** BSTR_I.C

*/

unsigned int bstr_i(char *cptr);

/*

** BITCNT_1.C

*/

int CDECL bit_count(long x);

/*

** BITCNT_2.C

*/

int CDECL bitcount(long i);

/*

** BITCNT_3.C

*/

int CDECL ntbl_bitcount(long int x);

int CDECL BW_btbl_bitcount(long int x);

int CDECL AR_btbl_bitcount(long int x);

/*

** BITCNT_4.C

*/

int CDECL ntbl_bitcnt(long x);

int CDECL btbl_bitcnt(long x);

#endif /* BITOPS__H */

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

//     

/*

** BITCNTS.C - Test program for bit counting functions

**

** public domain by Bob Stout & Auke Reitsma

*/

#include 

#include 

#include 

#include 

#include 

#include 

#include "bitops.h"

#define ITERS 1500000L

#define FUNCS 8

static int CDECL bit_shifter(long int x);

int main(void)

    {

    clock_t start, stop;

    double ct, cmin = DBL_MAX, cmax = 0;

    int i, cminix, cmaxix;

    long j, n;

        static int (* CDECL pBitCntFunc[FUNCS])(long) = {

        bit_count,

        bitcount,

        ntbl_bitcnt,

        ntbl_bitcount,

        btbl_bitcnt,

        BW_btbl_bitcount,

        AR_btbl_bitcount,

        bit_shifter

    };

        static char *text[FUNCS] = {

        "Optimized 1 bit/loop counter",

        "Ratko's mystery algorithm",

        "Recursive bit count by nybbles",

        "Non-recursive bit count by nybbles",

        "Recursive bit count by bytes",

        "Non-recursive bit count by bytes (BW)",

        "Non-recursive bit count by bytes (AR)",

        "Shift and count bits"

    };

    puts("Bit counter algorithm benchmark\n");

    for (i = 0; i < FUNCS; i++)

        {

        start = clock();

        for (j = n = 0; j < ITERS; j++)

        n += pBitCntFunc[i](j);

        stop = clock();

        ct = (stop - start) / (double)CLOCKS_PER_SEC;

        if (ct < cmin)

            {

            cmin = ct;

            cminix = i;

        }

        if (ct > cmax)

            {

            cmax = ct;

            cmaxix = i;

        }

        printf("%-38s> Time: %7.3f sec.; Bits: %ld\n", text[i], ct, n);

    }

    printf("\nBest > %s\n", text[cminix]);

    printf("Worst > %s\n", text[cmaxix]);

    return 0;

}

static int CDECL bit_shifter(long int x)

    {

    int i, n;

    for (i = n = 0; x && (i < (sizeof(long) * CHAR_BIT)); ++i, x >>= 1)

    n += (int)(x & 1L);

    return n;

}