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
func even(n: Int, a: Int) -> Int { n.distance(to: a) } func signed1(n: Int64, a: Int64) -> Int32 { n.distance1(to: a) } func signed2(n: Int64, a: Int64) -> Int32 { n.distance2(to: a) } func unsigned1(n: UInt64, a: UInt64) -> Int32 { n.distance1(to: a) } func unsigned2(n: UInt64, a: UInt64) -> Int32 { n.distance2(to: a) } extension FixedWidthInteger { public func distance1(to other: Self) -> Int32 { if !Self.isSigned { if self > other { if let result = Int32(exactly: self - other) { return -result //.................................................(1) crashes on UInt.max.distance(to: UInt.max / 2) } } else { if let result = Int32(exactly: other - self) { return result } } } else { let isNegative = self < (0 as Self) if isNegative == (other < (0 as Self)) { if let result = Int32(exactly: other - self) { return result } } else { if let result = Int32(exactly: self.magnitude + other.magnitude) { return isNegative ? result : -result //...........................(2) crashes on Int.max.distance(to: -1) } } } preconditionFailure("Distance is not representable in Int") } public func distance2(to other: Self) -> Int32 { if Self.isSigned { if self.bitWidth <= Int32.bitWidth && other.bitWidth <= Int32.bitWidth { //.....(2a) constant-foldable bit width comparison return Int32(truncatingIfNeeded: other) - Int32(truncatingIfNeeded: self) //..(2b) checked subtraction } else { return Int32(other - self) //.................................................(2c) checked subtraction and Int32.init(_:) } } else { if self > other { let result: Self = self - other //..............................................................(1a) result is in 0... if result.bitWidth < Int32.bitWidth || result <= Self(truncatingIfNeeded: Int32.min.magnitude) { // (1b) smaller unsigned bit widths always fit return ~Int32(truncatingIfNeeded: result) &+ 1 //...............................................(1c) result is in 0...Int32.min.magnitude } } else { let result: Self = other - self //..............................................................(1d) result is in 0... if result.bitWidth < Int32.bitWidth || result <= Self(truncatingIfNeeded: Int32.max.magnitude) { // (1e) smaller unsigned bit widths always fit return Int32(truncatingIfNeeded: result) //.....................................................(1f) result is in 0...Int32.max.magnitude } } } preconditionFailure("Distance is not representable in Int32") } }
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