-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #78818 - scottmcm:as_rchunks, r=KodrAus
Add `as_rchunks` (and friends) to slices `@est31` mentioned (#76354 (comment)) that, for completeness, there needed to be an `as_chunks`-like method that chunks from the end (with the remainder at the beginning) like `rchunks` does. So here's a PR for `as_rchunks: &[T] -> (&[T], &[[T; N]])` and `as_rchunks_mut: &mut [T] -> (&mut [T], &mut [[T; N]])`. But as I was doing this and copy-pasting `from_raw_parts` calls, I thought that I should extract that into an unsafe method. It started out a private helper, but it seemed like `as_chunks_unchecked` could be reasonable as a "real" method, so I added docs and made it public. Let me know if you think it doesn't pull its weight.
- Loading branch information
Showing
2 changed files
with
187 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// no-system-llvm | ||
// compile-flags: -O | ||
// only-64bit (because the LLVM type of i64 for usize shows up) | ||
// ignore-debug: the debug assertions get in the way | ||
|
||
#![crate_type = "lib"] | ||
#![feature(slice_as_chunks)] | ||
|
||
// CHECK-LABEL: @chunks4 | ||
#[no_mangle] | ||
pub fn chunks4(x: &[u8]) -> &[[u8; 4]] { | ||
// CHECK-NEXT: start: | ||
// CHECK-NEXT: lshr i64 %x.1, 2 | ||
// CHECK-NOT: shl | ||
// CHECK-NOT: mul | ||
// CHECK-NOT: udiv | ||
// CHECK-NOT: urem | ||
// CHECK: ret | ||
x.as_chunks().0 | ||
} | ||
|
||
// CHECK-LABEL: @chunks4_with_remainder | ||
#[no_mangle] | ||
pub fn chunks4_with_remainder(x: &[u8]) -> (&[[u8; 4]], &[u8]) { | ||
// CHECK: and i64 %x.1, -4 | ||
// CHECK: and i64 %x.1, 3 | ||
// CHECK: lshr exact | ||
// CHECK-NOT: mul | ||
// CHECK-NOT: udiv | ||
// CHECK-NOT: urem | ||
// CHECK: ret | ||
x.as_chunks() | ||
} |