[libc-commits] [libc] [libc] Add helper function for aligning pointers and values (PR #183324)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Wed Feb 25 09:28:22 PST 2026


jhuber6 wrote:

> FWIW I'm in favor of unification, but I find the difference between "align_up" and "align_to" overloads confusing, and the lack of compile-time checks (I suspect we don't want runtime asserts for performance reasons) makes it even harder... Will the compiler be smart enough to pick builtins or use bitwise operations when the alignment value is a power of two (I'd hope so). Do we have cases when compiler won't know the alignment value in advance and emit divisions, but in practice it would always be powers of two?

I really wish we had an assertion that only applied to the test build, we do them separately and should at least make use of that. But yes, we don't want assertions like that in the shipped version of the library.

This is mostly inspired by https://github.com/llvm/llvm-project/blob/5e5c4acd4e2b039a352ba0ddd34e1da0c8bf6876/llvm/include/llvm/Support/MathExtras.h#L477, though they specifically use `AlignTo` and `AlignDown` with `PowerOf2` as a suffix for the other variant. I wanted to keep the name used for the common case.

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


More information about the libc-commits mailing list