52 #ifndef __vtkFastNumericConversion_h
53 #define __vtkFastNumericConversion_h
59 #if defined(NDEBUG) && (defined i386 || defined _M_IX86)
70 #if defined(__linux__)
75 #ifdef VTK_TEST_HACK_TO_EMULATE_LINUX_UNDER_WINDOWS
87 int TestQuickFloor(
double val)
92 int TestSafeFloor(
double val)
97 int TestRound(
double val)
102 int TestConvertFixedPointIntPart(
double val)
105 return ConvertFixedPoint(val, frac);
108 int TestConvertFixedPointFracPart(
double val)
111 ConvertFixedPoint(val, frac);
128 static inline double two30()
130 return static_cast<double>(
static_cast<unsigned long>(1) << 30);
137 static inline double two52()
139 return (static_cast<unsigned long>(1) << (52-30)) * two30();
149 static inline double two51()
151 return (static_cast<unsigned long>(1) << (51-30)) * two30();
159 static inline double two63()
161 return (static_cast<unsigned long>(1) << (63-60)) * two30() * two30();
169 static inline double two62()
171 return (static_cast<unsigned long>(1) << (62-60)) * two30() * two30();
221 static inline double RoundingTieBreaker()
223 return 1.0 / (two30() * (
static_cast<unsigned long>(1) << (
EXT_BITS -
INT_BITS - 30)));
227 static inline double RoundingTieBreaker()
240 static inline double QuickFloorDenormalizer()
241 {
return two52() * BorrowBit(); };
250 static inline double SafeFloorDenormalizer()
251 {
return two51() * BorrowBit(); };
258 static inline double QuickExtPrecTempDenormalizer()
259 {
return two63() * BorrowBit(); };
266 static inline double SafeExtPrecTempDenormalizer()
267 {
return two62() * BorrowBit(); };
275 #ifdef VTK_WORDS_BIGENDIAN
276 enum {exponent_pos = 0, mantissa_pos = 1};
278 enum {exponent_pos = 1, mantissa_pos = 0};
296 void SetReservedFracBits(
int bits)
302 unsigned long mtime = this->
GetMTime();
303 this->SetinternalReservedFracBits(bits);
306 this->InternalRebuild();
330 inline static int QuickFloor(
const double &val)
333 union {
int i[2];
double d; } u;
335 u.d = (((val - (QuickRoundAdjust() - RoundingTieBreaker()))
337 + QuickExtPrecTempDenormalizer())
339 - QuickExtPrecTempDenormalizer())
340 + QuickFloorDenormalizer();
341 #else // ! VTK_EXT_PREC
342 u.d = (val - (QuickRoundAdjust() - RoundingTieBreaker()))
343 + QuickFloorDenormalizer();
344 #endif // VTK_EXT_PREC
345 return u.i[mantissa_pos];
346 #else // ! VTK_USE_TRICK
347 return static_cast<int>(val);
348 #endif // VTK_USE_TRICK
367 inline static int SafeFloor(
const double &val)
370 union {
int i[2];
double d; } u;
372 u.d = (((val - SafeRoundAdjust())
373 + SafeExtPrecTempDenormalizer())
374 - SafeExtPrecTempDenormalizer())
375 + SafeFloorDenormalizer();
376 #else // ! VTK_EXT_PREC
377 u.d = (val - SafeRoundAdjust())
378 + SafeFloorDenormalizer();
379 #endif // VTK_EXT_PREC
380 return u.i[mantissa_pos] >> SafeFinalShift();
381 #else // ! VTK_USE_TRICK
382 return static_cast<int>(val);
383 #endif // VTK_USE_TRICK
397 inline static int Round(
const double &val)
400 union {
int i[2];
double d; } u;
403 + QuickExtPrecTempDenormalizer())
404 - QuickExtPrecTempDenormalizer())
405 + QuickFloorDenormalizer();
406 #else // ! VTK_EXT_PREC
408 + QuickFloorDenormalizer();
409 #endif // VTK_EXT_PREC
410 return u.i[mantissa_pos];
411 #else // ! VTK_USE_TRICK
414 return static_cast<int>(val + 0.5);
418 return static_cast<int>(val - 0.5);
420 #endif // VTK_USE_TRICK
428 inline int ConvertFixedPoint(
const double &val,
int &fracPart)
430 union {
int i[2];
double d; } u;
432 u.d = (((val - fixRound)
433 + this->epTempDenormalizer)
434 - this->epTempDenormalizer)
435 + this->fpDenormalizer;
436 #else // ! VTK_EXT_PREC
437 u.d = (val - fixRound)
438 + this->fpDenormalizer;
439 #endif // VTK_EXT_PREC
440 fracPart = (u.i[mantissa_pos] & fracMask) >> 1;
441 return u.i[mantissa_pos] >> this->internalReservedFracBits;
451 #ifdef VTK_TEST_HACK_TO_EMULATE_LINUX_UNDER_WINDOWS
452 _controlfp( _PC_64, MCW_PC );
456 this->internalReservedFracBits = 0;
458 this->fpDenormalizer = 0;
461 void InternalRebuild(
void);
464 vtkSetMacro(internalReservedFracBits,
int);
465 vtkGetMacro(internalReservedFracBits,
int);
466 int internalReservedFracBits;
472 double fpDenormalizer;
475 double epTempDenormalizer;