1
by brian
clean slate |
1 |
/******************************************************************
|
2 |
Various utilities
|
|
3 |
||
4 |
(c) 1994, 1995 Innobase Oy
|
|
5 |
||
6 |
Created 5/30/1994 Heikki Tuuri
|
|
7 |
*******************************************************************/
|
|
8 |
||
9 |
/**********************************************************
|
|
10 |
Calculates the minimum of two ulints. */
|
|
11 |
UNIV_INLINE |
|
12 |
ulint |
|
13 |
ut_min(
|
|
14 |
/*===*/
|
|
15 |
/* out: minimum */ |
|
16 |
ulint n1, /* in: first number */ |
|
17 |
ulint n2) /* in: second number */ |
|
18 |
{
|
|
19 |
return((n1 <= n2) ? n1 : n2); |
|
20 |
}
|
|
21 |
||
22 |
/**********************************************************
|
|
23 |
Calculates the maximum of two ulints. */
|
|
24 |
UNIV_INLINE |
|
25 |
ulint |
|
26 |
ut_max(
|
|
27 |
/*===*/
|
|
28 |
/* out: maximum */ |
|
29 |
ulint n1, /* in: first number */ |
|
30 |
ulint n2) /* in: second number */ |
|
31 |
{
|
|
32 |
return((n1 <= n2) ? n2 : n1); |
|
33 |
}
|
|
34 |
||
35 |
/********************************************************************
|
|
36 |
Calculates minimum of two ulint-pairs. */
|
|
37 |
UNIV_INLINE |
|
38 |
void |
|
39 |
ut_pair_min(
|
|
40 |
/*========*/
|
|
41 |
ulint* a, /* out: more significant part of minimum */ |
|
42 |
ulint* b, /* out: less significant part of minimum */ |
|
43 |
ulint a1, /* in: more significant part of first pair */ |
|
44 |
ulint b1, /* in: less significant part of first pair */ |
|
45 |
ulint a2, /* in: more significant part of second pair */ |
|
46 |
ulint b2) /* in: less significant part of second pair */ |
|
47 |
{
|
|
48 |
if (a1 == a2) { |
|
49 |
*a = a1; |
|
50 |
*b = ut_min(b1, b2); |
|
51 |
} else if (a1 < a2) { |
|
52 |
*a = a1; |
|
53 |
*b = b1; |
|
54 |
} else { |
|
55 |
*a = a2; |
|
56 |
*b = b2; |
|
57 |
} |
|
58 |
}
|
|
59 |
||
60 |
/**********************************************************
|
|
61 |
Compares two ulints. */
|
|
62 |
UNIV_INLINE |
|
63 |
int |
|
64 |
ut_ulint_cmp(
|
|
65 |
/*=========*/
|
|
66 |
/* out: 1 if a > b, 0 if a == b, -1 if a < b */ |
|
67 |
ulint a, /* in: ulint */ |
|
68 |
ulint b) /* in: ulint */ |
|
69 |
{
|
|
70 |
if (a < b) { |
|
71 |
return(-1); |
|
72 |
} else if (a == b) { |
|
73 |
return(0); |
|
74 |
} else { |
|
75 |
return(1); |
|
76 |
} |
|
77 |
}
|
|
78 |
||
79 |
/***********************************************************
|
|
80 |
Compares two pairs of ulints. */
|
|
81 |
UNIV_INLINE |
|
82 |
int |
|
83 |
ut_pair_cmp(
|
|
84 |
/*========*/
|
|
85 |
/* out: -1 if a < b, 0 if a == b, 1 if a > b */ |
|
86 |
ulint a1, /* in: more significant part of first pair */ |
|
87 |
ulint a2, /* in: less significant part of first pair */ |
|
88 |
ulint b1, /* in: more significant part of second pair */ |
|
89 |
ulint b2) /* in: less significant part of second pair */ |
|
90 |
{
|
|
91 |
if (a1 > b1) { |
|
92 |
return(1); |
|
93 |
} else if (a1 < b1) { |
|
94 |
return(-1); |
|
95 |
} else if (a2 > b2) { |
|
96 |
return(1); |
|
97 |
} else if (a2 < b2) { |
|
98 |
return(-1); |
|
99 |
} else { |
|
100 |
return(0); |
|
101 |
} |
|
102 |
}
|
|
103 |
||
104 |
/*****************************************************************
|
|
105 |
Calculates fast the remainder when divided by a power of two. */
|
|
106 |
UNIV_INLINE |
|
107 |
ulint |
|
108 |
ut_2pow_remainder(
|
|
109 |
/*==============*/ /* out: remainder */ |
|
110 |
ulint n, /* in: number to be divided */ |
|
111 |
ulint m) /* in: divisor; power of 2 */ |
|
112 |
{
|
|
113 |
ut_ad(0x80000000UL % m == 0); |
|
114 |
||
115 |
return(n & (m - 1)); |
|
116 |
}
|
|
117 |
||
118 |
/*****************************************************************
|
|
119 |
Calculates fast a value rounded to a multiple of a power of 2. */
|
|
120 |
UNIV_INLINE |
|
121 |
ulint |
|
122 |
ut_2pow_round(
|
|
123 |
/*==========*/ /* out: value of n rounded down to nearest |
|
124 |
multiple of m */
|
|
125 |
ulint n, /* in: number to be rounded */ |
|
126 |
ulint m) /* in: divisor; power of 2 */ |
|
127 |
{
|
|
128 |
ut_ad(0x80000000UL % m == 0); |
|
129 |
||
130 |
return(n & ~(m - 1)); |
|
131 |
}
|
|
132 |
||
133 |
/*****************************************************************
|
|
134 |
Calculates fast the 2-logarithm of a number, rounded upward to an
|
|
135 |
integer. */
|
|
136 |
UNIV_INLINE |
|
137 |
ulint |
|
138 |
ut_2_log(
|
|
139 |
/*=====*/
|
|
140 |
/* out: logarithm in the base 2, rounded upward */ |
|
141 |
ulint n) /* in: number != 0 */ |
|
142 |
{
|
|
143 |
ulint res; |
|
144 |
||
145 |
res = 0; |
|
146 |
||
147 |
ut_ad(n > 0); |
|
148 |
||
149 |
n = n - 1; |
|
150 |
||
151 |
for (;;) { |
|
152 |
n = n / 2; |
|
153 |
||
154 |
if (n == 0) { |
|
155 |
break; |
|
156 |
} |
|
157 |
||
158 |
res++; |
|
159 |
} |
|
160 |
||
161 |
return(res + 1); |
|
162 |
}
|
|
163 |
||
164 |
/*****************************************************************
|
|
165 |
Calculates 2 to power n. */
|
|
166 |
UNIV_INLINE |
|
167 |
ulint |
|
168 |
ut_2_exp(
|
|
169 |
/*=====*/
|
|
170 |
/* out: 2 to power n */ |
|
171 |
ulint n) /* in: number */ |
|
172 |
{
|
|
173 |
return((ulint) 1 << n); |
|
174 |
}
|