浏览代码

Replace some __builtin with their C++20 equivalent

Panagiotis Christopoulos Charitos 7 月之前
父节点
当前提交
fd63b3a9d1
共有 7 个文件被更改,包括 25 次插入24 次删除
  1. 0 16
      AnKi/Config.h.cmake
  2. 1 1
      AnKi/Gr/Vulkan/VkCommon.cpp
  3. 15 0
      AnKi/Shaders/Functions.hlsl
  4. 3 3
      AnKi/Util/BitSet.h
  5. 1 1
      AnKi/Util/BuddyAllocatorBuilder.h
  6. 3 2
      AnKi/Util/Enum.h
  7. 2 1
      AnKi/Util/Functions.h

+ 0 - 16
AnKi/Config.h.cmake

@@ -273,22 +273,6 @@ inline constexpr const char* kAnKiBuildConfigString =
 #	define ANKI_END_PACKED_STRUCT _Pragma("pack (pop)")
 #endif
 
-// Builtins
-#if ANKI_COMPILER_MSVC
-#	include <intrin.h>
-#	define __builtin_popcount __popcnt
-#	define __builtin_popcountl(x) int(__popcnt64(x))
-#	define __builtin_clzll(x) int(__lzcnt64(x))
-
-#pragma intrinsic(_BitScanForward)
-inline int __builtin_ctzll(unsigned long long x)
-{
-	unsigned long o;
-	_BitScanForward64(&o, x);
-	return o;
-}
-#endif
-
 // Constants
 #define ANKI_SAFE_ALIGNMENT 16
 #define ANKI_CACHE_LINE_SIZE 64

+ 1 - 1
AnKi/Gr/Vulkan/VkCommon.cpp

@@ -548,7 +548,7 @@ VkShaderStageFlags convertShaderTypeBit(ShaderTypeBit bit)
 	}
 
 	ANKI_ASSERT(out != 0);
-	ANKI_ASSERT(__builtin_popcount(U32(bit)) == __builtin_popcount(out));
+	ANKI_ASSERT(std::popcount(U32(bit)) == std::popcount(out));
 	return out;
 }
 

+ 15 - 0
AnKi/Shaders/Functions.hlsl

@@ -893,8 +893,23 @@ vector<T, kComp> linearTextureSampling(Texture3D<Vec4> sam, Vec3 uv)
 	return o;
 }
 
+/// Generate a 4x MSAA pattern. Returns the numbers in
+/// https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_standard_multisample_quality_levels
+/// Divide the result by 8.0 to normalize.
+IVec2 generateMsaa4x(U32 sample)
+{
+	sample <<= 2u;
+	IVec2 pattern = IVec2(41702, 60002);
+	pattern >>= sample;
+	pattern &= 0xF;
+	pattern -= 8;
+
+	return pattern;
+}
+
 /// Generate a 16x MSAA pattern. Returns the numbers in
 /// https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_standard_multisample_quality_levels
+/// Divide the result by 8.0 to normalize.
 IVec2 generateMsaa16x(U32 sample)
 {
 	const IVec2 packed[2] = {IVec2(0xBDA3C579, 0x3BD67A59), IVec2(0x1EF02486, 0xF48C21E)};

+ 3 - 3
AnKi/Util/BitSet.h

@@ -242,7 +242,7 @@ public:
 		U32 count = 0;
 		for(U i = 0; i < kChunkCount; ++i)
 		{
-			count += __builtin_popcountl(m_chunks[i]);
+			count += std::popcount(m_chunks[i]);
 		}
 		return count;
 	}
@@ -256,7 +256,7 @@ public:
 			const U64 bits = m_chunks[i];
 			if(bits != 0)
 			{
-				const U32 msb = U32(__builtin_clzll(bits));
+				const U32 msb = U32(std::countl_zero(bits));
 				return (63 - msb) + (i * kChunkBitCount);
 			}
 		}
@@ -272,7 +272,7 @@ public:
 			const U64 bits = m_chunks[i];
 			if(bits != 0)
 			{
-				const U32 lsb = U32(__builtin_ctzll(bits));
+				const U32 lsb = U32(std::countr_zero(bits));
 				return lsb + (i * kChunkBitCount);
 			}
 		}

+ 1 - 1
AnKi/Util/BuddyAllocatorBuilder.h

@@ -91,7 +91,7 @@ private:
 	static constexpr U32 log2(PtrSize v)
 	{
 		ANKI_ASSERT(isPowerOfTwo(v));
-		return U32(__builtin_ctzll(v));
+		return U32(std::countr_zero(v));
 	}
 
 	using FreeList = DynamicArray<Address, TMemoryPool, PtrSize>;

+ 3 - 2
AnKi/Util/Enum.h

@@ -6,6 +6,7 @@
 #pragma once
 
 #include <AnKi/Util/Assert.h>
+#include <bit>
 
 namespace anki {
 
@@ -217,7 +218,7 @@ public:
 	TEnum operator*() const
 	{
 		ANKI_ASSERT(m_val);
-		const TEnum out = TEnum(__builtin_ctzll(m_val));
+		const TEnum out = TEnum(std::countr_zero(m_val));
 		ANKI_ASSERT(out >= TEnum::kFirst && out < TEnum::kCount);
 		return out;
 	}
@@ -225,7 +226,7 @@ public:
 	void operator++()
 	{
 		ANKI_ASSERT(m_val);
-		m_val ^= Type(1_U64 << __builtin_ctzll(m_val));
+		m_val ^= Type(1_U64 << std::countr_zero<U64>(m_val));
 	}
 
 	bool operator!=(EnumBitsIterableIterator b) const

+ 2 - 1
AnKi/Util/Functions.h

@@ -17,6 +17,7 @@
 #include <cstring>
 #include <algorithm>
 #include <functional>
+#include <bit>
 
 namespace anki {
 
@@ -142,7 +143,7 @@ inline constexpr Int nextPowerOfTwo(Int x) requires(std::is_integral<Int>::value
 template<typename Int>
 inline constexpr Int previousPowerOfTwo(Int x) requires(std::is_integral<Int>::value)
 {
-	const U64 out = (x != 0) ? (1_U64 << ((sizeof(U64) * 8 - 1) - __builtin_clzll(x))) : 0;
+	const U64 out = (x != 0) ? (1_U64 << ((sizeof(U64) * 8 - 1) - std::countl_zero<U64>(x))) : 0;
 	return Int(out);
 }