[llvm] [CHERI] Add a Support utility for determining alignment requirements of CHERI capabilities. (PR #197402)

Owen Anderson via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 6 13:11:15 PDT 2026


================

----------------
resistor wrote:

In spirit yes, but there's a bit more fiddliness. Here's a version that I've exhausted tested as matching the loop:

```
#include <cstdint>
#include <bit>
#include <cassert>

uint32_t a(uint32_t Length) {
  for (unsigned e = 0; e <= 14; ++e) {
    if (Length <= 511ULL << e)
      return (~static_cast<uint32_t>(0) << e);
  }

  return (~static_cast<uint32_t>(0) << 24);
}


uint32_t b(uint32_t Length) {
    uint32_t E;
    if (Length > 511ULL << 14)
        E = 24;
    else {
        E = Length > 511 ? 32 - std::countl_zero(Length) - 9 : 0;
        if (Length > 511ULL << E)
            ++E;
    }
    return ~static_cast<uint32_t>(0)<< E;
}

int main() {
    for (uint32_t i = 0; i < ~static_cast<uint32_t>(0); ++i)
      assert(a(i) == b(i));
    assert(a(~static_cast<uint32_t>(0)) == b(~static_cast<uint32_t>(0)));
    return 0;
}
```

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


More information about the llvm-commits mailing list