bits.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #ifndef BITS_H
  28. #define BITS_H
  29. #include "mpdecimal.h"
  30. #include <stdio.h>
  31. /* Check if n is a power of 2 */
  32. static inline int
  33. ispower2(mpd_size_t n)
  34. {
  35. return n != 0 && (n & (n-1)) == 0;
  36. }
  37. #if defined(ANSI)
  38. /*
  39. * Returns the most significant bit position of n from 0 to 32 (64).
  40. * Caller has to make sure that n is not 0.
  41. */
  42. static inline int
  43. mpd_bsr(mpd_size_t n)
  44. {
  45. int pos = 0;
  46. mpd_size_t tmp;
  47. #ifdef CONFIG_64
  48. tmp = n >> 32;
  49. if (tmp != 0) { n = tmp; pos += 32; }
  50. #endif
  51. tmp = n >> 16;
  52. if (tmp != 0) { n = tmp; pos += 16; }
  53. tmp = n >> 8;
  54. if (tmp != 0) { n = tmp; pos += 8; }
  55. tmp = n >> 4;
  56. if (tmp != 0) { n = tmp; pos += 4; }
  57. tmp = n >> 2;
  58. if (tmp != 0) { n = tmp; pos += 2; }
  59. tmp = n >> 1;
  60. if (tmp != 0) { n = tmp; pos += 1; }
  61. return pos + (int)n - 1;
  62. }
  63. /*
  64. * Returns the least significant bit position of n from 0 to 32 (64).
  65. * Caller has to make sure that n is not 0.
  66. */
  67. static inline int
  68. mpd_bsf(mpd_size_t n)
  69. {
  70. int pos;
  71. #ifdef CONFIG_64
  72. pos = 63;
  73. if (n & 0x00000000FFFFFFFFULL) { pos -= 32; } else { n >>= 32; }
  74. if (n & 0x000000000000FFFFULL) { pos -= 16; } else { n >>= 16; }
  75. if (n & 0x00000000000000FFULL) { pos -= 8; } else { n >>= 8; }
  76. if (n & 0x000000000000000FULL) { pos -= 4; } else { n >>= 4; }
  77. if (n & 0x0000000000000003ULL) { pos -= 2; } else { n >>= 2; }
  78. if (n & 0x0000000000000001ULL) { pos -= 1; }
  79. #else
  80. pos = 31;
  81. if (n & 0x000000000000FFFFUL) { pos -= 16; } else { n >>= 16; }
  82. if (n & 0x00000000000000FFUL) { pos -= 8; } else { n >>= 8; }
  83. if (n & 0x000000000000000FUL) { pos -= 4; } else { n >>= 4; }
  84. if (n & 0x0000000000000003UL) { pos -= 2; } else { n >>= 2; }
  85. if (n & 0x0000000000000001UL) { pos -= 1; }
  86. #endif
  87. return pos;
  88. }
  89. /* END ANSI */
  90. #elif defined(ASM)
  91. /*
  92. * Bit scan reverse.
  93. * Caller has to make sure that a is not 0.
  94. */
  95. static inline int
  96. mpd_bsr(mpd_size_t a)
  97. {
  98. mpd_size_t retval;
  99. __asm__ (
  100. #ifdef CONFIG_64
  101. "bsrq %1, %0\n\t"
  102. #else
  103. "bsr %1, %0\n\t"
  104. #endif
  105. :"=r" (retval)
  106. :"r" (a)
  107. :"cc"
  108. );
  109. return (int)retval;
  110. }
  111. /*
  112. * Bit scan forward.
  113. * Caller has to make sure that a is not 0.
  114. */
  115. static inline int
  116. mpd_bsf(mpd_size_t a)
  117. {
  118. mpd_size_t retval;
  119. __asm__ (
  120. #ifdef CONFIG_64
  121. "bsfq %1, %0\n\t"
  122. #else
  123. "bsf %1, %0\n\t"
  124. #endif
  125. :"=r" (retval)
  126. :"r" (a)
  127. :"cc"
  128. );
  129. return (int)retval;
  130. }
  131. /* END ASM */
  132. #elif defined(MASM)
  133. #include <intrin.h>
  134. /*
  135. * Bit scan reverse.
  136. * Caller has to make sure that a is not 0.
  137. */
  138. static inline int __cdecl
  139. mpd_bsr(mpd_size_t a)
  140. {
  141. unsigned long retval;
  142. #ifdef CONFIG_64
  143. _BitScanReverse64(&retval, a);
  144. #else
  145. _BitScanReverse(&retval, a);
  146. #endif
  147. return (int)retval;
  148. }
  149. /*
  150. * Bit scan forward.
  151. * Caller has to make sure that a is not 0.
  152. */
  153. static inline int __cdecl
  154. mpd_bsf(mpd_size_t a)
  155. {
  156. unsigned long retval;
  157. #ifdef CONFIG_64
  158. _BitScanForward64(&retval, a);
  159. #else
  160. _BitScanForward(&retval, a);
  161. #endif
  162. return (int)retval;
  163. }
  164. /* END MASM (_MSC_VER) */
  165. #else
  166. #error "missing preprocessor definitions"
  167. #endif /* BSR/BSF */
  168. #endif /* BITS_H */