================
@@ -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) {
----------------
Icohedron wrote:

Perhaps I could do something like this:
```hlsl
template <typename K, typename T, int BitWidth> constexpr uint 
firstbithigh_impl(T X) {
  K 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.
  K Inversion = (BitWidth - 1) - FBH;
  FBH = select(FBH == -1, FBH, Inversion);
#endif
  return FBH;
}
```

and call it like so:
```hlsl
template <typename T, int N>
const inline __detail::enable_if_t<__detail::is_same<int64_t, T>::value ||
                                       __detail::is_same<uint64_t, T>::value,
                                   vector<uint, N>>
firstbithigh(vector<T, N> X) {
  return __detail::firstbithigh_impl<vector<uint, N>, vector<T, N>, 64>(X);
}
```

https://github.com/llvm/llvm-project/pull/166419
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to