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