Commit 650e1da5 authored by Peter Bell's avatar Peter Bell
Browse files

good_size: optimise by cost heuristic

parent 6f1bba7d
......@@ -461,25 +461,29 @@ struct util // hack to avoid duplicate symbols
if (n<=12) return n;
size_t bestfac=2*n;
for (size_t f11=1; f11<bestfac; f11*=11)
for (size_t f117=f11; f117<bestfac; f117*=7)
for (size_t f1175=f117; f1175<bestfac; f1175*=5)
{
size_t x=f1175;
while (x<n) x*=2;
double bestcost = double(bestfac) * bestfac;
for (size_t f11=1, p11=0; f11<bestfac; f11*=11, ++p11)
for (size_t f117=f11, p7=0; f117<bestfac; f117*=7, ++p7)
for (size_t f1175=f117, p5=0; f1175<bestfac; f1175*=5, ++p5)
{
size_t x=f1175, p=11*p11 + 7*p7 + 5*p5;
while (x<n)
{ x*=2; p+=2; }
for (;;)
{
if (x<n)
x*=3;
else if (x>n)
{ x*=3; p+=3; }
else
{
if (x<bestfac) bestfac=x;
double cost = double(x) * p;
if (cost<bestcost)
{ bestfac=x; bestcost=cost;}
if (x&1) break;
x>>=1;
p-=2;
}
else
return n;
}
if (bestfac==n) return n;
}
return bestfac;
}
......@@ -490,23 +494,27 @@ struct util // hack to avoid duplicate symbols
if (n<=6) return n;
size_t bestfac=2*n;
for (size_t f5=1; f5<bestfac; f5*=5)
double bestcost = double(bestfac) * bestfac;
for (size_t f5=1, p5=0; f5<bestfac; f5*=5, ++p5)
{
size_t x = f5;
while (x<n) x *= 2;
size_t x=f5, p=5*p5;
while (x<n)
{ x*=2; p+=2; }
for (;;)
{
if (x<n)
x*=3;
else if (x>n)
{ x*=3; p+=3; }
else
{
if (x<bestfac) bestfac=x;
double cost = double(x) * p;
if (cost<bestcost)
{ bestfac=x; bestcost=cost;}
if (x&1) break;
x>>=1;
p-=2;
}
else
return n;
}
if (bestfac==n) return n;
}
return bestfac;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment