2
0

PreciseMathTest.cpp 854 B

1234567891011121314151617181920212223242526272829
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include <atomic>
  6. // Implemented as a global atomic so the compiler can't optimize it to a constant
  7. extern atomic<float> One, OneTenth, Ten;
  8. atomic<float> One = 1.0f, OneTenth = 0.1f /* Actually: 0.100000001f */, Ten = 10.0f;
  9. JPH_PRECISE_MATH_ON
  10. TEST_SUITE("PreciseMathTest")
  11. {
  12. TEST_CASE("CheckNoFMA")
  13. {
  14. // Should not use the FMA instruction and end up being zero
  15. // If FMA is active, a * b will not be rounded to 1.0f so the result will be a small positive number
  16. float a = OneTenth, b = Ten, c = One;
  17. float result = a * b - c;
  18. CHECK(result == 0.0f);
  19. // Compiler should not optimize these variables away
  20. One = OneTenth = Ten = 2.0f;
  21. }
  22. }
  23. JPH_PRECISE_MATH_OFF