1/*****************************************************************************
2
3Copyright (c) 1994, 2013, Oracle and/or its affiliates. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc.,
1551 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16
17*****************************************************************************/
18
19/**************************************************************//**
20@file include/ut0byte.ic
21Utilities for byte operations
22
23Created 5/30/1994 Heikki Tuuri
24*******************************************************************/
25
26/*******************************************************//**
27Creates a 64-bit integer out of two 32-bit integers.
28@return created integer */
29UNIV_INLINE
30ib_uint64_t
31ut_ull_create(
32/*==========*/
33 ulint high, /*!< in: high-order 32 bits */
34 ulint low) /*!< in: low-order 32 bits */
35{
36 ut_ad(high <= ULINT32_MASK);
37 ut_ad(low <= ULINT32_MASK);
38 return(((ib_uint64_t) high) << 32 | low);
39}
40
41/********************************************************//**
42Rounds a 64-bit integer downward to a multiple of a power of 2.
43@return rounded value */
44UNIV_INLINE
45ib_uint64_t
46ut_uint64_align_down(
47/*=================*/
48 ib_uint64_t n, /*!< in: number to be rounded */
49 ulint align_no) /*!< in: align by this number
50 which must be a power of 2 */
51{
52 ut_ad(align_no > 0);
53 ut_ad(ut_is_2pow(align_no));
54
55 return(n & ~((ib_uint64_t) align_no - 1));
56}
57
58/********************************************************//**
59Rounds ib_uint64_t upward to a multiple of a power of 2.
60@return rounded value */
61UNIV_INLINE
62ib_uint64_t
63ut_uint64_align_up(
64/*===============*/
65 ib_uint64_t n, /*!< in: number to be rounded */
66 ulint align_no) /*!< in: align by this number
67 which must be a power of 2 */
68{
69 ib_uint64_t align_1 = (ib_uint64_t) align_no - 1;
70
71 ut_ad(align_no > 0);
72 ut_ad(ut_is_2pow(align_no));
73
74 return((n + align_1) & ~align_1);
75}
76
77/*********************************************************//**
78The following function rounds up a pointer to the nearest aligned address.
79@return aligned pointer */
80UNIV_INLINE
81void*
82ut_align(
83/*=====*/
84 const void* ptr, /*!< in: pointer */
85 ulint align_no) /*!< in: align by this number */
86{
87 ut_ad(align_no > 0);
88 ut_ad(((align_no - 1) & align_no) == 0);
89 ut_ad(ptr);
90
91 ut_ad(sizeof(void*) == sizeof(ulint));
92
93 return((void*)((((ulint) ptr) + align_no - 1) & ~(align_no - 1)));
94}
95
96/*********************************************************//**
97The following function rounds down a pointer to the nearest
98aligned address.
99@return aligned pointer */
100UNIV_INLINE
101void*
102ut_align_down(
103/*==========*/
104 const void* ptr, /*!< in: pointer */
105 ulint align_no) /*!< in: align by this number */
106{
107 ut_ad(align_no > 0);
108 ut_ad(((align_no - 1) & align_no) == 0);
109 ut_ad(ptr);
110
111 ut_ad(sizeof(void*) == sizeof(ulint));
112
113 return((void*)(((ulint) ptr) & ~(align_no - 1)));
114}
115
116/*********************************************************//**
117The following function computes the offset of a pointer from the nearest
118aligned address.
119@return distance from aligned pointer */
120UNIV_INLINE
121ulint
122ut_align_offset(
123/*============*/
124 const void* ptr, /*!< in: pointer */
125 ulint align_no) /*!< in: align by this number */
126{
127 ut_ad(align_no > 0);
128 ut_ad(((align_no - 1) & align_no) == 0);
129 ut_ad(ptr);
130
131 ut_ad(sizeof(void*) == sizeof(ulint));
132
133 return(((ulint) ptr) & (align_no - 1));
134}
135
136/*****************************************************************//**
137Gets the nth bit of a ulint.
138@return TRUE if nth bit is 1; 0th bit is defined to be the least significant */
139UNIV_INLINE
140ibool
141ut_bit_get_nth(
142/*===========*/
143 ulint a, /*!< in: ulint */
144 ulint n) /*!< in: nth bit requested */
145{
146 ut_ad(n < 8 * sizeof(ulint));
147 return(1 & (a >> n));
148}
149
150/*****************************************************************//**
151Sets the nth bit of a ulint.
152@return the ulint with the bit set as requested */
153UNIV_INLINE
154ulint
155ut_bit_set_nth(
156/*===========*/
157 ulint a, /*!< in: ulint */
158 ulint n, /*!< in: nth bit requested */
159 ibool val) /*!< in: value for the bit to set */
160{
161 ut_ad(n < 8 * sizeof(ulint));
162 if (val) {
163 return(((ulint) 1 << n) | a);
164 } else {
165 return(~((ulint) 1 << n) & a);
166 }
167}
168