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