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

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

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;
return a*y + b*x;
Fix16 floor(Fix16 val)
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