]> git.lizzy.rs Git - rust.git/commit
Auto merge of #50319 - nagisa:align_to, r=alexcrichton
authorbors <bors@rust-lang.org>
Fri, 18 May 2018 21:49:38 +0000 (21:49 +0000)
committerbors <bors@rust-lang.org>
Fri, 18 May 2018 21:49:38 +0000 (21:49 +0000)
commit37a409177c80e6f301c41833fc7ee8fda2412c00
treee5f1161550985792701d06edfb9584d1a6b6fe5d
parent952f344cdc0bca58d9f6c54dcfbae0890246e886
parent59bb0fe66ea6d80c5e1466b9609887e4cf7cde47
Auto merge of #50319 - nagisa:align_to, r=alexcrichton

Implement [T]::align_to

Note that this PR deviates from what is accepted by RFC slightly by making `align_offset` to return an offset in elements, rather than bytes. This is necessary to sanely support `[T]::align_to` and also simply makes more sense™. The caveat is that trying to align a pointer of ZST is now an equivalent to `is_aligned` check, rather than anything else (as no number of ZST elements will align a misaligned ZST pointer).

It also implements the `align_to` slightly differently than proposed in the RFC to properly handle cases where size of T and U aren’t co-prime.

Furthermore, a promise is made that the slice containing `U`s will be as large as possible (contrary to the RFC) – otherwise the function is quite useless.

The implementation uses quite a few underhanded tricks and takes advantage of the fact that alignment is a power-of-two quite heavily to optimise the machine code down to something that results in as few known-expensive instructions as possible. Currently calling `ptr.align_offset` with an unknown-at-compile-time `align` results in code that has just a single "expensive" modulo operation; the rest is "cheap" arithmetic and bitwise ops.

cc https://github.com/rust-lang/rust/issues/44488 @oli-obk

As mentioned in the commit message for align_offset, many thanks go to Chris McDonald.
src/libcore/intrinsics.rs
src/libcore/ptr.rs
src/libcore/tests/lib.rs