Skip to content

Add System.Math.DivRem for ulong and other integers #42156

Closed

Description

@Maykeye

Background and Motivation

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:

            bytes = Math.DivRem(bitOffset, 8ul, out bits);
            Console.WriteLine($"{bytes}+({bits}b)");

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)

Activity

  1. added
    api-suggestion
    on Sep 12, 2020
  2. EgorBo commented on Sep 12, 2020

    @EgorBo
    Member

    I wish we could rename out result into out remainder 😕 it's so misleading

  3. added
    untriaged
    on Sep 12, 2020
  4. Wraith2 commented on Sep 12, 2020

    @Wraith2
    Contributor

    It'd be neater to use a ValueTuple return and have (ulong Count, ulong Remainder) Math.DivRem(ulong value) et al.

  5. added this to the Future milestone on Sep 14, 2020
  6. tannergooding commented on Sep 14, 2020

    @tannergooding
    Member

    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.

  7. removed
    untriaged
    on Sep 14, 2020
  8. added
    api-ready-for-review
    and removed
    api-suggestion
    on Oct 27, 2020
  9. tannergooding commented on Nov 17, 2020

    @tannergooding
    Member

    Using value tuple

    +          public static (byte Quotient, byte Remainder) DivRem(byte left, byte right);
    +          public static (sbyte Quotient, sbyte Remainder) DivRem(sbyte left, sbyte right);
    +          public static (short Quotient, short Remainder) DivRem(short left, short right);
    +          public static (ushort Quotient, ushort Remainder) DivRem(ushort left, ushort right);
    +          public static (int Quotient, int Remainder) DivRem(int left, int right);
    +          public static (uint Quotient, uint Remainder) DivRem(uint left, uint right);
    +          public static (long Quotient, long Remainder) DivRem(long left, long right);
    +          public static (ulong Quotient, ulong Remainder) DivRem(ulong left, ulong right);
    +          public static (nint Quotient, nint Remainder) DivRem(nint left, nint right);
    +          public static (nuint Quotient, nuint Remainder) DivRem(nuint left, nuint right);
  10. terrajobst commented on Nov 17, 2020

    @terrajobst
    Contributor

    Video

    • Let's move to the value tuple pattern
    • Let's only addd the tuple pattern for the new overloads
    • But let's also add value pattern overloads for the existing types
    namespace System
    {
        public static partial class Math
        {
            public static (byte Quotient, byte Remainder) DivRem(byte left, byte right);
            public static (sbyte Quotient, sbyte Remainder) DivRem(sbyte left, sbyte right);
            public static (short Quotient, short Remainder) DivRem(short left, short right);
            public static (ushort Quotient, ushort Remainder) DivRem(ushort left, ushort right);
            public static (int Quotient, int Remainder) DivRem(int left, int right);
            public static (uint Quotient, uint Remainder) DivRem(uint left, uint right);
            public static (long Quotient, long Remainder) DivRem(long left, long right);
            public static (ulong Quotient, ulong Remainder) DivRem(ulong left, ulong right);
            public static (nint Quotient, nint Remainder) DivRem(nint left, nint right);
            public static (nuint Quotient, nuint Remainder) DivRem(nuint left, nuint right);
         }
    }
  11. added
    api-approved
    and removed
    api-ready-for-review
    on Nov 17, 2020
  12. Wraith2 commented on Nov 17, 2020

    @Wraith2
    Contributor

    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.

  13. CarolEidt commented on Nov 17, 2020

    @CarolEidt
    Contributor

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

  14. Symbai commented on Nov 22, 2020

    @Symbai

    The legacy APIs have the benefit that it can be used in if-cases and saves one line:

    if (Math.DivRem(12, 2, out int remainder) == 6)
    {
      Console.WriteLine(remainder);
    }
    

    With tuple based it would not only be longer code but also more ugly:

    var (result, remainder) = Math.DivRem(12, 2);
    if (result  == 6)
    {
      Console.WriteLine(remainder);
    }
    
  15. tannergooding commented on Nov 22, 2020

    @tannergooding
    Member
  16. Symbai commented on Nov 23, 2020

    @Symbai

    Didn't know that 👀

  17. tannergooding commented on Jan 6, 2021

    @tannergooding
    Member

    Closing, this was resolved in #45074

  18. ghost locked as resolved and limited conversation to collaborators on Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Numerics

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions