[clang] [HLSL] Invert the result of `firstbithigh` (PR #166419)

Ashley Coleman via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 4 14:46:02 PST 2025


================
@@ -148,6 +148,15 @@ template <typename T> constexpr T ldexp_impl(T X, T Exp) {
   return exp2(Exp) * X;
 }
 
+template <typename T, int Bitwidth> constexpr uint firstbithigh_impl(T X) {
+  return (Bitwidth - 1) - __builtin_hlsl_elementwise_firstbithigh(X);
+}
+
+template <typename T, int N, int Bitwidth>
+constexpr vector<uint, N> firstbithigh_impl(vector<T, N> X) {
+  return (Bitwidth - 1) - __builtin_hlsl_elementwise_firstbithigh(X);
----------------
V-FEXrt wrote:

Are we sure that DXC has divergent behavior here between DX and SPRIV? IIRC the opcodes are divergent but the result of the hlsl intrinsic is the same

I'm trying to find proof of this right now but IIRC the DXIL ops (for high bit only) return the number counting from the wrong side (which is why we need the `31 - ans`) but on SPIRV they are consistent so not having the `31 - ans` is the correct lowering

https://github.com/llvm/llvm-project/pull/166419


More information about the cfe-commits mailing list