Skip to content

Commit

Permalink
custom smart bitshifts (#60) (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Apr 18, 2023
1 parent d026af7 commit d4fab1a
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Sources/ANKFullWidthKit/ANKFullWidth+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ extension ANKFullWidth {
// MARK: Transformations
//=------------------------------------------------------------------------=

@inlinable public static func <<=(lhs: inout Self, rhs: some BinaryInteger) {
lhs._bitshiftLeftSmart(by: Int(clamping: rhs))
}

@_transparent public static func <<(lhs: Self, rhs: some BinaryInteger) -> Self {
var lhs = lhs; lhs <<= rhs; return lhs
}

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

@inlinable public static func &<<=(lhs: inout Self, rhs: Self) {
lhs._bitshiftLeft(by: rhs._moduloBitWidth)
}
Expand All @@ -31,6 +43,26 @@ extension ANKFullWidth {
// MARK: Transformations x Int
//=------------------------------------------------------------------------=

/// - Parameters:
/// - amount: `Int.min <= amount <= Int.max`
///
@inlinable mutating func _bitshiftLeftSmart(by amount: Int) {
let amountAbsoluteValue: Int = abs(amount)
switch (amount >= 0, amountAbsoluteValue < Self.bitWidth) {
case (true, true ): self._bitshiftLeft(by: amountAbsoluteValue)
case (true, false): self = Self(repeating: false)
case (false, true ): self._bitshiftRight(by: amountAbsoluteValue)
case (false, false): self = Self(repeating: self.isLessThanZero)
}
}

/// - Parameters:
/// - amount: `Int.min <= amount <= Int.max`
///
@_transparent @usableFromInline func _bitshiftedLeftSmart(by amount: Int) -> Self {
var x = self; x._bitshiftLeftSmart(by: amount); return x
}

/// - Parameters:
/// - amount: `0 <= amount < Self.bitWidth`
///
Expand Down Expand Up @@ -95,6 +127,18 @@ extension ANKFullWidth {
// MARK: Transformations
//=------------------------------------------------------------------------=

@inlinable public static func >>=(lhs: inout Self, rhs: some BinaryInteger) {
lhs._bitshiftRightSmart(by: Int(clamping: rhs))
}

@_transparent public static func >>(lhs: Self, rhs: some BinaryInteger) -> Self {
var lhs = lhs; lhs >>= rhs; return lhs
}

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

@inlinable public static func &>>=(lhs: inout Self, rhs: Self) {
lhs._bitshiftRight(by: rhs._moduloBitWidth)
}
Expand All @@ -107,6 +151,26 @@ extension ANKFullWidth {
// MARK: Transformations x Int
//=------------------------------------------------------------------------=

/// - Parameters:
/// - amount: `Int.min <= amount <= Int.max`
///
@inlinable mutating func _bitshiftRightSmart(by amount: Int) {
let amountAbsoluteValue: Int = abs(amount)
switch (amount >= 0, amountAbsoluteValue < Self.bitWidth) {
case (true, true ): self._bitshiftRight(by: amountAbsoluteValue)
case (true, false): self = Self(repeating: self.isLessThanZero)
case (false, true ): self._bitshiftLeft(by: amountAbsoluteValue)
case (false, false): self = Self(repeating: false)
}
}

/// - Parameters:
/// - amount: `Int.min <= amount <= Int.max`
///
@_transparent @usableFromInline func _bitshiftedRightSmart(by amount: Int) -> Self {
var x = self; x._bitshiftRightSmart(by: amount); return x
}

/// - Parameters:
/// - amount: `0 <= amount < Self.bitWidth`
///
Expand Down

0 comments on commit d4fab1a

Please sign in to comment.