-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Background and Motivation
The motivation behind this proposal is to provide API that can be useful for adding/subtracting big integers without the need of calculating carry manually. Currently, the only way that I'm aware of .NET uses ADC assembly instruction is an overflow check, followed by a conditional jump to the exception throw. With a proper intrinsics/Math method it could be leveraged to provide a fast add with carry operation. This would be beneficial for BitInteger but also for any other code using this API.
Proposed API
namespace System
{
public static partial class Math
{
+ ulong AddWithCarry(ulong a, ulong b, ref ulong carry);
}
}Usage Examples
The immediate beneficent would be BigInteger with methods like
runtime/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.AddSub.cs
Lines 75 to 79 in 21a03c3
| for (; i < rightLength; i++) | |
| { | |
| long digit = (left[i] + carry) + right[i]; | |
| bits[i] = unchecked((uint)digit); | |
| carry = digit >> 32; |
Risks
The implementation overhead. This would require to go to JIT/intrinsics area and provide a proper fallback, something similar to what was done in Math.BigMul.