-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Use Unsafe.BitCast for Interlocked.{Compare}Exchange of float/double #87166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1372442
Update {Compare}Exchange in coreclr to use Unsafe.As
huoyaoyuan 91bf45c
Remove corresponding FCalls
huoyaoyuan c7673f0
Share with mono
huoyaoyuan 35a6b88
Remove Mono icalls
huoyaoyuan a9bbe6d
Add AggressiveInlining
huoyaoyuan c4b9f5f
nativeaot
huoyaoyuan 0358183
Remove unused union type in mono
huoyaoyuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,24 @@ public static uint Exchange(ref uint location1, uint value) => | |
public static ulong Exchange(ref ulong location1, ulong value) => | ||
(ulong)Exchange(ref Unsafe.As<ulong, long>(ref location1), (long)value); | ||
|
||
/// <summary>Sets a single-precision floating point number to a specified value and returns the original value, as an atomic operation.</summary> | ||
/// <param name="location1">The variable to set to the specified value.</param> | ||
/// <param name="value">The value to which the <paramref name="location1"/> parameter is set.</param> | ||
/// <returns>The original value of <paramref name="location1"/>.</returns> | ||
/// <exception cref="NullReferenceException">The address of location1 is a null pointer.</exception> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static float Exchange(ref float location1, float value) | ||
=> Unsafe.BitCast<int, float>(Exchange(ref Unsafe.As<float, int>(ref location1), Unsafe.BitCast<float, int>(value))); | ||
|
||
/// <summary>Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation.</summary> | ||
/// <param name="location1">The variable to set to the specified value.</param> | ||
/// <param name="value">The value to which the <paramref name="location1"/> parameter is set.</param> | ||
/// <returns>The original value of <paramref name="location1"/>.</returns> | ||
/// <exception cref="NullReferenceException">The address of location1 is a null pointer.</exception> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static double Exchange(ref double location1, double value) | ||
=> Unsafe.BitCast<long, double>(Exchange(ref Unsafe.As<double, long>(ref location1), Unsafe.BitCast<double, long>(value))); | ||
|
||
/// <summary>Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.</summary> | ||
/// <param name="location1">The variable to set to the specified value.</param> | ||
/// <param name="value">The value to which the <paramref name="location1"/> parameter is set.</param> | ||
|
@@ -126,6 +144,26 @@ public static uint CompareExchange(ref uint location1, uint value, uint comparan | |
public static ulong CompareExchange(ref ulong location1, ulong value, ulong comparand) => | ||
(ulong)CompareExchange(ref Unsafe.As<ulong, long>(ref location1), (long)value, (long)comparand); | ||
|
||
/// <summary>Compares two single-precision floating point numbers for equality and, if they are equal, replaces the first value.</summary> | ||
/// <param name="location1">The destination, whose value is compared with <paramref name="comparand"/> and possibly replaced.</param> | ||
/// <param name="value">The value that replaces the destination value if the comparison results in equality.</param> | ||
/// <param name="comparand">The value that is compared to the value at <paramref name="location1"/>.</param> | ||
/// <returns>The original value in <paramref name="location1"/>.</returns> | ||
/// <exception cref="NullReferenceException">The address of <paramref name="location1"/> is a null pointer.</exception> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AggressiveInlining wasn't shown necessary in my first explorations, added just for consistency with uint/ulong ones. |
||
public static float CompareExchange(ref float location1, float value, float comparand) | ||
=> Unsafe.BitCast<int, float>(CompareExchange(ref Unsafe.As<float, int>(ref location1), Unsafe.BitCast<float, int>(value), Unsafe.BitCast<float, int>(comparand))); | ||
|
||
/// <summary>Compares two double-precision floating point numbers for equality and, if they are equal, replaces the first value.</summary> | ||
/// <param name="location1">The destination, whose value is compared with <paramref name="comparand"/> and possibly replaced.</param> | ||
/// <param name="value">The value that replaces the destination value if the comparison results in equality.</param> | ||
/// <param name="comparand">The value that is compared to the value at <paramref name="location1"/>.</param> | ||
/// <returns>The original value in <paramref name="location1"/>.</returns> | ||
/// <exception cref="NullReferenceException">The address of <paramref name="location1"/> is a null pointer.</exception> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static double CompareExchange(ref double location1, double value, double comparand) | ||
=> Unsafe.BitCast<long, double>(CompareExchange(ref Unsafe.As<double, long>(ref location1), Unsafe.BitCast<double, long>(value), Unsafe.BitCast<double, long>(comparand))); | ||
|
||
/// <summary>Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one.</summary> | ||
/// <param name="location1">The destination <see cref="IntPtr"/>, whose value is compared with the value of <paramref name="comparand"/> and possibly replaced by <paramref name="value"/>.</param> | ||
/// <param name="value">The <see cref="IntPtr"/> that replaces the destination value if the comparison results in equality.</param> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.