Browse Source

Added testpow2 test, and use improved version. Issue #184.

Branimir Karadžić 7 years ago
parent
commit
e930ad8fa1
2 changed files with 18 additions and 6 deletions
  1. 4 6
      include/bx/inline/uint32_t.inl
  2. 14 0
      tests/uint32_test.cpp

+ 4 - 6
include/bx/inline/uint32_t.inl

@@ -453,12 +453,10 @@ namespace bx
 
 	inline uint32_t uint32_testpow2(uint32_t _a)
 	{
-		const uint32_t tmp0   = uint32_not(_a);
-		const uint32_t tmp1   = uint32_inc(tmp0);
-		const uint32_t tmp2   = uint32_and(_a, tmp1);
-		const uint32_t tmp3   = uint32_cmpeq(tmp2, _a);
-		const uint32_t tmp4   = uint32_cmpneq(_a, 0);
-		const uint32_t result = uint32_and(tmp3, tmp4);
+		const uint32_t tmp0   = uint32_dec(_a);
+		const uint32_t tmp1   = uint32_xor(_a, tmp0);
+		const uint32_t tmp2   = uint32_srl(tmp1, 1);
+		const uint32_t result = uint32_cmpeq(tmp2, tmp0);
 
 		return result;
 	}

+ 14 - 0
tests/uint32_test.cpp

@@ -69,3 +69,17 @@ TEST_CASE("halfTo/FromFloat", "")
 		REQUIRE(orig == hff);
 	}
 }
+
+TEST_CASE("uint32_testpow2", "")
+{
+	uint32_t shift = 0;
+
+	for (uint32_t ii = 0; ii < UINT32_MAX; ++ii)
+	{
+		if (bx::uint32_testpow2(ii) )
+		{
+			REQUIRE(ii == 1 << shift);
+			++shift;
+		}
+	}
+}