timer: Fix type punning strict aliasing warning

Fixes the warning from gcc in Qsqrt(). Verified that the generated assembly
code is the same as the original.
This commit is contained in:
Chris
2022-09-18 03:58:34 -04:00
parent c2ac620686
commit 23b2bb4660

View File

@@ -77,17 +77,19 @@ void Timer_ClockCmd(tmr_type* TIMx, bool Enable)
static float Qsqrt(float number) static float Qsqrt(float number)
{ {
union {
long i; long i;
float x2, y; float y;
} cast;
float x2;
const float threehalfs = 1.5f; const float threehalfs = 1.5f;
x2 = number * 0.5f; x2 = number * 0.5f;
y = number; cast.y = number;
i = *(long*)&y; cast.i = 0x5f3759df - (cast.i >> 1);
i = 0x5f3759df - (i >> 1); cast.y = cast.y * (threehalfs - (x2 * cast.y * cast.y));
y = *(float*)&i; cast.y = cast.y * (threehalfs - (x2 * cast.y * cast.y));
y = y * (threehalfs - (x2 * y * y)); return 1.0f / cast.y;
y = y * (threehalfs - (x2 * y * y));
return 1.0f / y;
} }
/** /**