189 lines
No EOL
7.6 KiB
HTML
189 lines
No EOL
7.6 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="API documentation for the Rust `weight` fn in crate `hamming`.">
|
||
<meta name="keywords" content="rust, rustlang, rust-lang, weight">
|
||
|
||
<title>hamming::weight - Rust</title>
|
||
|
||
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
|
||
<link rel="stylesheet" type="text/css" href="../main.css">
|
||
|
||
|
||
|
||
|
||
</head>
|
||
<body class="rustdoc">
|
||
<!--[if lte IE 8]>
|
||
<div class="warning">
|
||
This old browser is unsupported and will most likely display funky
|
||
things.
|
||
</div>
|
||
<![endif]-->
|
||
|
||
|
||
|
||
<nav class="sidebar">
|
||
|
||
<p class='location'><a href='index.html'>hamming</a></p><script>window.sidebarCurrent = {name: 'weight', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script>
|
||
</nav>
|
||
|
||
<nav class="sub">
|
||
<form class="search-form js-only">
|
||
<div class="search-container">
|
||
<input class="search-input" name="search"
|
||
autocomplete="off"
|
||
placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
|
||
type="search">
|
||
</div>
|
||
</form>
|
||
</nav>
|
||
|
||
<section id='main' class="content fn">
|
||
<h1 class='fqn'><span class='in-band'>Function <a href='index.html'>hamming</a>::<wbr><a class='fn' href=''>weight</a></span><span class='out-of-band'><span id='render-detail'>
|
||
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
|
||
[<span class='inner'>−</span>]
|
||
</a>
|
||
</span><a id='src-12' class='srclink' href='../src/hamming/weight_.rs.html#41-94' title='goto source code'>[src]</a></span></h1>
|
||
<pre class='rust fn'>pub fn weight(x: <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.slice.html'>&[</a><a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.u8.html'>u8</a><a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.slice.html'>]</a>) -> <a class='primitive' href='https://doc.rust-lang.org/nightly/std/primitive.u64.html'>u64</a></pre><div class='docblock'><p>Computes the <a href="https://en.wikipedia.org/wiki/Hamming_weight">Hamming
|
||
weight</a> of <code>x</code>, that
|
||
is, the population count, or number of ones.</p>
|
||
|
||
<p>This is a highly optimised version of the following naive version:</p>
|
||
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='kw'>fn</span> <span class='ident'>naive</span>(<span class='ident'>x</span>: <span class='kw-2'>&</span>[<span class='ident'>u8</span>]) <span class='op'>-></span> <span class='ident'>u64</span> {
|
||
<span class='ident'>x</span>.<span class='ident'>iter</span>().<span class='ident'>fold</span>(<span class='number'>0</span>, <span class='op'>|</span><span class='ident'>a</span>, <span class='ident'>b</span><span class='op'>|</span> <span class='ident'>a</span> <span class='op'>+</span> <span class='ident'>b</span>.<span class='ident'>count_ones</span>() <span class='kw'>as</span> <span class='ident'>u64</span>)
|
||
}</pre>
|
||
|
||
<p>This uses Lauradoux Cédric's <a href="http://web.archive.org/web/20120411185540/http://perso.citi.insa-lyon.fr/claurado/hamming.html">tree-merging
|
||
approach</a>
|
||
(as implemented by Kim Walisch in
|
||
<a href="http://primesieve.org/">primesieve</a>) and achieves on the order of
|
||
1-2 cycles per byte.</p>
|
||
|
||
<h1 id='performance-comparison' class='section-header'><a href='#performance-comparison'>Performance Comparison</a></h1>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th style="text-align: right">length</th>
|
||
<th style="text-align: right"><code>naive</code> (ns)</th>
|
||
<th style="text-align: right"><code>weight</code> (ns)</th>
|
||
<th style="text-align: right"><code>naive</code>/<code>weight</code></th>
|
||
</tr>
|
||
</thead>
|
||
|
||
<tbody>
|
||
<tr>
|
||
<td style="text-align: right">1</td>
|
||
<td style="text-align: right">5</td>
|
||
<td style="text-align: right">16</td>
|
||
<td style="text-align: right">0.31</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="text-align: right">10</td>
|
||
<td style="text-align: right">29</td>
|
||
<td style="text-align: right">51</td>
|
||
<td style="text-align: right">0.56</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="text-align: right">100</td>
|
||
<td style="text-align: right">284</td>
|
||
<td style="text-align: right">392</td>
|
||
<td style="text-align: right">0.72</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="text-align: right">1,000</td>
|
||
<td style="text-align: right">2,780</td>
|
||
<td style="text-align: right">340</td>
|
||
<td style="text-align: right">8.2</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="text-align: right">10,000</td>
|
||
<td style="text-align: right">27,700</td>
|
||
<td style="text-align: right">2,300</td>
|
||
<td style="text-align: right">12</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="text-align: right">100,000</td>
|
||
<td style="text-align: right">276,000</td>
|
||
<td style="text-align: right">17,900</td>
|
||
<td style="text-align: right">15</td>
|
||
</tr>
|
||
<tr>
|
||
<td style="text-align: right">1,000,000</td>
|
||
<td style="text-align: right">2,770,000</td>
|
||
<td style="text-align: right">172,000</td>
|
||
<td style="text-align: right">16</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<h1 id='example' class='section-header'><a href='#example'>Example</a></h1>
|
||
<pre class='rust rust-example-rendered'>
|
||
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='ident'>hamming</span>::<span class='ident'>weight</span>(<span class='kw-2'>&</span>[<span class='number'>1</span>, <span class='number'>0xFF</span>, <span class='number'>1</span>, <span class='number'>0xFF</span>]), <span class='number'>1</span> <span class='op'>+</span> <span class='number'>8</span> <span class='op'>+</span> <span class='number'>1</span> <span class='op'>+</span> <span class='number'>8</span>);</pre>
|
||
</div></section>
|
||
<section id='search' class="content hidden"></section>
|
||
|
||
<section class="footer"></section>
|
||
|
||
<aside id="help" class="hidden">
|
||
<div>
|
||
<h1 class="hidden">Help</h1>
|
||
|
||
<div class="shortcuts">
|
||
<h2>Keyboard Shortcuts</h2>
|
||
|
||
<dl>
|
||
<dt>?</dt>
|
||
<dd>Show this help dialog</dd>
|
||
<dt>S</dt>
|
||
<dd>Focus the search field</dd>
|
||
<dt>⇤</dt>
|
||
<dd>Move up in search results</dd>
|
||
<dt>⇥</dt>
|
||
<dd>Move down in search results</dd>
|
||
<dt>⏎</dt>
|
||
<dd>Go to active search result</dd>
|
||
</dl>
|
||
</div>
|
||
|
||
<div class="infos">
|
||
<h2>Search Tricks</h2>
|
||
|
||
<p>
|
||
Prefix searches with a type followed by a colon (e.g.
|
||
<code>fn:</code>) to restrict the search to a given type.
|
||
</p>
|
||
|
||
<p>
|
||
Accepted types are: <code>fn</code>, <code>mod</code>,
|
||
<code>struct</code>, <code>enum</code>,
|
||
<code>trait</code>, <code>type</code>, <code>macro</code>,
|
||
and <code>const</code>.
|
||
</p>
|
||
|
||
<p>
|
||
Search functions by type signature (e.g.
|
||
<code>vec -> usize</code> or <code>* -> vec</code>)
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
|
||
|
||
<script>
|
||
window.rootPath = "../";
|
||
window.currentCrate = "hamming";
|
||
window.playgroundUrl = "";
|
||
</script>
|
||
<script src="../jquery.js"></script>
|
||
<script src="../main.js"></script>
|
||
|
||
<script defer src="../search-index.js"></script>
|
||
</body>
|
||
</html> |