bits.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2008-2016 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. * Return the most significant bit position of n from 0 to 31 (63).
  40. * Assumptions: n != 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. * Return the least significant bit position of n from 0 to 31 (63).
  65. * Assumptions: n != 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. Assumptions: a != 0.
  93. */
  94. static inline int
  95. mpd_bsr(mpd_size_t a)
  96. {
  97. mpd_size_t retval;
  98. __asm__ (
  99. #ifdef CONFIG_64
  100. "bsrq %1, %0\n\t"
  101. #else
  102. "bsr %1, %0\n\t"
  103. #endif
  104. :"=r" (retval)
  105. :"r" (a)
  106. :"cc"
  107. );
  108. return (int)retval;
  109. }
  110. /*
  111. * Bit scan forward. Assumptions: a != 0.
  112. */
  113. static inline int
  114. mpd_bsf(mpd_size_t a)
  115. {
  116. mpd_size_t retval;
  117. __asm__ (
  118. #ifdef CONFIG_64
  119. "bsfq %1, %0\n\t"
  120. #else
  121. "bsf %1, %0\n\t"
  122. #endif
  123. :"=r" (retval)
  124. :"r" (a)
  125. :"cc"
  126. );
  127. return (int)retval;
  128. }
  129. /* END ASM */
  130. #elif defined(MASM)
  131. #include <intrin.h>
  132. /*
  133. * Bit scan reverse. Assumptions: a != 0.
  134. */
  135. static inline int __cdecl
  136. mpd_bsr(mpd_size_t a)
  137. {
  138. unsigned long retval;
  139. #ifdef CONFIG_64
  140. _BitScanReverse64(&retval, a);
  141. #else
  142. _BitScanReverse(&retval, a);
  143. #endif
  144. return (int)retval;
  145. }
  146. /*
  147. * Bit scan forward. Assumptions: a != 0.
  148. */
  149. static inline int __cdecl
  150. mpd_bsf(mpd_size_t a)
  151. {
  152. unsigned long retval;
  153. #ifdef CONFIG_64
  154. _BitScanForward64(&retval, a);
  155. #else
  156. _BitScanForward(&retval, a);
  157. #endif
  158. return (int)retval;
  159. }
  160. /* END MASM (_MSC_VER) */
  161. #else
  162. #error "missing preprocessor definitions"
  163. #endif /* BSR/BSF */
  164. #endif /* BITS_H */