Correct C FixedMul() off-by-one errors

The FixedMul() C implementation would produce off by one results,
causing constant desyncs on 64 bit builds and builds without an
ASM implementation of the function.

This is fixed by shifting instead of dividing, possibly avoiding
rounding errors.
This commit is contained in:
Tasos Sahanidis 2018-05-17 17:55:38 +03:00
parent 8e5ac64d7c
commit 001e4e11ca
No known key found for this signature in database
GPG key ID: 01A1DCBA22E005C4

View file

@ -33,7 +33,9 @@
*/
fixed_t FixedMul(fixed_t a, fixed_t b)
{
return (fixed_t)((((INT64)a * b) ) / FRACUNIT);
// Need to cast to unsigned before shifting to avoid undefined behaviour
// for negative integers
return (fixed_t)(((UINT64)((INT64)a * b)) >> FRACBITS);
}
#endif //__USE_C_FIXEDMUL__