uint32_test.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/uint32_t.h>
  7. TEST_CASE("StrideAlign")
  8. {
  9. REQUIRE(0 == bx::strideAlign(0, 12) );
  10. for (uint32_t ii = 0; ii < 12; ++ii)
  11. {
  12. REQUIRE(12 == bx::strideAlign(ii+1, 12) );
  13. }
  14. REQUIRE(0 == bx::strideAlign<16>(0, 12) );
  15. for (uint32_t ii = 0; ii < 12; ++ii)
  16. {
  17. REQUIRE(48 == bx::strideAlign<16>(ii+1, 12) );
  18. }
  19. uint32_t offset = 11;
  20. offset = bx::strideAlign(offset, 32);
  21. REQUIRE(offset == 32);
  22. offset = bx::strideAlign(offset, 24);
  23. REQUIRE(offset == 48);
  24. }
  25. TEST_CASE("uint32_cnt")
  26. {
  27. REQUIRE( 0 == bx::uint32_cnttz(UINT32_C(1) ) );
  28. REQUIRE(31 == bx::uint32_cntlz(UINT32_C(1) ) );
  29. REQUIRE( 0 == bx::uint64_cnttz(UINT64_C(1) ) );
  30. REQUIRE(63 == bx::uint64_cntlz(UINT64_C(1) ) );
  31. REQUIRE( 1 == bx::uint32_cntbits(1) );
  32. REQUIRE(16 == bx::uint32_cntbits(UINT16_MAX) );
  33. REQUIRE(32 == bx::uint32_cntbits(UINT32_MAX) );
  34. }
  35. TEST_CASE("uint32_part")
  36. {
  37. REQUIRE(UINT32_C(0x55555555) == bx::uint32_part1by1(UINT16_MAX) );
  38. REQUIRE(UINT32_C(0x09249249) == bx::uint32_part1by2(0x3ff) );
  39. }
  40. TEST_CASE("halfTo/FromFloat", "")
  41. {
  42. for (uint32_t ii = 0; ii < 0x7c00; ++ii)
  43. {
  44. const uint16_t orig = uint16_t(ii);
  45. const float htf = bx::halfToFloat(orig);
  46. const uint16_t hff = bx::halfFromFloat(htf);
  47. REQUIRE(orig == hff);
  48. }
  49. for (uint32_t ii = 0x8000; ii < 0xfc00; ++ii)
  50. {
  51. const uint16_t orig = uint16_t(ii);
  52. const float htf = bx::halfToFloat(orig);
  53. const uint16_t hff = bx::halfFromFloat(htf);
  54. REQUIRE(orig == hff);
  55. }
  56. }
  57. TEST_CASE("uint32_testpow2", "")
  58. {
  59. uint32_t shift = 0;
  60. for (uint32_t ii = 0; ii < UINT32_MAX; ++ii)
  61. {
  62. if (bx::uint32_testpow2(ii) )
  63. {
  64. REQUIRE(ii == 1u << shift);
  65. ++shift;
  66. }
  67. }
  68. }
  69. TEST_CASE("uint32_roX", "")
  70. {
  71. REQUIRE(bx::uint32_rol(0x80000000, 1) == 1);
  72. REQUIRE(bx::uint32_ror(1, 1) == 0x80000000);
  73. }
  74. TEST_CASE("uint64_roX", "")
  75. {
  76. REQUIRE(bx::uint64_rol(0x8000000000000000, 1) == 1);
  77. REQUIRE(bx::uint64_ror(1, 1) == 0x8000000000000000);
  78. }