Skip to content

Conversation

@tannergooding
Copy link
Member

No description provided.

@ghost
Copy link

ghost commented Apr 16, 2022

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Apr 16, 2022

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@ghost
Copy link

ghost commented Apr 16, 2022

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Issue Details

null

Author: tannergooding
Assignees: tannergooding
Labels:

area-System.Numerics, new-api-needs-documentation

Milestone: -

@tannergooding tannergooding added this to the 7.0.0 milestone Apr 16, 2022
// /// <inheritdoc cref="IShiftOperators{TSelf, TResult}.op_UnsignedRightShift(TSelf, int)" />
// static short IShiftOperators<short, short>.operator >>>(short value, int shiftAmount) => (short)((ushort)value >> shiftAmount);
/// <inheritdoc cref="IShiftOperators{TSelf, TResult}.op_UnsignedRightShift(TSelf, int)" />
static short IShiftOperators<short, short>.operator >>>(short value, int shiftAmount) => (short)((ushort)value >>> shiftAmount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the cast to ushort still needed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C# doesn't define most operators for byte, sbyte, short, ushort, or char (outside the op assignment ones, like += or --).

So in C#, short >>> int is actually (int)short >>> int which means -1 >>> 1 goes from 0xFFFF to 0xFFFF_FFFF and then right shifts one giving 0x7FFF_FFFF. Casting back to short then results in 0xFFFF again.

Generic math returns and operates on the same type (like F# does) and so we need to explicitly treat it as a zero-extension so we get back 0x7FFF

/// <param name="shiftAmount">The amount by which <paramref name="value" /> is shifted right.</param>
/// <returns>The result of shifting <paramref name="value" /> right by <paramref name="shiftAmount" />.</returns>
/// <remarks>This operation is meant to perform n unsigned (otherwise known as a logical) right shift on all types.</remarks>
static abstract TResult operator >>>(TSelf value, int shiftAmount); // TODO_GENERIC_MATH: shiftAmount should be TOther
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume there's an issue tracking this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general design doc is tracking this at the moment. I'm just waiting for Aleksey to finish his PR and have the relevant changes stashed in a local branch

Copy link
Contributor

@dakersnar dakersnar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, besides the TODO Stephen mentioned.

@tannergooding tannergooding merged commit 8ea879a into dotnet:main Apr 18, 2022
@tannergooding
Copy link
Member Author

@stephentoub let me know if you have any other feedback. Given you only had two minor questions, I've merged due to the deadline to get this in. Happy to address anything else in a follow up PR.

directhex pushed a commit to directhex/runtime that referenced this pull request Apr 21, 2022
* Updating Microsoft.Net.Compilers.Toolset to v4.3.0-1.22215.4 for unsigned-right-shift

* Exposing operator >>> (Unsigned Right Shift)

* Ensure that SByte and Int16 have unsigned-right-shift operate on themselves

* Adding tests covering Unsigned Right Shift
@ghost ghost locked as resolved and limited conversation to collaborators May 18, 2022
@tannergooding tannergooding deleted the generic-math-unsigned-right-shift branch November 11, 2022 15:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants