BLI_math_base_test.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include "BLI_math.h"
  4. /* In tests below, when we are using -1.0f as max_diff value, we actually turn the function into a
  5. * pure-ULP one. */
  6. /* Put this here, since we cannot use BLI_assert() in inline math files it seems... */
  7. TEST(math_base, CompareFFRelativeValid)
  8. {
  9. EXPECT_TRUE(sizeof(float) == sizeof(int));
  10. }
  11. TEST(math_base, CompareFFRelativeNormal)
  12. {
  13. float f1 = 1.99999988f; /* *(float *)&(*(int *)&f2 - 1) */
  14. float f2 = 2.00000000f;
  15. float f3 = 2.00000048f; /* *(float *)&(*(int *)&f2 + 2) */
  16. float f4 = 2.10000000f; /* *(float *)&(*(int *)&f2 + 419430) */
  17. const float max_diff = FLT_EPSILON * 0.1f;
  18. EXPECT_TRUE(compare_ff_relative(f1, f2, max_diff, 1));
  19. EXPECT_TRUE(compare_ff_relative(f2, f1, max_diff, 1));
  20. EXPECT_TRUE(compare_ff_relative(f3, f2, max_diff, 2));
  21. EXPECT_TRUE(compare_ff_relative(f2, f3, max_diff, 2));
  22. EXPECT_FALSE(compare_ff_relative(f3, f2, max_diff, 1));
  23. EXPECT_FALSE(compare_ff_relative(f2, f3, max_diff, 1));
  24. EXPECT_FALSE(compare_ff_relative(f3, f2, -1.0f, 1));
  25. EXPECT_FALSE(compare_ff_relative(f2, f3, -1.0f, 1));
  26. EXPECT_TRUE(compare_ff_relative(f3, f2, -1.0f, 2));
  27. EXPECT_TRUE(compare_ff_relative(f2, f3, -1.0f, 2));
  28. EXPECT_FALSE(compare_ff_relative(f4, f2, max_diff, 64));
  29. EXPECT_FALSE(compare_ff_relative(f2, f4, max_diff, 64));
  30. EXPECT_TRUE(compare_ff_relative(f1, f3, max_diff, 64));
  31. EXPECT_TRUE(compare_ff_relative(f3, f1, max_diff, 64));
  32. }
  33. TEST(math_base, CompareFFRelativeZero)
  34. {
  35. float f0 = 0.0f;
  36. float f1 = 4.2038954e-045f; /* *(float *)&(*(int *)&f0 + 3) */
  37. float fn0 = -0.0f;
  38. float fn1 = -2.8025969e-045f; /* *(float *)&(*(int *)&fn0 - 2) */
  39. const float max_diff = FLT_EPSILON * 0.1f;
  40. EXPECT_TRUE(compare_ff_relative(f0, f1, -1.0f, 3));
  41. EXPECT_TRUE(compare_ff_relative(f1, f0, -1.0f, 3));
  42. EXPECT_FALSE(compare_ff_relative(f0, f1, -1.0f, 1));
  43. EXPECT_FALSE(compare_ff_relative(f1, f0, -1.0f, 1));
  44. EXPECT_TRUE(compare_ff_relative(fn0, fn1, -1.0f, 8));
  45. EXPECT_TRUE(compare_ff_relative(fn1, fn0, -1.0f, 8));
  46. EXPECT_TRUE(compare_ff_relative(f0, f1, max_diff, 1));
  47. EXPECT_TRUE(compare_ff_relative(f1, f0, max_diff, 1));
  48. EXPECT_TRUE(compare_ff_relative(fn0, f0, max_diff, 1));
  49. EXPECT_TRUE(compare_ff_relative(f0, fn0, max_diff, 1));
  50. EXPECT_TRUE(compare_ff_relative(f0, fn1, max_diff, 1));
  51. EXPECT_TRUE(compare_ff_relative(fn1, f0, max_diff, 1));
  52. /* Note: in theory, this should return false, since 0.0f and -0.0f have 0x80000000 diff,
  53. * but overflow in subtraction seems to break something here
  54. * (abs(*(int *)&fn0 - *(int *)&f0) == 0x80000000 == fn0), probably because int32 cannot
  55. * hold this abs value. this is yet another illustration of why one shall never use (near-)zero
  56. * floats in pure-ULP comparison. */
  57. // EXPECT_FALSE(compare_ff_relative(fn0, f0, -1.0f, 1024));
  58. // EXPECT_FALSE(compare_ff_relative(f0, fn0, -1.0f, 1024));
  59. EXPECT_FALSE(compare_ff_relative(fn0, f1, -1.0f, 1024));
  60. EXPECT_FALSE(compare_ff_relative(f1, fn0, -1.0f, 1024));
  61. }