From fe20e39d5cff94693b1a7ffae01f9cc31dd4c49f Mon Sep 17 00:00:00 2001 From: Alessio Morale Date: Tue, 14 Oct 2014 05:31:40 +0200 Subject: [PATCH] OP-1535 - uses union in fast_invsqrt --- flight/libraries/math/mathmisc.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/flight/libraries/math/mathmisc.h b/flight/libraries/math/mathmisc.h index b9df0be18..57271f115 100644 --- a/flight/libraries/math/mathmisc.h +++ b/flight/libraries/math/mathmisc.h @@ -124,17 +124,20 @@ static inline float y_on_curve(float x, const pointf points[], int num_points) static inline float fast_invsqrtf(float number) { - uint32_t i; float x2, y; const float threehalfs = 1.5F; + union { + float f; + uint32_t u; + } i; + x2 = number * 0.5F; y = number; - void *tmp = &y; - i = *(uint32_t *)tmp; // evil floating point bit level hacking - i = 0x5f3759df - (i >> 1); // what the fxck? - tmp = &i; - y = *(float *)tmp; + + i.f = y; // evil floating point bit level hacking + i.u = 0x5f3759df - (i.u >> 1); // what the fxck? + y = i.f; y = y * (threehalfs - (x2 * y * y)); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed