What's New

v0.17.0

2023-12-06T13:10:03Z

Added a prime sieve to NBKCoreKit.

GitHub (v0.16.0...v0.17.0)

  • #114 A prime sieve

NBKPrimeSieve

public final class NBKPrimeSieve {

    /// Creates a new instance and sieves the first page.
    public init(cache: Cache = .KiB(32), wheel: Wheel = .x07, culls: Culls = .x11, capacity: Int? = nil)

    /// The highest value checked for primality.
    public var limit: UInt { get }
    
    /// A list of all primes from zero through `limit`.
    public var elements: [UInt] { get }

    /// Sieves the next page of numbers.
    public func increment()
}

NBKPrimeSieve: Changelog

Time (s) to append primes up to 109 on a MacBook Pro, 13-inch, M1, 2020:

  • 3.6 from 7 by skipping even numbers.
  • 2.2 from 3.6 by using [UInt] as a wannabe-bit-set.
  • 1.7 from 2.2 by wheeling 3, 5, 7.
  • 1.5 from 1.7 by using wrapping arithmetic in hotspot.
  • 0.9 from 1.5 by chunking it (wheeling has not been reimplemented yet).
  • 0.8 from 0.9 by reimplementing wheeling in increment().
  • 0.6 from 0.8 with NBKPrimeSieve(cache: .KiB(128), wheel: .x11, culls: .x31).
  • 0.58 from 0.64 by adding a capacity reservation option.

✨ An arithmagick overhaul in Swift.

Package Swift iOS Mac Catalyst macOS tvOS watchOS
0.17.0 5.7 14.0 14.0 11.0 14.0 7.0

Table of Contents

NBKCoreKit (Sources, Tests, Benchmarks)

A new protocol hierarchy that refines Swift's standard library.

Protocols

Models

A composable, large, fixed-width, two's complement, binary integer.

🧩 Composable

NBKDoubleWidth is a generic software model for working with fixed-width integers larger than one machine word. Its bit width is double the bit width of its High component. In this way, you may construct new integer types:

typealias  Int256 = NBKDoubleWidth< Int128>
typealias UInt256 = NBKDoubleWidth<UInt128>

💕 Two's Complement

Like other binary integers, NBKDoubleWidth has two's complement semantics.

The two's complement representation of  0 is an infinite sequence of 0s.
The two's complement representation of -1 is an infinite sequence of 1s.

🏰 Fixed-Width Integer

Each NBKDoubleWidth has a fixed bit width, and so do its halves. This design comes with a suite of overflow and bit-casting operations. The even split also lends itself to divide-and-conquer strategies.

📖 Trivial UInt Collection

NBKDoubleWidth models a trivial UInt collection, where UInt is an unsigned machine word. It contains at least two words, but the exact count depends on the platform's architecture. You should, therefore, use properties like startIndex and endIndex instead of hard-coded indices.

// Int256 and UInt256, as constructed on a 64-bit platform:
┌───────────────────────────┐ ┌───────────────────────────┐
│           Int256          │ │          UInt256          │
├─────────────┬─────────────┤ ├─────────────┬─────────────┤
│    Int128   │   UInt128   │ │   UInt128   │   UInt128   │
├──────┬──────┼──────┬──────┤ ├──────┬──────┼──────┬──────┤
│  Int │ UInt │ UInt │ UInt │ │ UInt │ UInt │ UInt │ UInt │
└──────┴──────┴──────┴──────┘ └──────┴──────┴──────┴──────┘

Swift's type system enforces proper layout insofar as Int and UInt are the only types in the standard library that meet its type requirements. Specifically, only Int and UInt have NBKCoreInteger<UInt> Digit types.

🚀 Single Digit Arithmagick

Alongside its ordinary arithmagick operations, NBKDoubleWidth provides single-digit operations, where a digit is an un/signed machine word. These operations are more efficient for small calculations. Here are some examples:

Int256(1) + Int(1), UInt256(1) + UInt(1)
Int256(2) - Int(2), UInt256(2) - UInt(2)
Int256(3) * Int(3), UInt256(3) * UInt(3)
Int256(4) / Int(4), UInt256(4) / UInt(4)
Int256(5) % Int(5), UInt256(5) % UInt(5)

Note

The Digit type is Int when Self is signed, and UInt otherwise.

⭐️ Feature: Swift.StaticBigInt

StaticBigInt is disabled by default. You enable it in Package.swift.

Note

You can use StaticString until StaticBigInt becomes available.

Numberick contains several modules. Import some or all of them.

Major version zero (0.y.z) is for initial development.

Anything MAY change at any time.

The public API SHOULD NOT be considered stable.

Using SwiftPM

Add this package to your list of package dependencies.

.package(url: "https://github.com/oscbyspro/Numberick.git", .upToNextMinor(from: "0.17.0")),

Choose target dependencies from the products in Package.swift.

.product(name: "Numberick",         package: "Numberick"),
.product(name: "NBKCoreKit",        package: "Numberick"),
.product(name: "NBKDoubleWidthKit", package: "Numberick"),

Using CocoaPods

Choose target dependencies from the pods listed in the root directory.

pod "Numberick",                   "~> 0.17.0"
pod "Numberick-NBKCoreKit",        "~> 0.17.0"
pod "Numberick-NBKDoubleWidthKit", "~> 0.17.0"

This project is inspired by Int128 and DoubleWidth by Apple.

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Mar 30 2024 04:32:14 GMT-0900 (Hawaii-Aleutian Daylight Time)