Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Last active October 23, 2019 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heatblazer/2097120f27af1d37161f15fca77869d3 to your computer and use it in GitHub Desktop.
Save heatblazer/2097120f27af1d37161f15fca77869d3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <time.h>
#include <math.h>
#include <map>
#define LOOPSIZE 100000
union fract_u
{
float dval;
unsigned char data[sizeof(double)];
};
fract_u preclevel(const fract_u& orig, unsigned int level = 0)
{
fract_u copy;
copy.dval = orig.dval;
switch (level)
{
case 0:
return copy;
case 1:
copy.data[0] = 0;
copy.data[1] = 0;
copy.data[2] = 0;
return copy;
case 2:
copy.data[0] = 0;
copy.data[1] = 0;
return copy;
case 3:
copy.data[0] = 0;
return copy;
default:
return copy;
}
return copy;
}
fract_u preclevel2(const fract_u& orig, unsigned int level = 0, unsigned const char mask=0xFF)
{
fract_u copy;
copy.dval = orig.dval;
switch (level)
{
case 0:
return copy;
case 1:
copy.data[0] &= ~(copy.data[0] & mask);
copy.data[1] &= ~(copy.data[1] & mask);
copy.data[2] &= ~(copy.data[2] & mask);
return copy;
case 2:
copy.data[0] &= ~(copy.data[0] & mask);
copy.data[1] &= ~(copy.data[1] & mask);
return copy;
case 3:
copy.data[0] &= ~(copy.data[0] & mask);
return copy;
default:
return copy;
}
return copy;
}
int main()
{
std::map<float, int> sine_periods;
std::map<float, int> cosine_periods;
for(int i=0; i < LOOPSIZE; ++i)
{
fract_u t1 = {::sin((double)i)};
fract_u t2 = preclevel2(t1, 1);
sine_periods[t2.dval]++;
}
for(int i=0; i < LOOPSIZE; ++i)
{
fract_u t1 = {::sin((double)i)};
fract_u t2 = preclevel2(t1, 1);
cosine_periods[t2.dval]++;
}
for (auto it = sine_periods.begin(); it != sine_periods.end(); ++it)
{
std::cout << "[" << it->first << "][" << it->second << "]\r\n";
}
std::cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\r\n";
for (auto it = cosine_periods.begin(); it != cosine_periods.end(); ++it)
{
std::cout << "[" << it->first << "][" << it->second << "]\r\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment