Skip to content

Commit

Permalink
[NBKCoreKit] Comparisons (#85).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Sep 21, 2023
1 parent 19c6e3d commit 4a37c71
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 256 deletions.
38 changes: 0 additions & 38 deletions Sources/NBKCoreKit/Private/NBK+Limbs+Comparisons.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//=----------------------------------------------------------------------------=
// This source file is part of the Numberick open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

//*============================================================================*
// MARK: * NBK x Strict Signed Integer x Comparisons
//*============================================================================*
//=----------------------------------------------------------------------------=
// MARK: + where Base is Unsafe Buffer Pointer
//=----------------------------------------------------------------------------=

extension NBK.StrictSignedInteger {

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

/// A three-way comparison of `lhs` against `rhs`.
///
/// ### Development
///
/// Specializing this it where `T == UInt` makes it faster.
///
@inlinable public static func compare<T>(_ lhs: Base, to rhs: Base) -> Int where Base == UnsafeBufferPointer<T> {
let lhs = NBK.SuccinctInt(fromStrictSignedInteger: lhs)!
let rhs = NBK.SuccinctInt(fromStrictSignedInteger: rhs)!
return lhs.compared(to: rhs) as Int
}

/// A three-way comparison of `lhs` against `rhs` at `index`.
///
/// ### Development
///
/// Specializing this it where `T == UInt` makes it faster.
///
@inlinable public static func compare<T>(_ lhs: Base, to rhs: Base, at index: Int) -> Int where Base == UnsafeBufferPointer<T> {
let lhs = NBK.SuccinctInt(fromStrictSignedInteger: lhs)!
let rhs = NBK.SuccinctInt(fromStrictSignedInteger: rhs)!
let partition = Swift.min(index, lhs.body.endIndex)
let suffix = Base(rebasing: lhs.body.suffix(from: partition))
let comparison = NBK.SuccinctInt(unchecked: suffix, sign: lhs.sign).compared(to: rhs)
if !comparison.isZero { return comparison }
let prefix = Base(rebasing: lhs.body.prefix(upTo: partition))
return Int(bit: partition == index ? !prefix.allSatisfy({ $0.isZero }) : lhs.sign)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ extension NBK.StrictUnsignedInteger.SubSequence {
let partition = Swift.min(index, lhs.endIndex)
let suffix = Base(rebasing: lhs.suffix(from: partition))
let comparison = self.compare(suffix, to: rhs) as Int
//=--------------------------------------=
if !comparison.isZero { return comparison }
//=--------------------------------------=
let prefix = Base(rebasing: lhs.prefix(upTo: partition))
return Int(bit: !prefix.allSatisfy({ $0.isZero }))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@ private typealias X = [UInt64]
private typealias Y = [UInt32]

//*============================================================================*
// MARK: * NBK x Limbs x Comparisons
// MARK: * NBK x Strict Signed Integer x Comparisons
//*============================================================================*

final class NBKBenchmarksOnLimbsByComparisons: XCTestCase {
final class NBKStrictSignedIntegerBenchmarksOnComparisons: XCTestCase {

typealias T = NBK.StrictSignedInteger<UnsafeBufferPointer<UInt>>

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

func testCompareStrictSignedInteger() {
func testCompare() {
var lhs = NBK.blackHoleIdentity([1, 2, 3, 4] as W)
var rhs = NBK.blackHoleIdentity([1, 2, 3, 4] as W)
var xyz = NBK.blackHoleIdentity((1) as Int)

for _ in 0 ..< 2_500_000 {
lhs.withUnsafeBufferPointer { lhs in
rhs.withUnsafeBufferPointer { rhs in
NBK.blackHole(NBK.compareStrictSignedInteger(lhs, to: rhs))
NBK.blackHole(T.compare(lhs, to: rhs))
}}

NBK.blackHoleInoutIdentity(&lhs)
Expand All @@ -43,15 +45,15 @@ final class NBKBenchmarksOnLimbsByComparisons: XCTestCase {
}
}

func testCompareStrictSignedIntegerAtIndex() {
func testCompareAtIndex() {
var lhs = NBK.blackHoleIdentity([1, 2, 3, 4] as W)
var rhs = NBK.blackHoleIdentity([2, 3, 4, 0] as W)
var xyz = NBK.blackHoleIdentity((1) as Int)

for _ in 0 ..< 2_500_000 {
lhs.withUnsafeBufferPointer { lhs in
rhs.withUnsafeBufferPointer { rhs in
NBK.blackHole(NBK.compareStrictSignedInteger(lhs, to: rhs, at: xyz))
NBK.blackHole(T.compare(lhs, to: rhs, at: xyz))
}}

NBK.blackHoleInoutIdentity(&lhs)
Expand Down
133 changes: 0 additions & 133 deletions Tests/NBKCoreKitTests/Private/NBK+Limbs+Comparisons.swift

This file was deleted.

Loading

0 comments on commit 4a37c71

Please sign in to comment.