Skip to content
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

Add DivRem with DivisionRounding (WIP + Review request) #104589

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

heathbm
Copy link
Contributor

@heathbm heathbm commented Jul 9, 2024

Partial implementation of #93568 (requesting review of general approach before implementing other types since this is mostly copy & paste).

Done:

  • Added DivisionRounding to System
  • Added default generic IBinaryInteger DivRem implementation
  • Added DivRem implementation for Int32
  • Added tests for all types of DivisionRounding for Int32.DivRem

Todo:

  • Add DivRem implementations for remaining types
  • Add Divide and Remainder methods for all types
  • Add remaining tests

Thanks for the feedback!

Copy link

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

Copy link

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

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 9, 2024
Copy link
Contributor

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

@@ -11138,6 +11149,7 @@ public partial interface IBinaryFloatingPointIeee754<TSelf> : System.IComparable
public partial interface IBinaryInteger<TSelf> : System.IComparable, System.IComparable<TSelf>, System.IEquatable<TSelf>, System.IFormattable, System.IParsable<TSelf>, System.ISpanFormattable, System.ISpanParsable<TSelf>, System.Numerics.IAdditionOperators<TSelf, TSelf, TSelf>, System.Numerics.IAdditiveIdentity<TSelf, TSelf>, System.Numerics.IBinaryNumber<TSelf>, System.Numerics.IBitwiseOperators<TSelf, TSelf, TSelf>, System.Numerics.IComparisonOperators<TSelf, TSelf, bool>, System.Numerics.IDecrementOperators<TSelf>, System.Numerics.IDivisionOperators<TSelf, TSelf, TSelf>, System.Numerics.IEqualityOperators<TSelf, TSelf, bool>, System.Numerics.IIncrementOperators<TSelf>, System.Numerics.IModulusOperators<TSelf, TSelf, TSelf>, System.Numerics.IMultiplicativeIdentity<TSelf, TSelf>, System.Numerics.IMultiplyOperators<TSelf, TSelf, TSelf>, System.Numerics.INumber<TSelf>, System.Numerics.INumberBase<TSelf>, System.Numerics.IShiftOperators<TSelf, int, TSelf>, System.Numerics.ISubtractionOperators<TSelf, TSelf, TSelf>, System.Numerics.IUnaryNegationOperators<TSelf, TSelf>, System.Numerics.IUnaryPlusOperators<TSelf, TSelf> where TSelf : System.Numerics.IBinaryInteger<TSelf>?
{
static virtual (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right) { throw null; }
static (TSelf Quotient, TSelf Remainder) DivRem(TSelf left, TSelf right, System.DivisionRounding rounding) { throw null; }
Copy link
Member

Choose a reason for hiding this comment

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

This one is incorrect. It should be static virtual as the same of implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason runtime\src\libraries\System.Runtime\src>dotnet build /t:GenerateReferenceAssemblySource would remove the virtual from those methods. I've added it manually.

Comment on lines 4 to 9
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:System.Numerics.IBinaryInteger`1.DivRem(`0,`0,System.DivisionRounding)</Target>
<Left>net8.0/System.Runtime.dll</Left>
<Right>net9.0/System.Runtime.dll</Right>
</Suppression>
Copy link
Member

Choose a reason for hiding this comment

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

This suppression shouldn't be required if you are creating the ref source correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed the suppression, I was able to build with build.cmd -configuration release /p:ApiCompatValidateAssemblies=false

{
public enum DivisionRounding
{
Truncate = 0,
Copy link
Member

Choose a reason for hiding this comment

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

Nit: assigning underlying numeric value isn't required for implementation source.

Copy link
Contributor Author

@heathbm heathbm Jul 11, 2024

Choose a reason for hiding this comment

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

DivisionRounding.Ceiling => DivRemCeiling(left, right),
DivisionRounding.AwayFromZero => DivRemAwayFromZero(left, right),
DivisionRounding.Euclidean => DivRemEuclidean(left, right),
_ => throw new ArgumentOutOfRangeException(nameof(rounding)),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should ideally use a throw helper.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@heathbm
Copy link
Contributor Author

heathbm commented Jul 11, 2024

@tannergooding Would you be able to comment on the Math.DivRem and IBinaryInteger.DivRem approaches or recommend someone who could? Thanks.
Is this acceptable, or is something lower-level expected for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Numerics community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants