Contents

dankogai/swift-int2x

Create Double-Width Integers with Ease

Synopsis

import Int2X

typealias U128 = UInt2X<UInt64> // Yes.  That's it!
typealias I128 = Int2X<UInt64>  // ditto for signed integers

Description

Thanks to [SE-0104], making your own integer types is easier than ever. This module makes use of it -- creating double-width integer from any given [FixedWidthInteger].

[SE-0104]: https://github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md [FixedWidthInteger]: https://developer.apple.com/documentation/swift/fixedwidthinteger

U?Int{128,256,512,1024} are predefined as follows:

#if swift(<6.0) // Swift 6.0 started supportings UInt128 natively
public typealias UInt128    = UInt2X<UInt64>
#endif
public typealias UInt256    = UInt2X<UInt128>
public typealias UInt512    = UInt2X<UInt256>
public typealias UInt1024   = UInt2X<UInt512>
#if swift(<6.0) // Swift 6.0 started supportings Int128 natively
public typealias Int128    = Int2X<UInt64>
#endif
public typealias Int256    = Int2X<UInt128>
public typealias Int512    = Int2X<UInt256>
public typealias Int1024   = Int2X<UInt512>

As you see, UInt2X and Int2X themselves are [FixedWidthInteger] so you can stack them up.

Usage

build

$ git clone https://github.com/dankogai/swift-int2x.git
$ cd swift-int2x # the following assumes your $PWD is here
$ swift build

test

$ swift test

REPL

$ scripts/run-repl.sh

or

$ swift run --repl

and in your repl,

Welcome to Apple Swift version 6.3 (swiftlang-6.3.3.1.3 clang-2100.1.1.101). Type :help for assistance.
  1> import Int2X 
  2> Int1024.max.description
$R0: String = "89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068607"

Playgrounds

The repository ships with macOS.playground, a guided tour in eight pages -- from the basic API to Mersenne primes:

  • Synopsis -- the elevator pitch, in running code
  • UInt2X -- anatomy and API of the unsigned type, magnified via UInt2X<UInt8>
  • Int2X -- its signed sibling, including known quirks
  • Factorial -- how far each width goes; where UInt1024 and Double both give up
  • Fibonacci -- fib(1476), the largest that fits Double and UInt1024 -- exactly
  • Mersenne -- Lucas–Lehmer primality test powered by UInt256
  • Accelerate -- toggling the macOS-only [Accelerate] support, with a micro-benchmark
  • Scratch -- a blank page to play in

[Accelerate]: https://developer.apple.com/documentation/accelerate

To use it, open the package in Xcode:

$ open -a Xcode .

build the scheme (⌘B), then select macOS.playground in the navigator and run any page.

From Your SwiftPM-Managed Projects

Add the following to the dependencies section:

.package(
  url: "https://github.com/dankogai/swift-int2x.git", from: "0.4.1"
)

and the following to the .target argument:

.target(
  name: "YourSwiftyPackage",
  dependencies: ["Int2X"])

Now all you have to do is:

import Int2X

in your code. Enjoy!

Prerequisite

Swift 6 or better, macOS or Linux to build. Xcode on macOS for the playgrounds.

Package Metadata

Repository: dankogai/swift-int2x

Default branch: main

README: README.md