From 3fb7747429c2f773fc40dd6e437a1da9cbbae9b7 Mon Sep 17 00:00:00 2001 From: Hill Ma Date: Fri, 3 Jul 2020 18:14:45 -0700 Subject: [PATCH] Improve ToBinary() for double precision. The constant 0x80000000 is specific to 32 bit types. Make the bit mask according to the size of types. --- code/Common/SpatialSort.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/code/Common/SpatialSort.cpp b/code/Common/SpatialSort.cpp index 3b8a64606..88f06b618 100644 --- a/code/Common/SpatialSort.cpp +++ b/code/Common/SpatialSort.cpp @@ -208,13 +208,14 @@ BinFloat ToBinary(const ai_real &pValue) { // floating-point numbers are of sign-magnitude format, so find out what signed number // representation we must convert negative values to. // See http://en.wikipedia.org/wiki/Signed_number_representations. + const BinFloat mask = BinFloat(1) << (CHAR_BIT * sizeof(BinFloat) - 1); // Two's complement? - const bool DefaultValue = ((-42 == (~42 + 1)) && (binValue & 0x80000000)); - const bool OneComplement = ((-42 == ~42) && (binValue & 0x80000000)); + const bool DefaultValue = ((-42 == (~42 + 1)) && (binValue & mask)); + const bool OneComplement = ((-42 == ~42) && (binValue & mask)); if (DefaultValue) - return BinFloat(BinFloat(1) << (CHAR_BIT * sizeof(BinFloat) - 1)) - binValue; + return mask - binValue; // One's complement? else if (OneComplement) return BinFloat(-0) - binValue;