Skip to content

Commit 37beb71

Browse files
authored
Merge pull request #14295 from xwu/integer-documentation
[docs] Update and fill in documentation for DoubleWidth and integer protocols
2 parents 72d2f84 + 21d59c4 commit 37beb71

File tree

2 files changed

+67
-66
lines changed

2 files changed

+67
-66
lines changed

stdlib/public/core/DoubleWidth.swift.gyb

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// A fixed-width integer that is twice the size of its base type.
13+
/// A fixed-width integer that has twice the bit width of its base type.
1414
///
1515
/// You can use the `DoubleWidth` type to continue calculations with the result
1616
/// of a full width arithmetic operation. Normally, when you perform a full
17-
/// width operation, the result is a tuple of the high and low components of
18-
/// the result.
17+
/// width operation, the result is a tuple of the high and low parts of the
18+
/// result.
1919
///
2020
/// let a = 2241543570477705381
2121
/// let b = 186319822866995413
2222
/// let c = a.multipliedFullWidth(by: b)
23-
/// // c == (22640526660490081, 7959093232766896457)
23+
/// // c == (high: 22640526660490081, low: 7959093232766896457)
2424
///
2525
/// The tuple `c` can't be used in any further comparisons or calculations. To
2626
/// use this value, create a `DoubleWidth` instance from the result. You can
27-
/// use the `DoubleWidth` instance the way you use any other integer type.
27+
/// use the `DoubleWidth` instance in the same way that you would use any other
28+
/// integer type.
2829
///
2930
/// let d = DoubleWidth(a.multipliedFullWidth(by: b))
3031
/// // d == 417644001000058515200174966092417353
@@ -40,9 +41,9 @@
4041
/// }
4142
/// // Prints "Too big to be an 'Int'!"
4243
///
43-
/// The `DoubleWidth` type is intended for intermediate calculations, not as a
44-
/// replacement for a variable-width integer type. Nesting `DoubleWidth`
45-
/// instances, in particular, can result in undesirable performance.
44+
/// The `DoubleWidth` type is not intended as a replacement for a variable-width
45+
/// integer type. Nesting `DoubleWidth` instances, in particular, may result in
46+
/// undesirable performance.
4647
@_fixed_layout // FIXME(sil-serialize-all)
4748
public struct DoubleWidth<Base : FixedWidthInteger> :
4849
_ExpressibleByBuiltinIntegerLiteral
@@ -60,26 +61,31 @@ public struct DoubleWidth<Base : FixedWidthInteger> :
6061
internal var _storage: (low: Low, high: High)
6162
#endif
6263

64+
/// The high part of the value.
6365
@_inlineable // FIXME(sil-serialize-all)
6466
@_transparent
6567
public var high: High {
6668
return _storage.high
6769
}
6870

71+
/// The low part of the value.
6972
@_inlineable // FIXME(sil-serialize-all)
7073
@_transparent
7174
public var low: Low {
7275
return _storage.low
7376
}
7477

78+
/// Creates a new instance from the given tuple of high and low parts.
79+
///
80+
/// - Parameter value: The tuple to use as the source of the new instance's
81+
/// high and low parts.
7582
@_inlineable // FIXME(sil-serialize-all)
7683
@_transparent
77-
public // @testable
78-
init(_ _value: (High, Low)) {
84+
public init(_ value: (high: High, low: Low)) {
7985
#if _endian(big)
80-
self._storage = (high: _value.0, low: _value.1)
86+
self._storage = (high: value.0, low: value.1)
8187
#else
82-
self._storage = (low: _value.1, high: _value.0)
88+
self._storage = (low: value.1, high: value.0)
8389
#endif
8490
}
8591

stdlib/public/core/Integers.swift.gyb

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,8 @@ def assignmentOperatorComment(operator, fixedWidth):
809809
def overflowOperationComment(operator):
810810
comments = {
811811
'+': """\
812-
/// Returns the sum of this value and the given value along with a flag
813-
/// indicating whether overflow occurred in the operation.
812+
/// Returns the sum of this value and the given value, along with a Boolean
813+
/// value indicating whether overflow occurred in the operation.
814814
///
815815
/// - Parameter rhs: The value to add to this value.
816816
/// - Returns: A tuple containing the result of the addition along with a
@@ -821,32 +821,34 @@ def overflowOperationComment(operator):
821821
/// and `rhs`.
822822
""",
823823
'-': """\
824-
/// Returns the difference of this value and the given value along with a
825-
/// flag indicating whether overflow occurred in the operation.
824+
/// Returns the difference obtained by subtracting the given value from this
825+
/// value, along with a Boolean value indicating whether overflow occurred in
826+
/// the operation.
826827
///
827828
/// - Parameter rhs: The value to subtract from this value.
828829
/// - Returns: A tuple containing the result of the subtraction along with a
829-
/// flag indicating whether overflow occurred. If the `overflow` component
830-
/// is `false`, the `partialValue` component contains the entire
831-
/// difference. If the `overflow` component is `true`, an overflow
832-
/// occurred and the `partialValue` component contains the truncated
833-
/// result of `rhs` subtracted from this value.
830+
/// Boolean value indicating whether overflow occurred. If the `overflow`
831+
/// component is `false`, the `partialValue` component contains the entire
832+
/// difference. If the `overflow` component is `true`, an overflow occurred
833+
/// and the `partialValue` component contains the truncated result of `rhs`
834+
/// subtracted from this value.
834835
""",
835836
'*': """\
836-
/// Returns the product of this value and the given value along with a flag
837-
/// indicating whether overflow occurred in the operation.
837+
/// Returns the product of this value and the given value, along with a
838+
/// Boolean value indicating whether overflow occurred in the operation.
838839
///
839840
/// - Parameter rhs: The value to multiply by this value.
840841
/// - Returns: A tuple containing the result of the multiplication along with
841842
/// a Boolean value indicating whether overflow occurred. If the `overflow`
842843
/// component is `false`, the `partialValue` component contains the entire
843-
/// product. If the `overflow` component is `true`, an overflow
844-
/// occurred and the `partialValue` component contains the truncated
845-
/// product of this value and `rhs`.
844+
/// product. If the `overflow` component is `true`, an overflow occurred and
845+
/// the `partialValue` component contains the truncated product of this
846+
/// value and `rhs`.
846847
""",
847848
'/': """\
848-
/// Returns the quotient of dividing this value by the given value along with
849-
/// a flag indicating whether overflow occurred in the operation.
849+
/// Returns the quotient obtained by dividing this value by the given value,
850+
/// along with a Boolean value indicating whether overflow occurred in the
851+
/// operation.
850852
///
851853
/// Dividing by zero is not an error when using this method. For a value `x`,
852854
/// the result of `x.dividedReportingOverflow(by: 0)` is `(x, true)`.
@@ -856,22 +858,24 @@ def overflowOperationComment(operator):
856858
/// Boolean value indicating whether overflow occurred. If the `overflow`
857859
/// component is `false`, the `partialValue` component contains the entire
858860
/// quotient. If the `overflow` component is `true`, an overflow occurred
859-
/// and the `partialValue` component contains the truncated quotient.
861+
/// and the `partialValue` component contains either the truncated quotient
862+
/// or, if the quotient is undefined, the dividend.
860863
""",
861864
'%': """\
862-
// FIXME(integers): the comment is for division instead of remainder
863-
/// Returns the remainder of dividing this value by the given value along
864-
/// with a flag indicating whether overflow occurred in the operation.
865+
/// Returns the remainder after dividing this value by the given value, along
866+
/// with a Boolean value indicating whether overflow occurred during division.
865867
///
866868
/// Dividing by zero is not an error when using this method. For a value `x`,
867-
/// the result of `x.dividedReportingOverflow(by: 0)` is `(x, true)`.
869+
/// the result of `x.remainderReportingOverflow(dividingBy: 0)` is
870+
/// `(x, true)`.
868871
///
869872
/// - Parameter rhs: The value to divide this value by.
870-
/// - Returns: A tuple containing the result of the division along with a
873+
/// - Returns: A tuple containing the result of the operation along with a
871874
/// Boolean value indicating whether overflow occurred. If the `overflow`
872875
/// component is `false`, the `partialValue` component contains the entire
873-
/// quotient. If the `overflow` component is `true`, an overflow occurred
874-
/// and the `partialValue` component contains the truncated quotient.
876+
/// remainder. If the `overflow` component is `true`, an overflow occurred
877+
/// during division and the `partialValue` component contains either the
878+
/// entire remainder or, if the remainder is undefined, the dividend.
875879
""",
876880
}
877881
return comments[operator]
@@ -896,15 +900,15 @@ def unsafeOperationComment(operator):
896900
/// - Returns: The sum of this value and `rhs`.
897901
""",
898902
'-': """\
899-
/// Returns the difference of this value and the given value without checking
900-
/// for arithmetic overflow.
903+
/// Returns the difference obtained by subtracting the given value from this
904+
/// value without checking for arithmetic overflow.
901905
///
902906
/// If an arithmetic overflow occurs, the behavior is undefined. Use this
903907
/// function only to avoid the cost of overflow checking when you are sure
904908
/// that the operation won't overflow.
905909
///
906910
/// - Parameter rhs: The value to subtract from this value.
907-
/// - Returns: The difference of this value and `rhs`.
911+
/// - Returns: The result of subtracting `rhs` from this value.
908912
""",
909913
'*': """\
910914
/// Returns the product of this value and the given value without checking
@@ -915,18 +919,18 @@ def unsafeOperationComment(operator):
915919
/// that the operation won't overflow.
916920
///
917921
/// - Parameter rhs: The value to multiply by this value.
918-
/// - Returns: The difference of this value and `rhs`.
922+
/// - Returns: The product of this value and `rhs`.
919923
""",
920924
'/': """\
921-
/// Returns the quotient of dividing this value by the given value without
922-
/// checking for arithmetic overflow.
925+
/// Returns the quotient obtained by dividing this value by the given value
926+
/// without checking for arithmetic overflow.
923927
///
924928
/// If an arithmetic overflow occurs, the behavior is undefined. Use this
925929
/// function only to avoid the cost of overflow checking when you are sure
926930
/// that the operation won't overflow.
927931
///
928932
/// - Parameter rhs: The value to divide this value by.
929-
/// - Returns: The quotient of dividing this value by `rhs`.
933+
/// - Returns: The result of dividing this value by `rhs`.
930934
""",
931935
}
932936
return comments[operator]
@@ -2084,7 +2088,7 @@ public protocol FixedWidthInteger :
20842088
/// `-(2 ** (bitWidth - 1))` through `(2 ** (bitWidth - 1)) - 1`. For example,
20852089
/// the `Int8` type has a `bitWidth` value of 8 and can store any integer in
20862090
/// the range `-128...127`.
2087-
static var bitWidth : Int { get }
2091+
static var bitWidth: Int { get }
20882092

20892093
/// The maximum representable integer in this type.
20902094
///
@@ -2112,10 +2116,10 @@ ${overflowOperationComment(x.operator)}
21122116
///
21132117
/// Use this method to calculate the full result of a product that would
21142118
/// otherwise overflow. Unlike traditional truncating multiplication, the
2115-
/// `multipliedFullWidth(by:)` method returns a tuple
2116-
/// containing both the `high` and `low` parts of the product of this value and
2117-
/// `other`. The following example uses this method to multiply two `UInt8`
2118-
/// values that normally overflow when multiplied:
2119+
/// `multipliedFullWidth(by:)` method returns a tuple containing both the
2120+
/// `high` and `low` parts of the product of this value and `other`. The
2121+
/// following example uses this method to multiply two `UInt8` values that
2122+
/// normally overflow when multiplied:
21192123
///
21202124
/// let x: UInt8 = 100
21212125
/// let y: UInt8 = 20
@@ -2136,20 +2140,18 @@ ${overflowOperationComment(x.operator)}
21362140
/// - Returns: A tuple containing the high and low parts of the result of
21372141
/// multiplying this value and `other`.
21382142
func multipliedFullWidth(by other: Self) -> (high: Self, low: Self.Magnitude)
2139-
// FIXME(integers): figure out how to return DoubleWidth<Self>
21402143

2141-
/// Returns a tuple containing the quotient and remainder of dividing the
2142-
/// given value by this value.
2144+
/// Returns a tuple containing the quotient and remainder obtained by dividing
2145+
/// the given value by this value.
21432146
///
21442147
/// The resulting quotient must be representable within the bounds of the
2145-
/// type. If the quotient of dividing `dividend` by this value is too large
2146-
/// to represent in the type, a runtime error may occur.
2148+
/// type. If the quotient is too large to represent in the type, a runtime
2149+
/// error may occur.
21472150
///
21482151
/// - Parameter dividend: A tuple containing the high and low parts of a
2149-
/// double-width integer. The `high` component of the value carries the
2150-
/// sign, if the type is signed.
2151-
/// - Returns: A tuple containing the quotient and remainder of `dividend`
2152-
/// divided by this value.
2152+
/// double-width integer.
2153+
/// - Returns: A tuple containing the quotient and remainder obtained by
2154+
/// dividing `dividend` by this value.
21532155
func dividingFullWidth(_ dividend: (high: Self, low: Self.Magnitude))
21542156
-> (quotient: Self, remainder: Self)
21552157

@@ -2555,11 +2557,8 @@ extension FixedWidthInteger {
25552557

25562558
% for x in binaryArithmetic['Numeric'] + binaryArithmetic["BinaryInteger"][:1]:
25572559
% callLabel = x.firstArg + ': ' if not x.firstArg == '_' else ''
2558-
// FIXME(integers): pending optimizer work on handling the case where the
2559-
// boolean value is wrapped into a two-case enum and then immediately
2560-
// unwrapped. <rdar://problem/29004429>
2561-
// Uncomment this block and remove the corresponding one from the concrete
2562-
// types once the optimizer is ready.
2560+
// FIXME(integers): uncomment this block and remove the corresponding one from
2561+
// the concrete types
25632562
#if false
25642563
${assignmentOperatorComment(x.operator, True)}
25652564
@_transparent
@@ -3085,9 +3084,6 @@ public struct ${Self}
30853084
return Bool(Builtin.cmp_${u}lt_Int${bits}(lhs._value, rhs._value))
30863085
}
30873086
3088-
// FIXME(integers): pending optimizer work on handling the case where the
3089-
// boolean value is wrapped into a two-case enum and then immediately
3090-
// unwrapped. <rdar://problem/29004429>
30913087
// See corresponding definitions in the FixedWidthInteger extension.
30923088
% for x in binaryArithmetic['Numeric'] + binaryArithmetic["BinaryInteger"][:1]:
30933089
${assignmentOperatorComment(x.operator, True)}
@@ -3121,7 +3117,6 @@ ${assignmentOperatorComment(x.operator, True)}
31213117
lhs = ${Self}(result)
31223118
}
31233119
% end
3124-
// end of FIXME(integers)
31253120
31263121
% for x in chain(*binaryArithmetic.values()):
31273122

0 commit comments

Comments
 (0)