uint32_test.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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(31 == bx::uint32_cntlz(UINT32_C(1) ) );
  24. REQUIRE( 0 == bx::uint64_cnttz(UINT64_C(1) ) );
  25. REQUIRE(63 == bx::uint64_cntlz(UINT64_C(1) ) );
  26. REQUIRE( 1 == bx::uint32_cntbits(1) );
  27. REQUIRE(16 == bx::uint32_cntbits(UINT16_MAX) );
  28. REQUIRE(32 == bx::uint32_cntbits(UINT32_MAX) );
  29. }
  30. TEST_CASE("uint32_part")
  31. {
  32. REQUIRE(UINT32_C(0x55555555) == bx::uint32_part1by1(UINT16_MAX) );
  33. REQUIRE(UINT32_C(0x09249249) == bx::uint32_part1by2(0x3ff) );
  34. }
  35. TEST_CASE("halfTo/FromFloat", "")
  36. {
  37. for (uint32_t ii = 0; ii < 0x7c00; ++ii)
  38. {
  39. const uint16_t orig = uint16_t(ii);
  40. const float htf = bx::halfToFloat(orig);
  41. const uint16_t hff = bx::halfFromFloat(htf);
  42. REQUIRE(orig == hff);
  43. }
  44. for (uint32_t ii = 0x8000; ii < 0xfc00; ++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. }
  52. TEST_CASE("uint32_testpow2", "")
  53. {
  54. uint32_t shift = 0;
  55. for (uint32_t ii = 0; ii < UINT32_MAX; ++ii)
  56. {
  57. if (bx::uint32_testpow2(ii) )
  58. {
  59. REQUIRE(ii == 1u << shift);
  60. ++shift;
  61. }
  62. }
  63. }
  64. TEST_CASE("uint32_roX", "")
  65. {
  66. REQUIRE(bx::uint32_rol(0x80000000, 1) == 1);
  67. REQUIRE(bx::uint32_ror(1, 1) == 0x80000000);
  68. }
  69. TEST_CASE("uint64_roX", "")
  70. {
  71. REQUIRE(bx::uint64_rol(0x8000000000000000, 1) == 1);
  72. REQUIRE(bx::uint64_ror(1, 1) == 0x8000000000000000);
  73. }