Skip to content

Instantly share code, notes, and snippets.

View shangsony's full-sized avatar

sonyshang shangsony

View GitHub Profile
@pps83
pps83 / ctz_clz.cpp
Last active May 9, 2024 20:21
__builtin_ctz (ctzl, ctzll) and __builtin_clz (clzl, clzll) for Visual Studio
// Note, bsf/bsr are used by default.
// Enable /arch:AVX2 compilation for better optimizations
#if defined(_MSC_VER) && !defined(__clang__)
#include <intrin.h>
static __forceinline int __builtin_ctz(unsigned x)
{
#if defined(_M_ARM) || defined(_M_ARM64) || defined(_M_HYBRID_X86_ARM64) || defined(_M_ARM64EC)
return (int)_CountTrailingZeros(x);