Skip to content

Windows/MSVC build fails because __builtin_clzll is not available #1007

Description

@SaurSum8

Windows/MSVC build failure: __builtin_clzll is not available

Summary

RasterizeToPixelsFromWorld3DGS.cu uses __builtin_clzll inside ceil_log2_u64, but this builtin is not available when compiling with MSVC on Windows. This causes Windows builds to fail.

Relevant code

uint32_t ceil_log2_u64(uint64_t x) {
    if (x <= 1) {
        return 0;
    }
    return 64u - static_cast<uint32_t>(__builtin_clzll(x - 1));
}

Problem

__builtin_clzll is a GCC/Clang-specific builtin. On MSVC, the equivalent operation should use _BitScanReverse64 from <intrin.h>, or the code should use std::countl_zero when C++20 is available.

Suggested fix

One possible MSVC-compatible implementation is:

#if defined(_MSC_VER)
#include <intrin.h>
#endif

namespace {

uint32_t ceil_log2_u64(uint64_t x) {
    if (x <= 1) {
        return 0;
    }

#if defined(_MSC_VER)
    unsigned long index;
    _BitScanReverse64(&index, x - 1);
    return static_cast<uint32_t>(index) + 1u;
#else
    return 64u - static_cast<uint32_t>(__builtin_clzll(x - 1));
#endif
}

} // namespace

This is equivalent because _BitScanReverse64 returns the zero-based index of the most significant set bit, while 64 - __builtin_clzll(v) returns the bit width of v.

Notes

The zero case is already handled by the x <= 1 guard, so _BitScanReverse64 is only called with a nonzero value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions