Skip to content

Commit

Permalink
better implementation of atomic sub
Browse files Browse the repository at this point in the history
  • Loading branch information
computablee committed Nov 24, 2023
1 parent 33a8e90 commit 708a1ff
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions DotMP/Atomic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public static uint Add(ref uint target, uint value)
/// <returns>The new value stored as a result of the operation.</returns>
public static uint Sub(ref uint target, uint value)
{
int neg = -(int)value;
return Interlocked.Add(ref target, Unsafe.As<int, uint>(ref neg));
uint neg = ~value + 1;
return Interlocked.Add(ref target, neg);
}

/// <summary>
Expand Down Expand Up @@ -246,8 +246,8 @@ public static ulong Add(ref ulong target, ulong value)
/// <returns>The new value stored as a result of the operation.</returns>
public static ulong Sub(ref ulong target, ulong value)
{
long neg = -(long)value;
return Interlocked.Add(ref target, Unsafe.As<long, ulong>(ref neg));
ulong neg = ~value + 1;
return Interlocked.Add(ref target, neg);
}

/// <summary>
Expand Down

0 comments on commit 708a1ff

Please sign in to comment.