Files
entt/bit_8hpp_source.html
2025-03-19 15:49:42 +01:00

157 lines
14 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.13.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>EnTT: src/entt/core/bit.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">EnTT<span id="projectnumber">&#160;3.15.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.13.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search',false);
$(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){ initResizable(false); });
/* @license-end */
</script>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="dir_68267d1309a1af8e8297ef4c3efbcdba.html">src</a></li><li class="navelem"><a class="el" href="dir_66e9674e8206a335795995fa32a03c91.html">entt</a></li><li class="navelem"><a class="el" href="dir_de8f4e6ba3f54a2a21309f742e93a373.html">core</a></li> </ul>
</div>
</div><!-- top -->
<div id="doc-content">
<div class="header">
<div class="headertitle"><div class="title">bit.hpp</div></div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"> 1</span><span class="preprocessor">#ifndef ENTT_CORE_BIT_HPP</span></div>
<div class="line"><a id="l00002" name="l00002"></a><span class="lineno"> 2</span><span class="preprocessor">#define ENTT_CORE_BIT_HPP</span></div>
<div class="line"><a id="l00003" name="l00003"></a><span class="lineno"> 3</span> </div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno"> 4</span><span class="preprocessor">#include &lt;cstddef&gt;</span></div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno"> 5</span><span class="preprocessor">#include &lt;limits&gt;</span></div>
<div class="line"><a id="l00006" name="l00006"></a><span class="lineno"> 6</span><span class="preprocessor">#include &lt;type_traits&gt;</span></div>
<div class="line"><a id="l00007" name="l00007"></a><span class="lineno"> 7</span><span class="preprocessor">#include &quot;../config/config.h&quot;</span></div>
<div class="line"><a id="l00008" name="l00008"></a><span class="lineno"> 8</span> </div>
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno"> 9</span><span class="keyword">namespace </span><a class="code hl_namespace" href="namespaceentt.html">entt</a> {</div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno"> 10</span></div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno"> 18</span><span class="keyword">template</span>&lt;<span class="keyword">typename</span> Type&gt;</div>
<div class="foldopen" id="foldopen00019" data-start="{" data-end="}">
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"><a class="line" href="namespaceentt.html#afab136c473d8c862b183cec9d4a13915"> 19</a></span>[[nodiscard]] <span class="keyword">constexpr</span> std::enable_if_t&lt;std::is_unsigned_v&lt;Type&gt;, <span class="keywordtype">int</span>&gt; <a class="code hl_function" href="namespaceentt.html#afab136c473d8c862b183cec9d4a13915">popcount</a>(<span class="keyword">const</span> Type value) <span class="keyword">noexcept</span> {</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno"> 20</span> <span class="keywordflow">return</span> value ? (int(value &amp; 1) + <a class="code hl_function" href="namespaceentt.html#afab136c473d8c862b183cec9d4a13915">popcount</a>(<span class="keyword">static_cast&lt;</span>Type<span class="keyword">&gt;</span>(value &gt;&gt; 1))) : 0;</div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno"> 21</span>}</div>
</div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno"> 22</span></div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno"> 30</span><span class="keyword">template</span>&lt;<span class="keyword">typename</span> Type&gt;</div>
<div class="foldopen" id="foldopen00031" data-start="{" data-end="}">
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno"><a class="line" href="namespaceentt.html#a1c0d94baafb4291b7dc5a49ba6f1b0e5"> 31</a></span>[[nodiscard]] <span class="keyword">constexpr</span> std::enable_if_t&lt;std::is_unsigned_v&lt;Type&gt;, <span class="keywordtype">bool</span>&gt; <a class="code hl_function" href="namespaceentt.html#a1c0d94baafb4291b7dc5a49ba6f1b0e5">has_single_bit</a>(<span class="keyword">const</span> Type value) <span class="keyword">noexcept</span> {</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno"> 32</span> <span class="keywordflow">return</span> value &amp;&amp; ((value &amp; (value - 1)) == 0);</div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno"> 33</span>}</div>
</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno"> 34</span></div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"> 42</span><span class="keyword">template</span>&lt;<span class="keyword">typename</span> Type&gt;</div>
<div class="foldopen" id="foldopen00043" data-start="{" data-end="}">
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"><a class="line" href="namespaceentt.html#a8f6fb41a1c8c0c5968bfca369e51a73e"> 43</a></span>[[nodiscard]] <span class="keyword">constexpr</span> std::enable_if_t&lt;std::is_unsigned_v&lt;Type&gt;, Type&gt; <a class="code hl_function" href="namespaceentt.html#a8f6fb41a1c8c0c5968bfca369e51a73e">next_power_of_two</a>(<span class="keyword">const</span> Type value) <span class="keyword">noexcept</span> {</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno"> 44</span> <span class="comment">// NOLINTNEXTLINE(bugprone-assert-side-effect)</span></div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno"> 45</span> ENTT_ASSERT_CONSTEXPR(value &lt; (Type{1u} &lt;&lt; (std::numeric_limits&lt;Type&gt;::digits - 1)), <span class="stringliteral">&quot;Numeric limits exceeded&quot;</span>);</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno"> 46</span> Type curr = value - (value != 0u);</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span> </div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span> <span class="keywordflow">for</span>(<span class="keywordtype">int</span> next = 1; next &lt; std::numeric_limits&lt;Type&gt;::digits; next = next * 2) {</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> curr |= (curr &gt;&gt; next);</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> }</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> </div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> <span class="keywordflow">return</span> ++curr;</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span>}</div>
</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span></div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span><span class="keyword">template</span>&lt;<span class="keyword">typename</span> Type&gt;</div>
<div class="foldopen" id="foldopen00063" data-start="{" data-end="}">
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"><a class="line" href="namespaceentt.html#a1ae98eeee0b0cb3e94d97d08a74a06f0"> 63</a></span>[[nodiscard]] <span class="keyword">constexpr</span> std::enable_if_t&lt;std::is_unsigned_v&lt;Type&gt;, Type&gt; <a class="code hl_function" href="namespaceentt.html#a1ae98eeee0b0cb3e94d97d08a74a06f0">fast_mod</a>(<span class="keyword">const</span> Type value, <span class="keyword">const</span> std::size_t mod) <span class="keyword">noexcept</span> {</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> ENTT_ASSERT_CONSTEXPR(<a class="code hl_function" href="namespaceentt.html#a1c0d94baafb4291b7dc5a49ba6f1b0e5">has_single_bit</a>(mod), <span class="stringliteral">&quot;Value must be a power of two&quot;</span>);</div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"> 65</span> <span class="keywordflow">return</span> <span class="keyword">static_cast&lt;</span>Type<span class="keyword">&gt;</span>(value &amp; (mod - 1u));</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span>}</div>
</div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span> </div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span>} <span class="comment">// namespace entt</span></div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> </div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span><span class="preprocessor">#endif</span></div>
<div class="ttc" id="anamespaceentt_html"><div class="ttname"><a href="namespaceentt.html">entt</a></div><div class="ttdoc">EnTT default namespace.</div><div class="ttdef"><b>Definition</b> <a href="dense__map_8hpp_source.html#l00022">dense_map.hpp:22</a></div></div>
<div class="ttc" id="anamespaceentt_html_a1ae98eeee0b0cb3e94d97d08a74a06f0"><div class="ttname"><a href="namespaceentt.html#a1ae98eeee0b0cb3e94d97d08a74a06f0">entt::fast_mod</a></div><div class="ttdeci">constexpr std::enable_if_t&lt; std::is_unsigned_v&lt; Type &gt;, Type &gt; fast_mod(const Type value, const std::size_t mod) noexcept</div><div class="ttdoc">Fast module utility function (powers of two only).</div><div class="ttdef"><b>Definition</b> <a href="#l00063">bit.hpp:63</a></div></div>
<div class="ttc" id="anamespaceentt_html_a1c0d94baafb4291b7dc5a49ba6f1b0e5"><div class="ttname"><a href="namespaceentt.html#a1c0d94baafb4291b7dc5a49ba6f1b0e5">entt::has_single_bit</a></div><div class="ttdeci">constexpr std::enable_if_t&lt; std::is_unsigned_v&lt; Type &gt;, bool &gt; has_single_bit(const Type value) noexcept</div><div class="ttdoc">Checks whether a value is a power of two or not (waiting for C++20 and std::has_single_bit).</div><div class="ttdef"><b>Definition</b> <a href="#l00031">bit.hpp:31</a></div></div>
<div class="ttc" id="anamespaceentt_html_a8f6fb41a1c8c0c5968bfca369e51a73e"><div class="ttname"><a href="namespaceentt.html#a8f6fb41a1c8c0c5968bfca369e51a73e">entt::next_power_of_two</a></div><div class="ttdeci">constexpr std::enable_if_t&lt; std::is_unsigned_v&lt; Type &gt;, Type &gt; next_power_of_two(const Type value) noexcept</div><div class="ttdoc">Computes the smallest power of two greater than or equal to a value (waiting for C++20 and std::bit_c...</div><div class="ttdef"><b>Definition</b> <a href="#l00043">bit.hpp:43</a></div></div>
<div class="ttc" id="anamespaceentt_html_afab136c473d8c862b183cec9d4a13915"><div class="ttname"><a href="namespaceentt.html#afab136c473d8c862b183cec9d4a13915">entt::popcount</a></div><div class="ttdeci">constexpr std::enable_if_t&lt; std::is_unsigned_v&lt; Type &gt;, int &gt; popcount(const Type value) noexcept</div><div class="ttdoc">Returns the number of set bits in a value (waiting for C++20 and std::popcount).</div><div class="ttdef"><b>Definition</b> <a href="#l00019">bit.hpp:19</a></div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2
</small></address>
</div><!-- doc-content -->
</body>
</html>