You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.7 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /**************************************************************//**
  14. @file include/ut0ut.ic
  15. Various utilities
  16. Created 5/30/1994 Heikki Tuuri
  17. *******************************************************************/
  18. /******************************************************//**
  19. Calculates the minimum of two ulints.
  20. @return minimum */
  21. UNIV_INLINE
  22. ulint
  23. ut_min(
  24. /*===*/
  25. ulint n1, /*!< in: first number */
  26. ulint n2) /*!< in: second number */
  27. {
  28. return((n1 <= n2) ? n1 : n2);
  29. }
  30. /******************************************************//**
  31. Calculates the maximum of two ulints.
  32. @return maximum */
  33. UNIV_INLINE
  34. ulint
  35. ut_max(
  36. /*===*/
  37. ulint n1, /*!< in: first number */
  38. ulint n2) /*!< in: second number */
  39. {
  40. return((n1 <= n2) ? n2 : n1);
  41. }
  42. /****************************************************************//**
  43. Calculates minimum of two ulint-pairs. */
  44. UNIV_INLINE
  45. void
  46. ut_pair_min(
  47. /*========*/
  48. ulint* a, /*!< out: more significant part of minimum */
  49. ulint* b, /*!< out: less significant part of minimum */
  50. ulint a1, /*!< in: more significant part of first pair */
  51. ulint b1, /*!< in: less significant part of first pair */
  52. ulint a2, /*!< in: more significant part of second pair */
  53. ulint b2) /*!< in: less significant part of second pair */
  54. {
  55. if (a1 == a2) {
  56. *a = a1;
  57. *b = ut_min(b1, b2);
  58. } else if (a1 < a2) {
  59. *a = a1;
  60. *b = b1;
  61. } else {
  62. *a = a2;
  63. *b = b2;
  64. }
  65. }
  66. /******************************************************//**
  67. Compares two ulints.
  68. @return 1 if a > b, 0 if a == b, -1 if a < b */
  69. UNIV_INLINE
  70. int
  71. ut_ulint_cmp(
  72. /*=========*/
  73. ulint a, /*!< in: ulint */
  74. ulint b) /*!< in: ulint */
  75. {
  76. if (a < b) {
  77. return(-1);
  78. } else if (a == b) {
  79. return(0);
  80. } else {
  81. return(1);
  82. }
  83. }
  84. /*******************************************************//**
  85. Compares two pairs of ulints.
  86. @return -1 if a < b, 0 if a == b, 1 if a > b */
  87. UNIV_INLINE
  88. int
  89. ut_pair_cmp(
  90. /*========*/
  91. ulint a1, /*!< in: more significant part of first pair */
  92. ulint a2, /*!< in: less significant part of first pair */
  93. ulint b1, /*!< in: more significant part of second pair */
  94. ulint b2) /*!< in: less significant part of second pair */
  95. {
  96. if (a1 > b1) {
  97. return(1);
  98. } else if (a1 < b1) {
  99. return(-1);
  100. } else if (a2 > b2) {
  101. return(1);
  102. } else if (a2 < b2) {
  103. return(-1);
  104. } else {
  105. return(0);
  106. }
  107. }
  108. /*************************************************************//**
  109. Calculates fast the 2-logarithm of a number, rounded upward to an
  110. integer.
  111. @return logarithm in the base 2, rounded upward */
  112. UNIV_INLINE
  113. ulint
  114. ut_2_log(
  115. /*=====*/
  116. ulint n) /*!< in: number != 0 */
  117. {
  118. ulint res;
  119. res = 0;
  120. ut_ad(n > 0);
  121. n = n - 1;
  122. for (;;) {
  123. n = n / 2;
  124. if (n == 0) {
  125. break;
  126. }
  127. res++;
  128. }
  129. return(res + 1);
  130. }
  131. /*************************************************************//**
  132. Calculates 2 to power n.
  133. @return 2 to power n */
  134. UNIV_INLINE
  135. ulint
  136. ut_2_exp(
  137. /*=====*/
  138. ulint n) /*!< in: number */
  139. {
  140. return((ulint) 1 << n);
  141. }