Skip to content

Commit

Permalink
[NBKCoreKit] Some sign stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Nov 4, 2023
1 parent 56acf68 commit f4d63f8
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 3 deletions.
116 changes: 113 additions & 3 deletions Sources/NBKCoreKit/Private/NBK+Sign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,118 @@ extension NBK {
// MARK: Utilities
//=------------------------------------------------------------------------=

/// Returns `bit` as a `plus` or `minus` sign.
@inlinable public static func sign(_ bit: Bool) -> FloatingPointSign {
bit ? .minus : .plus
/// Returns the `sign`'s ascii representation.
///
/// ```
/// ┌────────── → ─────────────┐
/// │ sign │ ascii │
/// ├────── = ─ → ────── = ────┤
/// │ plus │ 0 │ 43 │ "+" │
/// │ minus │ 1 │ 45 │ "-" │
/// └────── = ─ → ────── = ────┘
/// ```
///
@inlinable public static func ascii(_ sign: Sign) -> UInt8 {
UInt8(ascii: sign == Sign.plus ? "+" : "-")
}

/// Returns the `sign`'s bit representation.
///
/// ```
/// ┌────────── → ──────┐
/// │ sign │ bit │
/// ├────── = ─ → ──────┤
/// │ plus │ 0 │ false │
/// │ minus │ 1 │ true │
/// └────── = ─ → ──────┘
/// ```
///
@inlinable public static func bit(_ sign: Sign) -> Bool {
sign == Sign.plus ? false : true
}

/// Returns the `bit`'s sign representation.
///
/// ```
/// ┌────── → ──────────┐
/// │ bit │ sign │
/// ├────── → ───── = ──┤
/// │ false │ plus │ 0 │
/// │ true │ minus │ 1 │
/// └────── → ───── = ──┘
/// ```
///
@inlinable public static func sign(_ bit: Bool) -> Sign {
bit ? Sign.minus : Sign.plus
}

//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=

/// Returns the opposite `sign`.
///
/// ```
/// ┌────────── → ──────────┐
/// │ sign │ NOT sign │
/// ├────── = ─ → ───── = ──┤
/// │ plus │ 0 │ minus │ 1 │
/// │ minus │ 1 │ plus │ 0 │
/// └────── = ─ → ───── = ──┘
/// ```
///
@inlinable public static func not(_ sign: Sign) -> Sign {
self.xor(sign, Sign.minus)
}

/// Returns the logical `AND` of `lhs` and `rhs`.
///
/// ```
/// ┌────────────────────── → ────────────┐
/// │ lhs │ rhs │ lhs AND rhs │
/// ├────── = ──┼────── = ─ → ───── = ────┤
/// │ plus │ 0 │ plus │ 0 │ plus │ 0 │
/// │ plus │ 0 │ minus │ 1 │ plus │ 0 │
/// │ minus │ 1 │ plus │ 0 │ plus │ 0 │
/// │ minus │ 1 │ minus │ 1 │ minus │ 1 │
/// └────── = ─ → ───── = ─ → ───── = ────┘
/// ```
///
@inlinable public static func and(_ lhs: Sign, _ rhs: Sign) -> Sign {
lhs == rhs ? lhs : Sign.plus
}

/// Returns the logical `OR` of `lhs` and `rhs`.
///
/// ```
/// ┌────────────────────── → ────────────┐
/// │ lhs │ rhs │ lhs OR rhs │
/// ├────── = ──┼────── = ─ → ───── = ────┤
/// │ plus │ 0 │ plus │ 0 │ plus │ 0 │
/// │ plus │ 0 │ minus │ 1 │ minus │ 1 │
/// │ minus │ 1 │ plus │ 0 │ minus │ 1 │
/// │ minus │ 1 │ minus │ 1 │ minus │ 1 │
/// └────── = ─ → ───── = ─ → ───── = ────┘
/// ```
///
@inlinable public static func or(_ lhs: Sign, _ rhs: Sign) -> Sign {
lhs == Sign.plus ? rhs : lhs
}

/// Returns the logical `XOR` of `lhs` and `rhs`.
///
/// ```
/// ┌────────────────────── → ────────────┐
/// │ lhs │ rhs │ lhs XOR rhs │
/// ├────── = ──┼────── = ─ → ───── = ────┤
/// │ plus │ 0 │ plus │ 0 │ plus │ 0 │
/// │ plus │ 0 │ minus │ 1 │ minus │ 1 │
/// │ minus │ 1 │ plus │ 0 │ minus │ 1 │
/// │ minus │ 1 │ minus │ 1 │ plus │ 0 │
/// └────── = ─ → ───── = ─ → ───── = ────┘
/// ```
///
@inlinable public static func xor(_ lhs: Sign, _ rhs: Sign) -> Sign {
lhs == rhs ? Sign.plus : Sign.minus
}
}
123 changes: 123 additions & 0 deletions Tests/NBKCoreKitBenchmarks/Private/NBK+Sign.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//=----------------------------------------------------------------------------=
// 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.
//=----------------------------------------------------------------------------=

#if !DEBUG

import NBKCoreKit
import XCTest

//*============================================================================*
// MARK: * NBK x Sign
//*============================================================================*

final class NBKBenchmarksOnSign: XCTestCase {

typealias T = FloatingPointSign

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

func testASCII() {
var abc = NBK.blackHoleIdentity(T.plus )
var xyz = NBK.blackHoleIdentity(T.minus)

for _ in 0 ..< 5_000_000 {
NBK.blackHole(NBK.ascii(abc))
NBK.blackHole(NBK.ascii(xyz))

NBK.blackHoleInoutIdentity(&abc)
NBK.blackHoleInoutIdentity(&xyz)
}
}

func testBit() {
var abc = NBK.blackHoleIdentity(T.plus )
var xyz = NBK.blackHoleIdentity(T.minus)

for _ in 0 ..< 5_000_000 {
NBK.blackHole(NBK.bit(abc))
NBK.blackHole(NBK.bit(xyz))

NBK.blackHoleInoutIdentity(&abc)
NBK.blackHoleInoutIdentity(&xyz)
}
}

func testSign() {
var abc = NBK.blackHoleIdentity(true )
var xyz = NBK.blackHoleIdentity(false)

for _ in 0 ..< 5_000_000 {
NBK.blackHole(NBK.sign(abc))
NBK.blackHole(NBK.sign(xyz))

NBK.blackHoleInoutIdentity(&abc)
NBK.blackHoleInoutIdentity(&xyz)
}
}

//=------------------------------------------------------------------------=
// MARK: Tests x Transformations
//=------------------------------------------------------------------------=

func testNot() {
var abc = NBK.blackHoleIdentity(T.plus )
var xyz = NBK.blackHoleIdentity(T.minus)

for _ in 0 ..< 5_000_000 {
NBK.blackHole(NBK.not(abc))
NBK.blackHole(NBK.not(xyz))

NBK.blackHoleInoutIdentity(&abc)
NBK.blackHoleInoutIdentity(&xyz)
}
}

func testAnd() {
var lhs = NBK.blackHoleIdentity(T.plus )
var rhs = NBK.blackHoleIdentity(T.minus)

for _ in 0 ..< 5_000_000 {
NBK.blackHole(NBK.and(lhs, rhs))
NBK.blackHole(NBK.and(rhs, lhs))

NBK.blackHoleInoutIdentity(&lhs)
NBK.blackHoleInoutIdentity(&rhs)
}
}

func testOr() {
var lhs = NBK.blackHoleIdentity(T.plus )
var rhs = NBK.blackHoleIdentity(T.minus)

for _ in 0 ..< 5_000_000 {
NBK.blackHole(NBK.or(lhs, rhs))
NBK.blackHole(NBK.or(rhs, lhs))

NBK.blackHoleInoutIdentity(&lhs)
NBK.blackHoleInoutIdentity(&rhs)
}
}

func testXor() {
var lhs = NBK.blackHoleIdentity(T.plus )
var rhs = NBK.blackHoleIdentity(T.minus)

for _ in 0 ..< 5_000_000 {
NBK.blackHole(NBK.xor(lhs, rhs))
NBK.blackHole(NBK.xor(rhs, lhs))

NBK.blackHoleInoutIdentity(&lhs)
NBK.blackHoleInoutIdentity(&rhs)
}
}
}

#endif
73 changes: 73 additions & 0 deletions Tests/NBKCoreKitTests/Private/NBK+Sign.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//=----------------------------------------------------------------------------=
// 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.
//=----------------------------------------------------------------------------=

#if DEBUG

import NBKCoreKit
import XCTest

//*============================================================================*
// MARK: * NBK x Sign
//*============================================================================*

final class NBKTestsOnSign: XCTestCase {

typealias T = FloatingPointSign

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

func testASCII() {
XCTAssertEqual(NBK.ascii(T.plus ), UInt8(ascii: "+"))
XCTAssertEqual(NBK.ascii(T.minus), UInt8(ascii: "-"))
}

func testBit() {
XCTAssertEqual(NBK.bit(T.plus ), false)
XCTAssertEqual(NBK.bit(T.minus), true )
}

func testSign() {
XCTAssertEqual(NBK.sign(false), T.plus )
XCTAssertEqual(NBK.sign(true ), T.minus)
}

//=------------------------------------------------------------------------=
// MARK: Tests x Transformations
//=------------------------------------------------------------------------=

func testNot() {
XCTAssertEqual(NBK.not(T.plus ), T.minus)
XCTAssertEqual(NBK.not(T.minus), T.plus )
}

func testAnd() {
XCTAssertEqual(NBK.and(T.plus, T.plus ), T.plus )
XCTAssertEqual(NBK.and(T.plus, T.minus), T.plus )
XCTAssertEqual(NBK.and(T.minus, T.plus ), T.plus )
XCTAssertEqual(NBK.and(T.minus, T.minus), T.minus)
}

func testOr() {
XCTAssertEqual(NBK.or (T.plus, T.plus ), T.plus )
XCTAssertEqual(NBK.or (T.plus, T.minus), T.minus)
XCTAssertEqual(NBK.or (T.minus, T.plus ), T.minus)
XCTAssertEqual(NBK.or (T.minus, T.minus), T.minus)
}

func testXor() {
XCTAssertEqual(NBK.xor(T.plus, T.plus ), T.plus )
XCTAssertEqual(NBK.xor(T.plus, T.minus), T.minus)
XCTAssertEqual(NBK.xor(T.minus, T.plus ), T.minus)
XCTAssertEqual(NBK.xor(T.minus, T.minus), T.plus )
}
}

#endif

0 comments on commit f4d63f8

Please sign in to comment.