MCPcopy Create free account
hub / github.com/Overload-Technologies/Overload / ToBinary

Function ToBinary

Dependencies/assimp/code/Common/SpatialSort.cpp:188–234  ·  view source on GitHub ↗

-------------------------------------------------------------------------------------------- Converts the bit pattern of a floating-point number to its signed integer representation.

Source from the content-addressed store, hash-verified

186// --------------------------------------------------------------------------------------------
187// Converts the bit pattern of a floating-point number to its signed integer representation.
188BinFloat ToBinary(const ai_real &pValue) {
189
190 // If this assertion fails, signed int is not big enough to store a float on your platform.
191 // Please correct the declaration of BinFloat a few lines above - but do it in a portable,
192 // #ifdef'd manner!
193 static_assert(sizeof(BinFloat) >= sizeof(ai_real), "sizeof(BinFloat) >= sizeof(ai_real)");
194
195#if defined(_MSC_VER)
196 // If this assertion fails, Visual C++ has finally moved to ILP64. This means that this
197 // code has just become legacy code! Find out the current value of _MSC_VER and modify
198 // the #if above so it evaluates false on the current and all upcoming VC versions (or
199 // on the current platform, if LP64 or LLP64 are still used on other platforms).
200 static_assert(sizeof(BinFloat) == sizeof(ai_real), "sizeof(BinFloat) == sizeof(ai_real)");
201
202 // This works best on Visual C++, but other compilers have their problems with it.
203 const BinFloat binValue = reinterpret_cast<BinFloat const &>(pValue);
204 //::memcpy(&binValue, &pValue, sizeof(pValue));
205 //return binValue;
206#else
207 // On many compilers, reinterpreting a float address as an integer causes aliasing
208 // problems. This is an ugly but more or less safe way of doing it.
209 union {
210 ai_real asFloat;
211 BinFloat asBin;
212 } conversion;
213 conversion.asBin = 0; // zero empty space in case sizeof(BinFloat) > sizeof(float)
214 conversion.asFloat = pValue;
215 const BinFloat binValue = conversion.asBin;
216#endif
217
218 // floating-point numbers are of sign-magnitude format, so find out what signed number
219 // representation we must convert negative values to.
220 // See http://en.wikipedia.org/wiki/Signed_number_representations.
221 const BinFloat mask = BinFloat(1) << (CHAR_BIT * sizeof(BinFloat) - 1);
222
223 // Two's complement?
224 const bool DefaultValue = ((-42 == (~42 + 1)) && (binValue & mask));
225 const bool OneComplement = ((-42 == ~42) && (binValue & mask));
226
227 if (DefaultValue)
228 return mask - binValue;
229 // One's complement?
230 else if (OneComplement)
231 return BinFloat(-0) - binValue;
232 // Sign-magnitude? -0 = 1000... binary
233 return binValue;
234}
235
236} // namespace
237

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected