Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
Clojure
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Helion
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
rust source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.83.0
rustc 1.84.0
rustc 1.85.0
rustc 1.86.0
rustc 1.87.0
rustc 1.88.0
rustc 1.89.0
rustc 1.9.0
rustc 1.90.0
rustc 1.91.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC assertions)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC assertions)
x86-64 GCCRS 14.2 (GCC)
x86-64 GCCRS 14.3 (GCC assertions)
x86-64 GCCRS 14.3 (GCC)
x86-64 GCCRS 15.1 (GCC assertions)
x86-64 GCCRS 15.1 (GCC)
x86-64 GCCRS 15.2 (GCC assertions)
x86-64 GCCRS 15.2 (GCC)
Options
Source code
use std::{ptr, mem::{self, MaybeUninit}, cmp}; pub unsafe fn rotate_u8_right1(slice: &mut [u8]) { rotate_right(slice, 1); } fn rotate_right<T>(slice: &mut [T], k: usize) { assert!(k <= slice.len()); let mid = slice.len() - k; let p = slice.as_mut_ptr(); // SAFETY: The range `[p.add(mid) - mid, p.add(mid) + k)` is trivially // valid for reading and writing, as required by `ptr_rotate`. unsafe { ptr_rotate(mid, p.add(mid), k); } } unsafe fn ptr_rotate<T>(mut left: usize, mut mid: *mut T, mut right: usize) { type BufType = [usize; 32]; if mem::size_of::<T>() == 0 { return; } loop { // N.B. the below algorithms can fail if these cases are not checked if (right == 0) || (left == 0) { return; } if (left + right < 24) || (mem::size_of::<T>() > mem::size_of::<[usize; 4]>()) { // Algorithm 1 // Microbenchmarks indicate that the average performance for random shifts is better all // the way until about `left + right == 32`, but the worst case performance breaks even // around 16. 24 was chosen as middle ground. If the size of `T` is larger than 4 // `usize`s, this algorithm also outperforms other algorithms. // SAFETY: callers must ensure `mid - left` is valid for reading and writing. let x = unsafe { mid.sub(left) }; // beginning of first round // SAFETY: see previous comment. let mut tmp: T = unsafe { x.read() }; let mut i = right; // `gcd` can be found before hand by calculating `gcd(left + right, right)`, // but it is faster to do one loop which calculates the gcd as a side effect, then // doing the rest of the chunk let mut gcd = right; // benchmarks reveal that it is faster to swap temporaries all the way through instead // of reading one temporary once, copying backwards, and then writing that temporary at // the very end. This is possibly due to the fact that swapping or replacing temporaries // uses only one memory address in the loop instead of needing to manage two. loop { // [long-safety-expl] // SAFETY: callers must ensure `[left, left+mid+right)` are all valid for reading and // writing. // // - `i` start with `right` so `mid-left <= x+i = x+right = mid-left+right < mid+right` // - `i <= left+right-1` is always true // - if `i < left`, `right` is added so `i < left+right` and on the next // iteration `left` is removed from `i` so it doesn't go further // - if `i >= left`, `left` is removed immediately and so it doesn't go further. // - overflows cannot happen for `i` since the function's safety contract ask for // `mid+right-1 = x+left+right` to be valid for writing // - underflows cannot happen because `i` must be bigger or equal to `left` for // a subtraction of `left` to happen. // // So `x+i` is valid for reading and writing if the caller respected the contract tmp = unsafe { x.add(i).replace(tmp) }; // instead of incrementing `i` and then checking if it is outside the bounds, we // check if `i` will go outside the bounds on the next increment. This prevents // any wrapping of pointers or `usize`. if i >= left { i -= left; if i == 0 { // end of first round // SAFETY: tmp has been read from a valid source and x is valid for writing // according to the caller. unsafe { x.write(tmp) }; break; } // this conditional must be here if `left + right >= 15` if i < gcd { gcd = i; } } else { i += right; } } // finish the chunk with more rounds for start in 1..gcd { // SAFETY: `gcd` is at most equal to `right` so all values in `1..gcd` are valid for // reading and writing as per the function's safety contract, see [long-safety-expl] // above tmp = unsafe { x.add(start).read() }; // [safety-expl-addition] // // Here `start < gcd` so `start < right` so `i < right+right`: `right` being the // greatest common divisor of `(left+right, right)` means that `left = right` so // `i < left+right` so `x+i = mid-left+i` is always valid for reading and writing // according to the function's safety contract. i = start + right; loop { // SAFETY: see [long-safety-expl] and [safety-expl-addition] tmp = unsafe { x.add(i).replace(tmp) }; if i >= left { i -= left; if i == start { // SAFETY: see [long-safety-expl] and [safety-expl-addition] unsafe { x.add(start).write(tmp) }; break; } } else { i += right; } } } return; // `T` is not a zero-sized type, so it's okay to divide by its size. } else if cmp::min(left, right) <= mem::size_of::<BufType>() / mem::size_of::<T>() { // Algorithm 2 // The `[T; 0]` here is to ensure this is appropriately aligned for T let mut rawarray = MaybeUninit::<(BufType, [T; 0])>::uninit(); let buf = rawarray.as_mut_ptr() as *mut T; // SAFETY: `mid-left <= mid-left+right < mid+right` let dim = unsafe { mid.sub(left).add(right) }; if left <= right { // SAFETY: // // 1) The `else if` condition about the sizes ensures `[mid-left; left]` will fit in // `buf` without overflow and `buf` was created just above and so cannot be // overlapped with any value of `[mid-left; left]` // 2) [mid-left, mid+right) are all valid for reading and writing and we don't care // about overlaps here. // 3) The `if` condition about `left <= right` ensures writing `left` elements to // `dim = mid-left+right` is valid because: // - `buf` is valid and `left` elements were written in it in 1) // - `dim+left = mid-left+right+left = mid+right` and we write `[dim, dim+left)` unsafe { // 1) ptr::copy_nonoverlapping(mid.sub(left), buf, left); // 2) ptr::copy(mid, mid.sub(left), right); // 3) ptr::copy_nonoverlapping(buf, dim, left); } } else { // SAFETY: same reasoning as above but with `left` and `right` reversed unsafe { ptr::copy_nonoverlapping(mid, buf, right); ptr::copy(mid.sub(left), dim, left); ptr::copy_nonoverlapping(buf, mid.sub(left), right); } } return; } else if left >= right { // Algorithm 3 // There is an alternate way of swapping that involves finding where the last swap // of this algorithm would be, and swapping using that last chunk instead of swapping // adjacent chunks like this algorithm is doing, but this way is still faster. loop { // SAFETY: // `left >= right` so `[mid-right, mid+right)` is valid for reading and writing // Subtracting `right` from `mid` each turn is counterbalanced by the addition and // check after it. unsafe { ptr::swap_nonoverlapping(mid.sub(right), mid, right); mid = mid.sub(right); } left -= right; if left < right { break; } } } else { // Algorithm 3, `left < right` loop { // SAFETY: `[mid-left, mid+left)` is valid for reading and writing because // `left < right` so `mid+left < mid+right`. // Adding `left` to `mid` each turn is counterbalanced by the subtraction and check // after it. unsafe { ptr::swap_nonoverlapping(mid.sub(left), mid, left); mid = mid.add(left); } right -= left; if right < left { break; } } } } }
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.83.0
rustc 1.84.0
rustc 1.85.0
rustc 1.86.0
rustc 1.87.0
rustc 1.88.0
rustc 1.89.0
rustc 1.9.0
rustc 1.90.0
rustc 1.91.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC assertions)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC assertions)
x86-64 GCCRS 14.2 (GCC)
x86-64 GCCRS 14.3 (GCC assertions)
x86-64 GCCRS 14.3 (GCC)
x86-64 GCCRS 15.1 (GCC assertions)
x86-64 GCCRS 15.1 (GCC)
x86-64 GCCRS 15.2 (GCC assertions)
x86-64 GCCRS 15.2 (GCC)
Options
Source code
use std::{ptr, mem::{self, MaybeUninit}, cmp}; pub unsafe fn rotate_u8_right1(slice: &mut [u8]) { rotate_right(slice, 1); } fn rotate_right<T>(slice: &mut [T], k: usize) { assert!(k <= slice.len()); let mid = slice.len() - k; let p = slice.as_mut_ptr(); // SAFETY: The range `[p.add(mid) - mid, p.add(mid) + k)` is trivially // valid for reading and writing, as required by `ptr_rotate`. unsafe { ptr_rotate(mid, p.add(mid), k); } } unsafe fn ptr_rotate<T>(mut left: usize, mut mid: *mut T, mut right: usize) { type BufType = [usize; 32]; if mem::size_of::<T>() == 0 { return; } loop { // N.B. the below algorithms can fail if these cases are not checked if (right == 0) || (left == 0) { return; } if (left + right < 24) || (mem::size_of::<T>() > mem::size_of::<[usize; 4]>()) { // Algorithm 1 // Microbenchmarks indicate that the average performance for random shifts is better all // the way until about `left + right == 32`, but the worst case performance breaks even // around 16. 24 was chosen as middle ground. If the size of `T` is larger than 4 // `usize`s, this algorithm also outperforms other algorithms. // SAFETY: callers must ensure `mid - left` is valid for reading and writing. let x = unsafe { mid.sub(left) }; // beginning of first round // SAFETY: see previous comment. let mut tmp: T = unsafe { x.read() }; let mut i = right; // `gcd` can be found before hand by calculating `gcd(left + right, right)`, // but it is faster to do one loop which calculates the gcd as a side effect, then // doing the rest of the chunk let mut gcd = right; // benchmarks reveal that it is faster to swap temporaries all the way through instead // of reading one temporary once, copying backwards, and then writing that temporary at // the very end. This is possibly due to the fact that swapping or replacing temporaries // uses only one memory address in the loop instead of needing to manage two. loop { // [long-safety-expl] // SAFETY: callers must ensure `[left, left+mid+right)` are all valid for reading and // writing. // // - `i` start with `right` so `mid-left <= x+i = x+right = mid-left+right < mid+right` // - `i <= left+right-1` is always true // - if `i < left`, `right` is added so `i < left+right` and on the next // iteration `left` is removed from `i` so it doesn't go further // - if `i >= left`, `left` is removed immediately and so it doesn't go further. // - overflows cannot happen for `i` since the function's safety contract ask for // `mid+right-1 = x+left+right` to be valid for writing // - underflows cannot happen because `i` must be bigger or equal to `left` for // a subtraction of `left` to happen. // // So `x+i` is valid for reading and writing if the caller respected the contract tmp = unsafe { x.add(i).replace(tmp) }; // instead of incrementing `i` and then checking if it is outside the bounds, we // check if `i` will go outside the bounds on the next increment. This prevents // any wrapping of pointers or `usize`. if i >= left { i -= left; if i == 0 { // end of first round // SAFETY: tmp has been read from a valid source and x is valid for writing // according to the caller. unsafe { x.write(tmp) }; break; } // this conditional must be here if `left + right >= 15` if i < gcd { gcd = i; } } else { i += right; } } // finish the chunk with more rounds for start in 1..gcd { // SAFETY: `gcd` is at most equal to `right` so all values in `1..gcd` are valid for // reading and writing as per the function's safety contract, see [long-safety-expl] // above tmp = unsafe { x.add(start).read() }; // [safety-expl-addition] // // Here `start < gcd` so `start < right` so `i < right+right`: `right` being the // greatest common divisor of `(left+right, right)` means that `left = right` so // `i < left+right` so `x+i = mid-left+i` is always valid for reading and writing // according to the function's safety contract. i = start + right; loop { // SAFETY: see [long-safety-expl] and [safety-expl-addition] tmp = unsafe { x.add(i).replace(tmp) }; if i >= left { i -= left; if i == start { // SAFETY: see [long-safety-expl] and [safety-expl-addition] unsafe { x.add(start).write(tmp) }; break; } } else { i += right; } } } return; // `T` is not a zero-sized type, so it's okay to divide by its size. } else if left <= mem::size_of::<BufType>() / mem::size_of::<T>() || right <= mem::size_of::<BufType>() / mem::size_of::<T>() { // Algorithm 2 // The `[T; 0]` here is to ensure this is appropriately aligned for T let mut rawarray = MaybeUninit::<(BufType, [T; 0])>::uninit(); let buf = rawarray.as_mut_ptr() as *mut T; // SAFETY: `mid-left <= mid-left+right < mid+right` let dim = unsafe { mid.sub(left).add(right) }; if left <= right { // SAFETY: // // 1) The `else if` condition about the sizes ensures `[mid-left; left]` will fit in // `buf` without overflow and `buf` was created just above and so cannot be // overlapped with any value of `[mid-left; left]` // 2) [mid-left, mid+right) are all valid for reading and writing and we don't care // about overlaps here. // 3) The `if` condition about `left <= right` ensures writing `left` elements to // `dim = mid-left+right` is valid because: // - `buf` is valid and `left` elements were written in it in 1) // - `dim+left = mid-left+right+left = mid+right` and we write `[dim, dim+left)` unsafe { // 1) ptr::copy_nonoverlapping(mid.sub(left), buf, left); // 2) ptr::copy(mid, mid.sub(left), right); // 3) ptr::copy_nonoverlapping(buf, dim, left); } } else { // SAFETY: same reasoning as above but with `left` and `right` reversed unsafe { ptr::copy_nonoverlapping(mid, buf, right); ptr::copy(mid.sub(left), dim, left); ptr::copy_nonoverlapping(buf, mid.sub(left), right); } } return; } else if left >= right { // Algorithm 3 // There is an alternate way of swapping that involves finding where the last swap // of this algorithm would be, and swapping using that last chunk instead of swapping // adjacent chunks like this algorithm is doing, but this way is still faster. loop { // SAFETY: // `left >= right` so `[mid-right, mid+right)` is valid for reading and writing // Subtracting `right` from `mid` each turn is counterbalanced by the addition and // check after it. unsafe { ptr::swap_nonoverlapping(mid.sub(right), mid, right); mid = mid.sub(right); } left -= right; if left < right { break; } } } else { // Algorithm 3, `left < right` loop { // SAFETY: `[mid-left, mid+left)` is valid for reading and writing because // `left < right` so `mid+left < mid+right`. // Adding `left` to `mid` each turn is counterbalanced by the subtraction and check // after it. unsafe { ptr::swap_nonoverlapping(mid.sub(left), mid, left); mid = mid.add(left); } right -= left; if right < left { break; } } } } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree