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 #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
unsafe extern "C" { fn generate_block(state: &mut [u32; 8], block: &mut [u32; 8]); } #[unsafe(no_mangle)] pub fn fill_bytes(state: &mut [u32; 8], buf: &mut [u32; 8], dst: &mut [u8]) { fill_bytes_via_gen_block(dst, buf, |block| unsafe { generate_block(state, block) }) } /// Implement `fill_bytes` using buffer and block generation closure. fn fill_bytes_via_gen_block<W: Word, const N: usize>( mut dst: &mut [u8], buf: &mut [W; N], mut generate_block: impl FnMut(&mut [W; N]), ) { let word_size = size_of::<W>(); let pos = buf[0]; let pos_usize = pos.into_usize(); if pos_usize < buf.len() { let buf_tail = &buf[pos_usize..]; let buf_rem = size_of_val(buf_tail); if buf_rem >= dst.len() { let new_pos = read_bytes(buf, dst, pos); buf[0] = new_pos; return; } let (l, r) = dst.split_at_mut(buf_rem); read_bytes(buf, l, pos); dst = r; } let mut blocks = dst.chunks_exact_mut(N * word_size); let zero = W::from_usize(0); for block in &mut blocks { // We intentionally use the temporary buffer to prevent unnecessary writes // to the original `buf` and to enable potential optimization of writing // generated data directly into `block`. let mut buf = [zero; N]; generate_block(&mut buf); read_bytes(&buf, block, zero); } let rem = blocks.into_remainder(); let new_pos = if rem.is_empty() { W::from_usize(N) } else { generate_block(buf); read_bytes::<W, N>(buf, rem, zero) }; buf[0] = new_pos; } /// Reads bytes from `&block[pos..new_pos]` to `dst` using little endian byte order /// ignoring the tail bytes if necessary and returns `new_pos`. /// /// This function is written in a way which helps the compiler to compile it down /// to one `memcpy`. The temporary buffer gets eliminated by the compiler, see: /// https://rust.godbolt.org/z/xbo88cbsn #[inline] fn read_bytes<W: Word, const N: usize>(block: &[W; N], dst: &mut [u8], pos: W) -> W { let word_size = size_of::<W>(); let pos = pos.into_usize(); assert!(size_of_val(&block[pos..]) >= size_of_val(dst)); // TODO: replace with `[0u8; { size_of::<W>() * N }]` on // stabilization of `generic_const_exprs` let mut buf = [W::from_usize(0); N]; // SAFETY: it's safe to reference `[u32/u64; N]` as `&mut [u8]` // with length equal to `size_of::<u32/u64>() * N` let buf: &mut [u8] = unsafe { let p: *mut u8 = buf.as_mut_ptr().cast(); let len = word_size * N; core::slice::from_raw_parts_mut(p, len) }; for (src, dst) in block.iter().zip(buf.chunks_exact_mut(word_size)) { let val = src.to_le_bytes(); dst.copy_from_slice(val.as_ref()) } let offset = pos * word_size; dst.copy_from_slice(&buf[offset..][..dst.len()]); let read_words = dst.len().div_ceil(word_size); W::from_usize(pos + read_words) } /// Sealed trait implemented for `u32` and `u64`. trait Word: crate::sealed::Sealed {} impl Word for u32 {} impl Word for u64 {} mod sealed { /// Sealed trait implemented for `u32` and `u64`. pub trait Sealed: Default + Copy + TryFrom<usize> { type Bytes: Sized + AsRef<[u8]> + for<'a> TryFrom<&'a [u8]>; fn from_usize(val: usize) -> Self; fn into_usize(self) -> usize; fn to_le_bytes(self) -> Self::Bytes; fn from_le_bytes(bytes: Self::Bytes) -> Self; fn increment(&mut self); } impl Sealed for u32 { type Bytes = [u8; 4]; #[inline(always)] fn from_usize(val: usize) -> Self { val.try_into().unwrap() } #[inline(always)] fn into_usize(self) -> usize { self.try_into().unwrap() } #[inline(always)] fn to_le_bytes(self) -> Self::Bytes { u32::to_le_bytes(self) } #[inline(always)] fn from_le_bytes(bytes: Self::Bytes) -> Self { u32::from_le_bytes(bytes) } #[inline(always)] fn increment(&mut self) { *self += 1; } } impl Sealed for u64 { type Bytes = [u8; 8]; #[inline(always)] fn from_usize(val: usize) -> Self { val.try_into().unwrap() } #[inline(always)] fn into_usize(self) -> usize { self.try_into().unwrap() } #[inline(always)] fn to_le_bytes(self) -> Self::Bytes { u64::to_le_bytes(self) } #[inline(always)] fn from_le_bytes(bytes: Self::Bytes) -> Self { u64::from_le_bytes(bytes) } #[inline(always)] fn increment(&mut self) { *self += 1; } } }
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