SkFloatUtils.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkFloatUtils_DEFINED
  8. #define SkFloatUtils_DEFINED
  9. #include "include/core/SkTypes.h"
  10. #include <limits.h>
  11. #include <float.h>
  12. template <size_t size>
  13. class SkTypeWithSize {
  14. public:
  15. // Prevents using SkTypeWithSize<N> with non-specialized N.
  16. typedef void UInt;
  17. };
  18. template <>
  19. class SkTypeWithSize<32> {
  20. public:
  21. typedef uint32_t UInt;
  22. };
  23. template <>
  24. class SkTypeWithSize<64> {
  25. public:
  26. typedef uint64_t UInt;
  27. };
  28. template <typename RawType>
  29. struct SkNumericLimits {
  30. static const int digits = 0;
  31. };
  32. template <>
  33. struct SkNumericLimits<double> {
  34. static const int digits = DBL_MANT_DIG;
  35. };
  36. template <>
  37. struct SkNumericLimits<float> {
  38. static const int digits = FLT_MANT_DIG;
  39. };
  40. //See
  41. //http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison/3423299#3423299
  42. //http://code.google.com/p/googletest/source/browse/trunk/include/gtest/internal/gtest-internal.h
  43. //http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
  44. template <typename RawType, unsigned int ULPs>
  45. class SkFloatingPoint {
  46. public:
  47. /** Bits is a unsigned integer the same size as the floating point number. */
  48. typedef typename SkTypeWithSize<sizeof(RawType) * CHAR_BIT>::UInt Bits;
  49. /** # of bits in a number. */
  50. static const size_t kBitCount = CHAR_BIT * sizeof(RawType);
  51. /** # of fraction bits in a number. */
  52. static const size_t kFractionBitCount = SkNumericLimits<RawType>::digits - 1;
  53. /** # of exponent bits in a number. */
  54. static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount;
  55. /** The mask for the sign bit. */
  56. static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1);
  57. /** The mask for the fraction bits. */
  58. static const Bits kFractionBitMask =
  59. ~static_cast<Bits>(0) >> (kExponentBitCount + 1);
  60. /** The mask for the exponent bits. */
  61. static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask);
  62. /** How many ULP's (Units in the Last Place) to tolerate when comparing. */
  63. static const size_t kMaxUlps = ULPs;
  64. /**
  65. * Constructs a FloatingPoint from a raw floating-point number.
  66. *
  67. * On an Intel CPU, passing a non-normalized NAN (Not a Number)
  68. * around may change its bits, although the new value is guaranteed
  69. * to be also a NAN. Therefore, don't expect this constructor to
  70. * preserve the bits in x when x is a NAN.
  71. */
  72. explicit SkFloatingPoint(const RawType& x) { fU.value = x; }
  73. /** Returns the exponent bits of this number. */
  74. Bits exponent_bits() const { return kExponentBitMask & fU.bits; }
  75. /** Returns the fraction bits of this number. */
  76. Bits fraction_bits() const { return kFractionBitMask & fU.bits; }
  77. /** Returns true iff this is NAN (not a number). */
  78. bool is_nan() const {
  79. // It's a NAN if both of the folloowing are true:
  80. // * the exponent bits are all ones
  81. // * the fraction bits are not all zero.
  82. return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0);
  83. }
  84. /**
  85. * Returns true iff this number is at most kMaxUlps ULP's away from ths.
  86. * In particular, this function:
  87. * - returns false if either number is (or both are) NAN.
  88. * - treats really large numbers as almost equal to infinity.
  89. * - thinks +0.0 and -0.0 are 0 DLP's apart.
  90. */
  91. bool AlmostEquals(const SkFloatingPoint& rhs) const {
  92. // Any comparison operation involving a NAN must return false.
  93. if (is_nan() || rhs.is_nan()) return false;
  94. const Bits dist = DistanceBetweenSignAndMagnitudeNumbers(fU.bits,
  95. rhs.fU.bits);
  96. //SkDEBUGF("(%f, %f, %d) ", u_.value_, rhs.u_.value_, dist);
  97. return dist <= kMaxUlps;
  98. }
  99. private:
  100. /** The data type used to store the actual floating-point number. */
  101. union FloatingPointUnion {
  102. /** The raw floating-point number. */
  103. RawType value;
  104. /** The bits that represent the number. */
  105. Bits bits;
  106. };
  107. /**
  108. * Converts an integer from the sign-and-magnitude representation to
  109. * the biased representation. More precisely, let N be 2 to the
  110. * power of (kBitCount - 1), an integer x is represented by the
  111. * unsigned number x + N.
  112. *
  113. * For instance,
  114. *
  115. * -N + 1 (the most negative number representable using
  116. * sign-and-magnitude) is represented by 1;
  117. * 0 is represented by N; and
  118. * N - 1 (the biggest number representable using
  119. * sign-and-magnitude) is represented by 2N - 1.
  120. *
  121. * Read http://en.wikipedia.org/wiki/Signed_number_representations
  122. * for more details on signed number representations.
  123. */
  124. static Bits SignAndMagnitudeToBiased(const Bits &sam) {
  125. if (kSignBitMask & sam) {
  126. // sam represents a negative number.
  127. return ~sam + 1;
  128. } else {
  129. // sam represents a positive number.
  130. return kSignBitMask | sam;
  131. }
  132. }
  133. /**
  134. * Given two numbers in the sign-and-magnitude representation,
  135. * returns the distance between them as an unsigned number.
  136. */
  137. static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1,
  138. const Bits &sam2) {
  139. const Bits biased1 = SignAndMagnitudeToBiased(sam1);
  140. const Bits biased2 = SignAndMagnitudeToBiased(sam2);
  141. return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
  142. }
  143. FloatingPointUnion fU;
  144. };
  145. #endif