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/ut0ut.ic 29Various utilities 30 31Created 5/30/1994 Heikki Tuuri 32*******************************************************************/ 33 34/******************************************************//** 35Calculates the minimum of two ulints. 36@return minimum */ 37UNIV_INLINE 38ulint 39ut_min( 40/*===*/ 41 ulint n1, /*!< in: first number */ 42 ulint n2) /*!< in: second number */ 43{ 44 return((n1 <= n2) ? n1 : n2); 45} 46 47/******************************************************//** 48Calculates the maximum of two ulints. 49@return maximum */ 50UNIV_INLINE 51ulint 52ut_max( 53/*===*/ 54 ulint n1, /*!< in: first number */ 55 ulint n2) /*!< in: second number */ 56{ 57 return((n1 <= n2) ? n2 : n1); 58} 59 60/****************************************************************//** 61Calculates minimum of two ulint-pairs. */ 62UNIV_INLINE 63void 64ut_pair_min( 65/*========*/ 66 ulint* a, /*!< out: more significant part of minimum */ 67 ulint* b, /*!< out: less significant part of minimum */ 68 ulint a1, /*!< in: more significant part of first pair */ 69 ulint b1, /*!< in: less significant part of first pair */ 70 ulint a2, /*!< in: more significant part of second pair */ 71 ulint b2) /*!< in: less significant part of second pair */ 72{ 73 if (a1 == a2) { 74 *a = a1; 75 *b = ut_min(b1, b2); 76 } else if (a1 < a2) { 77 *a = a1; 78 *b = b1; 79 } else { 80 *a = a2; 81 *b = b2; 82 } 83} 84 85/******************************************************//** 86Compares two ulints. 87@return 1 if a > b, 0 if a == b, -1 if a < b */ 88UNIV_INLINE 89int 90ut_ulint_cmp( 91/*=========*/ 92 ulint a, /*!< in: ulint */ 93 ulint b) /*!< in: ulint */ 94{ 95 if (a < b) { 96 return(-1); 97 } else if (a == b) { 98 return(0); 99 } else { 100 return(1); 101 } 102} 103 104/*******************************************************//** 105Compares two pairs of ulints. 106@return -1 if a < b, 0 if a == b, 1 if a > b */ 107UNIV_INLINE 108int 109ut_pair_cmp( 110/*========*/ 111 ulint a1, /*!< in: more significant part of first pair */ 112 ulint a2, /*!< in: less significant part of first pair */ 113 ulint b1, /*!< in: more significant part of second pair */ 114 ulint b2) /*!< in: less significant part of second pair */ 115{ 116 if (a1 > b1) { 117 return(1); 118 } else if (a1 < b1) { 119 return(-1); 120 } else if (a2 > b2) { 121 return(1); 122 } else if (a2 < b2) { 123 return(-1); 124 } else { 125 return(0); 126 } 127} 128 129/*************************************************************//** 130Calculates fast the 2-logarithm of a number, rounded upward to an 131integer. 132@return logarithm in the base 2, rounded upward */ 133UNIV_INLINE 134ulint 135ut_2_log( 136/*=====*/ 137 ulint n) /*!< in: number != 0 */ 138{ 139 ulint res; 140 141 res = 0; 142 143 ut_ad(n > 0); 144 145 n = n - 1; 146 147 for (;;) { 148 n = n / 2; 149 150 if (n == 0) { 151 break; 152 } 153 154 res++; 155 } 156 157 return(res + 1); 158} 159 160/*************************************************************//** 161Calculates 2 to power n. 162@return 2 to power n */ 163UNIV_INLINE 164ulint 165ut_2_exp( 166/*=====*/ 167 ulint n) /*!< in: number */ 168{ 169 return((ulint) 1 << n); 170} 171