runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[API Proposal]: UInt64.DivRemByUInt32

Open lilinus opened this issue 2 years ago • 5 comments

Background and motivation

There are multiple places in the runtime where we do a DivRem. If we can't get the quotient and remainder in one go using intrinsics or something else, then it is faster to compute the 32-bit remainder (guaranteed since divisor is 32-bit), compared to a 64-bit remainder.

Examples of such places: Decimal.DecCalc (one example from file) https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/Decimal.DecCalc.cs#L215-L217

Uint64 (formatting) https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs#L2338-L2339

BigInteger https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.DivRem.cs#L20-L29 DateTime (for decomposing into years, months, days, hours etc). https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/DateTime.cs#L1419-L1424

It should be possible to leverage hardware intrisics:

  • If we can use intrisicts (like in DivRem(ulong, ulong)), we can just truncate the remainder to uint.
  • Otherwise remainder can be calculated as (uint)dividend - ((uint)quotient * divisor) since 32 bit multiplication should be faster than 64-bit. This is of course preceeded by a 64-bit division.

Haven't seen any use of the same DivRem for signed numbers yet so excluded them from this suggestion

API Proposal

namespace System;

public readonly struct UInt64
{
    public static (ulong Quotient, uint Remainder) DivRemByUInt32(ulong left, uint right);
}

API Usage

//
ulong dividend = ulong.MaxValue;
uint divisor = 10;
(ulong quotient, uint remainder) = ulong.DivRemByUInt32(divident, divisor)

Alternative Designs

No response

Risks

No response

lilinus avatar Feb 14 '24 09:02 lilinus

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

Issue Details

Background and motivation

There are multiple places in the runtime where we do a DivRem.

Examples of such places: Decimal.DecCalc (one example from file) https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/Decimal.DecCalc.cs#L215-L217

Uint64 (formatting) https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs#L2338-L2339

BigInteger https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.DivRem.cs#L20-L29 DateTime (for decomposing into years, months, days, hours etc). https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/DateTime.cs#L1419-L1424

It should be possible to leverage hardware intrisics:

  • If DivRem(ulong, ulong) is intrisinc, we can just truncate the remainder to uint.
  • Otherwise remainder can be calculated as (uint)dividend - ((uint)quotient * divisor) since 32 bit multiplication should be faster than 64-bit.

Haven't seen any use of the same DivRem for signed numbers yet so excluded them from this suggestion

API Proposal

namespace System;

public readonly struct UInt64
{
    public static (ulong Quotient, uint Remainder) DivRemByUInt32(ulong left, uint right);
}

API Usage

//
ulong dividend = ulong.MaxValue;
uint divisor = 10;
(ulong quotient, uint remainder) = ulong.DivRemByUInt32(divident, divisor)

Alternative Designs

No response

Risks

No response

Author: lilinus
Assignees: -
Labels:

api-suggestion, area-System.Runtime, untriaged

Milestone: -

ghost avatar Feb 14 '24 14:02 ghost

I don't know about arm64 but on x86 you will probably need to 64bit divide just as Math.DivRem would do and then truncate the result as long as there is a risk of overflow.

For decimal you probably don't want such a general api, but instead want to use the x86base instrinct, which works only when upper 32bits < divisor which is known to be the case for the majority of the instructions. (I did some modifications back when the code was in c++ and got it 300% faster for financial calculations using just that simple approach togheter with som add/sub with carry).

Hardware division performance has improved since then, but sticking with 32bit divide (64 by 32) instead of 64 bit (128 by 64) is still probably faster than what the code does today. (2 32bit instructions was faster than one 64bit on sandy bridge) and is supported in both 32 and 64bit mode.

Daniel-Svensson avatar Feb 14 '24 21:02 Daniel-Svensson

Yes, I was thinking it would just be a faster alternative to use when calling DivRem with ulong and uint, and expecting to get a uint remainder (and ulong quotient). This can be consumed in various places, such as those I pointed out in proposal

lilinus avatar Feb 15 '24 10:02 lilinus

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

Issue Details

Background and motivation

There are multiple places in the runtime where we do a DivRem. If we can't get the quotient and remainder in one go using intrinsics or something else, then it is faster to compute the 32-bit remainder (guaranteed since divisor is 32-bit), compared to a 64-bit remainder.

Examples of such places: Decimal.DecCalc (one example from file) https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/Decimal.DecCalc.cs#L215-L217

Uint64 (formatting) https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs#L2338-L2339

BigInteger https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.DivRem.cs#L20-L29 DateTime (for decomposing into years, months, days, hours etc). https://github.com/dotnet/runtime/blob/d4643138e8b9f23482618a32535cc40552da0bb7/src/libraries/System.Private.CoreLib/src/System/DateTime.cs#L1419-L1424

It should be possible to leverage hardware intrisics:

  • If we can use intrisicts (like in DivRem(ulong, ulong)), we can just truncate the remainder to uint.
  • Otherwise remainder can be calculated as (uint)dividend - ((uint)quotient * divisor) since 32 bit multiplication should be faster than 64-bit.

Haven't seen any use of the same DivRem for signed numbers yet so excluded them from this suggestion

API Proposal

namespace System;

public readonly struct UInt64
{
    public static (ulong Quotient, uint Remainder) DivRemByUInt32(ulong left, uint right);
}

API Usage

//
ulong dividend = ulong.MaxValue;
uint divisor = 10;
(ulong quotient, uint remainder) = ulong.DivRemByUInt32(divident, divisor)

Alternative Designs

No response

Risks

No response

Author: lilinus
Assignees: -
Labels:

api-suggestion, area-System.Numerics, untriaged

Milestone: -

ghost avatar Feb 15 '24 10:02 ghost

This is something we'd rather add pattern recognition in the JIT. Namely ulong / uint should be recognized and just do the right thing.

tannergooding avatar Jul 09 '25 03:07 tannergooding