1
/******************************************************************
2
Utilities for byte operations
4
(c) 1994, 1995 Innobase Oy
6
Created 5/30/1994 Heikki Tuuri
7
*******************************************************************/
9
/***********************************************************
10
Creates a 64-bit dulint out of two ulints. */
15
/* out: created dulint */
16
ulint high, /* in: high-order 32 bits */
17
ulint low) /* in: low-order 32 bits */
21
ut_ad(high <= 0xFFFFFFFF);
22
ut_ad(low <= 0xFFFFFFFF);
30
/***********************************************************
31
Gets the high-order 32 bits of a dulint. */
36
/* out: 32 bits in ulint */
37
dulint d) /* in: dulint */
42
/***********************************************************
43
Gets the low-order 32 bits of a dulint. */
48
/* out: 32 bits in ulint */
49
dulint d) /* in: dulint */
54
/***********************************************************
55
Converts a dulint (a struct of 2 ulints) to ib_longlong, which is a 64-bit
59
ut_conv_dulint_to_longlong(
60
/*=======================*/
61
/* out: value in ib_longlong type */
62
dulint d) /* in: dulint */
64
return((ib_longlong)d.low
65
+ (((ib_longlong)d.high) << 32));
68
/***********************************************************
69
Tests if a dulint is zero. */
74
/* out: TRUE if zero */
75
dulint a) /* in: dulint */
77
if ((a.low == 0) && (a.high == 0)) {
85
/***********************************************************
86
Compares two dulints. */
91
/* out: -1 if a < b, 0 if a == b,
93
dulint a, /* in: dulint */
94
dulint b) /* in: dulint */
96
if (a.high > b.high) {
98
} else if (a.high < b.high) {
100
} else if (a.low > b.low) {
102
} else if (a.low < b.low) {
109
/***********************************************************
110
Calculates the max of two dulints. */
116
dulint a, /* in: dulint */
117
dulint b) /* in: dulint */
119
if (ut_dulint_cmp(a, b) > 0) {
127
/***********************************************************
128
Calculates the min of two dulints. */
134
dulint a, /* in: dulint */
135
dulint b) /* in: dulint */
137
if (ut_dulint_cmp(a, b) > 0) {
145
/***********************************************************
146
Adds a ulint to a dulint. */
152
dulint a, /* in: dulint */
153
ulint b) /* in: ulint */
155
if (0xFFFFFFFFUL - b >= a.low) {
161
a.low = a.low - (0xFFFFFFFFUL - b) - 1;
168
/***********************************************************
169
Subtracts a ulint from a dulint. */
175
dulint a, /* in: dulint */
176
ulint b) /* in: ulint, b <= a */
186
a.low = 0xFFFFFFFFUL - b;
195
/***********************************************************
196
Subtracts a dulint from another. NOTE that the difference must be positive
197
and smaller that 4G. */
203
dulint a, /* in: dulint; NOTE a must be >= b and at most
204
2 to power 32 - 1 greater */
205
dulint b) /* in: dulint */
209
if (a.high == b.high) {
210
ut_ad(a.low >= b.low);
212
return(a.low - b.low);
215
ut_ad(a.high == b.high + 1);
217
diff = (ulint)(0xFFFFFFFFUL - b.low);
225
/************************************************************
226
Rounds a dulint downward to a multiple of a power of 2. */
229
ut_dulint_align_down(
230
/*=================*/
231
/* out: rounded value */
232
dulint n, /* in: number to be rounded */
233
ulint align_no) /* in: align by this number which must be a
239
ut_ad(((align_no - 1) & align_no) == 0);
241
low = ut_dulint_get_low(n);
242
high = ut_dulint_get_high(n);
244
low = low & ~(align_no - 1);
246
return(ut_dulint_create(high, low));
249
/************************************************************
250
Rounds a dulint upward to a multiple of a power of 2. */
255
/* out: rounded value */
256
dulint n, /* in: number to be rounded */
257
ulint align_no) /* in: align by this number which must be a
260
return(ut_dulint_align_down(ut_dulint_add(n, align_no - 1), align_no));
263
/************************************************************
264
The following function calculates the value of an integer n rounded
265
to the least product of align_no which is >= n. align_no
266
has to be a power of 2. */
271
/* out: rounded value */
272
ulint n, /* in: number to be rounded */
273
ulint align_no) /* in: align by this number */
276
ut_ad(((align_no - 1) & align_no) == 0);
278
return((n + align_no - 1) & ~(align_no - 1));
281
/*************************************************************
282
The following function rounds up a pointer to the nearest aligned address. */
287
/* out: aligned pointer */
288
void* ptr, /* in: pointer */
289
ulint align_no) /* in: align by this number */
292
ut_ad(((align_no - 1) & align_no) == 0);
295
ut_ad(sizeof(void*) == sizeof(ulint));
297
return((void*)((((ulint)ptr) + align_no - 1) & ~(align_no - 1)));
300
/************************************************************
301
The following function calculates the value of an integer n rounded
302
to the biggest product of align_no which is <= n. align_no has to be a
308
/* out: rounded value */
309
ulint n, /* in: number to be rounded */
310
ulint align_no) /* in: align by this number */
313
ut_ad(((align_no - 1) & align_no) == 0);
315
return(n & ~(align_no - 1));
318
/*************************************************************
319
The following function rounds down a pointer to the nearest
325
/* out: aligned pointer */
326
void* ptr, /* in: pointer */
327
ulint align_no) /* in: align by this number */
330
ut_ad(((align_no - 1) & align_no) == 0);
333
ut_ad(sizeof(void*) == sizeof(ulint));
335
return((void*)((((ulint)ptr)) & ~(align_no - 1)));
338
/*************************************************************
339
The following function computes the offset of a pointer from the nearest
345
/* out: distance from
347
const void* ptr, /* in: pointer */
348
ulint align_no) /* in: align by this number */
351
ut_ad(((align_no - 1) & align_no) == 0);
354
ut_ad(sizeof(void*) == sizeof(ulint));
356
return(((ulint)ptr) & (align_no - 1));
359
/*********************************************************************
360
Gets the nth bit of a ulint. */
365
/* out: TRUE if nth bit is 1; 0th bit is defined to
366
be the least significant */
367
ulint a, /* in: ulint */
368
ulint n) /* in: nth bit requested */
370
ut_ad(n < 8 * sizeof(ulint));
374
return(1 & (a >> n));
377
/*********************************************************************
378
Sets the nth bit of a ulint. */
383
/* out: the ulint with the bit set as requested */
384
ulint a, /* in: ulint */
385
ulint n, /* in: nth bit requested */
386
ibool val) /* in: value for the bit to set */
388
ut_ad(n < 8 * sizeof(ulint));
393
return(((ulint) 1 << n) | a);
395
return(~((ulint) 1 << n) & a);