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

//blow out any cache
//#define BIGARRAY (1<<24)
#define BIGARRAY (1<<3)

int colors[BIGARRAY];
float fcolors[BIGARRAY][4];

typedef struct
{
	unsigned char r;
	unsigned char b;
	unsigned char g;
	unsigned char a;
}fourcolor;

typedef union
{
	fourcolor colors;
	unsigned int intcolor;
}combocolor;

	
static inline int unionconvert(float *a5color)
{
	combocolor thesecolors;
	thesecolors.colors.r = a5color[0] * 255.0;
	thesecolors.colors.g = a5color[1] * 255.0;
	thesecolors.colors.b = a5color[2] * 255.0;
	thesecolors.colors.a = a5color[3] * 255.0;
	return thesecolors.intcolor;
}
	
static inline int shiftconvert(float *a5color)
{
	unsigned char r,g,b,a;
	r = a5color[0] * 255.0;
	g = a5color[1] * 255.0;
	b = a5color[2] * 255.0;
	a = a5color[3] * 255.0;
	return (a << 24) | (r << 16) | (g << 8) | b;
}

int main(void)
{
	time_t starttimeu;
	time_t endtimeu;
	time_t diffu = 0;

	time_t starttimes;
	time_t endtimes;
	time_t diffs = 0;
	
	int i,j;

	for(j=0;j<BIGARRAY;j++)
		for(i=0;i<4;i++)
		{
			float tmp = ( (rand() << 16) + rand() );
			fcolors[j][i] = tmp;
		}

	//start timing union conversion
	for(j=0;j<1000000;j++)
	{
		starttimeu = clock();

		for(i=0;i<BIGARRAY;i++)
			colors[i] = unionconvert(&fcolors[i][0]);

		endtimeu = clock();

		diffu += endtimeu - starttimeu;

		//printf("Converting with union took %lld clocks\n",(unsigned long long)diff);

		//start timing shift conversion
		starttimes = clock();

		for(i=0;i<BIGARRAY;i++)
			colors[i] = shiftconvert(&fcolors[i][0]);

		endtimes = clock();

		diffs += endtimes - starttimes;

		//printf("Converting with shifting took %lld clocks\n",(unsigned long long)diff);
	}

	printf("Converting with unions took %lld clocks\n",(unsigned long long)diffu);
	printf("Converting with shifting took %lld clocks\n",(unsigned long long)diffs);
	return 0;
}
	
