uint32_test.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/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::strideAlign16(0, 12) );
  15. for (uint32_t ii = 0; ii < 12; ++ii)
  16. {
  17. REQUIRE(48 == bx::strideAlign16(ii+1, 12) );
  18. }
  19. }
  20. TEST_CASE("uint32_cnt")
  21. {
  22. REQUIRE( 0 == bx::uint32_cnttz(UINT32_C(1) ) );
  23. REQUIRE( 0 == bx::uint32_cnttz_ref(UINT32_C(1) ) );
  24. REQUIRE(31 == bx::uint32_cntlz(UINT32_C(1) ) );
  25. REQUIRE(31 == bx::uint32_cntlz_ref(UINT32_C(1) ) );
  26. REQUIRE( 0 == bx::uint64_cnttz(UINT64_C(1) ) );
  27. REQUIRE( 0 == bx::uint64_cnttz_ref(UINT64_C(1) ) );
  28. REQUIRE(63 == bx::uint64_cntlz(UINT64_C(1) ) );
  29. REQUIRE(63 == bx::uint64_cntlz_ref(UINT64_C(1) ) );
  30. REQUIRE( 1 == bx::uint32_cntbits(1) );
  31. REQUIRE( 1 == bx::uint32_cntbits_ref(1) );
  32. REQUIRE(16 == bx::uint32_cntbits(UINT16_MAX) );
  33. REQUIRE(16 == bx::uint32_cntbits_ref(UINT16_MAX) );
  34. REQUIRE(32 == bx::uint32_cntbits(UINT32_MAX) );
  35. REQUIRE(32 == bx::uint32_cntbits_ref(UINT32_MAX) );
  36. }
  37. TEST_CASE("uint32_part")
  38. {
  39. REQUIRE(UINT32_C(0x55555555) == bx::uint32_part1by1(UINT16_MAX) );
  40. REQUIRE(UINT32_C(0x09249249) == bx::uint32_part1by2(0x3ff) );
  41. }
  42. TEST_CASE("halfTo/FromFloat", "")
  43. {
  44. for (uint32_t ii = 0; ii < 0x7c00; ++ii)
  45. {
  46. const uint16_t orig = uint16_t(ii);
  47. const float htf = bx::halfToFloat(orig);
  48. const uint16_t hff = bx::halfFromFloat(htf);
  49. REQUIRE(orig == hff);
  50. }
  51. for (uint32_t ii = 0x8000; ii < 0xfc00; ++ii)
  52. {
  53. const uint16_t orig = uint16_t(ii);
  54. const float htf = bx::halfToFloat(orig);
  55. const uint16_t hff = bx::halfFromFloat(htf);
  56. REQUIRE(orig == hff);
  57. }
  58. }
  59. TEST_CASE("uint32_testpow2", "")
  60. {
  61. uint32_t shift = 0;
  62. for (uint32_t ii = 0; ii < UINT32_MAX; ++ii)
  63. {
  64. if (bx::uint32_testpow2(ii) )
  65. {
  66. REQUIRE(ii == 1 << shift);
  67. ++shift;
  68. }
  69. }
  70. }