From 1994c5b5a9b7122e609730aefc08ff54850ee113 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Mon, 17 Aug 2015 01:51:04 +0200 Subject: [PATCH] Improve performances by using an approximation for hypot(Fix16, Fix16) see https://en.wikipedia.org/wiki/Alpha_max_plus_beta_min_algorithm --- src/fixmath/fix16.cpp | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/fixmath/fix16.cpp b/src/fixmath/fix16.cpp index 9b14a2e..f66c6f6 100644 --- a/src/fixmath/fix16.cpp +++ b/src/fixmath/fix16.cpp @@ -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) -- GitLab