Skip to content

Commit

Permalink
[StdlibIntKit] Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Jun 22, 2024
1 parent 20ea5c5 commit 73e6f22
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
12 changes: 7 additions & 5 deletions Sources/StdlibIntKit/StdlibInt+Bitwise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ extension StdlibInt {
// MARK: Transformations
//=------------------------------------------------------------------------=

@inlinable public static prefix func ~(instance: Self) -> Self {
@inlinable public static prefix func ~(instance: consuming Self) -> Self {
Self(~instance.base)
}

@inlinable public static func ^=(lhs: inout Self, rhs: Self) {
@inlinable public static func ^=(lhs: inout Self, rhs: borrowing Self) {
lhs.base ^= rhs.base
}

@inlinable public static func |=(lhs: inout Self, rhs: Self) {
@inlinable public static func |=(lhs: inout Self, rhs: borrowing Self) {
lhs.base |= rhs.base
}

@inlinable public static func &=(lhs: inout Self, rhs: Self) {
@inlinable public static func &=(lhs: inout Self, rhs: borrowing Self) {
lhs.base &= rhs.base
}

Expand All @@ -41,6 +41,8 @@ extension StdlibInt {
//=------------------------------------------------------------------------=

@inlinable public var magnitude: Self {
Self(Base(raw: self.base.magnitude()))
consuming get {
Self(Base(raw: self.base.magnitude()))
}
}
}
18 changes: 14 additions & 4 deletions Sources/StdlibIntKit/StdlibInt+Count.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ extension StdlibInt {
// MARK: Utilities
//=------------------------------------------------------------------------=

@inlinable public var bitWidth: Int {
@inlinable public var bitWidth: Swift.Int {
borrowing get {
Int(self.base.withUnsafeBinaryIntegerElements({ $0.entropy() }))
self.base.withUnsafeBinaryIntegerElements {
Swift.Int($0.entropy())
}
}
}

@inlinable public var trailingZeroBitCount: Int {
@inlinable public var trailingZeroBitCount: Swift.Int {
borrowing get {
Int(self.base.withUnsafeBinaryIntegerElements({ $0.isZero ? 1 : $0.body.ascending(0) }))
self.base.withUnsafeBinaryIntegerElements {
Swift.assert($0.isNormal, "InfiniInt<IX>/elements")

if $0.body.count.isZero {
return Swift.Int(IX($0.appendix.toggled()))
} else {
return Swift.Int($0.body.ascending(000000))
}
}
}
}
}
4 changes: 2 additions & 2 deletions Sources/StdlibIntKit/StdlibInt+Validation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ extension StdlibInt {
self.init(truncatingIfNeeded: source)
}

@inlinable public init(truncatingIfNeeded source: some Swift.BinaryInteger) {
let appendix = Bit(source < .zero)
@inlinable public init<T>(truncatingIfNeeded source: T) where T: Swift.BinaryInteger {
let appendix = Bit(T.isSigned && source < T.zero)
let body = source.words.lazy.map(UX.init(raw:))
self.init(Base(consume body, repeating: appendix))
}
Expand Down
23 changes: 13 additions & 10 deletions Tests/StdlibIntKitTests/StdlibInt+Bitwise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ extension StdlibIntTests {
//=------------------------------------------------------------------------=

func testBitwiseNot() {
func check(_ test: Test, _ instance: T, _ expectation: T) {
test.same(~instance, expectation)
test.same(~expectation, instance)
let values: [T] = [
0x00000000000000000000000000000000,
0x00000000000000000000000000000001,
0x0f0e0d0c0b0a09080706050403020100,
0xf0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,
0xfffffffffffffffffffffffffffffffe,
0xffffffffffffffffffffffffffffffff,
]

for value in values {
let expectation: T = -1 - value
Test().same(~value, expectation)
Test().same(~expectation, value)
}

check(Test(), 0x00000000000000000000000000000000 as T, -0x00000000000000000000000000000001 as T)
check(Test(), 0x00000000000000000000000000000001 as T, -0x00000000000000000000000000000002 as T)
check(Test(), 0x0f0e0d0c0b0a09080706050403020100 as T, -0x0f0e0d0c0b0a09080706050403020101 as T)
check(Test(), 0xf0f1f2f3f4f5f6f7f8f9fafbfcfdfeff as T, -0xf0f1f2f3f4f5f6f7f8f9fafbfcfdff00 as T)
check(Test(), 0xfffffffffffffffffffffffffffffffe as T, -0xffffffffffffffffffffffffffffffff as T)
check(Test(), 0xffffffffffffffffffffffffffffffff as T, -0x0100000000000000000000000000000000 as T)
}

func testBitwiseAnd() {
Expand Down

0 comments on commit 73e6f22

Please sign in to comment.