Duckstation/src/common/bitutils.h
Connor McLaughlin 8c7a192128 Misc: Add copyright/license statement to applicable files
Should've did this in the beginning.
2022-12-04 21:03:49 +10:00

62 lines
1.8 KiB
C++

// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once
#include "types.h"
#ifdef _MSC_VER
#include <intrin.h>
#endif
/// Returns the number of zero bits before the first set bit, going MSB->LSB.
template<typename T>
ALWAYS_INLINE unsigned CountLeadingZeros(T value)
{
#ifdef _MSC_VER
if constexpr (sizeof(value) >= sizeof(u64))
{
unsigned long index;
_BitScanReverse64(&index, ZeroExtend64(value));
return static_cast<unsigned>(index) ^ static_cast<unsigned>((sizeof(value) * 8u) - 1u);
}
else
{
unsigned long index;
_BitScanReverse(&index, ZeroExtend32(value));
return static_cast<unsigned>(index) ^ static_cast<unsigned>((sizeof(value) * 8u) - 1u);
}
#else
if constexpr (sizeof(value) >= sizeof(u64))
return static_cast<unsigned>(__builtin_clzl(ZeroExtend64(value)));
else if constexpr (sizeof(value) == sizeof(u32))
return static_cast<unsigned>(__builtin_clz(ZeroExtend32(value)));
else
return static_cast<unsigned>(__builtin_clz(ZeroExtend32(value))) & static_cast<unsigned>((sizeof(value) * 8u) - 1u);
#endif
}
/// Returns the number of zero bits before the first set bit, going LSB->MSB.
template<typename T>
ALWAYS_INLINE unsigned CountTrailingZeros(T value)
{
#ifdef _MSC_VER
if constexpr (sizeof(value) >= sizeof(u64))
{
unsigned long index;
_BitScanForward64(&index, ZeroExtend64(value));
return index;
}
else
{
unsigned long index;
_BitScanForward(&index, ZeroExtend32(value));
return index;
}
#else
if constexpr (sizeof(value) >= sizeof(u64))
return static_cast<unsigned>(__builtin_ctzl(ZeroExtend64(value)));
else
return static_cast<unsigned>(__builtin_ctz(ZeroExtend32(value)));
#endif
}