From 708a1ff5816f6ca0e8c0d4ef6aec245aca2743e7 Mon Sep 17 00:00:00 2001 From: Phillip Allen Lane Date: Thu, 23 Nov 2023 23:02:40 -0600 Subject: [PATCH] better implementation of atomic sub --- DotMP/Atomic.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/DotMP/Atomic.cs b/DotMP/Atomic.cs index cef47c2f..7c7648f1 100644 --- a/DotMP/Atomic.cs +++ b/DotMP/Atomic.cs @@ -113,8 +113,8 @@ public static uint Add(ref uint target, uint value) /// The new value stored as a result of the operation. public static uint Sub(ref uint target, uint value) { - int neg = -(int)value; - return Interlocked.Add(ref target, Unsafe.As(ref neg)); + uint neg = ~value + 1; + return Interlocked.Add(ref target, neg); } /// @@ -246,8 +246,8 @@ public static ulong Add(ref ulong target, ulong value) /// The new value stored as a result of the operation. public static ulong Sub(ref ulong target, ulong value) { - long neg = -(long)value; - return Interlocked.Add(ref target, Unsafe.As(ref neg)); + ulong neg = ~value + 1; + return Interlocked.Add(ref target, neg); } ///