#include "Intel_Compatibility.h"
#include "dSFMT.h"
#include <iostream>
#include <ctime>

#define HEIGHT 16777217
#define LOG_HEIGHT 24

using namespace std;

int main()
{
	cout << "RUNNING PHOENIX = BINARY with HEIGHT = " << HEIGHT << std::endl;
	
	double *alpha = new double[2*HEIGHT];
	double sum = 0.0;
	
	dsfmt_t dsfmt;
	dsfmt_init_gen_rand(&dsfmt, 10235);
	
	// initialize the array
	for(int i = HEIGHT; i < 2 * HEIGHT; i++)
	{
		alpha[i] = dsfmt_genrand_close_open(&dsfmt);
		sum += alpha[i];
	}
	
	for(int i = HEIGHT; i < 2 * HEIGHT; i++)
		alpha[i] /= sum;
	
	for(int i = HEIGHT; i < 2 * HEIGHT; i++)
	{
		double t = alpha[i];
		int z = i;
		while((z >> 1) > 0)
		{
			z >>= 1;
			alpha[z] += t;
		}
	}
	
	int numTimes = 1000000000;
	
	int p = 0;
	
	cout << "Timing Falcon for " << numTimes << std::endl;
	
	// Start timing here
	long timeStart = clock();
	
	// Now do this like 1000000 times
	for(int i = 0; i < numTimes; i++)
	{
		// Generate a random number, and try to find it in alpha
		double randNum = dsfmt_genrand_close_open(&dsfmt);
		int t = 1;
		
		for(int i = 0; i < LOG_HEIGHT; i++)
		{
			if(randNum <= alpha[2*t])
			{
				t <<= 1;
			}
			else
			{
				randNum -= alpha[2*t];
				t <<= 1;
				t += 1;
			}
		}
	}
	
	// End Timing here
	long timeEnd = clock();

	cout << "Consumed a total of " << timeEnd - timeStart << " milliseconds." << std::endl;
	
	return 0;
}
