fpumath_test.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/fpumath.h>
  7. #if !BX_COMPILER_MSVC || BX_COMPILER_MSVC >= 1800
  8. #include <cmath>
  9. TEST_CASE("isFinite, isInfinite, isNan", "")
  10. {
  11. for (uint64_t ii = 0; ii < UINT32_MAX; ii += rand()%(1<<13)+1)
  12. {
  13. union { uint32_t ui; float f; } u = { uint32_t(ii) };
  14. REQUIRE(std::isnan(u.f) == bx::isNan(u.f) );
  15. REQUIRE(std::isfinite(u.f) == bx::isFinite(u.f) );
  16. REQUIRE(std::isinf(u.f) == bx::isInfinite(u.f) );
  17. }
  18. }
  19. #endif // !BX_COMPILER_MSVC || BX_COMPILER_MSVC >= 1800
  20. TEST_CASE("ToBits", "")
  21. {
  22. REQUIRE(UINT32_C(0x12345678) == bx::floatToBits( bx::bitsToFloat( UINT32_C(0x12345678) ) ) );
  23. REQUIRE(UINT64_C(0x123456789abcdef0) == bx::doubleToBits(bx::bitsToDouble(UINT32_C(0x123456789abcdef0) ) ) );
  24. }
  25. void mtxCheck(const float* _a, const float* _b)
  26. {
  27. if (!bx::fequal(_a, _b, 16, 0.01f) )
  28. {
  29. DBG("\n"
  30. "A:\n"
  31. "%10.4f %10.4f %10.4f %10.4f\n"
  32. "%10.4f %10.4f %10.4f %10.4f\n"
  33. "%10.4f %10.4f %10.4f %10.4f\n"
  34. "%10.4f %10.4f %10.4f %10.4f\n"
  35. "B:\n"
  36. "%10.4f %10.4f %10.4f %10.4f\n"
  37. "%10.4f %10.4f %10.4f %10.4f\n"
  38. "%10.4f %10.4f %10.4f %10.4f\n"
  39. "%10.4f %10.4f %10.4f %10.4f\n"
  40. , _a[ 0], _a[ 1], _a[ 2], _a[ 3]
  41. , _a[ 4], _a[ 5], _a[ 6], _a[ 7]
  42. , _a[ 8], _a[ 9], _a[10], _a[11]
  43. , _a[12], _a[13], _a[14], _a[15]
  44. , _b[ 0], _b[ 1], _b[ 2], _b[ 3]
  45. , _b[ 4], _b[ 5], _b[ 6], _b[ 7]
  46. , _b[ 8], _b[ 9], _b[10], _b[11]
  47. , _b[12], _b[13], _b[14], _b[15]
  48. );
  49. CHECK(false);
  50. }
  51. }
  52. TEST_CASE("quaternion", "")
  53. {
  54. float mtxQ[16];
  55. float mtx[16];
  56. float quat[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
  57. bx::mtxQuat(mtxQ, quat);
  58. bx::mtxIdentity(mtx);
  59. mtxCheck(mtxQ, mtx);
  60. float ax = bx::pi/27.0f;
  61. float ay = bx::pi/13.0f;
  62. float az = bx::pi/7.0f;
  63. bx::quatRotateX(quat, ax);
  64. bx::mtxQuat(mtxQ, quat);
  65. bx::mtxRotateX(mtx, ax);
  66. mtxCheck(mtxQ, mtx);
  67. float euler[3];
  68. bx::quatToEuler(euler, quat);
  69. CHECK(bx::fequal(euler[0], ax, 0.001f) );
  70. bx::quatRotateY(quat, ay);
  71. bx::mtxQuat(mtxQ, quat);
  72. bx::mtxRotateY(mtx, ay);
  73. mtxCheck(mtxQ, mtx);
  74. bx::quatToEuler(euler, quat);
  75. CHECK(bx::fequal(euler[1], ay, 0.001f) );
  76. bx::quatRotateZ(quat, az);
  77. bx::mtxQuat(mtxQ, quat);
  78. bx::mtxRotateZ(mtx, az);
  79. mtxCheck(mtxQ, mtx);
  80. bx::quatToEuler(euler, quat);
  81. CHECK(bx::fequal(euler[2], az, 0.001f) );
  82. }