1414//
1515
1616#if canImport( FoundationEssentials )
17- import FoundationEssentials
17+ import FoundationEssentials
1818#else
19- import Foundation
19+ import Foundation
2020#endif
2121
2222struct UInt128 : Equatable , ExpressibleByIntegerLiteral {
@@ -31,9 +31,9 @@ struct UInt128: Equatable, ExpressibleByIntegerLiteral {
3131 init ( _ raw : Array < UInt8 > ) {
3232 precondition ( raw . count >= 16 , " UInt128 requires at least 16 bytes " )
3333 let a = UInt64 ( raw [ 0 ] ) << 56 | UInt64 ( raw [ 1 ] ) << 48 | UInt64 ( raw [ 2 ] ) << 40 | UInt64 ( raw [ 3 ] ) << 32 |
34- UInt64 ( raw [ 4 ] ) << 24 | UInt64 ( raw [ 5 ] ) << 16 | UInt64 ( raw [ 6 ] ) << 8 | UInt64 ( raw [ 7 ] )
34+ UInt64 ( raw [ 4 ] ) << 24 | UInt64 ( raw [ 5 ] ) << 16 | UInt64 ( raw [ 6 ] ) << 8 | UInt64 ( raw [ 7 ] )
3535 let b = UInt64 ( raw [ 8 ] ) << 56 | UInt64 ( raw [ 9 ] ) << 48 | UInt64 ( raw [ 10 ] ) << 40 | UInt64 ( raw [ 11 ] ) << 32 |
36- UInt64 ( raw [ 12 ] ) << 24 | UInt64 ( raw [ 13 ] ) << 16 | UInt64 ( raw [ 14 ] ) << 8 | UInt64 ( raw [ 15 ] )
36+ UInt64 ( raw [ 12 ] ) << 24 | UInt64 ( raw [ 13 ] ) << 16 | UInt64 ( raw [ 14 ] ) << 8 | UInt64 ( raw [ 15 ] )
3737 self . init ( ( a , b ) )
3838 }
3939
@@ -76,13 +76,12 @@ struct UInt128: Equatable, ExpressibleByIntegerLiteral {
7676 }
7777
7878 static func >> ( value : UInt128 , by : Int ) -> UInt128 {
79- var result = value
80- for _ in 0 ..< by {
81- let a = result . i . a >> 1
82- let b = result . i . b >> 1 + ( ( result . i . a & 1 ) << 63 )
83- result = UInt128 ( ( a , b ) )
84- }
85- return result
79+ guard by > 0 else { return value }
80+ guard by < 128 else { return UInt128 ( a : 0 , b : 0 ) }
81+ guard by < 64 else { return UInt128 ( a : 0 , b : value . i . a >> ( by - 64 ) ) }
82+ let a = value . i . a >> by
83+ let b = ( value . i . b >> by ) | ( value . i . a << ( 64 - by ) )
84+ return UInt128 ( a : a , b : b )
8685 }
8786
8887 // Equatable.
0 commit comments