Commit 1994c5b5 authored by louiz’'s avatar louiz’

Improve performances by using an approximation for hypot(Fix16, Fix16)

see https://en.wikipedia.org/wiki/Alpha_max_plus_beta_min_algorithm
parent 51de4a31
......@@ -111,21 +111,15 @@ namespace std
Fix16 hypot(Fix16 x, Fix16 y)
{
// Avoid overflow by dividing everything by m during the intermediate
// steps. Then multiply the result by m before returning. If m is too
// big, we lose some precision, if it's too small we will overflow
// anyway.
// So, first find the ideal value of m.
static const Fix16 max(181); // sqrt(Fix16Max)
int m = 2;
while (std::abs(x)/m + std::abs(y)/m >= max)
m *= m;
x /= m;
y /= m;
Fix16 t = x*x + y*y;
return std::sqrt(t)*m;
static const Fix16 a = 0.96943387;
static const Fix16 b = 0.39782473;
x = std::abs(x);
y = std::abs(y);
if (x >= y)
return a*x + b*y;
else
return a*y + b*x;
}
Fix16 floor(Fix16 val)
......
Markdown is supported
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