51 lines
7.4 KiB
HTML
51 lines
7.4 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="This crate provides `AtomicF32` and `AtomicF64` types. They’re implemented on top of `AtomicU32` and `AtomicU64` respectively."><title>atomic_float - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="atomic_float" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../atomic_float/index.html">atomic_<wbr>float</a><span class="version">1.1.0</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#portability" title="Portability">Portability</a></li><li><a href="#potential-use-cases" title="Potential Use Cases">Potential Use Cases</a></li><li><a href="#performance" title="Performance">Performance</a></li></ul><h3><a href="#structs">Crate Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>atomic_float</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/atomic_float/lib.rs.html#1-89">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>This crate provides <a href="struct.AtomicF32.html" title="struct atomic_float::AtomicF32"><code>AtomicF32</code></a> and <a href="struct.AtomicF64.html" title="struct atomic_float::AtomicF64"><code>AtomicF64</code></a> types. They’re
|
||
implemented on top of <code>AtomicU32</code> and <code>AtomicU64</code> respectively.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">static </span>DELTA_TIME: AtomicF32 = AtomicF32::new(<span class="number">1.0</span>);
|
||
|
||
<span class="comment">// In some main simulation loop:
|
||
</span>DELTA_TIME.store(compute_delta_time(), Ordering::Release);
|
||
|
||
<span class="comment">// elsewhere, perhaps on other threads:
|
||
</span><span class="kw">let </span>dt = DELTA_TIME.load(Ordering::Acquire);
|
||
<span class="comment">// Use `dt` to compute simulation...</span></code></pre></div>
|
||
<h2 id="portability"><a class="doc-anchor" href="#portability">§</a>Portability</h2>
|
||
<p>In general, this library is as portable as <a href="https://doc.rust-lang.org/1.84.0/core/sync/atomic/struct.AtomicU32.html" title="struct core::sync::atomic::AtomicU32"><code>AtomicU32</code></a>/<a href="https://doc.rust-lang.org/1.84.0/core/sync/atomic/struct.AtomicU64.html" title="struct core::sync::atomic::AtomicU64"><code>AtomicU64</code></a>
|
||
(fairly portable). See the module documentation for <a href="https://doc.rust-lang.org/1.84.0/core/sync/atomic/index.html" title="mod core::sync::atomic">core::sync::atomic</a> for
|
||
information about the portability of atomic operations as a whole.</p>
|
||
<p>Some architectures do not support 64-bit atomics, so <a href="struct.AtomicF64.html" title="struct atomic_float::AtomicF64"><code>AtomicF64</code></a> is not
|
||
available on such architectures. Examples include 32-bit PowerPC, MIPS, and
|
||
Arm M-Profile.</p>
|
||
<h2 id="potential-use-cases"><a class="doc-anchor" href="#potential-use-cases">§</a>Potential Use Cases</h2>
|
||
<p>The motivating cases for this were:</p>
|
||
<ul>
|
||
<li>
|
||
<p>Tunable parameters loaded from a file that otherwise behaved as global
|
||
constants (still compelling to me).</p>
|
||
</li>
|
||
<li>
|
||
<p>Global variables like time deltas (see example above) which would need to
|
||
be threaded through a large amount of code. (Not as compelling).</p>
|
||
</li>
|
||
</ul>
|
||
<p>But really it was another 90% finished project that I had meant to get out
|
||
the door.</p>
|
||
<h2 id="performance"><a class="doc-anchor" href="#performance">§</a>Performance</h2>
|
||
<p>On x86 and x86_64: basically 0 cost if you pick the right orderings and
|
||
stick to load/store.</p>
|
||
<p>On everything else: acceptable if you pick the right orderings.</p>
|
||
<p>In general, this depends on your architecture. If you’re on x86{,_64}, you
|
||
can get away with a lot of dodgy atomic code. Even <code>SeqCst</code> usage won’t bite
|
||
you too bad, so long as stores are rare. That said, I’d try to stick to
|
||
<code>Acquire</code>/<code>Release</code> even on x86. For load/store, this has roughly 0 cost
|
||
compared to write/read to a global variable directly. Also, if you just need
|
||
atomicity, and not any global orderings, feel free to use Relaxed.</p>
|
||
<p>(I normally wouldn’t give this advice, but you’re probably not using
|
||
floating point in a situation where the exact value you get must follow
|
||
absolute rules).</p>
|
||
<p>Beyond all of this, we provide a few convenient RMW operations. Ones that
|
||
have to perform actual float operations, such as <code>fetch_add</code>/<code>fetch_sub</code>
|
||
(but not ones that operate solely on the binary representation, like
|
||
<code>fetch_abs</code> or <code>fetch_neg</code>) need to perform a CAS loop. That means they’re
|
||
much slower than <code>fetch_add</code>/<code>fetch_sub</code> are for AtomicU32, for example.</p>
|
||
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.AtomicF32.html" title="struct atomic_float::AtomicF32">Atomic<wbr>F32</a></div><div class="desc docblock-short">A floating point type which can be safely shared between threads.</div></li><li><div class="item-name"><a class="struct" href="struct.AtomicF64.html" title="struct atomic_float::AtomicF64">Atomic<wbr>F64</a></div><div class="desc docblock-short">A floating point type which can be safely shared between threads.</div></li></ul></section></div></main></body></html> |