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