bits.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2008-2020 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 LIBMPDEC_BITS_H_
  28. #define LIBMPDEC_BITS_H_
  29. #include "mpdecimal.h"
  30. /* Check if n is a power of 2. */
  31. static inline int
  32. ispower2(mpd_size_t n)
  33. {
  34. return n != 0 && (n & (n-1)) == 0;
  35. }
  36. #if defined(ANSI)
  37. /*
  38. * Return the most significant bit position of n from 0 to 31 (63).
  39. * Assumptions: n != 0.
  40. */
  41. static inline int
  42. mpd_bsr(mpd_size_t n)
  43. {
  44. int pos = 0;
  45. mpd_size_t tmp;
  46. #ifdef CONFIG_64
  47. tmp = n >> 32;
  48. if (tmp != 0) { n = tmp; pos += 32; }
  49. #endif
  50. tmp = n >> 16;
  51. if (tmp != 0) { n = tmp; pos += 16; }
  52. tmp = n >> 8;
  53. if (tmp != 0) { n = tmp; pos += 8; }
  54. tmp = n >> 4;
  55. if (tmp != 0) { n = tmp; pos += 4; }
  56. tmp = n >> 2;
  57. if (tmp != 0) { n = tmp; pos += 2; }
  58. tmp = n >> 1;
  59. if (tmp != 0) { n = tmp; pos += 1; }
  60. return pos + (int)n - 1;
  61. }
  62. /*
  63. * Return the least significant bit position of n from 0 to 31 (63).
  64. * Assumptions: n != 0.
  65. */
  66. static inline int
  67. mpd_bsf(mpd_size_t n)
  68. {
  69. int pos;
  70. #ifdef CONFIG_64
  71. pos = 63;
  72. if (n & 0x00000000FFFFFFFFULL) { pos -= 32; } else { n >>= 32; }
  73. if (n & 0x000000000000FFFFULL) { pos -= 16; } else { n >>= 16; }
  74. if (n & 0x00000000000000FFULL) { pos -= 8; } else { n >>= 8; }
  75. if (n & 0x000000000000000FULL) { pos -= 4; } else { n >>= 4; }
  76. if (n & 0x0000000000000003ULL) { pos -= 2; } else { n >>= 2; }
  77. if (n & 0x0000000000000001ULL) { pos -= 1; }
  78. #else
  79. pos = 31;
  80. if (n & 0x000000000000FFFFUL) { pos -= 16; } else { n >>= 16; }
  81. if (n & 0x00000000000000FFUL) { pos -= 8; } else { n >>= 8; }
  82. if (n & 0x000000000000000FUL) { pos -= 4; } else { n >>= 4; }
  83. if (n & 0x0000000000000003UL) { pos -= 2; } else { n >>= 2; }
  84. if (n & 0x0000000000000001UL) { pos -= 1; }
  85. #endif
  86. return pos;
  87. }
  88. /* END ANSI */
  89. #elif defined(ASM)
  90. /*
  91. * Bit scan reverse. Assumptions: a != 0.
  92. */
  93. static inline int
  94. mpd_bsr(mpd_size_t a)
  95. {
  96. mpd_size_t retval;
  97. __asm__ (
  98. #ifdef CONFIG_64
  99. "bsrq %1, %0\n\t"
  100. #else
  101. "bsr %1, %0\n\t"
  102. #endif
  103. :"=r" (retval)
  104. :"r" (a)
  105. :"cc"
  106. );
  107. return (int)retval;
  108. }
  109. /*
  110. * Bit scan forward. Assumptions: a != 0.
  111. */
  112. static inline int
  113. mpd_bsf(mpd_size_t a)
  114. {
  115. mpd_size_t retval;
  116. __asm__ (
  117. #ifdef CONFIG_64
  118. "bsfq %1, %0\n\t"
  119. #else
  120. "bsf %1, %0\n\t"
  121. #endif
  122. :"=r" (retval)
  123. :"r" (a)
  124. :"cc"
  125. );
  126. return (int)retval;
  127. }
  128. /* END ASM */
  129. #elif defined(MASM)
  130. #include <intrin.h>
  131. /*
  132. * Bit scan reverse. Assumptions: a != 0.
  133. */
  134. static inline int __cdecl
  135. mpd_bsr(mpd_size_t a)
  136. {
  137. unsigned long retval;
  138. #ifdef CONFIG_64
  139. _BitScanReverse64(&retval, a);
  140. #else
  141. _BitScanReverse(&retval, a);
  142. #endif
  143. return (int)retval;
  144. }
  145. /*
  146. * Bit scan forward. Assumptions: a != 0.
  147. */
  148. static inline int __cdecl
  149. mpd_bsf(mpd_size_t a)
  150. {
  151. unsigned long retval;
  152. #ifdef CONFIG_64
  153. _BitScanForward64(&retval, a);
  154. #else
  155. _BitScanForward(&retval, a);
  156. #endif
  157. return (int)retval;
  158. }
  159. /* END MASM (_MSC_VER) */
  160. #else
  161. #error "missing preprocessor definitions"
  162. #endif /* BSR/BSF */
  163. #endif /* LIBMPDEC_BITS_H_ */