Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
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
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
swift 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
x86-64 swiftc 3.1.1
x86-64 swiftc 4.0.2
x86-64 swiftc 4.0.3
x86-64 swiftc 4.1
x86-64 swiftc 4.1.1
x86-64 swiftc 4.1.2
x86-64 swiftc 4.2
x86-64 swiftc 5.0
x86-64 swiftc 5.1
x86-64 swiftc 5.10
x86-64 swiftc 5.2
x86-64 swiftc 5.3
x86-64 swiftc 5.4
x86-64 swiftc 5.5
x86-64 swiftc 5.6
x86-64 swiftc 5.7
x86-64 swiftc 5.8
x86-64 swiftc 5.9
x86-64 swiftc nightly
Options
Source code
// fills a buffer with random bytes, the bytes are reproducible if // the provided generator is reproducible. func fill(_ buffer: UnsafeMutableRawBufferPointer, using rng: inout some RandomNumberGenerator) { guard var base = buffer.baseAddress else { return } var count = buffer.count while count > 0 { let stride = min(count, MemoryLayout<UInt64>.size) withUnsafeBytes(of: rng.next()) { let bytes = $0.baseAddress.unsafelyUnwrapped base.copyMemory(from: bytes, byteCount: stride) } (base, count) = (base + stride, count &- stride) } } struct SplitMix64: RandomNumberGenerator { var state: UInt64 init(seed: UInt64) { state = seed } init(from rng: inout some RandomNumberGenerator) { state = rng.next() } mutating func next() -> UInt64 { state &+= 0x9e37_79b9_7f4a_7c15 var z = state z = (z ^ (z >> 30)) &* 0xbf58_476d_1ce4_e5b9 z = (z ^ (z >> 27)) &* 0x94d0_49bb_1331_11eb return z ^ (z >> 31) } } struct BigEndianRNG<T: RandomNumberGenerator>: RandomNumberGenerator { var wrapped: T init(_ wrapped: T) { self.wrapped = wrapped } mutating func next() -> UInt64 { wrapped.next().bigEndian } } struct LittleEndianRNG<T: RandomNumberGenerator>: RandomNumberGenerator { var wrapped: T init(_ wrapped: T) { self.wrapped = wrapped } mutating func next() -> UInt64 { wrapped.next().littleEndian } } func test(_ rng: consuming some RandomNumberGenerator) { var ne = copy rng var le = LittleEndianRNG(copy rng) var be = BigEndianRNG(rng) // bytes as if generated on a little-endian machine let leBytes = [UInt8](unsafeUninitializedCapacity: 7) { buff, initializedCount in fill(UnsafeMutableRawBufferPointer(buff), using: &le) initializedCount = buff.count } // bytes as if generated on a big-endian machine let beBytes = [UInt8](unsafeUninitializedCapacity: 7) { buff, initializedCount in fill(UnsafeMutableRawBufferPointer(buff), using: &be) initializedCount = buff.count } let neBytes = [UInt8](unsafeUninitializedCapacity: 7) { buff, initializedCount in fill(UnsafeMutableRawBufferPointer(buff), using: &ne) initializedCount = buff.count } print("Little endian: \(leBytes)") print("Big endian: \(beBytes)") print("Native endian: \(neBytes)") print("Bytes are the same: \(Set(leBytes) == Set(beBytes) && Set(leBytes) == Set(neBytes))") } do { let seed: UInt64 = 0x4DE12D496011118A print("Seed: 0x\(String(seed, radix: 16, uppercase: true))") test(SplitMix64(seed: seed)) }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree