Common: Add bitutils.h
This commit is contained in:
parent
e6bd6587fd
commit
02db665d4a
|
@ -5,6 +5,7 @@ add_library(common
|
||||||
audio_stream.cpp
|
audio_stream.cpp
|
||||||
audio_stream.h
|
audio_stream.h
|
||||||
bitfield.h
|
bitfield.h
|
||||||
|
bitutils.h
|
||||||
byte_stream.cpp
|
byte_stream.cpp
|
||||||
byte_stream.h
|
byte_stream.h
|
||||||
cd_image.cpp
|
cd_image.cpp
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
#pragma once
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#include <intrin.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
ALWAYS_INLINE unsigned CountLeadingZeros(T value)
|
||||||
|
{
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
if constexpr (sizeof(value) >= sizeof(u64))
|
||||||
|
{
|
||||||
|
unsigned long index;
|
||||||
|
return _BitScanReverse64(&index, ZeroExtend64(value)) ? static_cast<unsigned>(index) : 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unsigned long index;
|
||||||
|
return _BitScanReverse(&index, ZeroExtend32(value)) ? static_cast<unsigned>(index) : 0;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if constexpr (sizeof(value) >= sizeof(u64))
|
||||||
|
{
|
||||||
|
const unsigned bits = static_cast<unsigned>(__builtin_clzl(ZeroExtend64(value)));
|
||||||
|
return (value != 0) ? static_cast<unsigned>(bits) : 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const unsigned bits = static_cast<unsigned>(__builtin_clz(ZeroExtend32(value)));
|
||||||
|
return (value != 0) ? static_cast<unsigned>(bits) : 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
ALWAYS_INLINE unsigned CountTrailingZeros(T value)
|
||||||
|
{
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
if constexpr (sizeof(value) >= sizeof(u64))
|
||||||
|
{
|
||||||
|
unsigned long index;
|
||||||
|
return _BitScanForward64(&index, ZeroExtend64(value)) ? static_cast<unsigned>(index) : 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unsigned long index;
|
||||||
|
return _BitScanForward(&index, ZeroExtend32(value)) ? static_cast<unsigned>(index) : 0;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if constexpr (sizeof(value) >= sizeof(u64))
|
||||||
|
{
|
||||||
|
const unsigned bits = static_cast<unsigned>(__builtin_ctzl(ZeroExtend64(value)));
|
||||||
|
return (value != 0) ? static_cast<unsigned>(bits) : 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const unsigned bits = static_cast<unsigned>(__builtin_ctz(ZeroExtend32(value)));
|
||||||
|
return (value != 0) ? static_cast<unsigned>(bits) : 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
|
@ -39,6 +39,7 @@
|
||||||
<ClInclude Include="assert.h" />
|
<ClInclude Include="assert.h" />
|
||||||
<ClInclude Include="audio_stream.h" />
|
<ClInclude Include="audio_stream.h" />
|
||||||
<ClInclude Include="bitfield.h" />
|
<ClInclude Include="bitfield.h" />
|
||||||
|
<ClInclude Include="bitutils.h" />
|
||||||
<ClInclude Include="byte_stream.h" />
|
<ClInclude Include="byte_stream.h" />
|
||||||
<ClInclude Include="cd_image.h" />
|
<ClInclude Include="cd_image.h" />
|
||||||
<ClInclude Include="cpu_detect.h" />
|
<ClInclude Include="cpu_detect.h" />
|
||||||
|
|
|
@ -57,6 +57,7 @@
|
||||||
<Filter>gl</Filter>
|
<Filter>gl</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="event.h" />
|
<ClInclude Include="event.h" />
|
||||||
|
<ClInclude Include="bitutils.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="jit_code_buffer.cpp" />
|
<ClCompile Include="jit_code_buffer.cpp" />
|
||||||
|
|
Loading…
Reference in New Issue