@@ -2027,7 +2027,7 @@ ${overflowOperationComment(x.operator)}
2027
2027
///
2028
2028
/// Use this method to calculate the full result of a product that would
2029
2029
/// otherwise overflow. Unlike traditional truncating multiplication, the
2030
- /// `multipliedFullWidth(by:)` method returns an instance of DoubleWith<Self> ,
2030
+ /// `multipliedFullWidth(by:)` method returns a tuple ,
2031
2031
/// containing both the `high` and `low` parts of the product of `self` and
2032
2032
/// `other`. The following example uses this method to multiply two `UInt8`
2033
2033
/// values that normally overflow when multiplied:
@@ -2063,7 +2063,7 @@ ${overflowOperationComment(x.operator)}
2063
2063
/// represent in the type, a runtime error may occur.
2064
2064
///
2065
2065
/// - Parameters:
2066
- /// - dividend: A DoubleWidth<Self> value containing the high and low parts
2066
+ /// - dividend: A tuple value containing the high and low parts
2067
2067
/// of a double-width integer. The `high` component of the value carries
2068
2068
/// the sign, if the type is signed.
2069
2069
/// - Returns: A tuple containing the quotient and remainder of `dividend`
@@ -3168,290 +3168,6 @@ public func numericCast<T : BinaryInteger, U : BinaryInteger>(_ x: T) -> U {
3168
3168
}
3169
3169
3170
3170
3171
- //===----------------------------------------------------------------------===//
3172
- //===--- DoubleWidth ------------------------------------------------------===//
3173
- //===----------------------------------------------------------------------===//
3174
-
3175
- public struct DoubleWidth< T : FixedWidthInteger>
3176
- : FixedWidthInteger, _ExpressibleByBuiltinIntegerLiteral
3177
- where
3178
- T . Magnitude : FixedWidthInteger,
3179
- T. Magnitude. Magnitude == T . Magnitude {
3180
-
3181
- public typealias High = T
3182
- public typealias Low = T . Magnitude
3183
-
3184
- internal var _storage : ( high: T , low: T . Magnitude )
3185
-
3186
- internal init( _ _value: ( High, Low) ) {
3187
- self . _storage = ( high: _value. 0 , low: _value. 1 )
3188
- }
3189
-
3190
- public var high : High {
3191
- return _storage. high
3192
- }
3193
-
3194
- public var low : Low {
3195
- return _storage. low
3196
- }
3197
-
3198
- // Numeric
3199
- //
3200
- public init( ) {
3201
- self . init ( ( High ( ) , Low ( ) ) )
3202
- }
3203
-
3204
- // integer
3205
- //
3206
- public var magnitude : DoubleWidth < Low > {
3207
- if T . isSigned && _storage. high < 0 {
3208
- return ( DoubleWidth < T > ( ) - self ) . magnitude
3209
- }
3210
- return DoubleWidth < Low > ( (
3211
- _storage. high. magnitude, _storage. low. magnitude) )
3212
- }
3213
-
3214
- public func isEqual( to rhs: DoubleWidth < T > ) -> Bool {
3215
- return ( _storage. high == rhs. _storage. high) &&
3216
- ( _storage. low == rhs. _storage. low)
3217
- }
3218
-
3219
- public func isLess( than rhs: DoubleWidth < T > ) -> Bool {
3220
- if _storage. high < rhs. _storage. high {
3221
- return true
3222
- }
3223
- if ( _storage. high > rhs. _storage. high) {
3224
- return false
3225
- }
3226
- return _storage. low < rhs. _storage. low
3227
- }
3228
-
3229
- public init< T : BinaryInteger> ( _ source: T) {
3230
- fatalError ( )
3231
- }
3232
-
3233
- public init? < T : BinaryInteger> ( exactly source: T) {
3234
- fatalError ( )
3235
- }
3236
-
3237
- public init< T : BinaryInteger> ( extendingOrTruncating source: T) {
3238
- fatalError ( )
3239
- }
3240
-
3241
- public init< T : FloatingPoint> ( _ source: T) {
3242
- fatalError ( )
3243
- }
3244
-
3245
- public init? < T : FloatingPoint> ( exactly source: T) {
3246
- fatalError ( )
3247
- }
3248
-
3249
- public func _word( at n: Int ) -> UInt {
3250
- if T . bitWidth < ${ word_bits} || T . bitWidth % ${ word_bits} != 0 {
3251
- fatalError ( " _word(at:) is not supported on this type " )
3252
- }
3253
- // TODO: move to Int128 just like init(_builtinIntegerLiteral:) ?
3254
- let wordsInT = T . bitWidth / ${ word_bits}
3255
- return ( n < wordsInT) ?
3256
- _storage. low. _word ( at: n) :
3257
- _storage. high. _word ( at: n - wordsInT)
3258
- }
3259
-
3260
- public static var isSigned : Bool {
3261
- return T . isSigned
3262
- }
3263
-
3264
- // fixed width
3265
- //
3266
- public static var max : DoubleWidth < T > {
3267
- return self . init ( ( High . max, Low . max) )
3268
- }
3269
-
3270
- public static var min : DoubleWidth < T > {
3271
- return self . init ( ( High . min, Low . min) )
3272
- }
3273
-
3274
- public static var bitWidth : Int { return 2 * T. bitWidth }
3275
-
3276
- % # This covers + and -
3277
- % for x in binaryArithmetic [ 'Numeric'] [ : 2 ] :
3278
- % highAffectedByLowOverflow = 'T. max' if x. operator == '+ ' else 'T. min'
3279
- public func ${ x. name} ReportingOverflow( _ rhs: DoubleWidth < T > )
3280
- - > ( partialValue: DoubleWidth < T > , overflow: ArithmeticOverflow) {
3281
- let ( low, lowOverflow) =
3282
- _storage. low . ${ x. name} ReportingOverflow( rhs. _storage. low)
3283
- let ( high, highOverflow) =
3284
- _storage. high . ${ x. name} ReportingOverflow( rhs. _storage. high)
3285
- let isLowOverflow = lowOverflow == . overflow
3286
- let result = ( high ${ x. operator} ( isLowOverflow ? 1 : 0 ) , low)
3287
- let overflow = ArithmeticOverflow (
3288
- highOverflow == . overflow ||
3289
- high == ${ highAffectedByLowOverflow} && isLowOverflow
3290
- )
3291
- return ( partialValue: DoubleWidth < T > ( result) ,
3292
- overflow: overflow)
3293
- }
3294
- % end
3295
-
3296
-
3297
- public func multipliedReportingOverflow(
3298
- by rhs: DoubleWidth < T >
3299
- ) -> ( partialValue: DoubleWidth < T > , overflow: ArithmeticOverflow) {
3300
- let isNegative = ( self < DoubleWidth < T > ( ) ) != ( rhs < DoubleWidth < T > ( ) )
3301
-
3302
- func mul( _ x: Low , _ y: Low , _ carry: Low ) -> ( partial: Low , carry: Low ) {
3303
- let pair = x. multipliedFullWidth ( by: y)
3304
- let t = DoubleWidth < Low > ( pair) + DoubleWidth < Low > ( ( 0 , carry) )
3305
- return ( partial: t. _storage. low, carry: t. _storage. high)
3306
- }
3307
-
3308
- var high : Low = 0
3309
- var low : Low = 0
3310
-
3311
- func mkResult( _ isOverflow: Bool )
3312
- -> ( partialValue: DoubleWidth < T > , overflow: ArithmeticOverflow ) {
3313
-
3314
- // TODO: High() cast fails
3315
- let result = DoubleWidth < T > ( ( High ( high) , low) )
3316
- if isNegative {
3317
- return DoubleWidth < T > ( ) . subtractingReportingOverflow ( result)
3318
- }
3319
- return ( partialValue: result, overflow: ArithmeticOverflow ( isOverflow) )
3320
- }
3321
-
3322
- var carry : Low = 0
3323
-
3324
- let lhs = self . magnitude
3325
- let rhs = rhs. magnitude
3326
-
3327
- // TODO: gyb me!
3328
- let a = mul ( rhs. _storage. low, lhs. _storage. low, carry)
3329
- low += a. partial
3330
- carry = a. carry
3331
- /*_log("(II) 1 (\(high), \(low)) carry: \(carry)")*/
3332
-
3333
- let b = mul ( rhs. _storage. low, lhs. _storage. high, carry)
3334
- high += b. partial
3335
- carry = b. carry
3336
- /*_log("(II) 2 (\(high), \(low)) carry: \(carry)")*/
3337
-
3338
- if carry != 0 {
3339
- /*_log("(EE) overflow")*/
3340
- return mkResult ( true )
3341
- }
3342
-
3343
- let c = mul ( rhs. _storage. high, lhs. _storage. low, carry)
3344
- low += c. partial
3345
- carry = c. carry
3346
- /*_log("(II) 3 (\(high), \(low)) carry: \(carry)")*/
3347
-
3348
- let d = mul ( rhs. _storage. high, lhs. _storage. high, carry)
3349
- high += d. partial
3350
- carry = d. carry
3351
- /*_log("(II) 4 (\(high), \(low)) carry: \(carry)")*/
3352
-
3353
- /*if (carry > 0) { _log("(EE) overflow") }*/
3354
- return mkResult ( carry > 0 )
3355
- }
3356
-
3357
- public func dividedReportingOverflow( by other: DoubleWidth < T > )
3358
- - > ( partialValue: DoubleWidth < T > , overflow: ArithmeticOverflow) {
3359
- fatalError ( )
3360
- }
3361
-
3362
- public func remainderReportingOverflow( dividingBy other: DoubleWidth < T > )
3363
- - > ( partialValue: DoubleWidth < T > , overflow: ArithmeticOverflow) {
3364
- fatalError ( )
3365
- }
3366
-
3367
- public func multipliedFullWidth( by other: DoubleWidth < T > )
3368
- - > ( high: DoubleWidth < T > , low: DoubleWidth< T> . Magnitude) {
3369
- fatalError ( )
3370
- }
3371
-
3372
- public func dividingFullWidth(
3373
- _ dividend: ( high: DoubleWidth < T > , low: DoubleWidth< T> . Magnitude)
3374
- ) - > ( quotient: DoubleWidth < T > , remainder: DoubleWidth < T > ) {
3375
- fatalError ( )
3376
- }
3377
-
3378
- % for x in binaryBitwise + maskingShifts:
3379
- public static func ${ x. operator} = (
3380
- lhs: inout DoubleWidth< T > , rhs: DoubleWidth < T >
3381
- ) {
3382
- fatalError ( )
3383
- }
3384
- % end
3385
-
3386
- % for x in chain( * binaryArithmetic. values ( ) ) :
3387
-
3388
- // FIXME(integers): remove this once the operators are back to Numeric
3389
- public static func ${ x. operator} (
3390
- lhs: DoubleWidth < T > , rhs: DoubleWidth < T >
3391
- ) -> DoubleWidth< T> {
3392
- var lhs = lhs
3393
- lhs ${ x. operator} = rhs
3394
- return lhs
3395
- }
3396
-
3397
- public static func ${ x. operator} = (
3398
- lhs: inout DoubleWidth< T > , rhs: DoubleWidth < T >
3399
- ) {
3400
- fatalError ( )
3401
- }
3402
- % end
3403
-
3404
- public init( _truncatingBits bits: UInt) {
3405
- fatalError ( )
3406
- }
3407
-
3408
- // other
3409
- //
3410
- public init( _builtinIntegerLiteral x: _MaxBuiltinIntegerType) {
3411
- fatalError ( " Method must be overridden " )
3412
- }
3413
-
3414
- public var description: String {
3415
- return " ( \( _storage. high) , \( _storage. low) ) "
3416
- }
3417
-
3418
- public var leadingZeroBitCount: Int {
3419
- fatalError ( )
3420
- }
3421
-
3422
- public var trailingZeroBitCount: Int {
3423
- fatalError ( )
3424
- }
3425
-
3426
- public var nonzeroBitCount: Int {
3427
- fatalError ( )
3428
- }
3429
-
3430
- public var hashValue: Int {
3431
- fatalError ( )
3432
- }
3433
-
3434
- public init( littleEndian: DoubleWidth < T > ) {
3435
- fatalError ( )
3436
- }
3437
-
3438
- public init( bigEndian: DoubleWidth < T > ) {
3439
- fatalError ( )
3440
- }
3441
-
3442
- public var littleEndian: DoubleWidth< T> {
3443
- fatalError ( )
3444
- }
3445
-
3446
- public var bigEndian: DoubleWidth< T> {
3447
- fatalError ( )
3448
- }
3449
-
3450
- public var byteSwapped: DoubleWidth< T> {
3451
- fatalError ( )
3452
- }
3453
- }
3454
-
3455
3171
// FIXME(integers): switch to using `FixedWidthInteger.unsafeAdding`
3456
3172
internal func _unsafePlus( _ lhs: Int, _ rhs: Int) - > Int {
3457
3173
#if INTERNAL_CHECKS_ENABLED
0 commit comments