~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/******************************************************************
Various utilities

(c) 1994, 1995 Innobase Oy

Created 5/30/1994 Heikki Tuuri
*******************************************************************/

/**********************************************************
Calculates the minimum of two ulints. */
UNIV_INLINE
ulint
ut_min(
/*===*/
			/* out: minimum */
	ulint	 n1,	/* in: first number */
	ulint	 n2)	/* in: second number */
{
	return((n1 <= n2) ? n1 : n2);
}

/**********************************************************
Calculates the maximum of two ulints. */
UNIV_INLINE
ulint
ut_max(
/*===*/
			/* out: maximum */
	ulint	 n1,	/* in: first number */
	ulint	 n2)	/* in: second number */
{
	return((n1 <= n2) ? n2 : n1);
}

/********************************************************************
Calculates minimum of two ulint-pairs. */
UNIV_INLINE
void
ut_pair_min(
/*========*/
	ulint*	a,	/* out: more significant part of minimum */
	ulint*	b,	/* out: less significant part of minimum */
	ulint	a1,	/* in: more significant part of first pair */
	ulint	b1,	/* in: less significant part of first pair */
	ulint	a2,	/* in: more significant part of second pair */
	ulint	b2)	/* in: less significant part of second pair */
{
	if (a1 == a2) {
		*a = a1;
		*b = ut_min(b1, b2);
	} else if (a1 < a2) {
		*a = a1;
		*b = b1;
	} else {
		*a = a2;
		*b = b2;
	}
}

/**********************************************************
Compares two ulints. */
UNIV_INLINE
int
ut_ulint_cmp(
/*=========*/
			/* out: 1 if a > b, 0 if a == b, -1 if a < b */
	ulint	a,	/* in: ulint */
	ulint	b)	/* in: ulint */
{
	if (a < b) {
		return(-1);
	} else if (a == b) {
		return(0);
	} else {
		return(1);
	}
}

/***********************************************************
Compares two pairs of ulints. */
UNIV_INLINE
int
ut_pair_cmp(
/*========*/
			/* out: -1 if a < b, 0 if a == b, 1 if a > b */
	ulint	a1,	/* in: more significant part of first pair */
	ulint	a2,	/* in: less significant part of first pair */
	ulint	b1,	/* in: more significant part of second pair */
	ulint	b2)	/* in: less significant part of second pair */
{
	if (a1 > b1) {
		return(1);
	} else if (a1 < b1) {
		return(-1);
	} else if (a2 > b2) {
		return(1);
	} else if (a2 < b2) {
		return(-1);
	} else {
		return(0);
	}
}

/*****************************************************************
Calculates fast the remainder when divided by a power of two. */
UNIV_INLINE
ulint
ut_2pow_remainder(
/*==============*/	/* out: remainder */
	ulint	n,	/* in: number to be divided */
	ulint	m)	/* in: divisor; power of 2 */
{
	ut_ad(0x80000000UL % m == 0);

	return(n & (m - 1));
}

/*****************************************************************
Calculates fast a value rounded to a multiple of a power of 2. */
UNIV_INLINE
ulint
ut_2pow_round(
/*==========*/		/* out: value of n rounded down to nearest
			multiple of m */
	ulint	n,	/* in: number to be rounded */
	ulint	m)	/* in: divisor; power of 2 */
{
	ut_ad(0x80000000UL % m == 0);

	return(n & ~(m - 1));
}

/*****************************************************************
Calculates fast the 2-logarithm of a number, rounded upward to an
integer. */
UNIV_INLINE
ulint
ut_2_log(
/*=====*/
			/* out: logarithm in the base 2, rounded upward */
	ulint	n)	/* in: number != 0 */
{
	ulint	res;

	res = 0;

	ut_ad(n > 0);

	n = n - 1;

	for (;;) {
		n = n / 2;

		if (n == 0) {
			break;
		}

		res++;
	}

	return(res + 1);
}

/*****************************************************************
Calculates 2 to power n. */
UNIV_INLINE
ulint
ut_2_exp(
/*=====*/
			/* out: 2 to power n */
	ulint	n)	/* in: number */
{
	return((ulint) 1 << n);
}