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
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
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
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
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
aarch64 swiftc 6.0.3
aarch64 swiftc 6.1
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 6.0.3
x86-64 swiftc 6.1
x86-64 swiftc devsnapshot
x86-64 swiftc nightly
Options
Source code
// MARK: Bespoke from Int to Int func bespokeIntInt(n: Int, a: Int) -> Int { n.distance(to: a) } // MARK: Before func oldInt64Int64(n: Int64, a: Int64) -> Int64 { n.distance1(to: a) } func oldInt64Int32(n: Int64, a: Int64) -> Int32 { n.distance1(to: a) } func oldInt32Int64(n: Int32, a: Int32) -> Int64 { n.distance1(to: a) } func oldInt32Int32(n: Int32, a: Int32) -> Int32 { n.distance1(to: a) } func oldUInt64Int64(n: UInt64, a: UInt64) -> Int64 { n.distance1(to: a) } func oldUInt64Int32(n: UInt64, a: UInt64) -> Int32 { n.distance1(to: a) } func oldUInt32Int64(n: UInt32, a: UInt32) -> Int64 { n.distance1(to: a) } func oldUInt32Int32(n: UInt32, a: UInt32) -> Int32 { n.distance1(to: a) } // MARK: After func newInt64Int64(n: Int64, a: Int64) -> Int64 { n.distance2(to: a) } func newInt64Int32(n: Int64, a: Int64) -> Int32 { n.distance2(to: a) } func newInt32Int64(n: Int32, a: Int32) -> Int64 { n.distance2(to: a) } func newInt32Int32(n: Int32, a: Int32) -> Int32 { n.distance2(to: a) } func newUInt64Int64(n: UInt64, a: UInt64) -> Int64 { n.distance2(to: a) } func newUInt64Int32(n: UInt64, a: UInt64) -> Int32 { n.distance2(to: a) } func newUInt32Int64(n: UInt32, a: UInt32) -> Int64 { n.distance2(to: a) } func newUInt32Int32(n: UInt32, a: UInt32) -> Int32 { n.distance2(to: a) } // MARK: Helpers @inlinable public func _preconditionFailure( _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line ) -> Never { preconditionFailure(message(), file: file, line: line) } // MARK: Generic extension BinaryInteger { public func distance1<T>(to other: Self) -> T where T: SignedInteger & FixedWidthInteger { typealias Int = T // this enables control-c control-v if !Self.isSigned { if self > other { if let result = Int(exactly: self - other) { return -result } } else { if let result = Int(exactly: other - self) { return result } } } else { let isNegative = self < (0 as Self) if isNegative == (other < (0 as Self)) { if let result = Int(exactly: other - self) { return result } } else { if let result = Int(exactly: self.magnitude + other.magnitude) { return isNegative ? result : -result } } } _preconditionFailure("Distance is not representable in Int") } public func distance2<T>(to other: Self) -> T where T: SignedInteger & FixedWidthInteger { typealias Int = T // this enables control-c control-v if self.bitWidth < Int.bitWidth && other.bitWidth < Int.bitWidth { // Smaller integers always fit. return Int(truncatingIfNeeded: other) &- Int(truncatingIfNeeded: self) } else if Self.isSigned || self <= other { // Use trapping subtraction for performance. if let result = Int(exactly: other - self) { return result } } else { // This type is unsigned and the distance is negative here. let absolute = self - other let distance = ~Int(truncatingIfNeeded: absolute) &+ 1 // The zero comparison generates better code in Swift 5.10. if absolute.bitWidth <= Int.bitWidth { if distance < Int.zero { return distance } } else { if absolute <= Self(truncatingIfNeeded: Int.min.magnitude) { return distance } } } _preconditionFailure("Distance is not representable in Int") } }
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