math_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/math.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. bool flog2_test(float _a)
  21. {
  22. return bx::flog2(_a) == bx::flog(_a) * (1.0f / bx::flog(2.0f) );
  23. }
  24. TEST_CASE("flog2", "")
  25. {
  26. flog2_test(0.0f);
  27. flog2_test(256.0f);
  28. }
  29. TEST_CASE("libm", "")
  30. {
  31. REQUIRE(1389.0f == bx::fabs(-1389.0f) );
  32. REQUIRE(1389.0f == bx::fabs( 1389.0f) );
  33. REQUIRE( 0.0f == bx::fabs(-0.0f) );
  34. REQUIRE( 0.0f == bx::fabs( 0.0f) );
  35. REQUIRE(389.0f == bx::fmod(1389.0f, 1000.0f) );
  36. REQUIRE(bx::isNan(bx::fmod(0.0f, 0.0f) ) );
  37. REQUIRE( 13.0f == bx::ffloor( 13.89f) );
  38. REQUIRE(-14.0f == bx::ffloor(-13.89f) );
  39. REQUIRE( 14.0f == bx::fceil( 13.89f) );
  40. REQUIRE(-13.0f == bx::fceil( -13.89f) );
  41. REQUIRE( 13.0f == bx::ftrunc( 13.89f) );
  42. REQUIRE(-13.0f == bx::ftrunc(-13.89f) );
  43. REQUIRE(bx::fequal( 0.89f, bx::ffract( 13.89f), 0.000001f) );
  44. REQUIRE(bx::fequal(-0.89f, bx::ffract(-13.89f), 0.000001f) );
  45. }
  46. TEST_CASE("ToBits", "")
  47. {
  48. REQUIRE(UINT32_C(0x12345678) == bx::floatToBits( bx::bitsToFloat( UINT32_C(0x12345678) ) ) );
  49. REQUIRE(UINT64_C(0x123456789abcdef0) == bx::doubleToBits(bx::bitsToDouble(UINT32_C(0x123456789abcdef0) ) ) );
  50. }
  51. void mtxCheck(const float* _a, const float* _b)
  52. {
  53. if (!bx::fequal(_a, _b, 16, 0.01f) )
  54. {
  55. DBG("\n"
  56. "A:\n"
  57. "%10.4f %10.4f %10.4f %10.4f\n"
  58. "%10.4f %10.4f %10.4f %10.4f\n"
  59. "%10.4f %10.4f %10.4f %10.4f\n"
  60. "%10.4f %10.4f %10.4f %10.4f\n"
  61. "B:\n"
  62. "%10.4f %10.4f %10.4f %10.4f\n"
  63. "%10.4f %10.4f %10.4f %10.4f\n"
  64. "%10.4f %10.4f %10.4f %10.4f\n"
  65. "%10.4f %10.4f %10.4f %10.4f\n"
  66. , _a[ 0], _a[ 1], _a[ 2], _a[ 3]
  67. , _a[ 4], _a[ 5], _a[ 6], _a[ 7]
  68. , _a[ 8], _a[ 9], _a[10], _a[11]
  69. , _a[12], _a[13], _a[14], _a[15]
  70. , _b[ 0], _b[ 1], _b[ 2], _b[ 3]
  71. , _b[ 4], _b[ 5], _b[ 6], _b[ 7]
  72. , _b[ 8], _b[ 9], _b[10], _b[11]
  73. , _b[12], _b[13], _b[14], _b[15]
  74. );
  75. CHECK(false);
  76. }
  77. }
  78. TEST_CASE("quaternion", "")
  79. {
  80. float mtxQ[16];
  81. float mtx[16];
  82. float quat[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
  83. bx::mtxQuat(mtxQ, quat);
  84. bx::mtxIdentity(mtx);
  85. mtxCheck(mtxQ, mtx);
  86. float ax = bx::kPi/27.0f;
  87. float ay = bx::kPi/13.0f;
  88. float az = bx::kPi/7.0f;
  89. bx::quatRotateX(quat, ax);
  90. bx::mtxQuat(mtxQ, quat);
  91. bx::mtxRotateX(mtx, ax);
  92. mtxCheck(mtxQ, mtx);
  93. float euler[3];
  94. bx::quatToEuler(euler, quat);
  95. CHECK(bx::fequal(euler[0], ax, 0.001f) );
  96. bx::quatRotateY(quat, ay);
  97. bx::mtxQuat(mtxQ, quat);
  98. bx::mtxRotateY(mtx, ay);
  99. mtxCheck(mtxQ, mtx);
  100. bx::quatToEuler(euler, quat);
  101. CHECK(bx::fequal(euler[1], ay, 0.001f) );
  102. bx::quatRotateZ(quat, az);
  103. bx::mtxQuat(mtxQ, quat);
  104. bx::mtxRotateZ(mtx, az);
  105. mtxCheck(mtxQ, mtx);
  106. bx::quatToEuler(euler, quat);
  107. CHECK(bx::fequal(euler[2], az, 0.001f) );
  108. }