|
@@ -53,83 +53,3 @@ func arrayOfBytes<T: Integer>(value: T, length totalBytes: Int = MemoryLayout<T>
|
|
|
|
|
|
return bytes
|
|
return bytes
|
|
}
|
|
}
|
|
-
|
|
|
|
-// MARK: - shiftLeft
|
|
|
|
-
|
|
|
|
-// helper to be able to make shift operation on T
|
|
|
|
-@_specialize(Int)
|
|
|
|
-func << <T: SignedInteger>(lhs: T, rhs: Int) -> Int {
|
|
|
|
- let a = lhs as! Int
|
|
|
|
- let b = rhs
|
|
|
|
- return a << b
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-@_specialize(UInt)
|
|
|
|
-func << <T: UnsignedInteger>(lhs: T, rhs: Int) -> UInt {
|
|
|
|
- let a = lhs as! UInt
|
|
|
|
- let b = rhs
|
|
|
|
- return a << b
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// Generic function itself
|
|
|
|
-// FIXME: this generic function is not as generic as I would. It crashes for smaller types
|
|
|
|
-@_specialize(Int)
|
|
|
|
-func shiftLeft<T: SignedInteger>(_ value: T, by count: Int) -> T where T: Initiable {
|
|
|
|
- if (value == 0) {
|
|
|
|
- return 0
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- let bitsCount = (MemoryLayout<T>.size * 8)
|
|
|
|
- let shiftCount = Int(Swift.min(count, bitsCount - 1))
|
|
|
|
-
|
|
|
|
- var shiftedValue: T = 0
|
|
|
|
- for bitIdx in 0 ..< bitsCount {
|
|
|
|
- let bit = T(IntMax(1 << bitIdx))
|
|
|
|
- if ((value & bit) == bit) {
|
|
|
|
- shiftedValue = shiftedValue | T(bit << shiftCount)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (shiftedValue != 0 && count >= bitsCount) {
|
|
|
|
- // clear last bit that couldn't be shifted out of range
|
|
|
|
- shiftedValue = shiftedValue & T(~(1 << (bitsCount - 1)))
|
|
|
|
- }
|
|
|
|
- return shiftedValue
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// for any f*** other Integer type - this part is so non-Generic
|
|
|
|
-func shiftLeft(_ value: UInt, by count: Int) -> UInt {
|
|
|
|
- return UInt(shiftLeft(Int(value), by: count))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func shiftLeft(_ value: UInt8, by count: Int) -> UInt8 {
|
|
|
|
- return UInt8(shiftLeft(UInt(value), by: count))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func shiftLeft(_ value: UInt16, by count: Int) -> UInt16 {
|
|
|
|
- return UInt16(shiftLeft(UInt(value), by: count))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func shiftLeft(_ value: UInt32, by count: Int) -> UInt32 {
|
|
|
|
- return UInt32(shiftLeft(UInt(value), by: count))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func shiftLeft(_ value: UInt64, by count: Int) -> UInt64 {
|
|
|
|
- return UInt64(shiftLeft(UInt(value), by: count))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func shiftLeft(_ value: Int8, by count: Int) -> Int8 {
|
|
|
|
- return Int8(shiftLeft(Int(value), by: count))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func shiftLeft(_ value: Int16, by count: Int) -> Int16 {
|
|
|
|
- return Int16(shiftLeft(Int(value), by: count))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func shiftLeft(_ value: Int32, by count: Int) -> Int32 {
|
|
|
|
- return Int32(shiftLeft(Int(value), by: count))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func shiftLeft(_ value: Int64, by count: Int) -> Int64 {
|
|
|
|
- return Int64(shiftLeft(Int(value), by: count))
|
|
|
|
-}
|
|
|