@@ -97,14 +97,14 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
97
97
}
98
98
}
99
99
100
- public init < T : FloatingPoint > ( _ source: T ) {
100
+ public init < T : BinaryFloatingPoint > ( _ source: T ) {
101
101
fatalError ( )
102
102
}
103
103
104
- public init ? < T : FloatingPoint > ( exactly source: T ) {
104
+ public init ? < T : BinaryFloatingPoint > ( exactly source: T ) {
105
105
fatalError ( )
106
106
}
107
-
107
+
108
108
public init < T : BinaryFloatingPoint > ( _ source: T )
109
109
where T. RawSignificand : FixedWidthInteger
110
110
{
@@ -269,27 +269,23 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
269
269
% for ( operator, name) in [ ( '+ ', 'adding') , ( '- ', 'subtracting') ] :
270
270
% highAffectedByLowOverflow = 'Base. max' if operator == '+ ' else 'Base. min'
271
271
public func ${ name} ReportingOverflow( _ rhs: DoubleWidth)
272
- -> ( partialValue: DoubleWidth, overflow: ArithmeticOverflow ) {
272
+ -> ( partialValue: DoubleWidth, overflow: Bool ) {
273
273
let ( low, lowOverflow) =
274
274
_storage. low . ${ name} ReportingOverflow( rhs. _storage. low)
275
275
let ( high, highOverflow) =
276
276
_storage. high . ${ name} ReportingOverflow( rhs. _storage. high)
277
- let isLowOverflow = lowOverflow == . overflow
278
- let result = ( high & ${ operator} ( isLowOverflow ? 1 : 0 ) , low)
279
- let overflow = ArithmeticOverflow (
280
- highOverflow == . overflow ||
281
- high == ${ highAffectedByLowOverflow} && isLowOverflow
282
- )
283
- return ( partialValue: DoubleWidth( result) ,
284
- overflow: overflow)
277
+ let result = ( high & ${ operator} ( lowOverflow ? 1 : 0 ) , low)
278
+ let overflow = highOverflow ||
279
+ high == ${ highAffectedByLowOverflow} && lowOverflow
280
+ return ( partialValue: DoubleWidth( result) , overflow: overflow)
285
281
}
286
282
% end
287
283
288
284
public func multipliedReportingOverflow(
289
285
by rhs: DoubleWidth
290
- ) -> ( partialValue: DoubleWidth , overflow: ArithmeticOverflow ) {
286
+ ) -> ( partialValue: DoubleWidth , overflow: Bool ) {
291
287
let ( carry, product) = multipliedFullWidth ( by: rhs)
292
- let result = DoubleWidth ( extendingOrTruncating : product)
288
+ let result = DoubleWidth ( truncatingIfNeeded : product)
293
289
294
290
let isNegative = ( self < ( 0 as DoubleWidth ) ) != ( rhs < ( 0 as DoubleWidth ) )
295
291
let didCarry = isNegative
@@ -298,7 +294,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
298
294
let hadPositiveOverflow = !isNegative &&
299
295
DoubleWidth . isSigned && product. leadingZeroBitCount == 0
300
296
301
- return ( result, ArithmeticOverflow ( didCarry || hadPositiveOverflow) )
297
+ return ( result, didCarry || hadPositiveOverflow)
302
298
}
303
299
304
300
public func quotientAndRemainder( dividingBy other: DoubleWidth )
@@ -350,25 +346,25 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
350
346
}
351
347
352
348
public func dividedReportingOverflow( by other: DoubleWidth )
353
- -> ( partialValue: DoubleWidth , overflow: ArithmeticOverflow ) {
349
+ -> ( partialValue: DoubleWidth , overflow: Bool ) {
354
350
if other == ( 0 as DoubleWidth ) ||
355
351
( DoubleWidth . isSigned && other == ( - 1 as Int ) && self == . min)
356
352
{
357
- return ( self , . overflow )
353
+ return ( self , true )
358
354
}
359
355
360
- return ( quotientAndRemainder ( dividingBy: other) . quotient, . none )
356
+ return ( quotientAndRemainder ( dividingBy: other) . quotient, false )
361
357
}
362
358
363
359
public func remainderReportingOverflow( dividingBy other: DoubleWidth )
364
- -> ( partialValue: DoubleWidth , overflow: ArithmeticOverflow ) {
360
+ -> ( partialValue: DoubleWidth , overflow: Bool ) {
365
361
if other == 0 ||
366
362
( DoubleWidth . isSigned && other == - 1 && self == . min)
367
363
{
368
- return ( self , . overflow )
364
+ return ( self , true )
369
365
}
370
366
371
- return ( quotientAndRemainder ( dividingBy: other) . remainder, . none )
367
+ return ( quotientAndRemainder ( dividingBy: other) . remainder, false )
372
368
}
373
369
374
370
public func multipliedFullWidth( by other: DoubleWidth )
@@ -384,8 +380,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
384
380
func sum( _ x: Low , _ y: Low , _ z: Low ) -> ( partial: Low , carry: Low ) {
385
381
let ( sum1, overflow1) = x. addingReportingOverflow ( y)
386
382
let ( sum2, overflow2) = sum1. addingReportingOverflow ( z)
387
- let carry : Low = ( overflow1 == . overflow ? 1 : 0 ) +
388
- ( overflow2 == . overflow ? 1 : 0 )
383
+ let carry : Low = ( overflow1 ? 1 : 0 ) + ( overflow2 ? 1 : 0 )
389
384
return ( sum2, carry)
390
385
}
391
386
@@ -406,7 +401,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
406
401
407
402
if isNegative {
408
403
let ( lowComplement, overflow) = ( ~ low) . addingReportingOverflow ( 1 )
409
- return ( ~ high + ( overflow == . overflow ? 1 : 0 ) , lowComplement)
404
+ return ( ~ high + ( overflow ? 1 : 0 ) , lowComplement)
410
405
} else {
411
406
return ( high, low)
412
407
}
@@ -447,7 +442,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
447
442
448
443
// Shift is exactly the width of `Base`, so low -> high.
449
444
if rhs. _storage. low == Base . bitWidth {
450
- lhs = DoubleWidth ( ( High ( extendingOrTruncating : lhs. _storage. low) , 0 ) )
445
+ lhs = DoubleWidth ( ( High ( truncatingIfNeeded : lhs. _storage. low) , 0 ) )
451
446
return
452
447
}
453
448
@@ -472,7 +467,7 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
472
467
if rhs. _storage. low == Base . bitWidth {
473
468
lhs = DoubleWidth ( (
474
469
lhs < ( 0 as DoubleWidth ) ? ~ 0 : 0 ,
475
- Low ( extendingOrTruncating : lhs. _storage. high)
470
+ Low ( truncatingIfNeeded : lhs. _storage. high)
476
471
) )
477
472
return
478
473
}
@@ -485,10 +480,10 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
485
480
486
481
lhs. _storage. high <<= High ( rhs. _storage. low)
487
482
if Base . bitWidth > rhs. _storage. low {
488
- lhs. _storage. high |= High ( extendingOrTruncating : lhs. _storage. low >>
483
+ lhs. _storage. high |= High ( truncatingIfNeeded : lhs. _storage. low >>
489
484
( numericCast ( Base . bitWidth) - rhs. _storage. low) )
490
485
} else {
491
- lhs. _storage. high |= High ( extendingOrTruncating : lhs. _storage. low <<
486
+ lhs. _storage. high |= High ( truncatingIfNeeded : lhs. _storage. low <<
492
487
( rhs. _storage. low - numericCast( Base . bitWidth) ) )
493
488
}
494
489
lhs. _storage. low <<= rhs. _storage. low
@@ -500,14 +495,14 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
500
495
lhs. _storage. low >>= rhs. _storage. low
501
496
if Base . bitWidth > rhs. _storage. low {
502
497
lhs. _storage. low |= Low (
503
- extendingOrTruncating :
498
+ truncatingIfNeeded :
504
499
lhs. _storage. high << ( numericCast ( Base . bitWidth) - rhs. _storage. low) )
505
500
} else {
506
501
lhs. _storage. low |= Low (
507
- extendingOrTruncating : lhs. _storage. high >>
502
+ truncatingIfNeeded : lhs. _storage. high >>
508
503
( rhs. _storage. low - numericCast( Base . bitWidth) ) )
509
504
}
510
- lhs. _storage. high >>= High ( extendingOrTruncating : rhs. _storage. low)
505
+ lhs. _storage. high >>= High ( truncatingIfNeeded : rhs. _storage. low)
511
506
}
512
507
513
508
% {
@@ -535,7 +530,7 @@ binaryOperators = [
535
530
lhs: inout DoubleWidth, rhs: DoubleWidth
536
531
) {
537
532
let ( result, overflow) = lhs . ${ name} ReportingOverflow( ${ argumentLabel} rhs)
538
- _precondition ( overflow == . none , " Overflow in ${operator}= " )
533
+ _precondition ( ! overflow, " Overflow in ${operator}= " )
539
534
lhs = result
540
535
}
541
536
% end
@@ -578,8 +573,8 @@ binaryOperators = [
578
573
579
574
@_transparent
580
575
public var byteSwapped: DoubleWidth {
581
- return DoubleWidth ( ( High ( extendingOrTruncating : low. byteSwapped) ,
582
- Low ( extendingOrTruncating : high. byteSwapped) ) )
576
+ return DoubleWidth ( ( High ( truncatingIfNeeded : low. byteSwapped) ,
577
+ Low ( truncatingIfNeeded : high. byteSwapped) ) )
583
578
}
584
579
}
585
580
0 commit comments