]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/BaseLib/Math64.c
Optimized HighBitSetXX() functions
[mirror_edk2.git] / MdePkg / Library / BaseLib / Math64.c
... / ...
CommitLineData
1/** @file\r
2 Leaf math worker functions that require 64-bit arithmetic support from the\r
3 compiler.\r
4\r
5 Copyright (c) 2006, Intel Corporation<BR>\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14 Module Name: Math64.c\r
15\r
16**/\r
17\r
18#include "BaseLibInternals.h"\r
19\r
20/**\r
21 Shifts a 64-bit integer left between 0 and 63 bits. The low bits\r
22 are filled with zeros. The shifted value is returned.\r
23\r
24 This function shifts the 64-bit value Operand to the left by Count bits. The\r
25 low Count bits are set to zero. The shifted value is returned.\r
26\r
27 @param Operand The 64-bit operand to shift left.\r
28 @param Count The number of bits to shift left.\r
29\r
30 @return Operand << Count\r
31\r
32**/\r
33UINT64\r
34EFIAPI\r
35InternalMathLShiftU64 (\r
36 IN UINT64 Operand,\r
37 IN UINTN Count\r
38 )\r
39{\r
40 return Operand << Count;\r
41}\r
42\r
43/**\r
44 Shifts a 64-bit integer right between 0 and 63 bits. This high bits\r
45 are filled with zeros. The shifted value is returned.\r
46\r
47 This function shifts the 64-bit value Operand to the right by Count bits. The\r
48 high Count bits are set to zero. The shifted value is returned.\r
49\r
50 @param Operand The 64-bit operand to shift right.\r
51 @param Count The number of bits to shift right.\r
52\r
53 @return Operand >> Count\r
54\r
55**/\r
56UINT64\r
57EFIAPI\r
58InternalMathRShiftU64 (\r
59 IN UINT64 Operand,\r
60 IN UINTN Count\r
61 )\r
62{\r
63 return Operand >> Count;\r
64}\r
65\r
66/**\r
67 Shifts a 64-bit integer right between 0 and 63 bits. The high bits\r
68 are filled with original integer's bit 63. The shifted value is returned.\r
69\r
70 This function shifts the 64-bit value Operand to the right by Count bits. The\r
71 high Count bits are set to bit 63 of Operand. The shifted value is returned.\r
72\r
73 If Count is greater than 63, then ASSERT().\r
74\r
75 @param Operand The 64-bit operand to shift right.\r
76 @param Count The number of bits to shift right.\r
77\r
78 @return Operand arithmetically shifted right by Count\r
79\r
80**/\r
81UINT64\r
82EFIAPI\r
83InternalMathARShiftU64 (\r
84 IN UINT64 Operand,\r
85 IN UINTN Count\r
86 )\r
87{\r
88 //\r
89 // Test if this compiler supports arithmetic shift\r
90 //\r
91 if ((((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1)) == -1) {\r
92 //\r
93 // Arithmetic shift is supported\r
94 //\r
95 return (UINT64)((INT64)Operand >> Count);\r
96 }\r
97\r
98 //\r
99 // Arithmetic is not supported\r
100 //\r
101 return (Operand >> Count) |\r
102 ((INTN)Operand < 0 ? ~((UINTN)-1 >> Count) : 0);\r
103}\r
104\r
105\r
106/**\r
107 Rotates a 64-bit integer left between 0 and 63 bits, filling\r
108 the low bits with the high bits that were rotated.\r
109\r
110 This function rotates the 64-bit value Operand to the left by Count bits. The\r
111 low Count bits are fill with the high Count bits of Operand. The rotated\r
112 value is returned.\r
113\r
114 @param Operand The 64-bit operand to rotate left.\r
115 @param Count The number of bits to rotate left.\r
116\r
117 @return Operand <<< Count\r
118\r
119**/\r
120UINT64\r
121EFIAPI\r
122InternalMathLRotU64 (\r
123 IN UINT64 Operand,\r
124 IN UINTN Count\r
125 )\r
126{\r
127 return (Operand << Count) | (Operand >> (64 - Count));\r
128}\r
129\r
130/**\r
131 Rotates a 64-bit integer right between 0 and 63 bits, filling\r
132 the high bits with the high low bits that were rotated.\r
133\r
134 This function rotates the 64-bit value Operand to the right by Count bits.\r
135 The high Count bits are fill with the low Count bits of Operand. The rotated\r
136 value is returned.\r
137\r
138 @param Operand The 64-bit operand to rotate right.\r
139 @param Count The number of bits to rotate right.\r
140\r
141 @return Operand >>> Count\r
142\r
143**/\r
144UINT64\r
145EFIAPI\r
146InternalMathRRotU64 (\r
147 IN UINT64 Operand,\r
148 IN UINTN Count\r
149 )\r
150{\r
151 return (Operand >> Count) | (Operand << (64 - Count));\r
152}\r
153\r
154/**\r
155 Switches the endianess of a 64-bit integer.\r
156\r
157 This function swaps the bytes in a 64-bit unsigned value to switch the value\r
158 from little endian to big endian or vice versa. The byte swapped value is\r
159 returned.\r
160\r
161 @param Operand A 64-bit unsigned value.\r
162\r
163 @return The byte swaped Operand.\r
164\r
165**/\r
166UINT64\r
167EFIAPI\r
168InternalMathSwapBytes64 (\r
169 IN UINT64 Operand\r
170 )\r
171{\r
172 return (UINT64)(\r
173 ((UINT64)SwapBytes32 ((UINT32)Operand) << 32) |\r
174 ((UINT64)SwapBytes32 ((UINT32)(Operand >> 32)))\r
175 );\r
176}\r
177\r
178/**\r
179 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer\r
180 and generates a 64-bit unsigned result.\r
181\r
182 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit\r
183 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-\r
184 bit unsigned result is returned.\r
185\r
186 @param Multiplicand A 64-bit unsigned value.\r
187 @param Multiplier A 32-bit unsigned value.\r
188\r
189 @return Multiplicand * Multiplier\r
190\r
191**/\r
192UINT64\r
193EFIAPI\r
194InternalMathMultU64x32 (\r
195 IN UINT64 Multiplicand,\r
196 IN UINT32 Multiplier\r
197 )\r
198{\r
199 return Multiplicand * Multiplier;\r
200}\r
201\r
202\r
203/**\r
204 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer\r
205 and generates a 64-bit unsigned result.\r
206\r
207 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit\r
208 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-\r
209 bit unsigned result is returned.\r
210\r
211 @param Multiplicand A 64-bit unsigned value.\r
212 @param Multiplier A 64-bit unsigned value.\r
213\r
214 @return Multiplicand * Multiplier\r
215\r
216**/\r
217UINT64\r
218EFIAPI\r
219InternalMathMultU64x64 (\r
220 IN UINT64 Multiplicand,\r
221 IN UINT64 Multiplier\r
222 )\r
223{\r
224 return Multiplicand * Multiplier;\r
225}\r
226\r
227/**\r
228 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
229 generates a 64-bit unsigned result.\r
230\r
231 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
232 unsigned value Divisor and generates a 64-bit unsigned quotient. This\r
233 function returns the 64-bit unsigned quotient.\r
234\r
235 @param Dividend A 64-bit unsigned value.\r
236 @param Divisor A 32-bit unsigned value.\r
237\r
238 @return Dividend / Divisor\r
239\r
240**/\r
241UINT64\r
242EFIAPI\r
243InternalMathDivU64x32 (\r
244 IN UINT64 Dividend,\r
245 IN UINT32 Divisor\r
246 )\r
247{\r
248 return Dividend / Divisor;\r
249}\r
250\r
251/**\r
252 Divides a 64-bit unsigned integer by a 32-bit unsigned integer\r
253 and generates a 32-bit unsigned remainder.\r
254\r
255 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
256 unsigned value Divisor and generates a 32-bit remainder. This function\r
257 returns the 32-bit unsigned remainder.\r
258\r
259 @param Dividend A 64-bit unsigned value.\r
260 @param Divisor A 32-bit unsigned value.\r
261\r
262 @return Dividend % Divisor\r
263\r
264**/\r
265UINT32\r
266EFIAPI\r
267InternalMathModU64x32 (\r
268 IN UINT64 Dividend,\r
269 IN UINT32 Divisor\r
270 )\r
271{\r
272 return (UINT32)(Dividend % Divisor);\r
273}\r
274\r
275/**\r
276 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
277 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.\r
278\r
279 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
280 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder\r
281 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.\r
282 This function returns the 64-bit unsigned quotient.\r
283\r
284 @param Dividend A 64-bit unsigned value.\r
285 @param Divisor A 32-bit unsigned value.\r
286 @param Remainder A pointer to a 32-bit unsigned value. This parameter is\r
287 optional and may be NULL.\r
288\r
289 @return Dividend / Divisor\r
290\r
291**/\r
292UINT64\r
293EFIAPI\r
294InternalMathDivRemU64x32 (\r
295 IN UINT64 Dividend,\r
296 IN UINT32 Divisor,\r
297 OUT UINT32 *Remainder OPTIONAL\r
298 )\r
299{\r
300 if (Remainder != NULL) {\r
301 *Remainder = (UINT32)(Dividend % Divisor);\r
302 }\r
303 return Dividend / Divisor;\r
304}\r
305\r
306/**\r
307 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and\r
308 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.\r
309\r
310 This function divides the 64-bit unsigned value Dividend by the 64-bit\r
311 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder\r
312 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.\r
313 This function returns the 64-bit unsigned quotient.\r
314\r
315 @param Dividend A 64-bit unsigned value.\r
316 @param Divisor A 64-bit unsigned value.\r
317 @param Remainder A pointer to a 64-bit unsigned value. This parameter is\r
318 optional and may be NULL.\r
319\r
320 @return Dividend / Divisor\r
321\r
322**/\r
323UINT64\r
324EFIAPI\r
325InternalMathDivRemU64x64 (\r
326 IN UINT64 Dividend,\r
327 IN UINT64 Divisor,\r
328 OUT UINT64 *Remainder OPTIONAL\r
329 )\r
330{\r
331 if (Remainder != NULL) {\r
332 *Remainder = Dividend % Divisor;\r
333 }\r
334 return Dividend / Divisor;\r
335}\r
336\r
337/**\r
338 Divides a 64-bit signed integer by a 64-bit signed integer and\r
339 generates a 64-bit signed result and a optional 64-bit signed remainder.\r
340\r
341 This function divides the 64-bit unsigned value Dividend by the 64-bit\r
342 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder\r
343 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.\r
344 This function returns the 64-bit unsigned quotient.\r
345\r
346 @param Dividend A 64-bit signed value.\r
347 @param Divisor A 64-bit signed value.\r
348 @param Remainder A pointer to a 64-bit signed value. This parameter is\r
349 optional and may be NULL.\r
350\r
351 @return Dividend / Divisor\r
352\r
353**/\r
354INT64\r
355InternalMathDivRemS64x64 (\r
356 IN INT64 Dividend,\r
357 IN INT64 Divisor,\r
358 OUT INT64 *Remainder OPTIONAL\r
359 )\r
360{\r
361 if (Remainder != NULL) {\r
362 *Remainder = Dividend % Divisor;\r
363 }\r
364 return Dividend / Divisor;\r
365}\r