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