From cad7efa639e67e12a7135931af15fd7d51a8096e Mon Sep 17 00:00:00 2001 From: Matt Mascarenhas Date: Mon, 29 Aug 2016 21:58:14 +0100 Subject: [PATCH] Accelerate HMM_Power() with the squaring technique Thanks to @mmozeiko for the algorithm --- HandmadeMath.h | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/HandmadeMath.h b/HandmadeMath.h index 5e6c8e9..540d512 100644 --- a/HandmadeMath.h +++ b/HandmadeMath.h @@ -598,28 +598,22 @@ HMM_Length(hmm_vec3 A) HINLINE float HMM_Power(float Base, int Exponent) { - float Result = 1; - - if(Exponent > 0) + float Result = 1.0f; + float Mul = Exponent < 0 ? 1.f / Base : Base; + unsigned int X = Exponent < 0 ? -Exponent : Exponent; + while (X) { - int i; - for(i = 0; i < Exponent; ++i) + if (X & 1) { - Result *= Base; + Result *= Mul; } + Mul *= Mul; + X >>= 1; } - else - { - int i; - for(i = 0; i > Exponent; --i) - { - Result /= Base; - } - } - return (Result); } + HINLINE float HMM_Lerp(float A, float Time, float B) {