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