From 2958797d9f6ab773138f142412b2bd1aa5896a0f Mon Sep 17 00:00:00 2001 From: Thilo Schulz Date: Sun, 15 May 2011 14:55:47 +0000 Subject: Optimise VectorNormalize functions, patch by Matt Turner --- src/qcommon/q_math.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/qcommon') diff --git a/src/qcommon/q_math.c b/src/qcommon/q_math.c index e435590f..7a480510 100644 --- a/src/qcommon/q_math.c +++ b/src/qcommon/q_math.c @@ -835,10 +835,12 @@ vec_t VectorNormalize( vec3_t v ) { float length, ilength; length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2]; - length = sqrt (length); if ( length ) { - ilength = 1/length; + /* writing it this way allows gcc to recognize that rsqrt can be used */ + ilength = 1/(float)sqrt (length); + /* sqrt(length) = length * (1 / sqrt(length)) */ + length *= ilength; v[0] *= ilength; v[1] *= ilength; v[2] *= ilength; @@ -851,11 +853,13 @@ vec_t VectorNormalize2( const vec3_t v, vec3_t out) { float length, ilength; length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2]; - length = sqrt (length); if (length) { - ilength = 1/length; + /* writing it this way allows gcc to recognize that rsqrt can be used */ + ilength = 1/(float)sqrt (length); + /* sqrt(length) = length * (1 / sqrt(length)) */ + length *= ilength; out[0] = v[0]*ilength; out[1] = v[1]*ilength; out[2] = v[2]*ilength; -- cgit