-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[docs] Update and fill in documentation for DoubleWidth and integer protocols #14295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
/// | ||
/// You can use the `DoubleWidth` type to continue calculations with the result | ||
/// of a full width arithmetic operation. Normally, when you perform a full | ||
/// width operation, the result is a tuple of the high and low components of | ||
/// the result. | ||
/// width operation, the result is a tuple of the high and low parts of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the documentation for integer protocols, "parts" seems to be the preferred term. I think that wording is superior in this case, as these aren't components in the mathematical sense.
@_inlineable // FIXME(sil-serialize-all) | ||
@_transparent | ||
public // @testable | ||
init(_ _value: (High, Low)) { | ||
public init(_ value: (high: High, low: Low)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although it appears that this initializer was originally not intended for public consumption, the documentation tells users to use it. In fact, this initializer is promoted as part of the main use case for DoubleWidth
(and I would agree with that characterization).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't even notice that this wasn't a public initializer when I wrote that—seems like a pretty intuitive usage of the type.
@moiseev, what do you think of making this public API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree we should just remove the underscore from _value
and a // @testable
comment. This initializer is quite useful and should be a part of DoubleWidth
API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. I think this PR is ready then :)
/// Returns the sum of this value and the given value along with a flag | ||
/// indicating whether overflow occurred in the operation. | ||
/// Returns the sum of this value and the given value, along with a Boolean | ||
/// flag indicating whether overflow occurred in the operation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some vacillation here between "flag" and "Boolean value," probably a leftover from the days of ArithmeticOverflow
. For consistency, I've standardized on "Boolean flag."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch on the "flag" business—these should all be "Boolean value indicating..." throughout to match the rest of the docs.
/// component is `false`, the `partialValue` component contains the entire | ||
/// quotient. If the `overflow` component is `true`, an overflow occurred | ||
/// and the `partialValue` component contains the truncated quotient. | ||
/// and the `partialValue` component contains either the truncated quotient | ||
/// or, if the quotient is undefined, the dividend. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This, I think, is a key clarification.
""", | ||
'%': """\ | ||
// FIXME(integers): the comment is for division instead of remainder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue is now fixed, and the return value is greatly clarified.
@@ -896,15 +900,15 @@ def unsafeOperationComment(operator): | |||
/// - Returns: The sum of this value and `rhs`. | |||
""", | |||
'-': """\ | |||
/// Returns the difference of this value and the given value without checking | |||
/// for arithmetic overflow. | |||
/// Returns the difference obtained by subtracting the given value from this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that there would have been terrible confusion, but we could stand to clarify which value is the subtrahend, so I did.
/// | ||
/// - Parameter dividend: A tuple containing the high and low parts of a | ||
/// double-width integer. The `high` component of the value carries the | ||
/// sign, if the type is signed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an unnecessary and potentially confusing remark; it is not found anywhere else, even though a similar tuple is returned in other functions.
The two's-complement value will differ from its magnitude in both the high and low parts, and it's highly doubtful that anyone will wonder if it's the low part and not the high part that carries the actual sign bit itself.
@@ -2555,11 +2557,8 @@ extension FixedWidthInteger { | |||
|
|||
% for x in binaryArithmetic['Numeric'] + binaryArithmetic["BinaryInteger"][:1]: | |||
% callLabel = x.firstArg + ': ' if not x.firstArg == '_' else '' | |||
// FIXME(integers): pending optimizer work on handling the case where the | |||
// boolean value is wrapped into a two-case enum and then immediately |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be a reference to the original ArithmeticOverflow
enum that was mercifully removed. The block remains disabled because the holdup is with overload resolution, I think, and nothing to do with optimization of two-case enums.
@swift-ci Please smoke test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing these, @xwu — I have one group of changes and a question for Max before we merge these.
/// Returns the sum of this value and the given value along with a flag | ||
/// indicating whether overflow occurred in the operation. | ||
/// Returns the sum of this value and the given value, along with a Boolean | ||
/// flag indicating whether overflow occurred in the operation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch on the "flag" business—these should all be "Boolean value indicating..." throughout to match the rest of the docs.
@_inlineable // FIXME(sil-serialize-all) | ||
@_transparent | ||
public // @testable | ||
init(_ _value: (High, Low)) { | ||
public init(_ value: (high: High, low: Low)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't even notice that this wasn't a public initializer when I wrote that—seems like a pretty intuitive usage of the type.
@moiseev, what do you think of making this public API?
@swift-ci Please smoke test |
/// instances, in particular, can result in undesirable performance. | ||
/// The `DoubleWidth` type is not intended as a replacement for a variable-width | ||
/// integer type. Nesting `DoubleWidth` instances, in particular, may result in | ||
/// undesirable performance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me "undesirable performance" sounds like "you'll get too much performance even if you don't need it" ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After I’m done with it, maybe you will... ;)
This PR contains documentation changes from PR #13784, which was split up to be committed in chunks after CI stopped cooperating.
In particular,
DoubleWidth
members now gain doc comments,dividedReportingOverflow
andremainderReportingOverflow
have their doc comments substantially updated, and several outdated comments are deleted from integer protocols.