hwint.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Operations on HOST_WIDE_INT.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "diagnostic-core.h"
  18. #if GCC_VERSION < 3004
  19. /* The functions clz_hwi, ctz_hwi, ffs_hwi, floor_log2, ceil_log2,
  20. and exact_log2 are defined as inline functions in hwint.h
  21. if GCC_VERSION >= 3004.
  22. The definitions here are used for older versions of GCC and
  23. non-GCC bootstrap compilers. */
  24. /* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
  25. If X is 0, return -1. */
  26. int
  27. floor_log2 (unsigned HOST_WIDE_INT x)
  28. {
  29. int t = 0;
  30. if (x == 0)
  31. return -1;
  32. if (HOST_BITS_PER_WIDE_INT > 64)
  33. if (x >= (unsigned HOST_WIDE_INT) 1 << (t + 64))
  34. t += 64;
  35. if (HOST_BITS_PER_WIDE_INT > 32)
  36. if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 32))
  37. t += 32;
  38. if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 16))
  39. t += 16;
  40. if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 8))
  41. t += 8;
  42. if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 4))
  43. t += 4;
  44. if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 2))
  45. t += 2;
  46. if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 1))
  47. t += 1;
  48. return t;
  49. }
  50. /* Given X, an unsigned number, return the largest Y such that 2**Y >= X. */
  51. int
  52. ceil_log2 (unsigned HOST_WIDE_INT x)
  53. {
  54. return floor_log2 (x - 1) + 1;
  55. }
  56. /* Return the logarithm of X, base 2, considering X unsigned,
  57. if X is a power of 2. Otherwise, returns -1. */
  58. int
  59. exact_log2 (unsigned HOST_WIDE_INT x)
  60. {
  61. if (x != (x & -x))
  62. return -1;
  63. return floor_log2 (x);
  64. }
  65. /* Given X, an unsigned number, return the number of least significant bits
  66. that are zero. When X == 0, the result is the word size. */
  67. int
  68. ctz_hwi (unsigned HOST_WIDE_INT x)
  69. {
  70. return x ? floor_log2 (x & -x) : HOST_BITS_PER_WIDE_INT;
  71. }
  72. /* Similarly for most significant bits. */
  73. int
  74. clz_hwi (unsigned HOST_WIDE_INT x)
  75. {
  76. return HOST_BITS_PER_WIDE_INT - 1 - floor_log2 (x);
  77. }
  78. /* Similar to ctz_hwi, except that the least significant bit is numbered
  79. starting from 1, and X == 0 yields 0. */
  80. int
  81. ffs_hwi (unsigned HOST_WIDE_INT x)
  82. {
  83. return 1 + floor_log2 (x & -x);
  84. }
  85. /* Return the number of set bits in X. */
  86. int
  87. popcount_hwi (unsigned HOST_WIDE_INT x)
  88. {
  89. int i, ret = 0;
  90. size_t bits = sizeof (x) * CHAR_BIT;
  91. for (i = 0; i < bits; i += 1)
  92. {
  93. ret += x & 1;
  94. x >>= 1;
  95. }
  96. return ret;
  97. }
  98. #endif /* GCC_VERSION < 3004 */
  99. /* Compute the greatest common divisor of two numbers A and B using
  100. Euclid's algorithm. */
  101. HOST_WIDE_INT
  102. gcd (HOST_WIDE_INT a, HOST_WIDE_INT b)
  103. {
  104. HOST_WIDE_INT x, y, z;
  105. x = abs_hwi (a);
  106. y = abs_hwi (b);
  107. while (x > 0)
  108. {
  109. z = y % x;
  110. y = x;
  111. x = z;
  112. }
  113. return y;
  114. }
  115. /* For X and Y positive integers, return X multiplied by Y and check
  116. that the result does not overflow. */
  117. HOST_WIDE_INT
  118. pos_mul_hwi (HOST_WIDE_INT x, HOST_WIDE_INT y)
  119. {
  120. if (x != 0)
  121. gcc_checking_assert ((HOST_WIDE_INT_MAX) / x >= y);
  122. return x * y;
  123. }
  124. /* Return X multiplied by Y and check that the result does not
  125. overflow. */
  126. HOST_WIDE_INT
  127. mul_hwi (HOST_WIDE_INT x, HOST_WIDE_INT y)
  128. {
  129. gcc_checking_assert (x != HOST_WIDE_INT_MIN
  130. && y != HOST_WIDE_INT_MIN);
  131. if (x >= 0)
  132. {
  133. if (y >= 0)
  134. return pos_mul_hwi (x, y);
  135. return -pos_mul_hwi (x, -y);
  136. }
  137. if (y >= 0)
  138. return -pos_mul_hwi (-x, y);
  139. return pos_mul_hwi (-x, -y);
  140. }
  141. /* Compute the least common multiple of two numbers A and B . */
  142. HOST_WIDE_INT
  143. least_common_multiple (HOST_WIDE_INT a, HOST_WIDE_INT b)
  144. {
  145. return mul_hwi (abs_hwi (a) / gcd (a, b), abs_hwi (b));
  146. }