[clang] [HLSL] [DirectX] Invert the result of `firstbithigh` (PR #166419)
Farzon Lotfi via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 4 21:20:08 PST 2025
================
@@ -148,6 +148,29 @@ 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) {
+ uint FBH = __builtin_hlsl_elementwise_firstbithigh(X);
+#if defined(__DIRECTX__)
+ // The firstbithigh DXIL ops count bits from the wrong side, so we need to
+ // invert it for DirectX.
+ uint Inversion = (BitWidth - 1) - FBH;
+ FBH = select(FBH == -1, FBH, Inversion);
+#endif
+ return FBH;
+}
+
+template <typename T, int N, int BitWidth>
+constexpr vector<uint, N> firstbithigh_impl(vector<T, N> X) {
----------------
farzonl wrote:
Why do you need a vector implementation of this function? Assuming the `Inversion` case is why you can't do something similar to`faceforward_impl` above? If you make T have an arethmetic requirment can you do `T Inversion = (BitWidth - 1) - FBH;`?
https://github.com/llvm/llvm-project/pull/166419
More information about the cfe-commits
mailing list