Skip to content

[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

Merged
merged 2 commits into from
Feb 1, 2018
Merged

[docs] Update and fill in documentation for DoubleWidth and integer protocols #14295

merged 2 commits into from
Feb 1, 2018

Conversation

xwu
Copy link
Collaborator

@xwu xwu commented Jan 31, 2018

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 and remainderReportingOverflow have their doc comments substantially updated, and several outdated comments are deleted from integer protocols.

///
/// 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
Copy link
Collaborator Author

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)) {
Copy link
Collaborator Author

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).

Copy link
Member

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?

Copy link
Contributor

@moiseev moiseev Feb 1, 2018

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.

Copy link
Collaborator Author

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.
Copy link
Collaborator Author

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."

Copy link
Member

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.
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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.
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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.

@xwu xwu requested a review from natecook1000 January 31, 2018 03:07
@xwu
Copy link
Collaborator Author

xwu commented Jan 31, 2018

@swift-ci Please smoke test

Copy link
Member

@natecook1000 natecook1000 left a 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.
Copy link
Member

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)) {
Copy link
Member

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?

@xwu
Copy link
Collaborator Author

xwu commented Feb 1, 2018

@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.
Copy link
Contributor

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" ;-)

Copy link
Collaborator Author

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... ;)

@xwu xwu merged commit 37beb71 into swiftlang:master Feb 1, 2018
@xwu xwu deleted the integer-documentation branch February 1, 2018 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants