[clang] [llvm] [HLSL] Implement frexp() (PR #235154)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 21 21:45:16 PST 2026


https://github.com/farzonl requested changes to this pull request.

Don't do this. I have major issues with doing code expansions both in clang codgen and then something extra in DXIL expansion. We really want to use DXIL Expansion as only a way of doing custom scalarization for things the scalarizer does not handle or for handling llvm intrinsics that could be introduced by things like InstCombine.

Second since this is a case we can do entirely in the header with the below code, lets just do it in the header.

Also we decided as a team that it is better to keep the behavior between DirectX and SPIR-V the same so if DirectX is not emitting an opcode we decided we did not need to emit the GLSL OpExtInst. In this case that means OpExtInst Frexp is not necessary. If we find later it would be better for perf we could add a #if defined(__SPIRV__) block in the header and have a target builtin for it, but I don't think it should be necessary

Therefore just do something like this pseudo code:
```hlsl
template <typename T, typename U>
constexpr T frexp_impl(T X, out T Exp) {
  U Bits = asuint(X);
  U ExpBits = Bits & 0x7f800000u;
  auto IsSpecial = (ExpBits == 0u) || (ExpBits == 0x7f800000u);
  // Use 0x007fffffu to match DXC's unsigned mantissa (or 0x807fffffu to preserve sign).
  U MantBits = (Bits & 0x007fffffu) | 0x3f000000u;
  T Mant = select(IsSpecial, abs(X), asfloat(MantBits));
  auto RawExp = (ExpBits >> 23u) - 126u;
  Exp = select(IsSpecial, (T)0, (T)asint(RawExp));
  return Mant;
}
```


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


More information about the cfe-commits mailing list