1
/*****************************************************************************
3
Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
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.
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.
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., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/******************************************************************
20
Utilities for byte operations
22
Created 5/30/1994 Heikki Tuuri
23
*******************************************************************/
25
/***********************************************************
26
Creates a 64-bit dulint out of two ulints. */
31
/* out: created dulint */
32
ulint high, /* in: high-order 32 bits */
33
ulint low) /* in: low-order 32 bits */
37
ut_ad(high <= 0xFFFFFFFF);
38
ut_ad(low <= 0xFFFFFFFF);
46
/***********************************************************
47
Gets the high-order 32 bits of a dulint. */
52
/* out: 32 bits in ulint */
53
dulint d) /* in: dulint */
58
/***********************************************************
59
Gets the low-order 32 bits of a dulint. */
64
/* out: 32 bits in ulint */
65
dulint d) /* in: dulint */
70
/***********************************************************
71
Converts a dulint (a struct of 2 ulints) to ib_int64_t, which is a 64-bit
75
ut_conv_dulint_to_longlong(
76
/*=======================*/
77
/* out: value in ib_int64_t type */
78
dulint d) /* in: dulint */
80
return((ib_int64_t)d.low
81
+ (((ib_int64_t)d.high) << 32));
84
/***********************************************************
85
Tests if a dulint is zero. */
90
/* out: TRUE if zero */
91
dulint a) /* in: dulint */
93
if ((a.low == 0) && (a.high == 0)) {
101
/***********************************************************
102
Compares two dulints. */
107
/* out: -1 if a < b, 0 if a == b,
109
dulint a, /* in: dulint */
110
dulint b) /* in: dulint */
112
if (a.high > b.high) {
114
} else if (a.high < b.high) {
116
} else if (a.low > b.low) {
118
} else if (a.low < b.low) {
125
/***********************************************************
126
Calculates the max of two dulints. */
132
dulint a, /* in: dulint */
133
dulint b) /* in: dulint */
135
if (ut_dulint_cmp(a, b) > 0) {
143
/***********************************************************
144
Calculates the min of two dulints. */
150
dulint a, /* in: dulint */
151
dulint b) /* in: dulint */
153
if (ut_dulint_cmp(a, b) > 0) {
161
/***********************************************************
162
Adds a ulint to a dulint. */
168
dulint a, /* in: dulint */
169
ulint b) /* in: ulint */
171
if (0xFFFFFFFFUL - b >= a.low) {
177
a.low = a.low - (0xFFFFFFFFUL - b) - 1;
184
/***********************************************************
185
Subtracts a ulint from a dulint. */
191
dulint a, /* in: dulint */
192
ulint b) /* in: ulint, b <= a */
202
a.low = 0xFFFFFFFFUL - b;
211
/***********************************************************
212
Subtracts a dulint from another. NOTE that the difference must be positive
213
and smaller that 4G. */
219
dulint a, /* in: dulint; NOTE a must be >= b and at most
220
2 to power 32 - 1 greater */
221
dulint b) /* in: dulint */
225
if (a.high == b.high) {
226
ut_ad(a.low >= b.low);
228
return(a.low - b.low);
231
ut_ad(a.high == b.high + 1);
233
diff = (ulint)(0xFFFFFFFFUL - b.low);
241
/************************************************************
242
Rounds a dulint downward to a multiple of a power of 2. */
245
ut_dulint_align_down(
246
/*=================*/
247
/* out: rounded value */
248
dulint n, /* in: number to be rounded */
249
ulint align_no) /* in: align by this number which must be a
255
ut_ad(((align_no - 1) & align_no) == 0);
257
low = ut_dulint_get_low(n);
258
high = ut_dulint_get_high(n);
260
low = low & ~(align_no - 1);
262
return(ut_dulint_create(high, low));
265
/************************************************************
266
Rounds a dulint upward to a multiple of a power of 2. */
271
/* out: rounded value */
272
dulint n, /* in: number to be rounded */
273
ulint align_no) /* in: align by this number which must be a
276
return(ut_dulint_align_down(ut_dulint_add(n, align_no - 1), align_no));
279
/************************************************************
280
Rounds ib_uint64_t downward to a multiple of a power of 2. */
283
ut_uint64_align_down(
284
/*=================*/
285
/* out: rounded value */
286
ib_uint64_t n, /* in: number to be rounded */
287
ulint align_no) /* in: align by this number
288
which must be a power of 2 */
291
ut_ad(ut_is_2pow(align_no));
293
return(n & ~((ib_uint64_t) align_no - 1));
296
/************************************************************
297
Rounds ib_uint64_t upward to a multiple of a power of 2. */
302
/* out: rounded value */
303
ib_uint64_t n, /* in: number to be rounded */
304
ulint align_no) /* in: align by this number
305
which must be a power of 2 */
307
ib_uint64_t align_1 = (ib_uint64_t) align_no - 1;
310
ut_ad(ut_is_2pow(align_no));
312
return((n + align_1) & ~align_1);
315
/*************************************************************
316
The following function rounds up a pointer to the nearest aligned address. */
321
/* out: aligned pointer */
322
void* ptr, /* in: pointer */
323
ulint align_no) /* in: align by this number */
326
ut_ad(((align_no - 1) & align_no) == 0);
329
ut_ad(sizeof(void*) == sizeof(ulint));
331
return((void*)((((ulint)ptr) + align_no - 1) & ~(align_no - 1)));
334
/*************************************************************
335
The following function rounds down a pointer to the nearest
341
/* out: aligned pointer */
342
const void* ptr, /* in: pointer */
343
ulint align_no) /* in: align by this number */
346
ut_ad(((align_no - 1) & align_no) == 0);
349
ut_ad(sizeof(void*) == sizeof(ulint));
351
return((void*)((((ulint)ptr)) & ~(align_no - 1)));
354
/*************************************************************
355
The following function computes the offset of a pointer from the nearest
361
/* out: distance from
363
const void* ptr, /* in: pointer */
364
ulint align_no) /* in: align by this number */
367
ut_ad(((align_no - 1) & align_no) == 0);
370
ut_ad(sizeof(void*) == sizeof(ulint));
372
return(((ulint)ptr) & (align_no - 1));
375
/*********************************************************************
376
Gets the nth bit of a ulint. */
381
/* out: TRUE if nth bit is 1; 0th bit is defined to
382
be the least significant */
383
ulint a, /* in: ulint */
384
ulint n) /* in: nth bit requested */
386
ut_ad(n < 8 * sizeof(ulint));
390
return(1 & (a >> n));
393
/*********************************************************************
394
Sets the nth bit of a ulint. */
399
/* out: the ulint with the bit set as requested */
400
ulint a, /* in: ulint */
401
ulint n, /* in: nth bit requested */
402
ibool val) /* in: value for the bit to set */
404
ut_ad(n < 8 * sizeof(ulint));
409
return(((ulint) 1 << n) | a);
411
return(~((ulint) 1 << n) & a);