bits.h.xml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <chapter xml:id="bits.h">
  2. <title><tt>__vic/bits.h</tt></title>
  3. <p>Bits and bytes manipulation tools.</p>
  4. <chapter xml:id="lo_nibble">
  5. <title><tt>lo_nibble()</tt>, <tt>hi_nibble()</tt></title>
  6. <code-block lang="C++">
  7. constexpr uint8_t lo_nibble(uint8_t byte);
  8. constexpr uint8_t hi_nibble(uint8_t byte);
  9. </code-block>
  10. <p>Return the value of the low-order/high-order half-byte (tetrad),
  11. respectively.</p>
  12. </chapter>
  13. <chapter xml:id="msb_ones">
  14. <title><tt>msb_ones()</tt>, <tt>lsb_ones()</tt></title>
  15. <code-block lang="C++"><![CDATA[
  16. template<class T> constexpr T lsb_ones(unsigned bits_num);
  17. template<class T> constexpr T msb_ones(unsigned bits_num);
  18. ]]></code-block>
  19. <p>Return the value of the type <tt>T</tt> with all least/most significant
  20. <tt>bits_num</tt> bits filled with <tt>1</tt>, respectively. All other bits
  21. are set to <tt>0</tt>.</p>
  22. </chapter>
  23. <chapter xml:id="get_lsbs">
  24. <title><tt>get_lsbs()</tt></title>
  25. <code-block lang="C++">
  26. template&lt;class T> constexpr T get_lsbs(T v, unsigned bits_num);
  27. </code-block>
  28. <p>Returns <tt>bits_num</tt> least significant bits of <tt>v</tt>. In other
  29. words, zeroes all but <tt>bits_num</tt> least significant bits.</p>
  30. </chapter>
  31. <chapter xml:id="ord">
  32. <title><tt>ord()</tt></title>
  33. <code-block lang="C++">
  34. constexpr int ord(char ch);
  35. </code-block>
  36. <p>Returns the character code from 0 to 255.</p>
  37. <postcondition>ord(ch) >= 0</postcondition>
  38. </chapter>
  39. <chapter xml:id="popcount">
  40. <title><tt>popcount()</tt></title>
  41. <code-block lang="C++">
  42. unsigned popcount(unsigned v);
  43. unsigned popcount(unsigned long v);
  44. unsigned popcount(unsigned long long v);
  45. unsigned popcount(unsigned short v);
  46. unsigned popcount(unsigned char v);
  47. </code-block>
  48. <p>Returns the number of one bits in the given value.</p>
  49. </chapter>
  50. <chapter xml:id="msb_position">
  51. <title><tt>msb_position()</tt></title>
  52. <code-block lang="C++">
  53. unsigned msb_position(unsigned v)
  54. unsigned msb_position(unsigned long v);
  55. unsigned msb_position(unsigned long long v);
  56. unsigned msb_position(unsigned short v);
  57. unsigned msb_position(unsigned char v);
  58. </code-block>
  59. <precondition><tt>v != 0</tt></precondition>
  60. <p>Returns the position of the most significant 1-bit. The result is unspecified
  61. if <tt>v == 0</tt>.</p>
  62. </chapter>
  63. <chapter xml:id="ispow2">
  64. <title><tt>ispow2()</tt></title>
  65. <code-block lang="C++"><![CDATA[
  66. template<class UInt>
  67. bool ispow2(UInt n);
  68. ]]></code-block>
  69. <precondition><tt>UInt</tt> is an unsigned integer type</precondition>
  70. <p>Returns <tt>true</tt> if <tt>n</tt> is an integral power of 2.</p>
  71. </chapter>
  72. <chapter xml:id="ceil2">
  73. <title><tt>ceil2()</tt></title>
  74. <code-block lang="C++"><![CDATA[
  75. template<class UInt>
  76. UInt ceil2(UInt n);
  77. ]]></code-block>
  78. <precondition><tt>UInt</tt> is an unsigned integer type</precondition>
  79. <p>Returns the minimal value <tt>m</tt> such that <tt>ispow2(m) &amp;&amp;
  80. m >= n</tt>. If <tt>m</tt> is not representable as a value of type
  81. <tt>UInt</tt>, the result is an unspecified value.</p>
  82. </chapter>
  83. <chapter xml:id="floor2">
  84. <title><tt>floor2()</tt></title>
  85. <code-block lang="C++"><![CDATA[
  86. template<class UInt>
  87. UInt floor2(UInt n);
  88. ]]></code-block>
  89. <precondition><tt>UInt</tt> is an unsigned integer type</precondition>
  90. <p>If <tt>n != 0</tt> returns the maximal value <tt>m</tt> such that
  91. <tt>ispow2(m) &amp;&amp; m &lt;= n</tt>. Otherwise <tt>0</tt> is returned.</p>
  92. </chapter>
  93. <chapter xml:id="ceil_log2">
  94. <title><tt>ceil_log2()</tt></title>
  95. <code-block lang="C++"><![CDATA[
  96. template<class UInt>
  97. unsigned ceil_log2(UInt n);
  98. ]]></code-block>
  99. <precondition><tt>UInt</tt> is an unsigned integer type</precondition>
  100. <p>Returns <tt>ceil(log2(n))</tt> if <tt>n != 0</tt> or 0 otherwise.</p>
  101. </chapter>
  102. <chapter xml:id="floor_log2">
  103. <title><tt>floor_log2()</tt></title>
  104. <code-block lang="C++"><![CDATA[
  105. template<class UInt>
  106. unsigned floor_log2(UInt n);
  107. ]]></code-block>
  108. <precondition><tt>UInt</tt> is an unsigned integer type</precondition>
  109. <p>Returns <tt>floor(log2(n))</tt> if <tt>n != 0</tt> or 0 otherwise.</p>
  110. </chapter>
  111. <chapter xml:id="rotl">
  112. <title><tt>rotl()</tt>, <tt>rotr()</tt></title>
  113. <code-block lang="C++">
  114. unsigned long long rotl(unsigned long long v, int shift);
  115. unsigned long rotl(unsigned long v, int shift);
  116. unsigned rotl(unsigned v, int shift);
  117. unsigned short rotl(unsigned short v, int shift);
  118. unsigned char rotl(unsigned char v, int shift);
  119. unsigned long long rotr(unsigned long long v, int shift);
  120. unsigned long rotr(unsigned long v, int shift);
  121. unsigned rotr(unsigned v, int shift);
  122. unsigned short rotr(unsigned short v, int shift);
  123. unsigned char rotr(unsigned char v, int shift);
  124. </code-block>
  125. <p>The functions perform circular left (<tt>rotl</tt>) or right
  126. (<tt>rotr</tt>) bitwise shift (rotation).</p>
  127. <precondition><tt>0 &lt;= shift &amp;&amp; shift &lt; sizeof(v)*CHAR_BIT</tt></precondition>
  128. </chapter>
  129. <chapter xml:id="swapped_nibbles">
  130. <title><tt>swapped_nibbles()</tt></title>
  131. <code-block lang="C++">
  132. constexpr uint8_t swapped_nibbles(uint8_t b);
  133. </code-block>
  134. <p>Swaps a low-order half-byte with a high-order one and returns the value.</p>
  135. </chapter>
  136. </chapter>