Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Fast ways to compare equality of two floats. Assumes
  4. // sizeof(float) == sizeof(int) and we are using IEEE format.
  5. //
  6. // Source: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
  7. //=====================================================================================//
  8. #include <float.h>
  9. #include <math.h>
  10. #include "mathlib/mathlib.h"
  11. static inline bool AE_IsInfinite(float a)
  12. {
  13. const int kInfAsInt = 0x7F800000;
  14. // An infinity has an exponent of 255 (shift left 23 positions) and
  15. // a zero mantissa. There are two infinities - positive and negative.
  16. if ((*(int*)&a & 0x7FFFFFFF) == kInfAsInt)
  17. return true;
  18. return false;
  19. }
  20. static inline bool AE_IsNan(float a)
  21. {
  22. // a NAN has an exponent of 255 (shifted left 23 positions) and
  23. // a non-zero mantissa.
  24. int exp = *(int*)&a & 0x7F800000;
  25. int mantissa = *(int*)&a & 0x007FFFFF;
  26. if (exp == 0x7F800000 && mantissa != 0)
  27. return true;
  28. return false;
  29. }
  30. static inline int AE_Sign(float a)
  31. {
  32. // The sign bit of a number is the high bit.
  33. return (*(int*)&a) & 0x80000000;
  34. }
  35. // This is the 'final' version of the AlmostEqualUlps function.
  36. // The optional checks are included for completeness, but in many
  37. // cases they are not necessary, or even not desirable.
  38. bool AlmostEqual(float a, float b, int maxUlps)
  39. {
  40. // There are several optional checks that you can do, depending
  41. // on what behavior you want from your floating point comparisons.
  42. // These checks should not be necessary and they are included
  43. // mainly for completeness.
  44. // If a or b are infinity (positive or negative) then
  45. // only return true if they are exactly equal to each other -
  46. // that is, if they are both infinities of the same sign.
  47. // This check is only needed if you will be generating
  48. // infinities and you don't want them 'close' to numbers
  49. // near FLT_MAX.
  50. if (AE_IsInfinite(a) || AE_IsInfinite(b))
  51. return a == b;
  52. // If a or b are a NAN, return false. NANs are equal to nothing,
  53. // not even themselves.
  54. // This check is only needed if you will be generating NANs
  55. // and you use a maxUlps greater than 4 million or you want to
  56. // ensure that a NAN does not equal itself.
  57. if (AE_IsNan(a) || AE_IsNan(b))
  58. return false;
  59. // After adjusting floats so their representations are lexicographically
  60. // ordered as twos-complement integers a very small positive number
  61. // will compare as 'close' to a very small negative number. If this is
  62. // not desireable, and if you are on a platform that supports
  63. // subnormals (which is the only place the problem can show up) then
  64. // you need this check.
  65. // The check for a == b is because zero and negative zero have different
  66. // signs but are equal to each other.
  67. if (AE_Sign(a) != AE_Sign(b))
  68. return a == b;
  69. int aInt = *(int*)&a;
  70. // Make aInt lexicographically ordered as a twos-complement int
  71. if (aInt < 0)
  72. aInt = 0x80000000 - aInt;
  73. // Make bInt lexicographically ordered as a twos-complement int
  74. int bInt = *(int*)&b;
  75. if (bInt < 0)
  76. bInt = 0x80000000 - bInt;
  77. // Now we can compare aInt and bInt to find out how far apart a and b
  78. // are.
  79. int intDiff = abs(aInt - bInt);
  80. if (intDiff <= maxUlps)
  81. return true;
  82. return false;
  83. }