Sfoglia il codice sorgente

Merge pull request #38 from dariomanesku/master

allocator fix + uint64_cntbits()
Branimir Karadžić 11 anni fa
parent
commit
9258b69888
2 ha cambiato i file con 30 aggiunte e 17 eliminazioni
  1. 6 16
      include/bx/allocator.h
  2. 24 1
      include/bx/uint32_t.h

+ 6 - 16
include/bx/allocator.h

@@ -24,9 +24,9 @@
 #	define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align, __FILE__, __LINE__)
 #	define BX_ALIGNED_FREE(_allocator, _ptr, _align)           bx::free(_allocator, _ptr, _align, __FILE__, __LINE__)
 #	define BX_NEW(_allocator, _type)                           ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
-#	define BX_DELETE(_allocator, _ptr)                         bx::deleteObject(_allocator, _ptr, __FILE__, __LINE__)
+#	define BX_DELETE(_allocator, _ptr)                         bx::deleteObject(_allocator, _ptr, 0, __FILE__, __LINE__)
 #	define BX_ALIGNED_NEW(_allocator, _type, _align)           ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
-#	define BX_ALIGNED_DELETE(_allocator, _ptr, _align)         bx::alignedDeleteObject(_allocator, _ptr, _align, __FILE__, __LINE__)
+#	define BX_ALIGNED_DELETE(_allocator, _ptr, _align)         bx::deleteObject(_allocator, _ptr, _align, __FILE__, __LINE__)
 #else
 #	define BX_ALLOC(_allocator, _size)                         bx::alloc(_allocator, _size, 0)
 #	define BX_REALLOC(_allocator, _ptr, _size)                 bx::realloc(_allocator, _ptr, _size, 0)
@@ -35,9 +35,9 @@
 #	define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align)
 #	define BX_ALIGNED_FREE(_allocator, _ptr, _align)           bx::free(_allocator, _ptr, _align)
 #	define BX_NEW(_allocator, _type)                           ::new(BX_ALLOC(_allocator, sizeof(_type) ) ) _type
-#	define BX_DELETE(_allocator, _ptr)                         bx::deleteObject(_allocator, _ptr)
+#	define BX_DELETE(_allocator, _ptr)                         bx::deleteObject(_allocator, _ptr, 0)
 #	define BX_ALIGNED_NEW(_allocator, _type, _align)           ::new(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align) ) _type
-#	define BX_ALIGNED_DELETE(_allocator, _ptr, _align)         bx::alignedDeleteObject(_allocator, _ptr, _align)
+#	define BX_ALIGNED_DELETE(_allocator, _ptr, _align)         bx::deleteObject(_allocator, _ptr, _align)
 #endif // BX_CONFIG_DEBUG_ALLOC
 
 #ifndef BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT
@@ -142,22 +142,12 @@ namespace bx
 	}
 
 	template <typename ObjectT>
-	inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, const char* _file = NULL, uint32_t _line = 0)
+	inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align = 0, const char* _file = NULL, uint32_t _line = 0)
 	{
 		if (NULL != _object)
 		{
 			_object->~ObjectT();
-			free(_allocator, _object, 0, _file, _line);
-		}
-	}
-
-	template <typename ObjectT>
-	inline void alignedDeleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align, const char* _file = NULL, uint32_t _line = 0)
-	{
-		if (NULL != _object)
-		{
-			_object->~ObjectT();
-			alignedFree(_allocator, _object, _align, _file, _line);
+			free(_allocator, _object, _align, _file, _line);
 		}
 	}
 

+ 24 - 1
include/bx/uint32_t.h

@@ -583,7 +583,7 @@ namespace bx
 		union { uint32_t ui; float flt;	} utof;
 		utof.ui = f_result;
 		return utof.flt;
-	} 
+	}
 
 	inline uint16_t uint16_min(uint16_t _a, uint16_t _b)
 	{
@@ -613,6 +613,29 @@ namespace bx
 		return result;
 	}
 
+	inline uint64_t uint64_cntbits_ref(uint64_t _val)
+	{
+		const uint32_t lo = uint32_t(_val&UINT32_MAX);
+		const uint32_t hi = uint32_t(_val>>32);
+
+		const uint32_t total = bx::uint32_cntbits(lo)
+							 + bx::uint32_cntbits(hi);
+
+		return total;
+	}
+
+	/// Count number of bits set.
+	inline uint64_t uint64_cntbits(uint64_t _val)
+	{
+#if BX_COMPILER_GCC || BX_COMPILER_CLANG
+		return __builtin_popcountll(_val);
+#elif BX_COMPILER_MSVC && BX_PLATFORM_WINDOWS
+		return __popcnt64(_val);
+#else
+		return uint64_cntbits_ref(_val);
+#endif // BX_COMPILER_
+	}
+
 	inline uint64_t uint64_cntlz_ref(uint64_t _val)
 	{
 		return _val & UINT64_C(0xffffffff00000000)