1 /*++ 2 3 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR> 4 This program and the accompanying materials 5 are licensed and made available under the terms and conditions of the BSD License 6 which accompanies this distribution. The full text of the license may be found at 7 http://opensource.org/licenses/bsd-license.php 8 9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 11 12 Module Name: 13 14 Math.c 15 16 Abstract: 17 18 Math worker functions. 19 20 --*/ 21 22 #include "Tiano.h" 23 24 UINT64 25 LShiftU64 ( 26 IN UINT64 Operand, 27 IN UINTN Count 28 ) 29 /*++ 30 31 Routine Description: 32 33 This routine allows a 64 bit value to be left shifted by 32 bits and 34 returns the shifted value. 35 Count is valid up 63. (Only Bits 0-5 is valid for Count) 36 37 Arguments: 38 39 Operand - Value to be shifted 40 Count - Number of times to shift left. 41 42 Returns: 43 44 Value shifted left identified by the Count. 45 46 --*/ 47 { 48 return Operand << Count; 49 } 50 51 UINT64 52 MultU64x32 ( 53 IN UINT64 Multiplicand, 54 IN UINTN Multiplier 55 ) 56 /*++ 57 58 Routine Description: 59 60 This routine allows a 64 bit value to be multiplied with a 32 bit 61 value returns 64bit result. 62 No checking if the result is greater than 64bits 63 64 Arguments: 65 66 Multiplicand - multiplicand 67 Multiplier - multiplier 68 69 Returns: 70 71 Multiplicand * Multiplier 72 73 --*/ 74 { 75 return Multiplicand * Multiplier; 76 } 77 78 UINT64 79 Power10U64 ( 80 IN UINT64 Operand, 81 IN UINTN Power 82 ) 83 { 84 UINT64 Result; 85 86 Result = Operand; 87 while (Power-- > 0) { 88 Result *= 10; 89 } 90 return Result; 91 } 92 93 UINT64 94 RShiftU64 ( 95 IN UINT64 Operand, 96 IN UINTN Count 97 ) 98 /*++ 99 100 Routine Description: 101 102 This routine allows a 64 bit value to be right shifted by 32 bits and returns the 103 shifted value. 104 Count is valid up 63. (Only Bits 0-5 is valid for Count) 105 106 Arguments: 107 108 Operand - Value to be shifted 109 Count - Number of times to shift right. 110 111 Returns: 112 113 Value shifted right identified by the Count. 114 115 --*/ 116 { 117 return Operand >> Count; 118 } 119 120 UINT64 121 DivU64x32 ( 122 IN UINT64 Dividend, 123 IN UINTN Divisor, 124 OUT UINTN *Remainder OPTIONAL 125 ) 126 /*++ 127 128 Routine Description: 129 130 This routine allows a 64 bit value to be divided with a 32 bit value returns 131 64bit result and the Remainder. 132 133 Arguments: 134 135 Dividend - dividend 136 Divisor - divisor 137 Remainder - buffer for remainder 138 139 Returns: 140 141 Dividend / Divisor 142 Remainder = Dividend mod Divisor 143 144 --*/ 145 { 146 if (Remainder != NULL) { 147 *Remainder = Dividend % Divisor; 148 } 149 150 return Dividend / Divisor; 151 } 152 153 UINT8 154 Log2 ( 155 IN UINT64 Operand 156 ) 157 /*++ 158 159 Routine Description: 160 161 This function computes rounded down log2 of the Operand. This is an equivalent 162 of the position of the highest bit set in the Operand treated as a mask. 163 E.g., Log2 (0x0001) == 0, Log2 (0x0002) == 1, Log2 (0x0003) == 1, Log2 (0x0005) == 2 164 Log2 (0x4000) == 14, Log2 (0x8000) == 15, Log2 (0xC000) == 15, Log2 (0xFFFF) == 15, etc. 165 166 Arguments: 167 Operand - value of which the Log2 is to be computed. 168 169 Returns: 170 Rounded down log2 of the Operand or 0xFF if zero passed in. 171 172 --*/ 173 { 174 UINT8 Bitpos; 175 Bitpos = 0; 176 177 if (Operand == 0) { 178 return (0xff); 179 } 180 181 while (Operand != 0) { 182 Operand >>= 1; 183 Bitpos++; 184 } 185 return (UINT8)(Bitpos - 1); 186 187 } 188 189 UINT64 190 GetPowerOfTwo ( 191 IN UINT64 Operand 192 ) 193 /*++ 194 195 Routine Description: 196 197 Calculates the largest integer that is both 198 a power of two and less than Input 199 200 Arguments: 201 202 Operand - value to calculate power of two 203 204 Returns: 205 206 the largest integer that is both a power of 207 two and less than Input 208 209 --*/ 210 { 211 UINT8 Bitpos; 212 Bitpos = 0; 213 214 if (Operand == 0) { 215 return 0; 216 } 217 218 while (Operand != 0) { 219 Operand >>= 1; 220 Bitpos++; 221 } 222 223 Operand = 1; 224 Bitpos--; 225 while (Bitpos != 0) { 226 Operand <<= 1; 227 Bitpos--; 228 } 229 230 return Operand; 231 } 232