EABitTricks.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>
  2. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  3. <title>EABitTricks</title>
  4. <link type="text/css" rel="stylesheet" href="UTFDoc.css">
  5. <meta name="author" content="Paul Pedriana">
  6. </head>
  7. <body bgcolor="#FFFFFF">
  8. <h1>EABitTricks</h1>
  9. <h2>Introduction</h2>
  10. <p>EABitTricks provides a number of integer bit-manipulation utilities that are
  11. very fast and simple. For example, it is well-known that (x * 2) can be also
  12. accomplished with (x << 1). And while this example may not be useful in practice,
  13. there are many more such tricks which have real use, particularly for speeding
  14. up code. Some of these tricks may seem odd or even useless, but it turns out
  15. that there are surprising uses for many of these.</p>
  16. <h2>Notes</h2>
  17. <ul>
  18. <li>Twos-complement integer storage is assumed. Nearly all modern processors
  19. use twos-complement storage.</li>
  20. <li>Some tricks assume that right shifts of signed values preserve the sign.
  21. While nearly all modern processors and C/C++ compilers support this, some
  22. primitive processors don't. By preserving sign, we mean that signed binary
  23. (10000000 >> 1) gives (11000000). </li>
  24. <li>Only 'tricky' efficient solutions are provided. Obvious brute force loops
  25. to do useful things aren't included. We attempt to use branchless and loopless
  26. logic where possible.</li>
  27. <li>We don't cover magic number tricks for simplifying multiplication and division
  28. by constants. For example (x * 17) can also be quickly accomplished with ((x
  29. << 4) + x). Optimized integer multiplication and division tricks such as this
  30. is something for a separate library.</li>
  31. <li>We don't cover floating point tricks. That is something for a separate library.
  32. </li>
  33. <li>Implementations here are written as standalone functions for readability.
  34. However, the user may find that it's better in some cases to copy the implementation
  35. to a macro or to simply copy the implementation directly inline into source
  36. code. EABitTricks is meant to be a reference for copy and paste as much as
  37. it is meant to be used as-is.</li>
  38. <li>Many of these functions are templated instead of taking a given integer
  39. type such as uint32_t. The reason for this is that we want 64 bit support
  40. and that can be had automatically in most cases by the use of templates. The
  41. generated code will be exactly as fast as the case when templates are not
  42. used.</li>
  43. </ul>
  44. <h2>Example usage </h2>
  45. <p>All examples presume a <font face="Courier New, Courier, mono" size="-1">#include
  46. &quot;Foundation/EABitTricks.h&quot;</font> and assume the using of <font face="Courier New, Courier, mono" size="-1">namespace
  47. EA</font>.</p>
  48. <p>TurnOffLowestBit</p>
  49. <pre class="code-example">int16_t i16 = 0x0005; <span class="code-example-comment">// i16 =&gt; 000000101</span>
  50. i16 = TurnOffLowestBit(i16); <span class="code-example-comment">// i16 =&gt; 000000100</span></pre>
  51. IsolateSingle0And1Bits
  52. <pre class="code-example">uint32_t u32 = 0xabababab; <span class="code-example-comment"> // u32 =&gt; 10101011101010111010101110101011</span>
  53. u32 = IsolateSingle0And1Bits(0xabababab); <span class="code-example-comment">// u32 =&gt; 11111100011111000111110001111100</span></pre>
  54. RoundUpToPowerOf2
  55. <pre class="code-example">uint32_t u32 = 66; <span class="code-example-comment">// u32 =&gt; 66 (01000010)</span>
  56. u32 = RoundUpToPowerOf2(u32); <span class="code-example-comment">// u32 =&gt; 128 (10000000)</span></pre>
  57. <p> UnsignedMultiplyWouldOverflow</p>
  58. <pre class="code-example">bool b = UnsignedMultiplyWouldOverflow(0xffffffff, 0xffffffff); <span class="code-example-comment">// b =&gt; true</span> </pre>
  59. <h2>Interface</h2>
  60. <p>The following is a simple listing of EABitTricks functions as of January 2006.
  61. For an up to date listing of current functionality, see EABitTricks.h. Also,
  62. the EABitTricks.h file has documentation and example usage for each function.</p>
  63. <p>Bit manipulation</p>
  64. <pre class="code-example">T TurnOffLowestBit(T x);
  65. T IsolateLowestBit(T x);
  66. T IsolateLowest0Bit(T x);
  67. T GetTrailing0Bits(T x);
  68. T GetTrailing1And0Bits(T x);
  69. T PropogateLowestBitDownward(T x);
  70. T TurnOffLowestContiguousBits(T x);
  71. T TurnOnLowest0Bit(T x);
  72. uint32_t GetNextWithEqualBitCount(uint32_t x);
  73. uint32_t IsolateSingleBits(uint32_t x);
  74. uint32_t IsolateSingle0Bits(uint32_t x);
  75. uint32_t IsolateSingle0And1Bits(uint32_t x);
  76. int32_t ShiftRightSigned(int32_t x, uint32_t n);
  77. uint32_t CountTrailing0Bits(uint32_t x);
  78. uint32_t CountLeading0Bits(uint32_t x);
  79. uint32_t CountBits(uint32_t x);
  80. uint32_t CountBits64(uint64_t x);
  81. uint32_t RotateLeft(uint32_t x, uint32_t n);
  82. uint32_t RotateRight(uint32_t x, uint32_t n);
  83. uint32_t ReverseBits(uint32_t x);
  84. uint32_t IsolateHighestBit(uint32_t x);
  85. uint32_t IsolateHighest0Bit(uint32_t x);
  86. uint32_t PropogateHighestBitDownward(uint32_t x);
  87. uint32_t GetHighestContiguous0Bits(uint32_t x);
  88. T GetBitwiseEquivalence(T x, T y);
  89. bool AreLessThan2BitsSet(int32_t x);
  90. T GetHighestBit(T t);
  91. </pre>
  92. <p>Alignment / Power of 2</p>
  93. <pre class="code-example">bool IsPowerOf2(T x);
  94. uint32_t RoundUpToPowerOf2(uint32_t x);
  95. bool IsMultipleOf(T x);
  96. bool IsPowerOf2Minus1(T x);
  97. uint32_t GetHighestBitPowerOf2(uint32_t x);
  98. bool CrossesPowerOf2(T x, T y, T n);
  99. bool CrossesPowerOf2(T x, T y);
  100. T GetNextGreaterEven(T x);<br>T GetNextGreaterOdd(T x);
  101. T RoundUpTo(T x);
  102. int32_t RoundUpToEx(T x);
  103. T RoundDownTo(T x);
  104. T RoundDownToEx(T x);
  105. uint32_t Log2(uint32_t x);</pre>
  106. <p>Overflow</p>
  107. <pre class="code-example">bool SignedAdditionWouldOverflow(T x, T y);
  108. bool SignedSubtractionWouldOverflow(T x, T y);
  109. bool UnsignedAdditionWouldOverflow(T x, T y);
  110. bool UnsignedSubtractionWouldOverflow(T x, T y);
  111. bool UnsignedMultiplyWouldOverflow(uint32_t x, uint32_t y);
  112. bool SignedMultiplyWouldOverflow(int32_t x, int32_t y);
  113. bool UnsignedDivisionWouldOverflow(uint32_t x, uint32_t y);
  114. bool SignedDivisionWouldOverflow(int32_t x, int32_t y);
  115. int GetAverage(int32_t x, int32_t y);
  116. int GetAverage_Ceiling(int32_t x, int32_t y);</pre>
  117. <p>Miscellaneous</p>
  118. <pre class="code-example">int32_t GetParity(uint32_t32_t x);
  119. bool GetIsBigEndian();
  120. int32_t ToggleBetween0And1(int32_t x);
  121. T ToggleBetweenint32_tegers(T x, T a, T b);
  122. bool IsBetween0AndValue(int32_t x, int32_t a);
  123. void ExchangeValues(T& x, T& y);
  124. T FloorMod(T n, T mod);
  125. int32_t GetSign(int32_t32_t x);
  126. int32_t GetSignEx(int32_t32_t x);
  127. int32_t SignExtend12(int32_t32_t x);
  128. int32_t SignExtend24(int32_t32_t x);
  129. bool IsUnsigned(T x);
  130. #define EAIsUnsigned(x)
  131. bool IsTwosComplement();
  132. bool IsOnesComplement();
  133. bool IsSignMagnitude();
  134. bool IsOffsetBinary();
  135. #define EAArrayCount(array);</pre>
  136. <hr>
  137. <p>&nbsp;</p>
  138. <p>&nbsp;</p>
  139. <p>&nbsp;</p>
  140. <p>&nbsp;</p>
  141. <p>&nbsp;</p>
  142. <p>&nbsp;</p>
  143. <p>&nbsp;</p>
  144. <p>&nbsp;</p>
  145. <p> </p>
  146. </body></html>