-
Notifications
You must be signed in to change notification settings - Fork 290
Add a x86_64::cmpxchg16b
intrinsic
#624
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use sync::atomic::Ordering; | ||
|
||
#[cfg(test)] | ||
use stdsimd_test::assert_instr; | ||
|
||
/// Compare and exchange 16 bytes (128 bits) of data atomically. | ||
/// | ||
/// This intrinsic corresponds to the `cmpxchg16b` instruction on x86_64 | ||
/// processors. It performs an atomic compare-and-swap, updating the `ptr` | ||
/// memory location to `val` if the current value in memory equals `old`. | ||
/// | ||
/// # Return value | ||
/// | ||
/// This function returns the previous value at the memory location. If it is | ||
/// equal to `old` then the memory was updated to `new`. | ||
/// | ||
/// # Memory Orderings | ||
/// | ||
/// This atomic operations has the same semantics of memory orderings as | ||
/// `AtomicUsize::compare_exchange` does, only operating on 16 bytes of memory | ||
/// instead of just a pointer. | ||
/// | ||
/// For more information on memory orderings here see the `compare_exchange` | ||
/// documentation for other `Atomic*` types in the standard library. | ||
/// | ||
/// # Unsafety | ||
/// | ||
/// This method is unsafe because it takes a raw pointer and will attempt to | ||
/// read and possibly write the memory at the pointer. The pointer must also be | ||
/// aligned on a 16-byte boundary. | ||
/// | ||
/// This method also requires the `cmpxchg16b` CPU feature to be available at | ||
/// runtime to work correctly. If the CPU running the binary does not actually | ||
/// support `cmpxchg16b` and the program enters an execution path that | ||
/// eventually would reach this function the behavior is undefined. | ||
/// | ||
/// The `success` ordering must also be stronger or equal to `failure`, or this | ||
/// function call is undefined. See the `Atomic*` documentation's | ||
/// `compare_exchange` function for more information. When `compare_exchange` | ||
/// panics, this is undefined behavior. Currently this function aborts the | ||
/// process with an undefined instruction. | ||
#[inline] | ||
#[cfg_attr(test, assert_instr(cmpxchg16b, success = Ordering::SeqCst, failure = Ordering::SeqCst))] | ||
#[target_feature(enable = "cmpxchg16b")] | ||
pub unsafe fn cmpxchg16b( | ||
dst: *mut u128, | ||
old: u128, | ||
new: u128, | ||
success: Ordering, | ||
failure: Ordering, | ||
) -> u128 { | ||
use intrinsics; | ||
use sync::atomic::Ordering::*; | ||
|
||
debug_assert!(dst as usize % 16 == 0); | ||
|
||
let (val, _ok) = match (success, failure) { | ||
(Acquire, Acquire) => intrinsics::atomic_cxchg_acq(dst, old, new), | ||
(Release, Relaxed) => intrinsics::atomic_cxchg_rel(dst, old, new), | ||
(AcqRel, Acquire) => intrinsics::atomic_cxchg_acqrel(dst, old, new), | ||
(Relaxed, Relaxed) => intrinsics::atomic_cxchg_relaxed(dst, old, new), | ||
(SeqCst, SeqCst) => intrinsics::atomic_cxchg(dst, old, new), | ||
(Acquire, Relaxed) => intrinsics::atomic_cxchg_acq_failrelaxed(dst, old, new), | ||
(AcqRel, Relaxed) => intrinsics::atomic_cxchg_acqrel_failrelaxed(dst, old, new), | ||
(SeqCst, Relaxed) => intrinsics::atomic_cxchg_failrelaxed(dst, old, new), | ||
(SeqCst, Acquire) => intrinsics::atomic_cxchg_failacq(dst, old, new), | ||
|
||
// The above block is all copied from libcore, and this statement is | ||
// also copied from libcore except that it's a panic in libcore and we | ||
// have a little bit more of a lightweight panic here. | ||
_ => ::coresimd::x86::ud2(), | ||
}; | ||
val | ||
} |
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 |
---|---|---|
|
@@ -38,3 +38,6 @@ pub use self::bswap::*; | |
|
||
mod rdrand; | ||
pub use self::rdrand::*; | ||
|
||
mod cmpxchg16b; | ||
pub use self::cmpxchg16b::*; |
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
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.