You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add System.Math.DivRem for ulong and other integers #42156
Math.DivRem currently accepts only int32 and int64. When you are working with uint64, as of now it's easier to calculate division and remainder "by hand".
Which is not only surprising and slightly inconvenient, but it also may lead to suboptimal code if you are not aware of #5213
So for convenience I suggest adding DivRem for uint64 and since, these integers are not special, add for all other integer types while we are at it: uint32, [u]int16, [s]byte
Proposed API
namespace System
{
public static partial class Math
{
+ public static ulong DivRem(ulong a, ulong b, out ulong result)+ public static uint DivRem(uint a, uint b, out uint result)+ public static ushort DivRem(ushort a, ushort b, out ushort result)+ public static short DivRem(short a, short b, out short result)+ public static byte DivRem(byte a, byte b, out byte result)+ public static sbyte DivRem(sbyte a, sbyte b, out sbyte result)
public static int DivRem(int a, int b, out int result)
}
Usage Examples
Personally I ran into it when tried to use the function while formatting bit address inside of really large address space:
However looking for duplicates, I noticed that ulongs are implemented already(just not exposed), so here is the usage example from the very this exact repo.
Risks
There might be confusion what is called due to implicit conversions(when a=b=short, result=int)
I believe ValueTuple is also the pattern that will produce better codegen with the changes @CarolEidt has been working on to allow multiple returns from a single instruction.
+publicstatic(byteQuotient,byteRemainder) DivRem(byteleft,byteright);+ public static(sbyteQuotient,sbyteRemainder) DivRem(sbyteleft,sbyteright);+ public static(shortQuotient,shortRemainder) DivRem(shortleft,shortright);+ public static(ushortQuotient,ushortRemainder) DivRem(ushortleft,ushortright);+ public static(intQuotient,intRemainder) DivRem(intleft,intright);+ public static(uintQuotient,uintRemainder) DivRem(uintleft,uintright);+ public static(longQuotient,longRemainder) DivRem(longleft,longright);+ public static(ulongQuotient,ulongRemainder) DivRem(ulongleft,ulongright);+ public static(nintQuotient,nintRemainder) DivRem(nintleft,nintright);+ public static(nuintQuotient,nuintRemainder) DivRem(nuintleft,nuint right);
It may also be worth considering an analyzer that recommends moving from the out version to valuetuple version since it seems the new version will perform better.
@tannergooding - I've updated my PR #37928, but we'll need to determine the appropriate naming (I simplistically appended a '2' to distinguish from the variant that returns a scalar).
@echesakovMSFT suggested we add a flag in the HW intrinsics table so that we don't have to special-case the methods that return ValueTuple in the JIT.
It may also be worth considering an analyzer that recommends moving from the out version to valuetuple version since it seems the new version will perform better.
For the mulx case, I found that I was able to get identical code by having the out version call the ValueTuple version, since the address-taking goes away once it's inlined.
Reacted by Tanner Gooding, Wraith and Pent Ploompuu
Activity
EgorBo commentedon Sep 12, 2020
I wish we could rename
out resultintoout remainder😕 it's so misleadingWraith2 commentedon Sep 12, 2020
It'd be neater to use a ValueTuple return and have
(ulong Count, ulong Remainder) Math.DivRem(ulong value)et al.tannergooding commentedon Sep 14, 2020
I believe
ValueTupleis also the pattern that will produce better codegen with the changes @CarolEidt has been working on to allow multiple returns from a single instruction.tannergooding commentedon Nov 17, 2020
Using value tuple
terrajobst commentedon Nov 17, 2020
Video
Wraith2 commentedon Nov 17, 2020
It may also be worth considering an analyzer that recommends moving from the out version to valuetuple version since it seems the new version will perform better.
CarolEidt commentedon Nov 17, 2020
@tannergooding - I've updated my PR #37928, but we'll need to determine the appropriate naming (I simplistically appended a '2' to distinguish from the variant that returns a scalar).
@echesakovMSFT suggested we add a flag in the HW intrinsics table so that we don't have to special-case the methods that return
ValueTuplein the JIT.For the
mulxcase, I found that I was able to get identical code by having theoutversion call theValueTupleversion, since the address-taking goes away once it's inlined.Symbai commentedon Nov 22, 2020
The legacy APIs have the benefit that it can be used in if-cases and saves one line:
With tuple based it would not only be longer code but also more ugly:
tannergooding commentedon Nov 22, 2020
@Symbai, actually you can do this using C# pattern matching:
https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQxNMAfAAgJgIwCwAUFgMwAEu5AwuQN7HlOUVYAs5AsgBQCU9jZkICWAM3LcAIsIBuAJThhuAViTlS/YVHrkAigFcA9jGFwAdqnIA2NQsjCzAE0QhyMiAnIJFEB888AvryCQkwMRKGRlHgAnNze9k6IvADcIaEB6ZkRzOlYeFYSDjB6RibmMGrF5Ha+SQj80vKK3NUANnCileTVCMIA5gAWMME5YelCWADsEh1d5AD0XgPDanMlAKTLQyNpY+TZAUA==
Symbai commentedon Nov 23, 2020
Didn't know that 👀
tannergooding commentedon Jan 6, 2021
Closing, this was resolved in #45074