1 | /***************************************************************************** |
2 | |
3 | Copyright (c) 1994, 2015, Oracle and/or its affiliates. All Rights Reserved. |
4 | |
5 | This program is free software; you can redistribute it and/or modify it under |
6 | the terms of the GNU General Public License as published by the Free Software |
7 | Foundation; version 2 of the License. |
8 | |
9 | This program is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
11 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License along with |
14 | this program; if not, write to the Free Software Foundation, Inc., |
15 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
16 | |
17 | *****************************************************************************/ |
18 | |
19 | /**************************************************************//** |
20 | @file include/ut0ut.ic |
21 | Various utilities |
22 | |
23 | Created 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 */ |
35 | UNIV_INLINE |
36 | void |
37 | ut_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 | /******************************************************//** |
58 | Compares two ulints. |
59 | @return 1 if a > b, 0 if a == b, -1 if a < b */ |
60 | UNIV_INLINE |
61 | int |
62 | ut_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) */ |
85 | UNIV_INLINE |
86 | int |
87 | ut_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 | /*************************************************************//** |
103 | Calculates fast the 2-logarithm of a number, rounded upward to an |
104 | integer. |
105 | @return logarithm in the base 2, rounded upward */ |
106 | UNIV_INLINE |
107 | ulint |
108 | ut_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 | /*************************************************************//** |
134 | Calculates 2 to power n. |
135 | @return 2 to power n */ |
136 | UNIV_INLINE |
137 | ulint |
138 | ut_2_exp( |
139 | /*=====*/ |
140 | ulint n) /*!< in: number */ |
141 | { |
142 | return((ulint) 1 << n); |
143 | } |
144 | |