Question

I am using anonymous structs in unions to get a%b quickly.

Do you know any other way to get a%b without using powers of 2 for b.

include list:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

and declaration of unions:

//C99
//my first program to test rand() for any periodicity
union //for array indexing without modulus operator
{
unsigned int counter; //32 bit on my computer
struct
{
    unsigned int lsb:16; //16 bit 
    unsigned int msb:16; //16 bit   
};
struct 
{
    unsigned int bit[32];
};
} modulo;

union // for rand()%256
{
unsigned int randoom; //from rand() 
struct
{
unsigned int lsb:5;//equivalent to rand()%32 without any calculations
unsigned int msb:27;
};
}random_modulus;

and here the main function:

int main()
{
srand(time(0));

modulo.counter=0;//init of for-loop counter

// i am takin (counter%65536) for my array index which is modulus.lsb
unsigned int array_1[65536]; 
float array_mean=0,delta_square=0;
clock_t clock_refe;


//taking counter%65536 using lsb (2x faster)
clock_refe=clock();
for(;modulo.counter<1000000000;modulo.counter++)
{
// i need accumulated randoms for later use for some std. dev. thing.
random_modulus.randoom=rand();
array_1[modulo.lsb]+=random_modulus.lsb;
}

//getting average clock cycles
for(int i=0;i<65536;i++)
{
array_mean+=array_1[i];

}
array_mean/=65536.0f;

//getting square of deltas
float array_2[65536];

for(int i=0;i<65536;i++)
{
array_2[i]=(array_1[i]-array_mean)*(array_1[i]-array_mean); 
}


//horizontal histogram for resoluton of 20 elements
for(int i=0;i<65536;i+=(65536)/20)
{


for(int j=0;j<(array_2[i]*0.01);j++)
{

    printf("*");
}
printf("\n");
}

//calculations continue .....
return 0;

}

Pre-calculated values in some array maybe? But if i use the % calc part for only once, this is same. Can you give some book references about bitwise operations manuals?

Was it helpful?

Solution

Bitwise operations are the most portable method in this case. Here's some example functions; they are written for readability, but can be easily made faster:

int lsb(int input) {
    int mask = 0x00FF; // 0x00FF is 0000000011111111 in binary
    return input & mask;
}

int msb(int input) {
    int mask = 0xFF00; // 0xFF00 is 1111111100000000 in binary
    return (input & mask) >> 8;
}

In general, mask the bits you want with &, and then align them to the right with >>. K&R 2nd edition (The C Programming Language by Brian Kernighan & Dennis Ritchie) has information on just about every C topic, including bitmasks.

If you want a % b where b is not a power of 2, the native % operator is the fastest way (in modern compilers it's as fast as the bitwise operations, even when b is a power of two). Bitwise operations are useful in other contexts, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top